00001
00007 #ifndef __HISTOGRAM_H__
00008 #define __HISTOGRAM_H__
00009
00010 #include <vector>
00011 #include "genericalgs.h"
00012
00013 namespace Histo {
00014
00015
00025 template<class Count=int, class Value=double>
00026 class OneDBinList {
00027 protected:
00028 vector<Count> counts;
00029 vector<Value> values;
00030 Count total_c;
00031 Value total_v;
00032
00033 template<class C, class V>
00034 friend OneDBinList<C,V> operator-(const OneDBinList<C,V> &A, const OneDBinList<C,V> &B);
00035
00036 public:
00037 typedef vector<Value>::size_type size_type;
00038
00039 OneDBinList(const int num_bins)
00040 : counts(num_bins), values(num_bins), total_c(0), total_v(0) { }
00041
00043
00044 OneDBinList& add(size_type bin, Value val=1) {
00045 total_c++;
00046 total_v+=val;
00047 counts[bin]++;
00048 values[bin]+=val;
00049 return *this;
00050 }
00051
00057 size_type num_bins() const { return counts.size(); }
00059 Value total_value() const { return total_v; }
00061 Count total_count() const { return total_c; }
00062
00063 Value max_value() const { return *(std::max_element(ISEQ(values))); }
00064 Value min_value() const { return *(std::min_element(ISEQ(values))); }
00065 Count max_count() const { return *(std::max_element(ISEQ(counts))); }
00066 Count min_count() const { return *(std::min_element(ISEQ(counts))); }
00068
00074 inline Value value(size_type i) const { return values[i]; }
00075 inline Count count(size_type i) const { return counts[i]; }
00076 inline double value_percent(size_type i) const { return values[i]*100.0/total_v; }
00077 inline double count_percent(size_type i) const { return counts[i]*100.0/total_c; }
00079 };
00080
00081
00082
00088 template<class Count, class Value>
00089 OneDBinList<Count,Value> operator-(const OneDBinList<Count,Value> &A, const OneDBinList<Count,Value> &B)
00090 {
00091 OneDBinList<Count,Value>::size_type N = A.num_bins();
00092 assert(N==B.num_bins());
00093
00094 OneDBinList<Count,Value> tmp(N);
00095
00096 for (OneDBinList<Count,Value>::size_type i=0; i<N; i++) {
00097 tmp.values[i] = A.values[i] - B.values[i];
00098 tmp.counts[i] = A.counts[i] - B.counts[i];
00099 }
00100
00101 tmp.total_v = A.total_v + B.total_v;
00102 tmp.total_c = A.total_c + B.total_c;
00103
00104 return tmp;
00105 }
00106
00107
00108
00115 template<class Count=int, class Value=double>
00116 class OneDHistogram : public OneDBinList<Count,Value> {
00117 public:
00118 OneDHistogram(const int num_bins)
00119 : OneDBinList<Count,Value>(num_bins) { }
00120 };
00121
00122
00123
00126 template<class Count=int, class Value=double>
00127 class OneDHistogramDifference : public OneDBinList<Count,Value> {
00128 public:
00129 OneDHistogramDifference(const OneDBinList<Count,Value> &A,
00130 const OneDBinList<Count,Value> &B)
00131 : OneDBinList<Count,Value>(A-B) { }
00132 };
00133
00134
00135
00136 }
00137 #endif