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 * 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"
25
26#include "ns3/type-name.h"
27
28namespace ns3
29{
30
31/**
32 * \ingroup stats
33 * \class MinMaxAvgTotalCalculator
34 * \brief Template class MinMaxAvgTotalCalculator
35 *
36 */
37//------------------------------------------------------------
38//--------------------------------------------
39template <typename T = uint32_t>
41{
42 public:
45
46 /**
47 * Register this type.
48 * \return The TypeId.
49 */
50 static TypeId GetTypeId();
51
52 /**
53 * Updates all variables of MinMaxAvgTotalCalculator
54 * \param i value of type T to use for updating the calculator
55 */
56 void Update(const T i);
57 /**
58 * Reinitializes all variables of MinMaxAvgTotalCalculator
59 */
60 void Reset();
61
62 /**
63 * Outputs the data based on the provided callback
64 * \param callback
65 */
66 void Output(DataOutputCallback& callback) const override;
67
68 /**
69 * Returns the count
70 * \return Count
71 */
72 long getCount() const override
73 {
74 return m_count;
75 }
76
77 /**
78 * Returns the sum
79 * \return Total
80 */
81 double getSum() const override
82 {
83 return m_total;
84 }
85
86 /**
87 * Returns the minimum value
88 * \return Min
89 */
90 double getMin() const override
91 {
92 return m_min;
93 }
94
95 /**
96 * Returns the maximum value
97 * \return Max
98 */
99 double getMax() const override
100 {
101 return m_max;
102 }
103
104 /**
105 * Returns the mean value
106 * \return Mean
107 */
108 double getMean() const override
109 {
110 return m_meanCurr;
111 }
112
113 /**
114 * Returns the standard deviation
115 * \return Standard deviation
116 */
117 double getStddev() const override
118 {
119 return std::sqrt(m_varianceCurr);
120 }
121
122 /**
123 * Returns the current variance
124 * \return Variance
125 */
126 double getVariance() const override
127 {
128 return m_varianceCurr;
129 }
130
131 /**
132 * Returns the sum of squares
133 * \return Sum of squares
134 */
135 double getSqrSum() const override
136 {
137 return m_squareTotal;
138 }
139
140 protected:
141 /**
142 * Dispose of this Object.
143 */
144 void DoDispose() override;
145
146 uint32_t m_count; //!< Count value of MinMaxAvgTotalCalculator
147
148 T m_total; //!< Total value of MinMaxAvgTotalCalculator
149 T m_squareTotal; //!< Sum of squares value of MinMaxAvgTotalCalculator
150 T m_min; //!< Minimum value of MinMaxAvgTotalCalculator
151 T m_max; //!< Maximum value of MinMaxAvgTotalCalculator
152
153 double m_meanCurr; //!< Current mean of MinMaxAvgTotalCalculator
154 double m_sCurr; //!< Current s of MinMaxAvgTotalCalculator
155 double m_varianceCurr; //!< Current variance of MinMaxAvgTotalCalculator
156
157 double m_meanPrev; //!< Previous mean of MinMaxAvgTotalCalculator
158 double m_sPrev; //!< Previous s of MinMaxAvgTotalCalculator
159
160 // end MinMaxAvgTotalCalculator
161};
162
163//----------------------------------------------
164template <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
180template <typename T>
182{
183}
184
185template <typename T>
186void
188{
190 // MinMaxAvgTotalCalculator::DoDispose
191}
192
193/* static */
194template <typename T>
195TypeId
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
205template <typename T>
206void
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
266template <typename T>
267void
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
284template <typename T>
285void
287{
288 callback.OutputStatistic(m_context, m_key, this);
289}
290
291/**
292 * \ingroup stats
293 * \class CounterCalculator
294 * \brief Template class CounterCalculator
295 *
296 */
297//------------------------------------------------------------
298//--------------------------------------------
299template <typename T = uint32_t>
301{
302 public:
304 ~CounterCalculator() override;
305
306 /**
307 * Register this type.
308 * \return The TypeId.
309 */
310 static TypeId GetTypeId();
311
312 /**
313 * Increments count by 1
314 */
315 void Update();
316 /**
317 * Increments count by i
318 * \param i value of type T to increment count
319 */
320 void Update(const T i);
321
322 /**
323 * Returns the count of the CounterCalculator
324 * \return Count as a value of type T
325 */
326 T GetCount() const;
327
328 /**
329 * Outputs the data based on the provided callback
330 * \param callback
331 */
332 void Output(DataOutputCallback& callback) const override;
333
334 protected:
335 /**
336 * Dispose of this Object.
337 */
338 void DoDispose() override;
339
340 T m_count; //!< Count value of CounterCalculator
341
342 // end CounterCalculator
343};
344
345//--------------------------------------------
346template <typename T>
348 : m_count(0)
349{
350}
351
352template <typename T>
354{
355}
356
357/* static */
358template <typename T>
359TypeId
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
369template <typename T>
370void
372{
374 // CounterCalculator::DoDispose
375}
376
377template <typename T>
378void
380{
381 if (m_enabled)
382 {
383 m_count++;
384 }
385 // end CounterCalculator::Update
386}
387
388template <typename T>
389void
391{
392 if (m_enabled)
393 {
394 m_count += i;
395 }
396 // end CounterCalculator::Update
397}
398
399template <typename T>
400T
402{
403 return m_count;
404 // end CounterCalculator::GetCount
405}
406
407template <typename T>
408void
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>"
420extern template class CounterCalculator<uint32_t>;
421
422// end namespace ns3
423}; // namespace ns3
424
425#endif /* BASIC_DATA_CALCULATORS_H */
Template class CounterCalculator.
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:932
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double NaN
Stored representation of NaN.