Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

comparer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <functional>
35 
36 #include <ogdf/basic/exceptions.h>
37 
38 namespace ogdf {
39 
41 
58 template<typename E> class StdComparer
59 {
60 public:
61  static bool less(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
62  static bool leq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
63  static bool greater(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
64  static bool geq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
65  static bool equal(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
66 };
67 
69 
72 #define OGDF_STD_COMPARER(type) \
73  template<> class StdComparer<type> \
74  { \
75  public: \
76  static bool less (const type &x, const type &y) { return x < y; } \
77  static bool leq (const type &x, const type &y) { return x <= y; } \
78  static bool greater(const type &x, const type &y) { return x > y; } \
79  static bool geq (const type &x, const type &y) { return x >= y; } \
80  static bool equal (const type &x, const type &y) { return x == y; } \
81  };
82 
83 OGDF_STD_COMPARER(short)
85 OGDF_STD_COMPARER(float)
86 OGDF_STD_COMPARER(double)
87 
88 template<> class StdComparer<bool> {
90 public:
91  static bool less (const bool &x, const bool &y) { return !x && y; }
92  static bool leq (const bool &x, const bool &y) { return !x || y; }
93  static bool greater(const bool &x, const bool &y) { return x && !y; }
94  static bool geq (const bool &x, const bool &y) { return x || !y; }
95  static bool equal (const bool &x, const bool &y) { return x == y; }
96 };
97 
98 
100 
105 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE> >
107  using CONTENTPOINTER = CONTENTTYPE*;
108 public:
109  static bool less (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::less (*x,*y); }
110  static bool leq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::leq (*x,*y); }
111  static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::greater(*x,*y); }
112  static bool geq (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::geq (*x,*y); }
113  static bool equal (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::equal (*x,*y); }
114 };
115 
116 
118 
157 #define OGDF_AUGMENT_COMPARER(type) \
158  public: \
159  bool less(const type &x, const type &y) const { return compare(x,y) < 0; } \
160  bool leq(const type &x, const type &y) const { return compare(x,y) <= 0; } \
161  bool greater(const type &x, const type &y) const { return compare(x,y) > 0; } \
162  bool geq(const type &x, const type &y) const { return compare(x,y) >= 0; } \
163  bool equal(const type &x, const type &y) const { return compare(x,y) == 0; }
164 
166 
203 #define OGDF_AUGMENT_STATICCOMPARER(type) \
204  public: \
205  static bool less(const type &x, const type &y) { return compare(x,y) < 0; } \
206  static bool leq(const type &x, const type &y) { return compare(x,y) <= 0; } \
207  static bool greater(const type &x, const type &y) { return compare(x,y) > 0; } \
208  static bool geq(const type &x, const type &y) { return compare(x,y) >= 0; } \
209  static bool equal(const type &x, const type &y) { return compare(x,y) == 0; }
210 
211 
213 
232 template<class E> class VComparer {
233 public:
235  VComparer() { }
236 
237  virtual ~VComparer() { }
238 
240 
245  virtual int compare(const E &x, const E &y) const = 0;
246 
248  virtual bool less(const E &x, const E &y) const {
249  return compare(x,y) < 0;
250  }
251 
253  virtual bool leq(const E &x, const E &y) const {
254  return compare(x,y) <= 0;
255  }
256 
258  virtual bool greater(const E &x, const E &y) const {
259  return compare(x,y) > 0;
260  }
261 
263  virtual bool geq(const E &x, const E &y) const {
264  return compare(x,y) >= 0;
265  }
266 
268  virtual bool equal(const E &x, const E &y) const {
269  return compare(x,y) == 0;
270  }
271 };
272 
274 
278 template<class X, class Priority=double>
279 class Prioritized {
280  X x;
281  Priority p;
282 
283 public:
285  Prioritized() : x(0), p(0) { }
286 
288  Prioritized(X xt, Priority pt) : x(xt),p(pt) { }
289 
291  Prioritized(const Prioritized& P) = default;
292 
294  Priority priority() const { return p; }
295 
297  X item() const { return x;}
298 
300  void setPriority(Priority pp) { p = pp; }
301 
303  void setItem(X item) { x=item; }
304 
306  Prioritized& operator=(const Prioritized<X,Priority>& P) = default;
307 
309  bool operator<(const Prioritized<X,Priority>& P) const { return p<P.p; }
310 
312  bool operator<=(const Prioritized<X,Priority>& P) const { return p<=P.p; }
313 
315  bool operator>(const Prioritized<X,Priority>& P) const { return p>P.p; }
316 
318  bool operator>=(const Prioritized<X,Priority>& P) const { return p>=P.p; }
319 
321  bool operator==(const Prioritized<X,Priority>& P) const { return p==P.p; }
322 
324  bool operator!=(const Prioritized<X,Priority>& P) const { return p!=P.p; }
325 };
326 
327 template<class X, class Priority> class StdComparer< Prioritized<X,Priority> >
328 {
329 public:
330  static bool less (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x < y; }
331  static bool leq (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x <= y; }
332  static bool greater(const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x > y; }
333  static bool geq (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x >= y; }
334  static bool equal (const Prioritized<X,Priority> &x, const Prioritized<X,Priority> &y) { return x == y; }
335 };
336 
343 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
344 class StlLess {
345 public:
346  bool operator()(const TYPE &x, const TYPE &y) const {
347  return COMPARER::less(x, y);
348  }
349 };
350 
351 
358 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
359 class StlGreater {
360 public:
361  bool operator()(const TYPE &x, const TYPE &y) const {
362  return COMPARER::greater(x, y);
363  }
364 };
365 
376 template<typename ELEM, typename NUM, bool ascending = true>
378  using OrderFunction = std::function<NUM(const ELEM&)>;
379 
381  GenericComparer(const OrderFunction& mapToValue) : m_mapToValue(mapToValue) {}
382 
384  int compare(const ELEM& x, const ELEM& y) const {
385  NUM a = m_mapToValue(x);
386  NUM b = m_mapToValue(y);
387 
388  return a == b ? 0 : ((a < b) == ascending ? -1 : 1);
389  }
390 
392 
393 private:
395 };
396 
413 #define OGDF_DECLARE_COMPARER(NAME, TYPE, NUMBER, GET_X_ATTR) \
414 struct NAME : public GenericComparer<TYPE, NUMBER> { \
415  NAME() : GenericComparer([&](const TYPE& x) { return GET_X_ATTR; }) {} \
416 }
417 
418 }
CONTENTTYPE * CONTENTPOINTER
Definition: comparer.h:107
void setItem(X item)
Sets value x.
Definition: comparer.h:303
A static comparer which compares the target of pointers ("content"), instead of the pointer&#39;s adresse...
Definition: comparer.h:106
static bool leq(const E &x, const E &y)
Definition: comparer.h:62
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:361
void setPriority(Priority pp)
Sets priority.
Definition: comparer.h:300
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:332
static bool less(const E &x, const E &y)
Definition: comparer.h:61
int compare(const ELEM &x, const ELEM &y) const
See OGDF_AUGMENT_COMPARER.
Definition: comparer.h:384
#define OGDF_AUGMENT_COMPARER(type)
Add this macro to your class to turn it into a full comparer.
Definition: comparer.h:157
const OrderFunction m_mapToValue
Definition: comparer.h:394
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:65
Priority priority() const
Returns the key of the element.
Definition: comparer.h:294
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:346
std::function< NUM(const ELEM &)> OrderFunction
Definition: comparer.h:378
#define OGDF_STD_COMPARER(type)
Generates a specialization of the standard static comparer for type based on compare operators...
Definition: comparer.h:72
bool operator!=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:324
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition: comparer.h:248
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:111
virtual ~VComparer()
Definition: comparer.h:237
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer...
Definition: comparer.h:279
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:333
bool operator>=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:318
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:344
Standard comparer (valid as a static comparer).
Definition: comparer.h:58
static bool equal(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:113
bool operator==(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:321
static bool geq(const E &x, const E &y)
Definition: comparer.h:64
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition: comparer.h:253
Abstract base class for comparer classes.
Definition: comparer.h:232
static bool less(const bool &x, const bool &y)
Definition: comparer.h:91
static bool greater(const E &x, const E &y)
Definition: comparer.h:63
X item() const
Returns the data of the element.
Definition: comparer.h:297
Definition of exception classes.
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition: comparer.h:268
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:334
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:359
static bool leq(const bool &x, const bool &y)
Definition: comparer.h:92
GenericComparer(const OrderFunction &mapToValue)
Construct a comparer with mapping mapToValue.
Definition: comparer.h:381
static bool equal(const bool &x, const bool &y)
Definition: comparer.h:95
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:330
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition: comparer.h:258
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:110
static bool greater(const bool &x, const bool &y)
Definition: comparer.h:93
Exception thrown when a required standard comparer has not been specialized.
Definition: exceptions.h:230
static bool equal(const E &x, const E &y)
Definition: comparer.h:65
static bool geq(const bool &x, const bool &y)
Definition: comparer.h:94
VComparer()
Initializes a comparer.
Definition: comparer.h:235
Prioritized()
Constructor of empty element. Be careful!
Definition: comparer.h:285
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition: comparer.h:263
The namespace for all OGDF objects.
Compare elements based on a single comparable attribute.
Definition: comparer.h:377
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:109
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition: comparer.h:288
static bool geq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:112
bool operator>(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:315
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:331