OverSim
Comparator.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __COMPARATOR_H
25 #define __COMPARATOR_H
26 
27 #include <OverlayKey.h>
28 #include <ProxNodeHandle.h>
29 
33 template<class T>
35 {
36 public:
40  virtual ~Comparator()
41  {}
42 
52  virtual int compare( const T& lhs, const T& rhs ) const
53  {
54  return lhs.compareTo(rhs);
55  }
56 };
57 
61 //class KeyComparator : public Comparator<OverlayKey>
62 //{
63 // //...
64 //};
65 typedef Comparator<OverlayKey> KeyComparator; //TODO needed?
66 
71 {
72 public:
80  inline OverlayKey distance(const OverlayKey& x,
81  const OverlayKey& y) const
82  {
83  return x > y ? (x-y) : (y-x);
84  }
85 };
86 
91 {
92 public:
100  inline OverlayKey distance(const OverlayKey& x,
101  const OverlayKey& y) const
102  {
103  return x^y;
104  }
105 };
106 
107 
112 {
113 public:
121  static inline OverlayKey distance(const OverlayKey& x,
122  const OverlayKey& y)
123  {
124  OverlayKey dist1(x - y);
125  OverlayKey dist2(y - x);
126 
127  if (dist1 > dist2)
128  return dist2;
129  else
130  return dist1;
131  }
132 };
133 
134 
139 {
140 public:
148  inline OverlayKey distance(const OverlayKey& x,
149  const OverlayKey& y) const
150  {
151  return y-x;
152  }
153 };
154 
159 {
160 public:
168  inline OverlayKey distance(const OverlayKey& x,
169  const OverlayKey& y) const
170  {
171  return x-y;
172  }
173 };
174 
175 
176 
181 {
182 public:
184  {
185  bitsPerDigit = 1;
186  }
187 
195  inline OverlayKey distance(const OverlayKey& x,
196  const OverlayKey& y) const
197  {
200  }
201 
202  inline void setBitsPerDigit(uint8_t bitsPerDigit)
203  {
204  this->bitsPerDigit = bitsPerDigit;
205  }
206 
207 private:
208  uint8_t bitsPerDigit;
209 };
210 
214 template<class Metric = KeyStdMetric>
215 class KeyDistanceComparator : public Comparator<OverlayKey>
216 {
217 private:
218  Metric m;
220 public:
221 
225  KeyDistanceComparator( const OverlayKey& relativeKey )
226  {
227  this->key = relativeKey;
228  }
229 
239  int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const
240  {
241  return m.distance(lhs, key).compareTo(m.distance(rhs, key));
242  }
243 };
244 
245 template<>
247 {
248 private:
251 public:
252 
256  KeyDistanceComparator(const OverlayKey& relativeKey, uint32_t bitsPerDigit = 1)
257  {
258  key = relativeKey;
259  m.setBitsPerDigit(bitsPerDigit);
260  }
270  int compare( const OverlayKey& lhs, const OverlayKey& rhs ) const
271  {
272  return m.distance(lhs, key).compareTo(m.distance(rhs, key));
273  }
274 };
275 
277 {
278  public:
280 
289  virtual int compare(const Prox& lhs, const Prox& rhs) const = 0;
290 };
291 
293 {
294  public:
295  int compare(const Prox& lhs, const Prox& rhs) const
296  {
297  // return 0 if accuracy is too low
298  if (lhs.accuracy < 0.5 || rhs.accuracy < 0.5) return 0;
299 
300  if (lhs.proximity < rhs.proximity) return -1;
301  if (lhs.proximity > rhs.proximity) return 1;
302  return 0;
303  }
304 };
305 
307 {
308  public:
310 
320  virtual int compare(const ProxKey& lhs, const ProxKey& rhs) const = 0;
321 };
322 
323 template<class Metric, class ProxComp = StdProxComparator>
325 {
326  protected:
327  Metric m;
328  ProxComp pc;
331  public:
335  ProxKeyComparator(const OverlayKey& relativeKey)
336  {
337  this->key = relativeKey;
338  }
339 };
340 
341 
342 template<>
344 {
345  protected:
350  public:
354  ProxKeyComparator(const OverlayKey& relativeKey, uint32_t bitsPerDigit = 1)
355  {
356  this->key = relativeKey;
357  m.setBitsPerDigit(bitsPerDigit);
358  }
359 };
360 
361 class KademliaPRComparator : public ProxKeyComparator<KeyPrefixMetric>
362 {
363  public:
364  KademliaPRComparator(const OverlayKey& relativeKey, uint32_t bitsPerDigit = 1)
365  : ProxKeyComparator<KeyPrefixMetric, StdProxComparator>(relativeKey, bitsPerDigit) { }
366 
367  int compare(const ProxKey& lhs, const ProxKey& rhs) const
368  {
369  int temp = m.distance(lhs.key, key).compareTo(m.distance(rhs.key, key));
370  if (temp != 0) {
371  return temp;
372  }
373  return pc.compare(lhs.prox, rhs.prox);
374  }
375 };
376 
377 class AccordionPRComparator : public ProxKeyComparator<KeyPrefixMetric>
378 {
379  public:
380  AccordionPRComparator(const OverlayKey& relativeKey, uint32_t bitsPerDigit = 1)
381  : ProxKeyComparator<KeyPrefixMetric, StdProxComparator>(relativeKey, bitsPerDigit) { }
382 
383  int compare(const ProxKey& lhs, const ProxKey& rhs) const
384  {
385  int temp = m.distance(lhs.key, key).compareTo(m.distance(rhs.key, key));
386  if (temp != 0) {
387  return temp;
388  }
389  return pc.compare(lhs.prox, rhs.prox);
390  }
391 };
392 #endif
393