Open
Graph Drawing
Framework

 v.2012.07
 

comparer.h
Go to the documentation of this file.
1 /*
2  * $Revision: 2564 $
3  *
4  * last checkin:
5  * $Author: gutwenger $
6  * $Date: 2012-07-07 00:03:48 +0200 (Sa, 07. Jul 2012) $
7  ***************************************************************/
8 
44 #ifdef _MSC_VER
45 #pragma once
46 #endif
47 
48 #ifndef OGDF_COMPARER_H
49 #define OGDF_COMPARER_H
50 
51 namespace ogdf {
52 
53 //--------------------------------------------------------------------
54 // A comparer interface is has to define
55 // bool less (const E &x, const E &y);
56 // bool leq (const E &x, const E &y);
57 // bool equal(const E &x, const E &y);
58 // bool geq (const E &x, const E &y);
59 // bool greater (const E &x, const E &y);
60 //
61 // "const E &" can be replaced by "E"
62 //--------------------------------------------------------------------
63 
65 
80 template<typename E> class StdComparer
81 {
82 public:
83  static bool less(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
84  static bool leq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
85  static bool greater(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
86  static bool geq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
87  static bool equal(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
88 };
89 
91 #define OGDF_STD_COMPARER(type) \
92  template<> class StdComparer<type> \
93  { \
94  public: \
95  static bool less (const type &x, const type &y) { return x < y; } \
96  static bool leq (const type &x, const type &y) { return x <= y; } \
97  static bool greater(const type &x, const type &y) { return x > y; } \
98  static bool geq (const type &x, const type &y) { return x >= y; } \
99  static bool equal (const type &x, const type &y) { return x == y; } \
100  };
101 
103 OGDF_STD_COMPARER(float)
104 OGDF_STD_COMPARER(double)
105 
107 template<> class StdComparer<bool> {
108 public:
109  static bool less (const bool &x, const bool &y) { return !x && y; }
110  static bool leq (const bool &x, const bool &y) { return !x || y; }
111  static bool greater(const bool &x, const bool &y) { return x && !y; }
112  static bool geq (const bool &x, const bool &y) { return x || !y; }
113  static bool equal (const bool &x, const bool &y) { return x == y; }
114 };
115 
116 
118 
121 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE> >
123  typedef CONTENTTYPE* CONTENTPOINTER;
124 public:
125  static bool less (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::less (*x,*y); }
126  static bool leq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::leq (*x,*y); }
127  static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::greater(*x,*y); }
128  static bool geq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::geq (*x,*y); }
129  static bool equal (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::equal (*x,*y); }
130 };
131 
132 
134 
171 #define OGDF_AUGMENT_COMPARER(type) \
172  public: \
173  bool less(const type &x, const type &y) const { return compare(x,y) < 0; } \
174  bool leq(const type &x, const type &y) const { return compare(x,y) <= 0; } \
175  bool greater(const type &x, const type &y) const { return compare(x,y) > 0; } \
176  bool geq(const type &x, const type &y) const { return compare(x,y) >= 0; } \
177  bool equal(const type &x, const type &y) const { return compare(x,y) == 0; }
178 
180 
215 #define OGDF_AUGMENT_STATICCOMPARER(type) \
216  public: \
217  static bool less(const type &x, const type &y) { return compare(x,y) < 0; } \
218  static bool leq(const type &x, const type &y) { return compare(x,y) <= 0; } \
219  static bool greater(const type &x, const type &y) { return compare(x,y) > 0; } \
220  static bool geq(const type &x, const type &y) { return compare(x,y) >= 0; } \
221  static bool equal(const type &x, const type &y) { return compare(x,y) == 0; }
222 
223 
225 
242 template<class E> class VComparer {
243 public:
245  VComparer() { }
246 
247  virtual ~VComparer() { }
248 
250 
255  virtual int compare(const E &x, const E &y) const = 0;
256 
258  virtual bool less(const E &x, const E &y) const {
259  return compare(x,y) < 0;
260  }
261 
263  virtual bool leq(const E &x, const E &y) const {
264  return compare(x,y) <= 0;
265  }
266 
268  virtual bool greater(const E &x, const E &y) const {
269  return compare(x,y) > 0;
270  }
271 
273  virtual bool geq(const E &x, const E &y) const {
274  return compare(x,y) >= 0;
275  }
276 
278  virtual bool equal(const E &x, const E &y) const {
279  return compare(x,y) == 0;
280  }
281 }; // class VComparer
282 
283 } //namespace
284 
285 #endif /*OGF_COMPARER_H*/