counters.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 #ifdef CompileWithBeTimeUtilities
11 #endif
15 {
16 double m_tolerance;
17 size_t numPositive;
18 size_t numNegative;
19 size_t numZero;
21 
22 size_t NumPositive (){ return numPositive;}
23 size_t NumNegative (){ return numNegative;}
24 size_t NumZero (){ return numZero;}
25 
26 SignCounter () : m_tolerance (0.0) {Clear ();}
27 SignCounter (double tolerance) : m_tolerance (fabs (tolerance)) {}
28 void Announce (double value)
29  {
30  if (value > m_tolerance)
31  numPositive++;
32  else if (-value > m_tolerance)
33  numNegative++;
34  else
35  numZero++;
36  }
37 };
38 
41 
42 struct UsageSums
43 {
44 double m_sums[3];
45 double m_min;
46 double m_max;
47 
48 void ClearSums ()
49  {
50  m_sums[0] = m_sums[1] = m_sums[2] = 0;
51  m_max = -DBL_MAX;
52  m_min = DBL_MAX;
53  }
54 
56  {
57  ClearSums ();
58  }
59 
60 void Accumulate (double x)
61  {
62  m_sums[0] += 1.0;
63  m_sums[1] += x;
64  m_sums[2] += x * x;
65  if (x > m_max)
66  m_max = x;
67  if (x < m_min)
68  m_min = x;
69  }
70 
71 void AccumulateAbsBMinusA (bvector<double> const &dataA, bvector<double> const &dataB)
72  {
73  for (size_t i = 0; i < dataA.size () && i < dataB.size (); i++)
74  {
75  Accumulate (fabs (dataB[i] - dataA[i]));
76  }
77  }
78 void Accumulate (size_t x) {Accumulate ((double)x);}
79 
80 void AccumulateWeighted (double x, double weight)
81  {
82  m_sums[0] += weight;
83  m_sums[1] += weight * x;
84  m_sums[2] += weight * x * x;
85  if (x > m_max)
86  m_max = x;
87  if (x < m_min)
88  m_min = x;
89  }
90 
91 void Accumulate (UsageSums const &other)
92  {
93  m_sums[0] += other.m_sums[0];
94  m_sums[1] += other.m_sums[1];
95  m_sums[2] += other.m_sums[2];
96  if (other.m_max > m_max)
97  m_max = other.m_max;
98  if (other.m_min < m_min)
99  m_min = other.m_min;
100  }
101 
102 double Min () const { return m_min;}
103 double Max () const { return m_max;}
104 double MaxAbs () const
105  {
106  double a = fabs (m_min);
107  double b = fabs (m_max);
108  return a > b ? a : b;
109  }
110 double Sum () const {return m_sums[1];}
111 double Count () const {return m_sums[0];}
112 double Mean () const { return m_sums[0] > 0 ? m_sums[1] / m_sums[0] : 0.0;}
113 double MeanSquare () const { return m_sums[0] > 0 ? m_sums[2] / m_sums[0] : 0.0;}
114 double StandardDeviation () const
115  {
116  double a = Mean ();
117  return sqrt (MeanSquare () - a * a);
118  }
119 };
120 
121 #ifdef CompileWithBeTimeUtilities
122 struct TimeAccumulator : UsageSums
124 {
125 double m_startTime;
126 double GetTime (){return BeTimeUtilities::GetCurrentTimeAsUnixMillisDouble();}
127 void Reset (){m_startTime = GetTime ();}
128 void AccumulateAndReset (){double time = GetTime ();Accumulate (time - m_startTime);Reset ();}
129 void ClearAndReset (){ClearSums (); Reset ();}
130 };
131 #endif
132 
136 {
137 private:
138 size_t m_true;
139 size_t m_false;
140 public:
141 
142 BoolCounter () : m_true (0), m_false (0) {}
143 
145 void Clear () {m_true = m_false = 0;}
147 size_t GetNumTrue (){ return m_true;}
149 size_t GetNumFalse (){ return m_false;}
151 bool Count (bool value)
152  {
153  if (value)
154  m_true++;
155  else
156  m_false++;
157  return value;
158  }
159 };
160 
161 // Record counts of small integers that are announced over time.
162 // out of bounds data (both left and right) is accumulated as UsageSums.
164 {
165 private:
166 bvector<size_t> m_counter;
167 UsageSums m_leftData;
168 UsageSums m_rightData;
169 public:
170 // Constructor for histograms with buckets for values 0 to maxValue (inclusive).
172  : m_counter (maxValue+1, 0)
173  {
174  Clear ();
175  }
176 size_t GetCount (ptrdiff_t value) const
177  {
178  if (value < 0)
179  return (size_t)m_leftData.Count ();
180 
181  size_t index = (size_t) value;
182  if (index >= m_counter.size ())
183  return (size_t)m_rightData.Count ();
184  return m_counter[index];
185  }
186 
187 UsageSums GetLeftData () const { return m_leftData;}
188 UsageSums GetRightData () const { return m_rightData;}
189 bool HasLeftOrRightData () const { return m_leftData.Count () > 0 || m_rightData.Count () > 0;}
190 
191 // return the number of valid entries.
192 // return the low and high valid entry values
193 size_t GetValidCount (size_t &low, size_t &high) const
194  {
195  size_t i = 0;
196  size_t m = m_counter.size ();
197  for (; i < m && m_counter[i] == 0;) { i++;}
198  low = high = i;
199  size_t n = 0;
200  for (; i < m; i++)
201  {
202  if (m_counter[i] != 0)
203  {
204  high = i;
205  n += m_counter[i];
206  }
207  }
208  return n;
209  }
210 // return sum of all histogram buckets
211 UsageSums GetSums (bool includeLeft = false, bool includeRight = false) const
212  {
213  UsageSums sums;
214  for (auto value : m_counter)
215  sums.Accumulate (value);
216  if (includeLeft)
217  sums.Accumulate (m_leftData);
218  if (includeRight)
219  sums.Accumulate (m_rightData);
220  return sums;
221  }
222 
223 void Clear ()
224  {
225  m_leftData.ClearSums ();
226  m_rightData.ClearSums ();
227  for (auto &n : m_counter)
228  n = 0;
229  }
230 
231 
232 
233 void Record (size_t value)
234  {
235  if (value > m_counter.size ())
236  m_rightData.Accumulate (value);
237  else
238  m_counter[value]++;
239  }
240 // Equivalent to
241 void Record (ptrdiff_t value, size_t count)
242  {
243  if (value < 0)
244  m_leftData.AccumulateWeighted ((double)value, (double)count);
245  else
246  {
247  size_t index = value;
248  if (index > m_counter.size ())
249  m_rightData.AccumulateWeighted ((double)index, (double)count);
250  else
251  m_counter[index] += count;
252  }
253  }
254 
255 };
BoolCounter()
Definition: counters.h:142
size_t numZero
Definition: counters.h:19
Accumulate counts of true/false values passed to Count ()
Definition: counters.h:135
void Accumulate(double x)
Definition: counters.h:60
size_t GetNumFalse()
Return the number of false hits.
Definition: counters.h:149
size_t GetNumTrue()
Return the number of true hits.
Definition: counters.h:147
size_t NumZero()
Definition: counters.h:24
double m_tolerance
Definition: counters.h:16
size_t GetValidCount(size_t &low, size_t &high) const
Definition: counters.h:193
void AccumulateWeighted(double x, double weight)
Definition: counters.h:80
void Record(ptrdiff_t value, size_t count)
Definition: counters.h:241
#define END_BENTLEY_GEOMETRY_NAMESPACE
Definition: Bentley.r.h:30
UsageSums GetSums(bool includeLeft=false, bool includeRight=false) const
Definition: counters.h:211
void Record(size_t value)
Definition: counters.h:233
size_type count(const key_type &__x) const
Definition: stdcxx/bstdmap.h:277
void Accumulate(UsageSums const &other)
Definition: counters.h:91
double MeanSquare() const
Definition: counters.h:113
void ClearSums()
Definition: counters.h:48
void Clear()
Clear all counters.
Definition: counters.h:145
double MaxAbs() const
Definition: counters.h:104
double Sum() const
Definition: counters.h:110
double m_max
Definition: counters.h:46
double Mean() const
Definition: counters.h:112
size_t numPositive
Definition: counters.h:17
Definition: counters.h:163
void Accumulate(size_t x)
Definition: counters.h:78
double Max() const
Definition: counters.h:103
size_type size() const
Definition: stdcxx/bvector.h:283
UsageSums GetRightData() const
Definition: counters.h:188
bool HasLeftOrRightData() const
Definition: counters.h:189
double StandardDeviation() const
Definition: counters.h:114
double Count() const
Definition: counters.h:111
bool Count(bool value)
Increment the counter selected by the value. Return the value.
Definition: counters.h:151
size_t NumPositive()
Definition: counters.h:22
SignCounter()
Definition: counters.h:26
void Clear()
Definition: counters.h:20
UsageSums()
Definition: counters.h:55
double m_sums[3]
Definition: counters.h:44
void AccumulateAbsBMinusA(bvector< double > const &dataA, bvector< double > const &dataB)
Definition: counters.h:71
Definition: counters.h:14
void Announce(double value)
Definition: counters.h:28
#define BEGIN_BENTLEY_GEOMETRY_NAMESPACE
Definition: Bentley.r.h:29
size_t GetCount(ptrdiff_t value) const
Definition: counters.h:176
unsigned int uint32_t
Definition: Bentley.r.h:93
size_t NumNegative()
Definition: counters.h:23
void Clear()
Definition: counters.h:223
SmallIntegerHistogram(uint32_t maxValue)
Definition: counters.h:171
UsageSums GetLeftData() const
Definition: counters.h:187
double m_min
Definition: counters.h:45
double Min() const
Definition: counters.h:102
Accumulate given values.
Definition: counters.h:42
size_t numNegative
Definition: counters.h:18
SignCounter(double tolerance)
Definition: counters.h:27

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.