1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_TIMEDATA_H
6 #define BITCOIN_TIMEDATA_H
13 static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT
= 70 * 60;
18 * Median filter over a stream of values.
19 * Returns the median of the last N numbers
25 std::vector
<T
> vValues
;
26 std::vector
<T
> vSorted
;
30 CMedianFilter(unsigned int size
, T initial_value
) : nSize(size
)
32 vValues
.reserve(size
);
33 vValues
.push_back(initial_value
);
39 if (vValues
.size() == nSize
) {
40 vValues
.erase(vValues
.begin());
42 vValues
.push_back(value
);
44 vSorted
.resize(vValues
.size());
45 std::copy(vValues
.begin(), vValues
.end(), vSorted
.begin());
46 std::sort(vSorted
.begin(), vSorted
.end());
51 int size
= vSorted
.size();
53 if (size
& 1) // Odd number of elements
55 return vSorted
[size
/ 2];
56 } else // Even number of elements
58 return (vSorted
[size
/ 2 - 1] + vSorted
[size
/ 2]) / 2;
64 return vValues
.size();
67 std::vector
<T
> sorted() const
73 /** Functions to keep track of adjusted P2P time */
74 int64_t GetTimeOffset();
75 int64_t GetAdjustedTime();
76 void AddTimeData(const CNetAddr
& ip
, int64_t nTime
);
78 #endif // BITCOIN_TIMEDATA_H