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 #include "ns3/type-name.h"
27 
28 namespace ns3 {
29 
36 //------------------------------------------------------------
37 //--------------------------------------------
38 template <typename T = uint32_t>
40  public StatisticalSummary {
41 public:
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  m_min = (i < m_min) ? i : m_min;
191  m_max = (i > m_max) ? i : m_max;
192  }
193 
194  // Calculate the variance based on equations (15) and (16) on
195  // page 216 of "The Art of Computer Programming, Volume 2",
196  // Second Edition. Donald E. Knuth. Addison-Wesley
197  // Publishing Company, 1973.
198  //
199  // The relationships between the variance, standard deviation,
200  // and s are as follows
201  //
202  // s
203  // variance = -----------
204  // count - 1
205  //
206  // -------------
207  // /
208  // standard_deviation = / variance
209  // \/
210  //
211  if (m_count == 1)
212  {
213  // Set the very first values.
214  m_meanCurr = i;
215  m_sCurr = 0;
216  m_varianceCurr = m_sCurr;
217  }
218  else
219  {
220  // Save the previous values.
221  m_meanPrev = m_meanCurr;
222  m_sPrev = m_sCurr;
223 
224  // Update the current values.
225  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
226  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
227  m_varianceCurr = m_sCurr / (m_count - 1);
228  }
229  }
230  // end MinMaxAvgTotalCalculator::Update
231 }
232 
233 template <typename T>
234 void
236 {
237  m_count = 0;
238 
239  m_total = 0;
240  m_squareTotal = 0;
241 
242  m_meanCurr = NaN;
243  m_sCurr = NaN;
244  m_varianceCurr = NaN;
245 
246  m_meanPrev = NaN;
247  m_sPrev = NaN;
248  // end MinMaxAvgTotalCalculator::Reset
249 }
250 
251 template <typename T>
252 void
254 {
255  callback.OutputStatistic (m_context, m_key, this);
256 }
257 
258 
265 //------------------------------------------------------------
266 //--------------------------------------------
267 template <typename T = uint32_t>
269 public:
272 
277  static TypeId GetTypeId (void);
278 
282  void Update ();
287  void Update (const T i);
288 
293  T GetCount () const;
294 
299  virtual void Output (DataOutputCallback &callback) const;
300 
301 protected:
302  virtual void DoDispose (void);
303 
305 
306  // end CounterCalculator
307 };
308 
309 
310 //--------------------------------------------
311 template <typename T>
313  m_count (0)
314 {
315 }
316 
317 template <typename T>
319 {
320 }
321 /* static */
322 template <typename T>
323 TypeId
325 {
326  static TypeId tid = TypeId ( ("ns3::CounterCalculator<"
327  + TypeNameGet<T> ()
328  + ">").c_str () )
329  .SetParent<Object> ()
330  .SetGroupName ("Stats")
331  .AddConstructor<CounterCalculator<T> > ()
332  ;
333  return tid;
334 }
335 
336 template <typename T>
337 void
339 {
341  // CounterCalculator::DoDispose
342 }
343 
344 template <typename T>
345 void
347 {
348  if (m_enabled) {
349  m_count++;
350  }
351  // end CounterCalculator::Update
352 }
353 
354 template <typename T>
355 void
357 {
358  if (m_enabled) {
359  m_count += i;
360  }
361  // end CounterCalculator::Update
362 }
363 
364 template <typename T>
365 T
367 {
368  return m_count;
369  // end CounterCalculator::GetCount
370 }
371 
372 template <typename T>
373 void
375 {
376  callback.OutputSingleton (m_context, m_key, m_count);
377  // end CounterCalculator::Output
378 }
379 
380 // end namespace ns3
381 };
382 
383 
384 #endif /* BASIC_DATA_CALCULATORS_H */
Template class CounterCalculator.
virtual void Output(DataOutputCallback &callback) const
Outputs the data based on the provided callback.
void Update(const T i)
Increments count by i.
virtual void DoDispose(void)
Destructor implementation.
static TypeId GetTypeId(void)
Register this type.
T m_count
Count value of CounterCalculator.
T GetCount() const
Returns the count of the CounterCalculator.
void Update()
Increments count by 1.
Calculates data during a simulation.
virtual void DoDispose(void)
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.
double getSqrSum() const
Returns the sum of squares.
T m_squareTotal
Sum of squares value of MinMaxAvgTotalCalculator.
double m_varianceCurr
Current variance of MinMaxAvgTotalCalculator.
T m_min
Minimum value of MinMaxAvgTotalCalculator.
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.
long getCount() const
Returns the count.
uint32_t m_count
Count value of MinMaxAvgTotalCalculator.
T m_max
Maximum value of MinMaxAvgTotalCalculator.
double getSum() const
Returns the sum.
double getStddev() const
Returns the standard deviation.
double getMean() const
Returns the mean value.
double m_sCurr
Current s of MinMaxAvgTotalCalculator.
double getVariance() const
Returns the current variance.
double getMax() const
Returns the maximum value.
static TypeId GetTypeId(void)
Register this type.
double getMin() const
Returns the minimum value.
void Update(const T i)
Updates all variables of MinMaxAvgTotalCalculator.
T m_total
Total value of MinMaxAvgTotalCalculator.
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:88
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:923
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double NaN
Stored representation of NaN.