Raven Core  3.0.0
P2P Digital Currency
timedata.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_TIMEDATA_H
7 #define RAVEN_TIMEDATA_H
8 
9 #include <algorithm>
10 #include <assert.h>
11 #include <stdint.h>
12 #include <vector>
13 
14 static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT = 70 * 60;
15 
16 class CNetAddr;
17 
22 template <typename T>
24 {
25 private:
26  std::vector<T> vValues;
27  std::vector<T> vSorted;
28  unsigned int nSize;
29 
30 public:
31  CMedianFilter(unsigned int _size, T initial_value) : nSize(_size)
32  {
33  vValues.reserve(_size);
34  vValues.push_back(initial_value);
35  vSorted = vValues;
36  }
37 
38  void input(T value)
39  {
40  if (vValues.size() == nSize) {
41  vValues.erase(vValues.begin());
42  }
43  vValues.push_back(value);
44 
45  vSorted.resize(vValues.size());
46  std::copy(vValues.begin(), vValues.end(), vSorted.begin());
47  std::sort(vSorted.begin(), vSorted.end());
48  }
49 
50  T median() const
51  {
52  int vSortedSize = vSorted.size();
53  assert(vSortedSize > 0);
54  if (vSortedSize & 1) // Odd number of elements
55  {
56  return vSorted[vSortedSize / 2];
57  } else // Even number of elements
58  {
59  return (vSorted[vSortedSize / 2 - 1] + vSorted[vSortedSize / 2]) / 2;
60  }
61  }
62 
63  int size() const
64  {
65  return vValues.size();
66  }
67 
68  std::vector<T> sorted() const
69  {
70  return vSorted;
71  }
72 };
73 
75 int64_t GetTimeOffset();
76 int64_t GetAdjustedTime();
77 void AddTimeData(const CNetAddr& ip, int64_t nTime);
78 
79 #endif // RAVEN_TIMEDATA_H
void AddTimeData(const CNetAddr &ip, int64_t nTime)
Definition: timedata.cpp:48
Median filter over a stream of values.
Definition: timedata.h:23
std::vector< T > vSorted
Definition: timedata.h:27
std::vector< T > sorted() const
Definition: timedata.h:68
std::vector< T > vValues
Definition: timedata.h:26
int64_t GetTimeOffset()
Functions to keep track of adjusted P2P time.
Definition: timedata.cpp:30
void input(T value)
Definition: timedata.h:38
int64_t GetAdjustedTime()
Definition: timedata.cpp:36
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:32
CMedianFilter(unsigned int _size, T initial_value)
Definition: timedata.h:31
unsigned int nSize
Definition: timedata.h:28
int size() const
Definition: timedata.h:63
T median() const
Definition: timedata.h:50