00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _COUNTER_HPP
00014 #define _COUNTER_HPP
00015
00016
00018
00025 class Counter {
00026 public:
00027
00028 virtual ~Counter() {}
00029
00030
00032 virtual double count(int eventIndex) = 0;
00034 virtual double sum() = 0;
00035
00037 virtual void startIteration() = 0;
00038 virtual bool hasMore() = 0;
00039 virtual void nextCount(int &eventIndex, double &count) = 0;
00040 };
00041
00043 class ModifiableCounter : public Counter {
00044 public:
00045
00046 virtual ~ModifiableCounter() {}
00047 virtual void incCount(int eventIndex, double count) = 0;
00048 virtual void setCount(int eventIndex, double count) = 0;
00049 };
00050
00052
00053 template<class T>
00054 class ArrayCounter : public ModifiableCounter {
00055 public:
00056
00057 ArrayCounter(int size) : sz(size), ct(new T[size]), total(0) {
00058 for (int i=0; i<size; i++) ct[i]=0;
00059 }
00060 virtual ~ArrayCounter() { delete [] ct;}
00061
00063 virtual double count(int eventIndex) {
00064 return ct[eventIndex];
00065 }
00066
00068 virtual double sum() {
00069 return total;
00070 }
00071
00072 virtual void incCount(int eventIndex, double count) {
00073 ct[eventIndex] += (T)count;
00074 total += (T)count;
00075 }
00076
00077 virtual void setCount(int eventIndex, double count) {
00078 total = total - ct[eventIndex]+ (T)count;
00079 ct[eventIndex] = (T)count;
00080 }
00081
00082
00083 virtual void startIteration() {
00084 pos=0;
00085 }
00086
00087 virtual bool hasMore() {
00088 while ((pos < sz) && (ct[pos] == 0))
00089 pos++;
00090 return (pos<sz);
00091 }
00092
00093 virtual void nextCount(int &eventIndex, double &count) {
00094 eventIndex = pos;
00095 count = ct[pos];
00096 pos++;
00097 }
00098
00099 protected:
00100 T *ct;
00101 int sz;
00102 T total;
00103 int pos;
00104 };
00105
00106 #endif
00107