A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
basic-data-calculators.h
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 
34 //------------------------------------------------------------
35 //--------------------------------------------
36 template <typename T = uint32_t>
38  public StatisticalSummary {
39 public:
41  virtual ~MinMaxAvgTotalCalculator();
42 
43  void Update (const T i);
44  void Reset ();
45 
46  virtual void Output (DataOutputCallback &callback) const;
47 
48  long getCount () const { return m_count; }
49  double getSum () const { return m_total; }
50  double getMin () const { return m_min; }
51  double getMax () const { return m_max; }
52  double getMean () const { return m_meanCurr; }
53  double getStddev () const { return std::sqrt (m_varianceCurr); }
54  double getVariance () const { return m_varianceCurr; }
55  double getSqrSum () const { return m_squareTotal; }
56 
57 protected:
58  virtual void DoDispose (void);
59 
60  uint32_t m_count;
61 
62  T m_total;
63  T m_squareTotal;
64  T m_min;
65  T m_max;
66 
67  double m_meanCurr;
68  double m_sCurr;
69  double m_varianceCurr;
70 
71  double m_meanPrev;
72  double m_sPrev;
73 
74  // end MinMaxAvgTotalCalculator
75 };
76 
77 //----------------------------------------------
78 template <typename T>
79 MinMaxAvgTotalCalculator<T>::MinMaxAvgTotalCalculator()
80 {
81  m_count = 0;
82 
83  m_total = 0;
84  m_squareTotal = 0;
85 
86  m_meanCurr = NaN;
87  m_sCurr = NaN;
88  m_varianceCurr = NaN;
89 
90  m_meanPrev = NaN;
91  m_sPrev = NaN;
92 }
93 
94 template <typename T>
95 MinMaxAvgTotalCalculator<T>::~MinMaxAvgTotalCalculator()
96 {
97 }
98 template <typename T>
99 void
101 {
103  // MinMaxAvgTotalCalculator::DoDispose
104 }
105 
106 template <typename T>
107 void
109 {
110  if (m_enabled) {
111  m_count++;
112 
113  m_total += i;
114  m_squareTotal += i*i;
115 
116  if (m_count == 1)
117  {
118  m_min = i;
119  m_max = i;
120  }
121  else
122  {
123  if (i < m_min)
124  {
125  m_min = i;
126  }
127  if (i > m_max)
128  {
129  m_max = i;
130  }
131  }
132 
133  // Calculate the variance based on equations (15) and (16) on
134  // page 216 of "The Art of Computer Programming, Volume 2",
135  // Second Edition. Donald E. Knuth. Addison-Wesley
136  // Publishing Company, 1973.
137  //
138  // The relationships between the variance, standard deviation,
139  // and s are as follows
140  //
141  // s
142  // variance = -----------
143  // count - 1
144  //
145  // -------------
146  // /
147  // standard_deviation = / variance
148  // \/
149  //
150  if (m_count == 1)
151  {
152  // Set the very first values.
153  m_meanCurr = i;
154  m_sCurr = 0;
155  m_varianceCurr = m_sCurr;
156  }
157  else
158  {
159  // Save the previous values.
160  m_meanPrev = m_meanCurr;
161  m_sPrev = m_sCurr;
162 
163  // Update the current values.
164  m_meanCurr = m_meanPrev + (i - m_meanPrev) / m_count;
165  m_sCurr = m_sPrev + (i - m_meanPrev) * (i - m_meanCurr);
166  m_varianceCurr = m_sCurr / (m_count - 1);
167  }
168  }
169  // end MinMaxAvgTotalCalculator::Update
170 }
171 
172 template <typename T>
173 void
174 MinMaxAvgTotalCalculator<T>::Reset ()
175 {
176  m_count = 0;
177 
178  m_total = 0;
179  m_squareTotal = 0;
180 
181  m_meanCurr = NaN;
182  m_sCurr = NaN;
183  m_varianceCurr = NaN;
184 
185  m_meanPrev = NaN;
186  m_sPrev = NaN;
187  // end MinMaxAvgTotalCalculator::Reset
188 }
189 
190 template <typename T>
191 void
192 MinMaxAvgTotalCalculator<T>::Output (DataOutputCallback &callback) const
193 {
194  callback.OutputStatistic (m_context, m_key, this);
195 }
196 
197 
202 //------------------------------------------------------------
203 //--------------------------------------------
204 template <typename T = uint32_t>
206 public:
208  virtual ~CounterCalculator();
209 
210  void Update ();
211  void Update (const T i);
212 
213  T GetCount () const;
214 
215  virtual void Output (DataOutputCallback &callback) const;
216 
217 protected:
218  virtual void DoDispose (void);
219 
220  T m_count;
221 
222  // end CounterCalculator
223 };
224 
225 
226 //--------------------------------------------
227 template <typename T>
229  m_count (0)
230 {
231 }
232 
233 template <typename T>
234 CounterCalculator<T>::~CounterCalculator()
235 {
236 }
237 template <typename T>
238 void
240 {
242  // CounterCalculator::DoDispose
243 }
244 
245 template <typename T>
246 void
248 {
249  if (m_enabled) {
250  m_count++;
251  }
252  // end CounterCalculator::Update
253 }
254 
255 template <typename T>
256 void
257 CounterCalculator<T>::Update (const T i)
258 {
259  if (m_enabled) {
260  m_count += i;
261  }
262  // end CounterCalculator::Update
263 }
264 
265 template <typename T>
266 T
267 CounterCalculator<T>::GetCount () const
268 {
269  return m_count;
270  // end CounterCalculator::GetCount
271 }
272 
273 template <typename T>
274 void
275 CounterCalculator<T>::Output (DataOutputCallback &callback) const
276 {
277  callback.OutputSingleton (m_context, m_key, m_count);
278  // end CounterCalculator::Output
279 }
280 
281 // end namespace ns3
282 };
283 
284 
285 #endif /* BASIC_DATA_CALCULATORS_H */
virtual void DoDispose(void)