LCOV - code coverage report
Current view: top level - src - timedata.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 31 31 100.0 %
Date: 2021-06-29 14:35:33 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 7 8 87.5 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2014-2018 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.
#       4                 :            : 
#       5                 :            : #ifndef BITCOIN_TIMEDATA_H
#       6                 :            : #define BITCOIN_TIMEDATA_H
#       7                 :            : 
#       8                 :            : #include <algorithm>
#       9                 :            : #include <assert.h>
#      10                 :            : #include <stdint.h>
#      11                 :            : #include <vector>
#      12                 :            : 
#      13                 :            : static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT = 70 * 60;
#      14                 :            : 
#      15                 :            : class CNetAddr;
#      16                 :            : 
#      17                 :            : /**
#      18                 :            :  * Median filter over a stream of values.
#      19                 :            :  * Returns the median of the last N numbers
#      20                 :            :  */
#      21                 :            : template <typename T>
#      22                 :            : class CMedianFilter
#      23                 :            : {
#      24                 :            : private:
#      25                 :            :     std::vector<T> vValues;
#      26                 :            :     std::vector<T> vSorted;
#      27                 :            :     unsigned int nSize;
#      28                 :            : 
#      29                 :            : public:
#      30                 :            :     CMedianFilter(unsigned int _size, T initial_value) : nSize(_size)
#      31                 :        386 :     {
#      32                 :        386 :         vValues.reserve(_size);
#      33                 :        386 :         vValues.push_back(initial_value);
#      34                 :        386 :         vSorted = vValues;
#      35                 :        386 :     }
#      36                 :            : 
#      37                 :            :     void input(T value)
#      38                 :        794 :     {
#      39 [ +  + ][ +  + ]:        794 :         if (vValues.size() == nSize) {
#      40                 :          6 :             vValues.erase(vValues.begin());
#      41                 :          6 :         }
#      42                 :        794 :         vValues.push_back(value);
#      43                 :            : 
#      44                 :        794 :         vSorted.resize(vValues.size());
#      45                 :        794 :         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
#      46                 :        794 :         std::sort(vSorted.begin(), vSorted.end());
#      47                 :        794 :     }
#      48                 :            : 
#      49                 :            :     T median() const
#      50                 :        210 :     {
#      51                 :        210 :         int vSortedSize = vSorted.size();
#      52                 :        210 :         assert(vSortedSize > 0);
#      53 [ +  + ][ +  - ]:        210 :         if (vSortedSize & 1) // Odd number of elements
#      54                 :        206 :         {
#      55                 :        206 :             return vSorted[vSortedSize / 2];
#      56                 :        206 :         } else // Even number of elements
#      57                 :          4 :         {
#      58                 :          4 :             return (vSorted[vSortedSize / 2 - 1] + vSorted[vSortedSize / 2]) / 2;
#      59                 :          4 :         }
#      60                 :        210 :     }
#      61                 :            : 
#      62                 :            :     int size() const
#      63                 :       1958 :     {
#      64                 :       1958 :         return vValues.size();
#      65                 :       1958 :     }
#      66                 :            : 
#      67                 :            :     std::vector<T> sorted() const
#      68                 :        196 :     {
#      69                 :        196 :         return vSorted;
#      70                 :        196 :     }
#      71                 :            : };
#      72                 :            : 
#      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);
#      77                 :            : 
#      78                 :            : #endif // BITCOIN_TIMEDATA_H

Generated by: LCOV version 1.14