include/lshkit/histogram.h

Go to the documentation of this file.
00001 /* 
00002     Copyright (C) 2008 Wei Dong <wdong@princeton.edu>. All Rights Reserved.
00003   
00004     This file is part of LSHKIT.
00005   
00006     LSHKIT is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     LSHKIT is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with LSHKIT.  If not, see <http://www.gnu.org/licenses/>.
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

Get LSHKIT at SourceForge.net. Fast, secure and Free Open Source software downloads doxygen