npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
CircularBuffer.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_CIRCULARBUFFER_HH_
2 #define NPSTAT_CIRCULARBUFFER_HH_
3 
4 /*!
5 // \file CircularBuffer.hh
6 //
7 // \brief Accumulates data in a circular buffer and calculates
8 // various descriptive statistics
9 //
10 // Author: I. Volobouev
11 //
12 // October 2013
13 */
14 
15 #include <vector>
16 
17 #include "npstat/nm/PreciseType.hh"
18 
19 namespace npstat {
20  template
21  <
22  typename Numeric,
23  typename Precise=typename PreciseType<Numeric>::type
24  >
26  {
27  public:
28  /** The constructor argument is the size of the buffer */
29  explicit CircularBuffer(unsigned long sz);
30 
31  /** The size of the buffer */
32  inline unsigned long size() const {return len_;}
33 
34  /** The total number of times the buffer was filled */
35  inline unsigned long nFills() const {return nfills_;}
36 
37  //@{
38  /**
39  // Element access which will fail if the buffer is not filled.
40  // The element with index 0 is the oldest one in the buffer.
41  */
42  inline const Numeric& at(const unsigned long i) const
43  {return data_.at((nfills_ + i) % len_);}
44 
45  inline Numeric& at(const unsigned long i)
46  {return data_.at((nfills_ + i) % len_);}
47  //@}
48 
49  //@{
50  /**
51  // Element access without checking that the buffer is filled.
52  // The element with index 0 is the oldest one in the buffer.
53  */
54  inline const Numeric& operator[](const unsigned long i) const
55  {return data_[(nfills_ + i) % len_];}
56 
57  inline Numeric& operator[](const unsigned long i)
58  {return data_[(nfills_ + i) % len_];}
59  //@}
60 
61  /** Check if the buffer is empty */
62  inline bool empty() const {return !nfills_;}
63 
64  /** Check if the buffer has been completely filled */
65  inline bool filled() const {return nfills_ >= len_;}
66 
67  /** Comparison for equality */
68  bool operator==(const CircularBuffer& r) const;
69 
70  /** Logical negation of operator== */
71  bool operator!=(const CircularBuffer& r) const;
72 
73  //@{
74  /** Accumulate the sample */
75  void accumulate(const Numeric& value);
76 
77  inline CircularBuffer& operator+=(const Numeric& r)
78  {accumulate(r); return *this;}
79  //@}
80 
81  /** Clear all accumulated data */
82  inline void reset() {data_.clear(); nfills_ = 0;}
83 
84  /** Minimum value in the accumulated sample */
85  Numeric min() const;
86 
87  /** Maximum value in the accumulated sample */
88  Numeric max() const;
89 
90  /** Sum of the accumulated values */
91  Precise sum() const;
92 
93  /** Accumulated sample average */
94  Precise mean() const;
95 
96  /** Estimate of the population standard deviation */
97  Precise stdev() const;
98 
99  /** Uncertainty of the population mean */
100  Precise meanUncertainty() const;
101 
102  private:
103  CircularBuffer();
104 
105  std::vector<Numeric> data_;
106  unsigned long len_;
107  unsigned long nfills_;
108  };
109 }
110 
111 #include "npstat/stat/CircularBuffer.icc"
112 
113 #endif // NPSTAT_CIRCULARBUFFER_HH_
Compile-time deduction of an appropriate precise numeric type.
Definition: CircularBuffer.hh:26
CircularBuffer(unsigned long sz)
const Numeric & operator[](const unsigned long i) const
Definition: CircularBuffer.hh:54
void reset()
Definition: CircularBuffer.hh:82
bool filled() const
Definition: CircularBuffer.hh:65
bool operator==(const CircularBuffer &r) const
const Numeric & at(const unsigned long i) const
Definition: CircularBuffer.hh:42
void accumulate(const Numeric &value)
unsigned long nFills() const
Definition: CircularBuffer.hh:35
Precise stdev() const
bool empty() const
Definition: CircularBuffer.hh:62
bool operator!=(const CircularBuffer &r) const
Numeric min() const
unsigned long size() const
Definition: CircularBuffer.hh:32
Precise mean() const
Precise meanUncertainty() const
Precise sum() const
Numeric max() const
Definition: AbsArrayProjector.hh:14