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 #include "ns3/type-name.h"
27 
28 namespace ns3 {
29 
36 //------------------------------------------------------------
37 //--------------------------------------------
38 template <typename T = uint32_t>
40  public StatisticalSummary {
41 public:
43  virtual ~MinMaxAvgTotalCalculator();
44 
49  static TypeId GetTypeId (void);
50 
55  void Update (const T i);
59  void Reset ();
60 
65  virtual void Output (DataOutputCallback &callback) const;
66 
71  long getCount () const { return m_count; }
76  double getSum () const { return m_total; }
81  double getMin () const { return m_min; }
86  double getMax () const { return m_max; }
91  double getMean () const { return m_meanCurr; }
96  double getStddev () const { return std::sqrt (m_varianceCurr); }
101  double getVariance () const { return m_varianceCurr; }
106  double getSqrSum () const { return m_squareTotal; }
107 
108 protected:
109  virtual void DoDispose (void);
110 
111  uint32_t m_count;
112 
115  T m_min;
116  T m_max;
117 
118  double m_meanCurr;
119  double m_sCurr;
120  double m_varianceCurr;
121 
122  double m_meanPrev;
123  double m_sPrev;
124 
125  // end MinMaxAvgTotalCalculator
126 };
127 
128 //----------------------------------------------
129 template <typename T>
131 {
132  m_count = 0;
133 
134  m_total = 0;
135  m_squareTotal = 0;
136 
137  m_meanCurr = NaN;
138  m_sCurr = NaN;
139  m_varianceCurr = NaN;
140 
141  m_meanPrev = NaN;
142  m_sPrev = NaN;
143 }
144 
145 template <typename T>
147 {
148 }
149 
150 template <typename T>
151 void
153 {
155  // MinMaxAvgTotalCalculator::DoDispose
156 }
157 
158 /* static */
159 template <typename T>
160 TypeId
162 {
163  static TypeId tid = TypeId ( ("ns3::MinMaxAvgTotalCalculator<"
164  + TypeNameGet<T> ()
165  + ">").c_str () )
166  .SetParent<Object> ()
167  .SetGroupName ("Stats")
168  .AddConstructor<MinMaxAvgTotalCalculator<T> > ()
169  ;
170  return tid;
171 }
172 
173 template <typename T>
174 void
176 {
177  if (m_enabled) {
178  m_count++;
179 
180  m_total += i;
181  m_squareTotal += i*i;
182 
183  if (m_count == 1)
184  {
185  m_min = i;
186  m_max = i;
187  }
188  else
189  {
190  if (i < m_min)
191  {
192  m_min = i;
193  }
194  if (i > m_max)
195  {
196  m_max = i;
197  }
198  }
199 
200  // Calculate the variance based on equations (15) and (16) on
201  // page 216 of "The Art of Computer Programming, Volume 2",
202  // Second Edition. Donald E. Knuth. Addison-Wesley
203  // Publishing Company, 1973.
204  //
205  // The relationships between the variance, standard deviation,
206  // and s are as follows
207  //
208  // s
209  // variance = -----------
210  // count - 1
211  //
212  // -------------
213  // /
214  // standard_deviation = / variance
215  // \/
216  //
217  if (m_count == 1)
218  {
219  // Set the very first values.
220  m_meanCurr = i;
221  m_sCurr = 0;
222  m_varianceCurr = m_sCurr;
223  }
224  else
225  {
226  // Save the previous values.
227  m_meanPrev = m_meanCurr;
228  m_sPrev = m_sCurr;
229 
230  // Update the current values.
231  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
232  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
233  m_varianceCurr = m_sCurr / (m_count - 1);
234  }
235  }
236  // end MinMaxAvgTotalCalculator::Update
237 }
238 
239 template <typename T>
240 void
242 {
243  m_count = 0;
244 
245  m_total = 0;
246  m_squareTotal = 0;
247 
248  m_meanCurr = NaN;
249  m_sCurr = NaN;
250  m_varianceCurr = NaN;
251 
252  m_meanPrev = NaN;
253  m_sPrev = NaN;
254  // end MinMaxAvgTotalCalculator::Reset
255 }
256 
257 template <typename T>
258 void
260 {
261  callback.OutputStatistic (m_context, m_key, this);
262 }
263 
264 
271 //------------------------------------------------------------
272 //--------------------------------------------
273 template <typename T = uint32_t>
275 public:
277  virtual ~CounterCalculator();
278 
283  static TypeId GetTypeId (void);
284 
288  void Update ();
293  void Update (const T i);
294 
299  T GetCount () const;
300 
305  virtual void Output (DataOutputCallback &callback) const;
306 
307 protected:
308  virtual void DoDispose (void);
309 
311 
312  // end CounterCalculator
313 };
314 
315 
316 //--------------------------------------------
317 template <typename T>
319  m_count (0)
320 {
321 }
322 
323 template <typename T>
325 {
326 }
327 /* static */
328 template <typename T>
329 TypeId
331 {
332  static TypeId tid = TypeId ( ("ns3::CounterCalculator<"
333  + TypeNameGet<T> ()
334  + ">").c_str () )
335  .SetParent<Object> ()
336  .SetGroupName ("Stats")
337  .AddConstructor<CounterCalculator<T> > ()
338  ;
339  return tid;
340 }
341 
342 template <typename T>
343 void
345 {
347  // CounterCalculator::DoDispose
348 }
349 
350 template <typename T>
351 void
353 {
354  if (m_enabled) {
355  m_count++;
356  }
357  // end CounterCalculator::Update
358 }
359 
360 template <typename T>
361 void
363 {
364  if (m_enabled) {
365  m_count += i;
366  }
367  // end CounterCalculator::Update
368 }
369 
370 template <typename T>
371 T
373 {
374  return m_count;
375  // end CounterCalculator::GetCount
376 }
377 
378 template <typename T>
379 void
381 {
382  callback.OutputSingleton (m_context, m_key, m_count);
383  // end CounterCalculator::Output
384 }
385 
386 // end namespace ns3
387 };
388 
389 
390 #endif /* BASIC_DATA_CALCULATORS_H */
double getSum() const
Returns the sum.
Abstract class for calculating statistical data.
double getMax() const
Returns the maximum value.
static TypeId GetTypeId(void)
Register this type.
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.
T m_count
Count value of CounterCalculator.
virtual void DoDispose(void)
Destructor implementation.
static TypeId GetTypeId(void)
Register this type.
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.
A base class which provides memory management and object aggregation.
Definition: object.h:87
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.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:827
long getCount() const
Returns the count.
virtual void DoDispose(void)
Destructor implementation.
uint32_t m_count
Count value of MinMaxAvgTotalCalculator.