A Discrete-Event Network Simulator
API
basic-data-calculators.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Drexel University
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Joe Kopena (tjkopena@cs.drexel.edu)
18  */
19 
20 #ifndef BASIC_DATA_CALCULATORS_H
21 #define BASIC_DATA_CALCULATORS_H
22 
23 #include "data-calculator.h"
24 #include "data-output-interface.h"
25 
26 #include "ns3/type-name.h"
27 
28 namespace ns3
29 {
30 
37 //------------------------------------------------------------
38 //--------------------------------------------
39 template <typename T = uint32_t>
41 {
42  public:
45 
50  static TypeId GetTypeId();
51 
56  void Update(const T i);
60  void Reset();
61 
66  void Output(DataOutputCallback& callback) const override;
67 
72  long getCount() const override
73  {
74  return m_count;
75  }
76 
81  double getSum() const override
82  {
83  return m_total;
84  }
85 
90  double getMin() const override
91  {
92  return m_min;
93  }
94 
99  double getMax() const override
100  {
101  return m_max;
102  }
103 
108  double getMean() const override
109  {
110  return m_meanCurr;
111  }
112 
117  double getStddev() const override
118  {
119  return std::sqrt(m_varianceCurr);
120  }
121 
126  double getVariance() const override
127  {
128  return m_varianceCurr;
129  }
130 
135  double getSqrSum() const override
136  {
137  return m_squareTotal;
138  }
139 
140  protected:
144  void DoDispose() override;
145 
146  uint32_t m_count;
147 
150  T m_min;
151  T m_max;
152 
153  double m_meanCurr;
154  double m_sCurr;
155  double m_varianceCurr;
156 
157  double m_meanPrev;
158  double m_sPrev;
159 
160  // end MinMaxAvgTotalCalculator
161 };
162 
163 //----------------------------------------------
164 template <typename T>
166 {
167  m_count = 0;
168 
169  m_total = 0;
170  m_squareTotal = 0;
171 
172  m_meanCurr = NaN;
173  m_sCurr = NaN;
174  m_varianceCurr = NaN;
175 
176  m_meanPrev = NaN;
177  m_sPrev = NaN;
178 }
179 
180 template <typename T>
182 {
183 }
184 
185 template <typename T>
186 void
188 {
190  // MinMaxAvgTotalCalculator::DoDispose
191 }
192 
193 /* static */
194 template <typename T>
195 TypeId
197 {
198  static TypeId tid = TypeId("ns3::MinMaxAvgTotalCalculator<" + TypeNameGet<T>() + ">")
199  .SetParent<Object>()
200  .SetGroupName("Stats")
201  .AddConstructor<MinMaxAvgTotalCalculator<T>>();
202  return tid;
203 }
204 
205 template <typename T>
206 void
208 {
209  if (m_enabled)
210  {
211  m_count++;
212 
213  m_total += i;
214  m_squareTotal += i * i;
215 
216  if (m_count == 1)
217  {
218  m_min = i;
219  m_max = i;
220  }
221  else
222  {
223  m_min = (i < m_min) ? i : m_min;
224  m_max = (i > m_max) ? i : m_max;
225  }
226 
227  // Calculate the variance based on equations (15) and (16) on
228  // page 216 of "The Art of Computer Programming, Volume 2",
229  // Second Edition. Donald E. Knuth. Addison-Wesley
230  // Publishing Company, 1973.
231  //
232  // The relationships between the variance, standard deviation,
233  // and s are as follows
234  //
235  // s
236  // variance = -----------
237  // count - 1
238  //
239  // -------------
240  // /
241  // standard_deviation = / variance
242  // \/
243  //
244  if (m_count == 1)
245  {
246  // Set the very first values.
247  m_meanCurr = i;
248  m_sCurr = 0;
249  m_varianceCurr = m_sCurr;
250  }
251  else
252  {
253  // Save the previous values.
254  m_meanPrev = m_meanCurr;
255  m_sPrev = m_sCurr;
256 
257  // Update the current values.
258  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
259  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
260  m_varianceCurr = m_sCurr / (m_count - 1);
261  }
262  }
263  // end MinMaxAvgTotalCalculator::Update
264 }
265 
266 template <typename T>
267 void
269 {
270  m_count = 0;
271 
272  m_total = 0;
273  m_squareTotal = 0;
274 
275  m_meanCurr = NaN;
276  m_sCurr = NaN;
277  m_varianceCurr = NaN;
278 
279  m_meanPrev = NaN;
280  m_sPrev = NaN;
281  // end MinMaxAvgTotalCalculator::Reset
282 }
283 
284 template <typename T>
285 void
287 {
288  callback.OutputStatistic(m_context, m_key, this);
289 }
290 
297 //------------------------------------------------------------
298 //--------------------------------------------
299 template <typename T = uint32_t>
301 {
302  public:
304  ~CounterCalculator() override;
305 
310  static TypeId GetTypeId();
311 
315  void Update();
320  void Update(const T i);
321 
326  T GetCount() const;
327 
332  void Output(DataOutputCallback& callback) const override;
333 
334  protected:
338  void DoDispose() override;
339 
341 
342  // end CounterCalculator
343 };
344 
345 //--------------------------------------------
346 template <typename T>
348  : m_count(0)
349 {
350 }
351 
352 template <typename T>
354 {
355 }
356 
357 /* static */
358 template <typename T>
359 TypeId
361 {
362  static TypeId tid = TypeId("ns3::CounterCalculator<" + TypeNameGet<T>() + ">")
363  .SetParent<Object>()
364  .SetGroupName("Stats")
365  .AddConstructor<CounterCalculator<T>>();
366  return tid;
367 }
368 
369 template <typename T>
370 void
372 {
374  // CounterCalculator::DoDispose
375 }
376 
377 template <typename T>
378 void
380 {
381  if (m_enabled)
382  {
383  m_count++;
384  }
385  // end CounterCalculator::Update
386 }
387 
388 template <typename T>
389 void
391 {
392  if (m_enabled)
393  {
394  m_count += i;
395  }
396  // end CounterCalculator::Update
397 }
398 
399 template <typename T>
400 T
402 {
403  return m_count;
404  // end CounterCalculator::GetCount
405 }
406 
407 template <typename T>
408 void
410 {
411  callback.OutputSingleton(m_context, m_key, m_count);
412  // end CounterCalculator::Output
413 }
414 
415 // The following explicit template instantiation declaration prevents modules
416 // including this header file from implicitly instantiating CounterCalculator<uint32_t>.
417 // This would cause some examples on Windows to crash at runtime with the
418 // following error message: "Trying to allocate twice the same UID:
419 // ns3::CounterCalculator<uint32_t>"
420 extern template class CounterCalculator<uint32_t>;
421 
422 // end namespace ns3
423 }; // namespace ns3
424 
425 #endif /* BASIC_DATA_CALCULATORS_H */
Template class CounterCalculator.
void Update(const T i)
Increments count by i.
void DoDispose() override
Dispose of this Object.
T m_count
Count value of CounterCalculator.
static TypeId GetTypeId()
Register this type.
void Output(DataOutputCallback &callback) const override
Outputs the data based on the provided callback.
T GetCount() const
Returns the count of the CounterCalculator.
void Update()
Increments count by 1.
Calculates data during a simulation.
void DoDispose() override
Destructor implementation.
Callback class for the DataOutput classes.
virtual void OutputStatistic(std::string key, std::string variable, const StatisticalSummary *statSum)=0
Outputs the data from the specified StatisticalSummary.
virtual void OutputSingleton(std::string key, std::string variable, int val)=0
Associates the integer value with the variable name for a specific output format.
Template class MinMaxAvgTotalCalculator.
T m_squareTotal
Sum of squares value of MinMaxAvgTotalCalculator.
double m_varianceCurr
Current variance of MinMaxAvgTotalCalculator.
T m_min
Minimum value of MinMaxAvgTotalCalculator.
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
long getCount() const override
Returns the count.
static TypeId GetTypeId()
Register this type.
double getVariance() const override
Returns the current variance.
uint32_t m_count
Count value of MinMaxAvgTotalCalculator.
T m_max
Maximum value of MinMaxAvgTotalCalculator.
double getMax() const override
Returns the maximum value.
double m_sCurr
Current s of MinMaxAvgTotalCalculator.
double getSqrSum() const override
Returns the sum of squares.
double getSum() const override
Returns the sum.
double getStddev() const override
Returns the standard deviation.
double getMean() const override
Returns the mean value.
double getMin() const override
Returns the minimum value.
void Output(DataOutputCallback &callback) const override
Outputs the data based on the provided callback.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
T m_total
Total value of MinMaxAvgTotalCalculator.
void DoDispose() override
Dispose of this Object.
double m_meanPrev
Previous mean of MinMaxAvgTotalCalculator.
double m_meanCurr
Current mean of MinMaxAvgTotalCalculator.
double m_sPrev
Previous s of MinMaxAvgTotalCalculator.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Abstract class for calculating statistical data.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double NaN
Stored representation of NaN.