]> Git Repo - VerusCoin.git/blame - src/timedata.h
Implement accurate memory accounting for mempool
[VerusCoin.git] / src / timedata.h
CommitLineData
f914f1a7 1// Copyright (c) 2014 The Bitcoin Core developers
fa94b9d5 2// Distributed under the MIT software license, see the accompanying
14f888ca
WL
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
fa94b9d5
MF
15/**
16 * Median filter over a stream of values.
d1e26d4e
WL
17 * Returns the median of the last N numbers
18 */
20e01b1a
PW
19template <typename T>
20class CMedianFilter
d1e26d4e
WL
21{
22private:
23 std::vector<T> vValues;
24 std::vector<T> vSorted;
25 unsigned int nSize;
20e01b1a 26
d1e26d4e 27public:
20e01b1a 28 CMedianFilter(unsigned int size, T initial_value) : nSize(size)
d1e26d4e
WL
29 {
30 vValues.reserve(size);
31 vValues.push_back(initial_value);
32 vSorted = vValues;
33 }
34
35 void input(T value)
36 {
20e01b1a 37 if (vValues.size() == nSize) {
d1e26d4e
WL
38 vValues.erase(vValues.begin());
39 }
40 vValues.push_back(value);
41
42 vSorted.resize(vValues.size());
43 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
44 std::sort(vSorted.begin(), vSorted.end());
45 }
46
47 T median() const
48 {
49 int size = vSorted.size();
20e01b1a
PW
50 assert(size > 0);
51 if (size & 1) // Odd number of elements
d1e26d4e 52 {
20e01b1a
PW
53 return vSorted[size / 2];
54 } else // Even number of elements
d1e26d4e 55 {
20e01b1a 56 return (vSorted[size / 2 - 1] + vSorted[size / 2]) / 2;
d1e26d4e
WL
57 }
58 }
59
60 int size() const
61 {
62 return vValues.size();
63 }
64
20e01b1a 65 std::vector<T> sorted() const
d1e26d4e
WL
66 {
67 return vSorted;
68 }
69};
70
fa94b9d5 71/** Functions to keep track of adjusted P2P time */
14f888ca
WL
72int64_t GetTimeOffset();
73int64_t GetAdjustedTime();
74void AddTimeData(const CNetAddr& ip, int64_t nTime);
75
093303a8 76#endif // BITCOIN_TIMEDATA_H
This page took 0.120204 seconds and 4 git commands to generate.