00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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