averager.h

Go to the documentation of this file.
00001 #ifndef AVERAGER_CLASS
00002 #define AVERAGER_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : averager                                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             * 
00009 *******************************************************************************
00010 * Copyright (c) 1997-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include <basis/array.h>
00019 
00021 
00027 template <class contents>
00028 class averager
00029 {
00030 public:
00031   averager(int entries = 100, bool compacting = true);
00033 
00042   void add(contents value, int count = 1);
00044 
00045   inline contents average() const { return average(0, length() - 1); }
00047 
00048   int samples() const;
00050 
00051   inline int length() const { return _averages.length(); }
00053 
00054   contents average(int start, int end) const;
00056 
00057   struct weighted_entry { contents value; int count; };
00059 
00062   inline weighted_entry get(int index) const { return _averages.get(index); }
00064 
00065   void compact();
00067 
00070   void check_for_compaction();
00072 
00076 private:
00077   bool _do_compaction;  
00078   array<weighted_entry> _averages;  
00079   int _entries;  
00080 };
00081 
00083 
00085 class int_averager : public averager<int>
00086 {
00087 public:
00088   int_averager(int entries = 100, bool compacting = true)
00089           : averager<int>(entries, compacting) {}
00090 };
00091 
00092 #endif
00093 

Generated on Wed Nov 19 04:28:50 2008 for HOOPLE Libraries by  doxygen 1.5.1