Update CMakeLists.txt - play with openssl
[novacoin.git] / src / timedata.h
1 // Copyright (c) 2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #ifndef NOVACOIN_TIMEDATA_H
6 #define NOVACOIN_TIMEDATA_H
7
8 #include <vector>
9 #include <algorithm>
10 #include <cassert>
11
12 class CNetAddr;
13
14 /** Median filter over a stream of values.
15  * Returns the median of the last N numbers
16  */
17 template <typename T> class CMedianFilter
18 {
19 private:
20     std::vector<T> vValues;
21     std::vector<T> vSorted;
22     unsigned int nSize;
23 public:
24     CMedianFilter(unsigned int size, T initial_value):
25         nSize(size)
26     {
27         vValues.reserve(size);
28         vValues.push_back(initial_value);
29         vSorted = vValues;
30     }
31
32     void input(T value)
33     {
34         if(vValues.size() == nSize)
35         {
36             vValues.erase(vValues.begin());
37         }
38         vValues.push_back(value);
39
40         vSorted.resize(vValues.size());
41         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
42         std::sort(vSorted.begin(), vSorted.end());
43     }
44
45     T median() const
46     {
47         size_t size = vSorted.size();
48         assert(size>0);
49         if(size & 1) // Odd number of elements
50         {
51             return vSorted[size/2];
52         }
53         else // Even number of elements
54         {
55             return (vSorted[size/2-1] + vSorted[size/2]) / 2;
56         }
57     }
58
59     int size() const
60     {
61         return static_cast<int>(vValues.size());
62     }
63
64     std::vector<T> sorted () const
65     {
66         return vSorted;
67     }
68 };
69
70 // Functions to keep track of adjusted P2P time
71 int64_t GetAdjustedTime();
72 int64_t GetTimeOffset();
73 int64_t GetNodesOffset();
74 void AddTimeData(const CNetAddr& ip, int64_t nTime);
75
76 #endif // NOVACOIN_TIMEDATA_H