]> Git Repo - VerusCoin.git/blame - src/timedata.h
Apply clang-format on some infrequently-updated files
[VerusCoin.git] / src / timedata.h
CommitLineData
14f888ca
WL
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 BITCOIN_TIMEDATA_H
6#define BITCOIN_TIMEDATA_H
7
d1e26d4e
WL
8#include <algorithm>
9#include <assert.h>
611116d4
PK
10#include <stdint.h>
11#include <vector>
14f888ca
WL
12
13class CNetAddr;
14
d1e26d4e
WL
15/** Median filter over a stream of values.
16 * Returns the median of the last N numbers
17 */
20e01b1a
PW
18template <typename T>
19class CMedianFilter
d1e26d4e
WL
20{
21private:
22 std::vector<T> vValues;
23 std::vector<T> vSorted;
24 unsigned int nSize;
20e01b1a 25
d1e26d4e 26public:
20e01b1a 27 CMedianFilter(unsigned int size, T initial_value) : nSize(size)
d1e26d4e
WL
28 {
29 vValues.reserve(size);
30 vValues.push_back(initial_value);
31 vSorted = vValues;
32 }
33
34 void input(T value)
35 {
20e01b1a 36 if (vValues.size() == nSize) {
d1e26d4e
WL
37 vValues.erase(vValues.begin());
38 }
39 vValues.push_back(value);
40
41 vSorted.resize(vValues.size());
42 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
43 std::sort(vSorted.begin(), vSorted.end());
44 }
45
46 T median() const
47 {
48 int size = vSorted.size();
20e01b1a
PW
49 assert(size > 0);
50 if (size & 1) // Odd number of elements
d1e26d4e 51 {
20e01b1a
PW
52 return vSorted[size / 2];
53 } else // Even number of elements
d1e26d4e 54 {
20e01b1a 55 return (vSorted[size / 2 - 1] + vSorted[size / 2]) / 2;
d1e26d4e
WL
56 }
57 }
58
59 int size() const
60 {
61 return vValues.size();
62 }
63
20e01b1a 64 std::vector<T> sorted() const
d1e26d4e
WL
65 {
66 return vSorted;
67 }
68};
69
14f888ca
WL
70/* Functions to keep track of adjusted P2P time */
71int64_t GetTimeOffset();
72int64_t GetAdjustedTime();
73void AddTimeData(const CNetAddr& ip, int64_t nTime);
74
093303a8 75#endif // BITCOIN_TIMEDATA_H
This page took 0.054369 seconds and 4 git commands to generate.