00001 #ifndef AVERAGER_CLASS
00002 #define AVERAGER_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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