Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

comparer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/exceptions.h>
35 
36 namespace ogdf {
37 
39 
56 template<typename E> class StdComparer
57 {
58 public:
59  static bool less(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
60  static bool leq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
61  static bool greater(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
62  static bool geq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
63  static bool equal(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
64 };
65 
67 
70 #define OGDF_STD_COMPARER(type) \
71  template<> class StdComparer<type> \
72  { \
73  public: \
74  static bool less (const type &x, const type &y) { return x < y; } \
75  static bool leq (const type &x, const type &y) { return x <= y; } \
76  static bool greater(const type &x, const type &y) { return x > y; } \
77  static bool geq (const type &x, const type &y) { return x >= y; } \
78  static bool equal (const type &x, const type &y) { return x == y; } \
79  };
80 
81 OGDF_STD_COMPARER(short)
83 OGDF_STD_COMPARER(float)
84 OGDF_STD_COMPARER(double)
85 
86 template<> class StdComparer<bool> {
88 public:
89  static bool less (const bool &x, const bool &y) { return !x && y; }
90  static bool leq (const bool &x, const bool &y) { return !x || y; }
91  static bool greater(const bool &x, const bool &y) { return x && !y; }
92  static bool geq (const bool &x, const bool &y) { return x || !y; }
93  static bool equal (const bool &x, const bool &y) { return x == y; }
94 };
95 
96 
98 
103 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE> >
105  using CONTENTPOINTER = CONTENTTYPE*;
106 public:
107  static bool less (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::less (*x,*y); }
108  static bool leq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::leq (*x,*y); }
109  static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::greater(*x,*y); }
110  static bool geq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::geq (*x,*y); }
111  static bool equal (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::equal (*x,*y); }
112 };
113 
114 
116 
155 #define OGDF_AUGMENT_COMPARER(type) \
156  public: \
157  bool less(const type &x, const type &y) const { return compare(x,y) < 0; } \
158  bool leq(const type &x, const type &y) const { return compare(x,y) <= 0; } \
159  bool greater(const type &x, const type &y) const { return compare(x,y) > 0; } \
160  bool geq(const type &x, const type &y) const { return compare(x,y) >= 0; } \
161  bool equal(const type &x, const type &y) const { return compare(x,y) == 0; }
162 
164 
201 #define OGDF_AUGMENT_STATICCOMPARER(type) \
202  public: \
203  static bool less(const type &x, const type &y) { return compare(x,y) < 0; } \
204  static bool leq(const type &x, const type &y) { return compare(x,y) <= 0; } \
205  static bool greater(const type &x, const type &y) { return compare(x,y) > 0; } \
206  static bool geq(const type &x, const type &y) { return compare(x,y) >= 0; } \
207  static bool equal(const type &x, const type &y) { return compare(x,y) == 0; }
208 
209 
211 
230 template<class E> class VComparer {
231 public:
233  VComparer() { }
234 
235  virtual ~VComparer() { }
236 
238 
243  virtual int compare(const E &x, const E &y) const = 0;
244 
246  virtual bool less(const E &x, const E &y) const {
247  return compare(x,y) < 0;
248  }
249 
251  virtual bool leq(const E &x, const E &y) const {
252  return compare(x,y) <= 0;
253  }
254 
256  virtual bool greater(const E &x, const E &y) const {
257  return compare(x,y) > 0;
258  }
259 
261  virtual bool geq(const E &x, const E &y) const {
262  return compare(x,y) >= 0;
263  }
264 
266  virtual bool equal(const E &x, const E &y) const {
267  return compare(x,y) == 0;
268  }
269 }; // class VComparer
270 
271 
273 
277 template<class X, class Priority=double>
278 class Prioritized {
279  X x;
280  Priority p;
281 
282 public:
284  Prioritized() : x(0), p(0) { }
285 
287  Prioritized(X xt, Priority pt) : x(xt),p(pt) { }
288 
290  Prioritized(const Prioritized& P) : x(P.x),p(P.p) { }
291 
293  Priority priority() const { return p; }
294 
296  X item() const { return x;}
297 
299  void setPriority(Priority pp) { p = pp; }
300 
302  void setItem(X item) { x=item; }
303 
305  bool operator<(const Prioritized<X,Priority>& P) const { return p<P.p; }
306 
308  bool operator<=(const Prioritized<X,Priority>& P) const { return p<=P.p; }
309 
311  bool operator>(const Prioritized<X,Priority>& P) const { return p>P.p; }
312 
314  bool operator>=(const Prioritized<X,Priority>& P) const { return p>=P.p; }
315 
317  bool operator==(const Prioritized<X,Priority>& P) const { return p==P.p; }
318 
320  bool operator!=(const Prioritized<X,Priority>& P) const { return p!=P.p; }
321 };
322 
323 template<class X, class Priority> class StdComparer< Prioritized<X,Priority> >
324 {
325 public:
326  static bool less (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x < y; }
327  static bool leq (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x <= y; }
328  static bool greater(const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x > y; }
329  static bool geq (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x >= y; }
330  static bool equal (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x == y; }
331 };
332 
337 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
338 class StlLess {
339 public:
340  bool operator()(const TYPE &x, const TYPE &y) const {
341  return COMPARER::less(x, y);
342  }
343 };
344 
345 
350 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
351 class StlGreater {
352 public:
353  bool operator()(const TYPE &x, const TYPE &y) const {
354  return COMPARER::greater(x, y);
355  }
356 };
357 
358 
359 } //namespace
CONTENTTYPE * CONTENTPOINTER
Definition: comparer.h:105
void setItem(X item)
Sets value x.
Definition: comparer.h:302
A static comparer which compares the target of pointers ("content"), instead of the pointer&#39;s adresse...
Definition: comparer.h:104
static bool leq(const E &x, const E &y)
Definition: comparer.h:60
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:353
void setPriority(Priority pp)
Sets priority.
Definition: comparer.h:299
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:328
static bool less(const E &x, const E &y)
Definition: comparer.h:59
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:60
Priority priority() const
Returns the key of the element.
Definition: comparer.h:293
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:340
#define OGDF_STD_COMPARER(type)
Generates a specialization of the standard static comparer for type based on compare operators...
Definition: comparer.h:70
bool operator!=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:320
Prioritized(const Prioritized &P)
Copy-constructor.
Definition: comparer.h:290
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition: comparer.h:246
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:109
virtual ~VComparer()
Definition: comparer.h:235
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer...
Definition: comparer.h:278
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:329
bool operator>=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:314
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:338
Standard comparer (valid as a static comparer).
Definition: comparer.h:56
static bool equal(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:111
bool operator==(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:317
static bool geq(const E &x, const E &y)
Definition: comparer.h:62
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition: comparer.h:251
Abstract base class for comparer classes.
Definition: comparer.h:230
static bool less(const bool &x, const bool &y)
Definition: comparer.h:89
static bool greater(const E &x, const E &y)
Definition: comparer.h:61
X item() const
Returns the data of the element.
Definition: comparer.h:296
Definition of exception classes.
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition: comparer.h:266
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:330
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:351
static bool leq(const bool &x, const bool &y)
Definition: comparer.h:90
static bool equal(const bool &x, const bool &y)
Definition: comparer.h:93
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:326
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition: comparer.h:256
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:108
static const int X
Definition: GmlParser.h:65
static bool greater(const bool &x, const bool &y)
Definition: comparer.h:91
Exception thrown when a required standard comparer has not been specialized.
Definition: exceptions.h:252
static bool equal(const E &x, const E &y)
Definition: comparer.h:63
static bool geq(const bool &x, const bool &y)
Definition: comparer.h:92
VComparer()
Initializes a comparer.
Definition: comparer.h:233
Prioritized()
Constructor of empty element. Be careful!
Definition: comparer.h:284
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition: comparer.h:261
The namespace for all OGDF objects.
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:107
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition: comparer.h:287
static bool geq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:110
bool operator>(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:311
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:327