A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
basic-data-calculators.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Drexel University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Joe Kopena (tjkopena@cs.drexel.edu)
7 */
8
9#ifndef BASIC_DATA_CALCULATORS_H
10#define BASIC_DATA_CALCULATORS_H
11
12#include "data-calculator.h"
14
15#include "ns3/type-name.h"
16
17namespace ns3
18{
19
20/**
21 * \ingroup stats
22 * \class MinMaxAvgTotalCalculator
23 * \brief Template class MinMaxAvgTotalCalculator
24 *
25 */
26//------------------------------------------------------------
27//--------------------------------------------
28template <typename T = uint32_t>
30{
31 public:
34
35 /**
36 * Register this type.
37 * \return The TypeId.
38 */
39 static TypeId GetTypeId();
40
41 /**
42 * Updates all variables of MinMaxAvgTotalCalculator
43 * \param i value of type T to use for updating the calculator
44 */
45 void Update(const T i);
46 /**
47 * Reinitializes all variables of MinMaxAvgTotalCalculator
48 */
49 void Reset();
50
51 /**
52 * Outputs the data based on the provided callback
53 * \param callback
54 */
55 void Output(DataOutputCallback& callback) const override;
56
57 /**
58 * Returns the count
59 * \return Count
60 */
61 long getCount() const override
62 {
63 return m_count;
64 }
65
66 /**
67 * Returns the sum
68 * \return Total
69 */
70 double getSum() const override
71 {
72 return m_total;
73 }
74
75 /**
76 * Returns the minimum value
77 * \return Min
78 */
79 double getMin() const override
80 {
81 return m_min;
82 }
83
84 /**
85 * Returns the maximum value
86 * \return Max
87 */
88 double getMax() const override
89 {
90 return m_max;
91 }
92
93 /**
94 * Returns the mean value
95 * \return Mean
96 */
97 double getMean() const override
98 {
99 return m_meanCurr;
100 }
101
102 /**
103 * Returns the standard deviation
104 * \return Standard deviation
105 */
106 double getStddev() const override
107 {
108 return std::sqrt(m_varianceCurr);
109 }
110
111 /**
112 * Returns the current variance
113 * \return Variance
114 */
115 double getVariance() const override
116 {
117 return m_varianceCurr;
118 }
119
120 /**
121 * Returns the sum of squares
122 * \return Sum of squares
123 */
124 double getSqrSum() const override
125 {
126 return m_squareTotal;
127 }
128
129 protected:
130 /**
131 * Dispose of this Object.
132 */
133 void DoDispose() override;
134
135 uint32_t m_count; //!< Count value of MinMaxAvgTotalCalculator
136
137 T m_total; //!< Total value of MinMaxAvgTotalCalculator
138 T m_squareTotal; //!< Sum of squares value of MinMaxAvgTotalCalculator
139 T m_min; //!< Minimum value of MinMaxAvgTotalCalculator
140 T m_max; //!< Maximum value of MinMaxAvgTotalCalculator
141
142 double m_meanCurr; //!< Current mean of MinMaxAvgTotalCalculator
143 double m_sCurr; //!< Current s of MinMaxAvgTotalCalculator
144 double m_varianceCurr; //!< Current variance of MinMaxAvgTotalCalculator
145
146 double m_meanPrev; //!< Previous mean of MinMaxAvgTotalCalculator
147 double m_sPrev; //!< Previous s of MinMaxAvgTotalCalculator
148
149 // end MinMaxAvgTotalCalculator
150};
151
152//----------------------------------------------
153template <typename T>
155{
156 m_count = 0;
157
158 m_total = 0;
159 m_squareTotal = 0;
160
161 m_meanCurr = NaN;
162 m_sCurr = NaN;
163 m_varianceCurr = NaN;
164
165 m_meanPrev = NaN;
166 m_sPrev = NaN;
167}
168
169template <typename T>
173
174template <typename T>
175void
177{
179 // MinMaxAvgTotalCalculator::DoDispose
180}
181
182/* static */
183template <typename T>
184TypeId
186{
187 static TypeId tid = TypeId("ns3::MinMaxAvgTotalCalculator<" + TypeNameGet<T>() + ">")
188 .SetParent<Object>()
189 .SetGroupName("Stats")
190 .AddConstructor<MinMaxAvgTotalCalculator<T>>();
191 return tid;
192}
193
194template <typename T>
195void
197{
198 if (m_enabled)
199 {
200 m_count++;
201
202 m_total += i;
203 m_squareTotal += i * i;
204
205 if (m_count == 1)
206 {
207 m_min = i;
208 m_max = i;
209 }
210 else
211 {
212 m_min = (i < m_min) ? i : m_min;
213 m_max = (i > m_max) ? i : m_max;
214 }
215
216 // Calculate the variance based on equations (15) and (16) on
217 // page 216 of "The Art of Computer Programming, Volume 2",
218 // Second Edition. Donald E. Knuth. Addison-Wesley
219 // Publishing Company, 1973.
220 //
221 // The relationships between the variance, standard deviation,
222 // and s are as follows
223 //
224 // s
225 // variance = -----------
226 // count - 1
227 //
228 // -------------
229 // /
230 // standard_deviation = / variance
231 // \/
232 //
233 if (m_count == 1)
234 {
235 // Set the very first values.
236 m_meanCurr = i;
237 m_sCurr = 0;
238 m_varianceCurr = m_sCurr;
239 }
240 else
241 {
242 // Save the previous values.
243 m_meanPrev = m_meanCurr;
244 m_sPrev = m_sCurr;
245
246 // Update the current values.
247 m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
248 m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
249 m_varianceCurr = m_sCurr / (m_count - 1);
250 }
251 }
252 // end MinMaxAvgTotalCalculator::Update
253}
254
255template <typename T>
256void
258{
259 m_count = 0;
260
261 m_total = 0;
262 m_squareTotal = 0;
263
264 m_meanCurr = NaN;
265 m_sCurr = NaN;
266 m_varianceCurr = NaN;
267
268 m_meanPrev = NaN;
269 m_sPrev = NaN;
270 // end MinMaxAvgTotalCalculator::Reset
271}
272
273template <typename T>
274void
276{
277 callback.OutputStatistic(m_context, m_key, this);
278}
279
280/**
281 * \ingroup stats
282 * \class CounterCalculator
283 * \brief Template class CounterCalculator
284 *
285 */
286//------------------------------------------------------------
287//--------------------------------------------
288template <typename T = uint32_t>
290{
291 public:
294
295 /**
296 * Register this type.
297 * \return The TypeId.
298 */
300
301 /**
302 * Increments count by 1
303 */
304 void Update();
305 /**
306 * Increments count by i
307 * \param i value of type T to increment count
308 */
309 void Update(const T i);
310
311 /**
312 * Returns the count of the CounterCalculator
313 * \return Count as a value of type T
314 */
315 T GetCount() const;
316
317 /**
318 * Outputs the data based on the provided callback
319 * \param callback
320 */
321 void Output(DataOutputCallback& callback) const override;
322
323 protected:
324 /**
325 * Dispose of this Object.
326 */
327 void DoDispose() override;
328
329 T m_count; //!< Count value of CounterCalculator
330
331 // end CounterCalculator
332};
333
334//--------------------------------------------
335template <typename T>
337 : m_count(0)
338{
339}
340
341template <typename T>
345
346/* static */
347template <typename T>
348TypeId
350{
351 static TypeId tid = TypeId("ns3::CounterCalculator<" + TypeNameGet<T>() + ">")
352 .SetParent<Object>()
353 .SetGroupName("Stats")
354 .AddConstructor<CounterCalculator<T>>();
355 return tid;
356}
357
358template <typename T>
359void
361{
363 // CounterCalculator::DoDispose
364}
365
366template <typename T>
367void
369{
370 if (m_enabled)
371 {
372 m_count++;
373 }
374 // end CounterCalculator::Update
375}
376
377template <typename T>
378void
380{
381 if (m_enabled)
382 {
383 m_count += i;
384 }
385 // end CounterCalculator::Update
386}
387
388template <typename T>
389T
391{
392 return m_count;
393 // end CounterCalculator::GetCount
394}
395
396template <typename T>
397void
399{
400 callback.OutputSingleton(m_context, m_key, m_count);
401 // end CounterCalculator::Output
402}
403
404// The following explicit template instantiation declaration prevents modules
405// including this header file from implicitly instantiating CounterCalculator<uint32_t>.
406// This would cause some examples on Windows to crash at runtime with the
407// following error message: "Trying to allocate twice the same UID:
408// ns3::CounterCalculator<uint32_t>"
409extern template class CounterCalculator<uint32_t>;
410
411// end namespace ns3
412}; // namespace ns3
413
414#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:78
Abstract class for calculating statistical data.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
std::string TypeNameGet()
Type name strings for AttributeValue types.
Definition type-name.h:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double NaN
Stored representation of NaN.