A Discrete-Event Network Simulator
API
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 
58  virtual void Output (DataOutputCallback &callback) const;
59 
64  long getCount () const { return m_count; }
69  double getSum () const { return m_total; }
74  double getMin () const { return m_min; }
79  double getMax () const { return m_max; }
84  double getMean () const { return m_meanCurr; }
89  double getStddev () const { return std::sqrt (m_varianceCurr); }
94  double getVariance () const { return m_varianceCurr; }
99  double getSqrSum () const { return m_squareTotal; }
100 
101 protected:
102  virtual void DoDispose (void);
103 
104  uint32_t m_count;
105 
108  T m_min;
109  T m_max;
110 
111  double m_meanCurr;
112  double m_sCurr;
113  double m_varianceCurr;
114 
115  double m_meanPrev;
116  double m_sPrev;
117 
118  // end MinMaxAvgTotalCalculator
119 };
120 
121 //----------------------------------------------
122 template <typename T>
124 {
125  m_count = 0;
126 
127  m_total = 0;
128  m_squareTotal = 0;
129 
130  m_meanCurr = NaN;
131  m_sCurr = NaN;
132  m_varianceCurr = NaN;
133 
134  m_meanPrev = NaN;
135  m_sPrev = NaN;
136 }
137 
138 template <typename T>
140 {
141 }
142 template <typename T>
143 void
145 {
147  // MinMaxAvgTotalCalculator::DoDispose
148 }
149 
150 template <typename T>
151 void
153 {
154  if (m_enabled) {
155  m_count++;
156 
157  m_total += i;
158  m_squareTotal += i*i;
159 
160  if (m_count == 1)
161  {
162  m_min = i;
163  m_max = i;
164  }
165  else
166  {
167  if (i < m_min)
168  {
169  m_min = i;
170  }
171  if (i > m_max)
172  {
173  m_max = i;
174  }
175  }
176 
177  // Calculate the variance based on equations (15) and (16) on
178  // page 216 of "The Art of Computer Programming, Volume 2",
179  // Second Edition. Donald E. Knuth. Addison-Wesley
180  // Publishing Company, 1973.
181  //
182  // The relationships between the variance, standard deviation,
183  // and s are as follows
184  //
185  // s
186  // variance = -----------
187  // count - 1
188  //
189  // -------------
190  // /
191  // standard_deviation = / variance
192  // \/
193  //
194  if (m_count == 1)
195  {
196  // Set the very first values.
197  m_meanCurr = i;
198  m_sCurr = 0;
199  m_varianceCurr = m_sCurr;
200  }
201  else
202  {
203  // Save the previous values.
204  m_meanPrev = m_meanCurr;
205  m_sPrev = m_sCurr;
206 
207  // Update the current values.
208  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
209  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
210  m_varianceCurr = m_sCurr / (m_count - 1);
211  }
212  }
213  // end MinMaxAvgTotalCalculator::Update
214 }
215 
216 template <typename T>
217 void
219 {
220  m_count = 0;
221 
222  m_total = 0;
223  m_squareTotal = 0;
224 
225  m_meanCurr = NaN;
226  m_sCurr = NaN;
227  m_varianceCurr = NaN;
228 
229  m_meanPrev = NaN;
230  m_sPrev = NaN;
231  // end MinMaxAvgTotalCalculator::Reset
232 }
233 
234 template <typename T>
235 void
237 {
238  callback.OutputStatistic (m_context, m_key, this);
239 }
240 
241 
248 //------------------------------------------------------------
249 //--------------------------------------------
250 template <typename T = uint32_t>
252 public:
254  virtual ~CounterCalculator();
255 
259  void Update ();
264  void Update (const T i);
265 
270  T GetCount () const;
271 
276  virtual void Output (DataOutputCallback &callback) const;
277 
278 protected:
279  virtual void DoDispose (void);
280 
282 
283  // end CounterCalculator
284 };
285 
286 
287 //--------------------------------------------
288 template <typename T>
290  m_count (0)
291 {
292 }
293 
294 template <typename T>
296 {
297 }
298 template <typename T>
299 void
301 {
303  // CounterCalculator::DoDispose
304 }
305 
306 template <typename T>
307 void
309 {
310  if (m_enabled) {
311  m_count++;
312  }
313  // end CounterCalculator::Update
314 }
315 
316 template <typename T>
317 void
319 {
320  if (m_enabled) {
321  m_count += i;
322  }
323  // end CounterCalculator::Update
324 }
325 
326 template <typename T>
327 T
329 {
330  return m_count;
331  // end CounterCalculator::GetCount
332 }
333 
334 template <typename T>
335 void
337 {
338  callback.OutputSingleton (m_context, m_key, m_count);
339  // end CounterCalculator::Output
340 }
341 
342 // end namespace ns3
343 };
344 
345 
346 #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 the data based on the provided callback.
void Reset()
Reinitializes all variables of MinMaxAvgTotalCalculator.
virtual void DoDispose(void)
Destructor implementation.
const double NaN
Stored representation of NaN.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
T m_count
Count value of CounterCalculator.
virtual void DoDispose(void)
Destructor implementation.
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)
Destructor implementation.
uint32_t m_count
Count value of MinMaxAvgTotalCalculator.