Comparator.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #ifndef __COMPARATOR_H
00025 #define __COMPARATOR_H
00026 
00027 #include <OverlayKey.h>
00028 
00032 template<class T>
00033 class Comparator
00034 {
00035 public:
00039     virtual ~Comparator()
00040     {}
00041 
00051     virtual int compare( const T& lhs, const T& rhs ) const
00052     {
00053         return lhs.compareTo(rhs);
00054     }
00055 };
00056 
00060 class KeyComparator : public Comparator<OverlayKey>
00061     {}
00062 ;
00063 
00067 class KeyStdMetric
00068 {
00069 public:
00077     inline OverlayKey distance(const OverlayKey& x,
00078                                const OverlayKey& y) const
00079     {
00080         return x > y ? (x-y) : (y-x);
00081     }
00082 };
00083 
00087 class KeyXorMetric
00088 {
00089 public:
00097     inline OverlayKey distance(const OverlayKey& x,
00098                                const OverlayKey& y) const
00099     {
00100         return x^y;
00101     }
00102 };
00103 
00104 
00108 class KeyRingMetric
00109 {
00110 public:
00118     inline OverlayKey distance(const OverlayKey& x,
00119                                const OverlayKey& y) const
00120     {
00121         OverlayKey dist1(x - y);
00122         OverlayKey dist2(y - x);
00123 
00124         if (dist1 > dist2)
00125             return dist2;
00126         else
00127             return dist1;
00128     }
00129 };
00130 
00131 
00135 class KeyUniRingMetric
00136 {
00137 public:
00145     inline OverlayKey distance(const OverlayKey& x,
00146                                const OverlayKey& y) const
00147     {
00148         return y-x;
00149     }
00150 };
00151 
00152 
00156 class KeyPrefixMetric
00157 {
00158 public:
00159     KeyPrefixMetric()
00160     {
00161         bitsPerDigit = 1;
00162     }
00163 
00171     inline OverlayKey distance(const OverlayKey& x,
00172                                const OverlayKey& y) const
00173     {
00174         return OverlayKey::getLength() / bitsPerDigit
00175                - x.sharedPrefixLength(y, bitsPerDigit);
00176     }
00177 
00178     inline void setBitsPerDigit(uint32_t bitsPerDigit)
00179     {
00180         this->bitsPerDigit = bitsPerDigit;
00181     }
00182 
00183 private:
00184     uint32_t bitsPerDigit;
00185 };
00186 
00190 template<class Metric = KeyStdMetric>
00191 class KeyDistanceComparator : public Comparator<OverlayKey>
00192 {
00193 private:
00194     Metric m; 
00195     OverlayKey key; 
00196 public:
00197 
00201     KeyDistanceComparator( const OverlayKey& relativeKey )
00202     {
00203         this->key = relativeKey;
00204     }
00205 
00215     int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const
00216     {
00217         return m.distance(lhs, key).compareTo(m.distance(rhs, key));
00218     }
00219 };
00220 
00221 template<>
00222 class KeyDistanceComparator<KeyPrefixMetric> : public Comparator<OverlayKey>
00223 {
00224 private:
00225     KeyPrefixMetric m; 
00226     OverlayKey key; 
00227 public:
00228 
00232     KeyDistanceComparator(const OverlayKey& relativeKey, uint32_t bitsPerDigit = 1)
00233     {
00234         key = relativeKey;
00235         m.setBitsPerDigit(bitsPerDigit);
00236     }
00246     int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const
00247     {
00248         return m.distance(lhs, key).compareTo(m.distance(rhs, key));
00249     }
00250 };
00251 
00252 #endif
00253 

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5