include/lshkit/metric.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 
00025 #ifndef __FERRET_METRIC__
00026 #define __FERRET_METRIC__
00027 
00028 // This file implements some common distance functions.
00029 
00030 #include <functional>
00031 
00032 namespace lshkit { namespace metric {
00033 
00035 template <typename T /* real type */>
00036 class l1 : public std::binary_function<T, T, float>
00037 {
00038     unsigned dim_;
00039 public:
00040     l1 (unsigned dim) : dim_(dim) {}
00041     float operator () (const T *first1, const T *first2) const
00042     {
00043         float r = 0.0;
00044         for (unsigned i = 0; i < dim_; ++i)
00045         {
00046             r += std::fabs(first1[i] - first2[i]);
00047         }
00048         return r;
00049     }
00050 };
00051 
00053 template <typename T /* real type */>
00054 class l2 : public std::binary_function<const T*, const T*, float>
00055 {
00056     unsigned dim_;
00057 public:
00058     l2 (unsigned dim) : dim_(dim) {}
00059     float operator () (const T *first1, const T *first2) const
00060     {
00061         float r = 0.0;
00062         for (unsigned i = 0; i < dim_; ++i)
00063         {
00064             r += sqr(first1[i] - first2[i]);
00065         }
00066         return std::sqrt(r);
00067     }
00068 };
00069 
00071 
00075 template <typename T /* real type */>
00076 class l2sqr : public std::binary_function<const T*, const T*, float>
00077 {
00078     unsigned dim_;
00079 public:
00080     l2sqr (unsigned dim) : dim_(dim) {}
00081     float operator () (const T *first1, const T *first2) const
00082     {
00083         float r = 0.0;
00084         for (unsigned i = 0; i < dim_; ++i)
00085         {
00086             r += sqr(first1[i] - first2[i]);
00087         }
00088         return r;
00089     }
00090 };
00091 
00093 template <typename T /* real type */>
00094 class max : public std::binary_function<const T*, const T*, float>
00095 {
00096     unsigned dim_;
00097 public:
00098     max (unsigned dim) : dim_(dim) {}
00099     float operator () (const T *first1, const T *first2) const
00100     {
00101         double r = std::numeric_limits<T>::min();
00102         for (unsigned i = 0; i < dim_; ++i)
00103         {
00104             r += max(r, std::fabs(first1[i] - first2[i]));
00105         }
00106         return (float)std::sqrt(r);
00107     }
00108 };
00109 
00111 
00114 struct basic_hamming
00115 {
00116     static unsigned char_bit_cnt[];
00117 
00118     template <typename B>
00119     unsigned __hamming (B a, B b)
00120     {
00121         B c = a ^ b;
00122         unsigned char *p = reinterpret_cast<unsigned char *>(&c);
00123         unsigned r = 0;
00124         for (unsigned i = 0; i < sizeof(B); i++)
00125         {
00126             r += char_bit_cnt[*p++];
00127         }
00128         return r;
00129     }
00130 
00131     unsigned __hamming (unsigned char c1, unsigned char c2) const
00132     {
00133         return char_bit_cnt[c1 ^ c2];
00134     }
00135 
00136 };
00137 
00139 
00145 template <typename T>
00146 struct hamming : public std::binary_function<const T*, const T*, float>, public basic_hamming
00147 {
00148     unsigned dim_;
00149 public:
00150 
00151     hamming(unsigned dim): dim_(dim) {}
00152     float operator () (const T *first1, const T *first2) const
00153     {
00154         unsigned r = 0;
00155         for (unsigned i = 0; i < dim_; ++i)
00156         {
00157             r += __hamming(first1[i], first2[i]);
00158         }
00159         return r;
00160     }
00161 };
00162 
00163 
00164 }}
00165 
00166 #endif
00167 
00168 

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