User Manual, Developers Guide and API Documentation

SlidingWindow.hpp

Go to the documentation of this file.
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. 5, 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_SLIDINGWINDOW_HPP
00029 #define WNS_SLIDINGWINDOW_HPP
00030 
00031 
00032 #include <WNS/simulator/ISimulator.hpp>
00033 #include <WNS/events/scheduler/Interface.hpp>
00034 
00035 #include <WNS/Assure.hpp>
00036 
00037 #include <list>
00038 
00039 namespace wns {
00040 
00057     class SlidingWindow
00058     {
00062         struct Helper
00063         {
00064             Helper(simTimeType _time, double _value) :
00065                 time(_time),
00066                 value(_value)
00067             {
00068             }
00069 
00070             simTimeType time;
00071             double value;
00072         };
00073 
00074     public:
00080         explicit
00081         SlidingWindow(simTimeType _windowSize, bool _includeNow=false) :
00082             windowSize(_windowSize),
00083             includeNow(_includeNow),
00084             queue()
00085         {
00086             assure(this->windowSize > 0.0, "Window size must be > 0.0");
00087         }
00088 
00092         virtual
00093         ~SlidingWindow()
00094         {
00095             this->reset();
00096         }
00097 
00102         virtual void
00103         put(double value)
00104         {
00105             this->removeOutdatedValues();
00106             simTimeType now = wns::simulator::getEventScheduler()->getTime();
00107             // add new value
00108             this->queue.push_back(Helper(now, value));
00109         }
00110 
00111 
00115         virtual double
00116         getPerSecond()
00117         {
00118             return this->getAbsolute() / this->windowSize;
00119         }
00120 
00121 
00128         virtual double
00129         getAbsolute()
00130         {
00131             this->removeOutdatedValues();
00132             double absolute = 0.0;
00133             simTimeType now = wns::simulator::getEventScheduler()->getTime();
00134             for(
00135                 std::list<Helper>::const_iterator itr = this->queue.begin();
00136                 itr != this->queue.end();
00137                 ++itr)
00138             {
00139                 if ((itr->time < now) || includeNow)
00140                 {
00141                     absolute += itr->value;
00142                 }
00143             }
00144 
00145             return absolute;
00146         }
00147 
00151         virtual void
00152         reset()
00153         {
00154             queue.clear();
00155         }
00156         
00162         virtual int
00163         getNumSamples()
00164         {
00165             this->removeOutdatedValues();
00166             int numSamples = 0;
00167             simTimeType now = wns::simulator::getEventScheduler()->getTime();
00168             for(std::list<Helper>::const_iterator itr = this->queue.begin();
00169                          itr != this->queue.end();
00170                          ++itr)
00171             {
00172                 if((itr->time < now) || includeNow)
00173                 {
00174                     ++numSamples;
00175                 }
00176             }
00177             return numSamples;
00178         }
00179 
00180     private:
00181 
00185         void
00186         removeOutdatedValues()
00187         {
00188             simTimeType now = wns::simulator::getEventScheduler()->getTime();
00189             while(
00190                 (this->queue.empty() == false) &&
00191                 (this->queue.front().time < now - this->windowSize)
00192                 )
00193             {
00194                 this->queue.pop_front();
00195             }
00196         }
00197 
00198 
00199         simTimeType windowSize;
00200         
00201         bool includeNow;
00202 
00203         std::list<Helper> queue;
00204     };
00205 
00206 } // wns
00207 
00208 #endif // WNS_SLIDINGWINDOW_HPP
00209 
00210 

Generated on Fri May 25 03:31:56 2012 for openWNS by  doxygen 1.5.5