00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __LSHKIT_HISTOGRAM__
00021 #define __LSHKIT_HISTOGRAM__
00022
00083 namespace lshkit {
00084
00086 template <typename LSH>
00087 class Histogram
00088 {
00089 BOOST_CONCEPT_ASSERT((LshConcept<LSH>));
00090
00091 std::vector<LSH> lsh_;
00092
00093 unsigned M_, N_;
00094 unsigned dim_;
00095 unsigned unit_;
00096 public:
00097 typedef typename LSH::Parameter Parameter;
00098 typedef typename LSH::Domain Domain;
00099
00102 Histogram () {
00103 }
00104
00111 template <typename RNG>
00112 void reset(unsigned M, unsigned N, Parameter param, RNG &rng)
00113 {
00114 lsh_.resize(M * N);
00115 M_ = M;
00116 N_ = N;
00117 for (unsigned i = 0; i < lsh_.size(); ++i)
00118 {
00119 lsh_[i].reset(param, rng);
00120 }
00121 unit_ = lsh_[0].getRange();
00122 dim_ = N_ * unit_;
00123 }
00124
00131 template <typename RNG>
00132 Histogram(unsigned M, unsigned N, Parameter parameter, RNG &rng)
00133 : lsh_(M * N), M_(M), N_(N)
00134 {
00135 for (unsigned i = 0; i < lsh_.size(); ++i)
00136 {
00137 lsh_[i].reset(parameter, rng);
00138 }
00139 unit_ = lsh_[0].getRange();
00140 dim_ = N_ * unit_;
00141 }
00142
00144 unsigned dim () const
00145 {
00146 return dim_;
00147 }
00148
00150
00153 void zero (float *out) const
00154 {
00155 std::fill(out, out + dim_, 0);
00156 }
00157
00159
00164 void add (float *out, Domain in, float weight = 1.0) const
00165 {
00166 unsigned k = 0;
00167 unsigned base = 0;
00168 for (unsigned i = 0; i < N_; ++i)
00169 {
00170 for (unsigned j = 0; j < M_; ++j)
00171 {
00172 unsigned index = base + (lsh_[k])(in);
00173 out[index] += weight;
00174 ++k;
00175 }
00176 base += unit_;
00177 }
00178 }
00179 };
00180
00181 }
00182
00183 #endif