A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
basic-data-calculators.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 Drexel University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Joe Kopena (tjkopena@cs.drexel.edu)
19  */
20 
21 #ifndef BASIC_DATA_CALCULATORS_H
22 #define BASIC_DATA_CALCULATORS_H
23 
24 #include "data-calculator.h"
25 #include "data-output-interface.h"
26 
27 namespace ns3 {
28 
35 //------------------------------------------------------------
36 //--------------------------------------------
37 template <typename T = uint32_t>
39  public StatisticalSummary {
40 public:
42  virtual ~MinMaxAvgTotalCalculator();
43 
48  void Update (const T i);
52  void Reset ();
53 
54  virtual void Output (DataOutputCallback &callback) const;
55 
60  long getCount () const { return m_count; }
65  double getSum () const { return m_total; }
70  double getMin () const { return m_min; }
75  double getMax () const { return m_max; }
80  double getMean () const { return m_meanCurr; }
85  double getStddev () const { return std::sqrt (m_varianceCurr); }
90  double getVariance () const { return m_varianceCurr; }
95  double getSqrSum () const { return m_squareTotal; }
96 
97 protected:
98  virtual void DoDispose (void);
99 
100  uint32_t m_count;
101 
104  T m_min;
105  T m_max;
106 
107  double m_meanCurr;
108  double m_sCurr;
109  double m_varianceCurr;
110 
111  double m_meanPrev;
112  double m_sPrev;
113 
114  // end MinMaxAvgTotalCalculator
115 };
116 
117 //----------------------------------------------
118 template <typename T>
120 {
121  m_count = 0;
122 
123  m_total = 0;
124  m_squareTotal = 0;
125 
126  m_meanCurr = NaN;
127  m_sCurr = NaN;
128  m_varianceCurr = NaN;
129 
130  m_meanPrev = NaN;
131  m_sPrev = NaN;
132 }
133 
134 template <typename T>
136 {
137 }
138 template <typename T>
139 void
141 {
143  // MinMaxAvgTotalCalculator::DoDispose
144 }
145 
146 template <typename T>
147 void
149 {
150  if (m_enabled) {
151  m_count++;
152 
153  m_total += i;
154  m_squareTotal += i*i;
155 
156  if (m_count == 1)
157  {
158  m_min = i;
159  m_max = i;
160  }
161  else
162  {
163  if (i < m_min)
164  {
165  m_min = i;
166  }
167  if (i > m_max)
168  {
169  m_max = i;
170  }
171  }
172 
173  // Calculate the variance based on equations (15) and (16) on
174  // page 216 of "The Art of Computer Programming, Volume 2",
175  // Second Edition. Donald E. Knuth. Addison-Wesley
176  // Publishing Company, 1973.
177  //
178  // The relationships between the variance, standard deviation,
179  // and s are as follows
180  //
181  // s
182  // variance = -----------
183  // count - 1
184  //
185  // -------------
186  // /
187  // standard_deviation = / variance
188  // \/
189  //
190  if (m_count == 1)
191  {
192  // Set the very first values.
193  m_meanCurr = i;
194  m_sCurr = 0;
195  m_varianceCurr = m_sCurr;
196  }
197  else
198  {
199  // Save the previous values.
200  m_meanPrev = m_meanCurr;
201  m_sPrev = m_sCurr;
202 
203  // Update the current values.
204  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
205  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
206  m_varianceCurr = m_sCurr / (m_count - 1);
207  }
208  }
209  // end MinMaxAvgTotalCalculator::Update
210 }
211 
212 template <typename T>
213 void
215 {
216  m_count = 0;
217 
218  m_total = 0;
219  m_squareTotal = 0;
220 
221  m_meanCurr = NaN;
222  m_sCurr = NaN;
223  m_varianceCurr = NaN;
224 
225  m_meanPrev = NaN;
226  m_sPrev = NaN;
227  // end MinMaxAvgTotalCalculator::Reset
228 }
229 
230 template <typename T>
231 void
233 {
234  callback.OutputStatistic (m_context, m_key, this);
235 }
236 
237 
244 //------------------------------------------------------------
245 //--------------------------------------------
246 template <typename T = uint32_t>
248 public:
250  virtual ~CounterCalculator();
251 
255  void Update ();
260  void Update (const T i);
261 
266  T GetCount () const;
267 
272  virtual void Output (DataOutputCallback &callback) const;
273 
274 protected:
275  virtual void DoDispose (void);
276 
278 
279  // end CounterCalculator
280 };
281 
282 
283 //--------------------------------------------
284 template <typename T>
286  m_count (0)
287 {
288 }
289 
290 template <typename T>
292 {
293 }
294 template <typename T>
295 void
297 {
299  // CounterCalculator::DoDispose
300 }
301 
302 template <typename T>
303 void
305 {
306  if (m_enabled) {
307  m_count++;
308  }
309  // end CounterCalculator::Update
310 }
311 
312 template <typename T>
313 void
315 {
316  if (m_enabled) {
317  m_count += i;
318  }
319  // end CounterCalculator::Update
320 }
321 
322 template <typename T>
323 T
325 {
326  return m_count;
327  // end CounterCalculator::GetCount
328 }
329 
330 template <typename T>
331 void
333 {
334  callback.OutputSingleton (m_context, m_key, m_count);
335  // end CounterCalculator::Output
336 }
337 
338 // end namespace ns3
339 };
340 
341 
342 #endif /* BASIC_DATA_CALCULATORS_H */
double getSum() const
Returns the sum.
Abstract class for calculating statistical data.
double getMax() const
Returns the maximum value.
T m_min
Minimum value of MinMaxAvgTotalCalculator.
double getMin() const
Returns the minimum value.
T GetCount() const
Returns the count of the CounterCalculator.
T m_total
Total value of MinMaxAvgTotalCalculator.
double getSqrSum() const
Returns the sum of squares.
double getVariance() const
Returns the current variance.
virtual void Output(DataOutputCallback &callback) const
Outputs data based on the provided callback.
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
const double NaN
Stored representation of NaN.
T m_count
Count value of CounterCalculator.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Template class MinMaxAvgTotalCalculator.
double m_meanPrev
Previous mean of MinMaxAvgTotalCalculator.
double m_meanCurr
Current mean of MinMaxAvgTotalCalculator.
Callback class for the DataOutput classes.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
double getStddev() const
Returns the standard deviation.
double m_varianceCurr
Current variance of MinMaxAvgTotalCalculator.
double m_sPrev
Previous s of MinMaxAvgTotalCalculator.
Calculates data during a simulation.
T m_max
Maximum value of MinMaxAvgTotalCalculator.
void Update()
Increments count by 1.
double getMean() const
Returns the mean value.
virtual void Output(DataOutputCallback &callback) const
Outputs the data based on the provided callback.
virtual void OutputStatistic(std::string key, std::string variable, const StatisticalSummary *statSum)=0
Outputs the data from the specified StatisticalSummary.
Template class CounterCalculator.
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. ...
T m_squareTotal
Sum of squares value of MinMaxAvgTotalCalculator.
double m_sCurr
Current s of MinMaxAvgTotalCalculator.
long getCount() const
Returns the count.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
uint32_t m_count
Count value of MinMaxAvgTotalCalculator.