![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_AVERAGE_HPP 00029 #define WNS_AVERAGE_HPP 00030 00031 #include <stdint.h> 00032 00033 namespace wns { 00038 class AverageBase 00039 { 00040 public: 00044 AverageBase() : 00045 mean(0.0), 00046 samples(0) 00047 { 00048 00049 } 00050 00054 virtual 00055 ~AverageBase() 00056 { 00057 00058 } 00059 00063 virtual void 00064 reset() 00065 { 00066 mean = 0.0; 00067 samples = 0; 00068 00069 } 00070 00071 protected: 00075 double mean; 00076 00081 unsigned long int samples; 00082 }; // AverageBase 00083 00088 template <typename C> 00089 class Average : 00090 public AverageBase 00091 { 00092 public: 00096 Average() : 00097 AverageBase() 00098 { 00099 00100 } 00101 00105 virtual 00106 ~Average() 00107 { 00108 00109 } 00110 00114 virtual void 00115 put(const C& c) 00116 { 00117 mean *= 1-1.0/++samples; 00118 mean += convertForAveraging(c)/samples; 00119 00120 } 00121 00125 virtual C 00126 get() const 00127 { 00128 00129 return convertForAveraged(mean); 00130 } 00131 00132 protected: 00140 C 00141 convertForAveraged(double result) const 00142 { 00143 00144 return C::convertForAveraged(result); 00145 } 00146 00154 double 00155 convertForAveraging(const C& c) const 00156 { 00157 00158 return c.convertForAveraging(); 00159 } 00160 }; // Average<C> 00161 00166 template <> 00167 class Average<double> : 00168 public AverageBase 00169 { 00170 public: 00175 virtual void 00176 put(const double& c) 00177 { 00178 mean *= 1-1.0/++samples; 00179 mean += convertForAveraging(c)/samples; 00180 00181 } 00182 00186 virtual double 00187 get() const 00188 { 00189 00190 return mean; 00191 } 00192 00193 protected: 00198 double 00199 convertForAveraged(double result) 00200 { 00201 00202 return result; 00203 } 00204 00209 double 00210 convertForAveraging(const double& c) 00211 { 00212 00213 return c; 00214 } 00215 }; // Average<double> 00216 00217 } // wns 00218 00219 #endif // NOT defined WNS_AVERAGE_HPP 00220 00221 00222 /* 00223 Local Variables: 00224 mode: c++ 00225 fill-column: 80 00226 c-basic-offset: 8 00227 c-tab-always-indent: t 00228 indent-tabs-mode: t 00229 tab-width: 8 00230 End: 00231 */
1.5.5