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:
112  virtual void DoDispose (void);
113 
114  uint32_t m_count;
115 
118  T m_min;
119  T m_max;
120 
121  double m_meanCurr;
122  double m_sCurr;
123  double m_varianceCurr;
124 
125  double m_meanPrev;
126  double m_sPrev;
127 
128  // end MinMaxAvgTotalCalculator
129 };
130 
131 //----------------------------------------------
132 template <typename T>
134 {
135  m_count = 0;
136 
137  m_total = 0;
138  m_squareTotal = 0;
139 
140  m_meanCurr = NaN;
141  m_sCurr = NaN;
142  m_varianceCurr = NaN;
143 
144  m_meanPrev = NaN;
145  m_sPrev = NaN;
146 }
147 
148 template <typename T>
150 {
151 }
152 
153 template <typename T>
154 void
156 {
158  // MinMaxAvgTotalCalculator::DoDispose
159 }
160 
161 /* static */
162 template <typename T>
163 TypeId
165 {
166  static TypeId tid = TypeId ("ns3::MinMaxAvgTotalCalculator<"
167  + TypeNameGet<T> ()
168  + ">")
169  .SetParent<Object> ()
170  .SetGroupName ("Stats")
171  .AddConstructor<MinMaxAvgTotalCalculator<T> > ()
172  ;
173  return tid;
174 }
175 
176 template <typename T>
177 void
179 {
180  if (m_enabled) {
181  m_count++;
182 
183  m_total += i;
184  m_squareTotal += i*i;
185 
186  if (m_count == 1)
187  {
188  m_min = i;
189  m_max = i;
190  }
191  else
192  {
193  m_min = (i < m_min) ? i : m_min;
194  m_max = (i > m_max) ? i : m_max;
195  }
196 
197  // Calculate the variance based on equations (15) and (16) on
198  // page 216 of "The Art of Computer Programming, Volume 2",
199  // Second Edition. Donald E. Knuth. Addison-Wesley
200  // Publishing Company, 1973.
201  //
202  // The relationships between the variance, standard deviation,
203  // and s are as follows
204  //
205  // s
206  // variance = -----------
207  // count - 1
208  //
209  // -------------
210  // /
211  // standard_deviation = / variance
212  // \/
213  //
214  if (m_count == 1)
215  {
216  // Set the very first values.
217  m_meanCurr = i;
218  m_sCurr = 0;
219  m_varianceCurr = m_sCurr;
220  }
221  else
222  {
223  // Save the previous values.
224  m_meanPrev = m_meanCurr;
225  m_sPrev = m_sCurr;
226 
227  // Update the current values.
228  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
229  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
230  m_varianceCurr = m_sCurr / (m_count - 1);
231  }
232  }
233  // end MinMaxAvgTotalCalculator::Update
234 }
235 
236 template <typename T>
237 void
239 {
240  m_count = 0;
241 
242  m_total = 0;
243  m_squareTotal = 0;
244 
245  m_meanCurr = NaN;
246  m_sCurr = NaN;
247  m_varianceCurr = NaN;
248 
249  m_meanPrev = NaN;
250  m_sPrev = NaN;
251  // end MinMaxAvgTotalCalculator::Reset
252 }
253 
254 template <typename T>
255 void
257 {
258  callback.OutputStatistic (m_context, m_key, this);
259 }
260 
261 
268 //------------------------------------------------------------
269 //--------------------------------------------
270 template <typename T = uint32_t>
272 public:
275 
280  static TypeId GetTypeId (void);
281 
285  void Update ();
290  void Update (const T i);
291 
296  T GetCount () const;
297 
302  virtual void Output (DataOutputCallback &callback) const;
303 
304 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  + ">")
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 */
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)
Dispose of this Object.
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)
Dispose of this Object.
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:922
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double NaN
Stored representation of NaN.