BIE |
/home/weinberg/src/BIE/PopModel/HistogramCache.h00001 // This is really -*- C++ -*- 00002 00003 00004 #include "Serializable.h" 00005 00006 00007 #include <algorithm> 00008 #include <string> 00009 #include <vector> 00010 using namespace std; 00011 00012 #include "gfunction.h" 00013 00014 #include "hash_map.h" 00015 00019 class HistoKey: public Serializable 00020 { 00021 protected: 00023 vector<unsigned short> indx; 00024 00026 unsigned int dim; 00027 00028 public: 00029 00031 HistoKey() {dim=0;} 00032 00034 HistoKey(vector<unsigned short>& Indx) { 00035 dim=Indx.size(); 00036 indx = vector<unsigned short>(dim); 00037 for (unsigned int k=0; k<dim; k++) indx[k]=Indx[k]; 00038 } 00039 00041 HistoKey(const HistoKey& t) {indx=t.indx; dim=t.dim;} 00042 00044 void reset(vector<short unsigned>& Indx) { 00045 if (Indx.size() != (unsigned)dim) { 00046 dim = Indx.size(); 00047 indx = vector<unsigned short>(dim); 00048 } 00049 for (unsigned k=0; k<Indx.size(); k++) indx[k]=Indx[k]; 00050 } 00051 00053 int get_I(int i) const { return indx[i]; } 00055 vector<unsigned short> get_I() const { return indx; } 00057 int get_dim() const { return dim; } 00058 00059 // AUTO GENERATED BY ../persistence/autopersist.py 00060 private: 00061 friend class boost::serialization::access; 00062 template<class Archive> 00063 void serialize(Archive & ar, const unsigned int version) { 00064 this->pre_serialize(ar, version); 00065 try { 00066 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Serializable); 00067 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00068 } 00069 try { 00070 ar & BOOST_SERIALIZATION_NVP(indx); 00071 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00072 } 00073 try { 00074 ar & BOOST_SERIALIZATION_NVP(dim); 00075 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00076 } 00077 this->post_serialize(ar, version); 00078 } 00079 00080 }; 00081 00082 00083 size_t munge_knuth(HistoKey& x); 00084 int hash_func_knuth(HistoKey& x, int Size); 00085 int hash_func(HistoKey& x, int Size); 00086 00087 HASHMAP_NAMESPACE_BEGIN 00089 template<> 00090 struct hash<HistoKey> 00091 { 00093 size_t operator()(HistoKey s) const 00094 { 00095 return munge_knuth(s); 00096 } 00097 }; 00098 HASHMAP_NAMESPACE_END 00099 00101 struct eqhkey 00102 { 00104 bool operator()(const HistoKey& k1, const HistoKey& k2) const 00105 { 00106 bool ok = true; 00107 for (int k=0; k<k1.get_dim(); k++) { 00108 if (k1.get_I(k) != k2.get_I(k)) ok = false; 00109 } 00110 return ok; 00111 } 00112 }; 00113 00114 00115 typedef hashers<HistoKey, double, hash<HistoKey>, eqhkey>::MapType mmapHK; 00116 00121 class HistogramCache: public Serializable 00122 { 00123 private: 00124 00125 mmapHK *histo; 00126 HistoKey key; 00127 00128 vector<double> low, high, width; 00129 vector<unsigned short> ndim, indx; 00130 unsigned int dim; 00131 00132 mmapHK::iterator hitr; 00133 00134 public: 00135 00137 static int numBuckets; 00138 00140 HistogramCache(vector<double>& lo, vector<double>& hi, vector<double>& wid); 00141 00143 HistogramCache(HistogramCache& p); 00144 00146 ~HistogramCache(); 00147 00149 void addValue(vector<double>& z, double value=1); 00150 00152 double getValue(vector<unsigned short>& indx); 00153 00155 double getValue(vector<double>& z); 00156 00158 bool addToExistingValue(vector<double>& z, double value=1); 00159 00161 int getNumber(void) { return histo->size(); } 00162 00164 00165 00166 mmapHK::iterator Reset() { return (hitr = histo->begin());} 00168 mmapHK::iterator Next() { return ++hitr; } 00170 mmapHK::iterator Current() { return hitr; } 00172 00174 void printList(); 00175 00176 // AUTO GENERATED BY ../persistence/autopersist.py 00177 protected: 00178 HistogramCache() {} 00179 private: 00180 friend class boost::serialization::access; 00181 template<class Archive> 00182 void serialize(Archive & ar, const unsigned int version) { 00183 this->pre_serialize(ar, version); 00184 try { 00185 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Serializable); 00186 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00187 } 00188 try { 00189 ar & BOOST_SERIALIZATION_NVP(histo); 00190 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00191 } 00192 try { 00193 ar & BOOST_SERIALIZATION_NVP(key); 00194 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00195 } 00196 try { 00197 ar & BOOST_SERIALIZATION_NVP(low); 00198 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00199 } 00200 try { 00201 ar & BOOST_SERIALIZATION_NVP(high); 00202 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00203 } 00204 try { 00205 ar & BOOST_SERIALIZATION_NVP(width); 00206 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00207 } 00208 try { 00209 ar & BOOST_SERIALIZATION_NVP(ndim); 00210 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00211 } 00212 try { 00213 ar & BOOST_SERIALIZATION_NVP(indx); 00214 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00215 } 00216 try { 00217 ar & BOOST_SERIALIZATION_NVP(dim); 00218 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00219 } 00220 try { 00221 ar & BOOST_SERIALIZATION_NVP(numBuckets); 00222 BIE_CATCH_BOOST_SERIALIZATION_EXCEPTION; 00223 } 00224 this->post_serialize(ar, version); 00225 } 00226 00227 00228 }; 00229 Send suggestions, questions, and feedback to WEINBERG at ASTRO dot UMASS dot EDU. Documentation generated at Fri Mar 26 00:35:11 2010 by
|