Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

comparer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/exceptions.h>
35 
36 #include <functional>
37 
38 namespace ogdf {
39 template<typename E>
41 template<typename ELEM, typename NUM, bool ascending = true>
43 
45 
62 template<typename E>
63 class StdComparer {
64 public:
65  static bool less(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
66 
67  static bool leq(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
68 
69  static bool greater(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
70 
71  static bool geq(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
72 
73  static bool equal(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
74 };
75 
77 
80 #define OGDF_STD_COMPARER(type) \
81  template<> \
82  class StdComparer<type> { \
83  public: \
84  static bool less(const type& x, const type& y) { return x < y; } \
85  static bool leq(const type& x, const type& y) { return x <= y; } \
86  static bool greater(const type& x, const type& y) { return x > y; } \
87  static bool geq(const type& x, const type& y) { return x >= y; } \
88  static bool equal(const type& x, const type& y) { return x == y; } \
89  };
90 
91 OGDF_STD_COMPARER(short)
93 OGDF_STD_COMPARER(float)
94 OGDF_STD_COMPARER(double)
95 
96 template<>
98 class StdComparer<bool> {
99 public:
100  static bool less(const bool& x, const bool& y) { return !x && y; }
101 
102  static bool leq(const bool& x, const bool& y) { return !x || y; }
103 
104  static bool greater(const bool& x, const bool& y) { return x && !y; }
105 
106  static bool geq(const bool& x, const bool& y) { return x || !y; }
107 
108  static bool equal(const bool& x, const bool& y) { return x == y; }
109 };
110 
112 
117 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE>>
119  using CONTENTPOINTER = CONTENTTYPE*;
120 
121 public:
122  static bool less(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
123  return STATICCONTENTCOMPARER::less(*x, *y);
124  }
125 
126  static bool leq(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
127  return STATICCONTENTCOMPARER::leq(*x, *y);
128  }
129 
130  static bool greater(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
131  return STATICCONTENTCOMPARER::greater(*x, *y);
132  }
133 
134  static bool geq(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
135  return STATICCONTENTCOMPARER::geq(*x, *y);
136  }
137 
138  static bool equal(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
139  return STATICCONTENTCOMPARER::equal(*x, *y);
140  }
141 };
142 
144 
183 #define OGDF_AUGMENT_COMPARER(type) \
184 public: \
185  bool less(const type& x, const type& y) const { return compare(x, y) < 0; } \
186  bool leq(const type& x, const type& y) const { return compare(x, y) <= 0; } \
187  bool greater(const type& x, const type& y) const { return compare(x, y) > 0; } \
188  bool geq(const type& x, const type& y) const { return compare(x, y) >= 0; } \
189  bool equal(const type& x, const type& y) const { return compare(x, y) == 0; }
190 
192 
229 #define OGDF_AUGMENT_STATICCOMPARER(type) \
230 public: \
231  static bool less(const type& x, const type& y) { return compare(x, y) < 0; } \
232  static bool leq(const type& x, const type& y) { return compare(x, y) <= 0; } \
233  static bool greater(const type& x, const type& y) { return compare(x, y) > 0; } \
234  static bool geq(const type& x, const type& y) { return compare(x, y) >= 0; } \
235  static bool equal(const type& x, const type& y) { return compare(x, y) == 0; }
236 
238 
257 template<class E>
258 class VComparer {
259 public:
261  VComparer() { }
262 
263  virtual ~VComparer() { }
264 
266 
271  virtual int compare(const E& x, const E& y) const = 0;
272 
274  virtual bool less(const E& x, const E& y) const { return compare(x, y) < 0; }
275 
277  virtual bool leq(const E& x, const E& y) const { return compare(x, y) <= 0; }
278 
280  virtual bool greater(const E& x, const E& y) const { return compare(x, y) > 0; }
281 
283  virtual bool geq(const E& x, const E& y) const { return compare(x, y) >= 0; }
284 
286  virtual bool equal(const E& x, const E& y) const { return compare(x, y) == 0; }
287 };
288 
290 
294 template<class X, class Priority = double>
295 class Prioritized {
296  X x;
297  Priority p;
298 
299 public:
301  Prioritized() : x(0), p(0) { }
302 
304  Prioritized(X xt, Priority pt) : x(xt), p(pt) { }
305 
307  Prioritized(const Prioritized& P) = default;
308 
310  Priority priority() const { return p; }
311 
313  X item() const { return x; }
314 
316  void setPriority(Priority pp) { p = pp; }
317 
319  void setItem(X item) { x = item; }
320 
322  Prioritized& operator=(const Prioritized<X, Priority>& P) = default;
323 
325  bool operator<(const Prioritized<X, Priority>& P) const { return p < P.p; }
326 
328  bool operator<=(const Prioritized<X, Priority>& P) const { return p <= P.p; }
329 
331  bool operator>(const Prioritized<X, Priority>& P) const { return p > P.p; }
332 
334  bool operator>=(const Prioritized<X, Priority>& P) const { return p >= P.p; }
335 
337  bool operator==(const Prioritized<X, Priority>& P) const { return p == P.p; }
338 
340  bool operator!=(const Prioritized<X, Priority>& P) const { return p != P.p; }
341 };
342 
343 template<class X, class Priority>
344 class StdComparer<Prioritized<X, Priority>> {
345 public:
346  static bool less(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
347  return x < y;
348  }
349 
350  static bool leq(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
351  return x <= y;
352  }
353 
355  return x > y;
356  }
357 
358  static bool geq(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
359  return x >= y;
360  }
361 
362  static bool equal(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
363  return x == y;
364  }
365 };
366 
373 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
374 class StlLess {
375 public:
376  bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::less(x, y); }
377 };
378 
385 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
386 class StlGreater {
387 public:
388  bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::greater(x, y); }
389 };
390 
401 template<typename ELEM, typename NUM, bool ascending>
402 struct GenericComparer {
403  using OrderFunction = std::function<NUM(const ELEM&)>;
404 
406  GenericComparer(const OrderFunction& mapToValue) : m_mapToValue(mapToValue) { }
407 
409  int compare(const ELEM& x, const ELEM& y) const {
410  NUM a = m_mapToValue(x);
411  NUM b = m_mapToValue(y);
412 
413  return a == b ? 0 : ((a < b) == ascending ? -1 : 1);
414  }
415 
417 
418 private:
420 };
421 
438 #define OGDF_DECLARE_COMPARER(NAME, TYPE, NUMBER, GET_X_ATTR) \
439  struct NAME : public GenericComparer<TYPE, NUMBER> { \
440  NAME() : GenericComparer([&](const TYPE& x) { return GET_X_ATTR; }) { } \
441  }
442 
443 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::VComparer::equal
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition: comparer.h:286
ogdf::Prioritized::setItem
void setItem(X item)
Sets value x.
Definition: comparer.h:319
ogdf::NoStdComparerException
Exception thrown when a required standard comparer has not been specialized.
Definition: exceptions.h:225
ogdf::StdComparer< bool >::greater
static bool greater(const bool &x, const bool &y)
Definition: comparer.h:104
exceptions.h
Definition of exception classes.
ogdf::internal::gcm::tools::equal
bool equal(const node a, const node b)
Definition: Universal.h:44
ogdf::Prioritized::Prioritized
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition: comparer.h:304
ogdf::StdComparer
Standard comparer (valid as a static comparer).
Definition: comparer.h:40
OGDF_AUGMENT_COMPARER
#define OGDF_AUGMENT_COMPARER(type)
Add this macro to your class to turn it into a full comparer.
Definition: comparer.h:183
ogdf::StdComparer::less
static bool less(const E &x, const E &y)
Definition: comparer.h:65
ogdf::TargetComparer::CONTENTPOINTER
CONTENTTYPE * CONTENTPOINTER
Definition: comparer.h:119
ogdf::StdComparer::geq
static bool geq(const E &x, const E &y)
Definition: comparer.h:71
OGDF_STD_COMPARER
#define OGDF_STD_COMPARER(type)
Generates a specialization of the standard static comparer for type based on compare operators.
Definition: comparer.h:80
ogdf::Prioritized::operator>=
bool operator>=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:334
ogdf::StlGreater
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:386
ogdf::VComparer::VComparer
VComparer()
Initializes a comparer.
Definition: comparer.h:261
ogdf::StdComparer::equal
static bool equal(const E &x, const E &y)
Definition: comparer.h:73
ogdf::Prioritized::priority
Priority priority() const
Returns the key of the element.
Definition: comparer.h:310
ogdf::StdComparer< bool >::leq
static bool leq(const bool &x, const bool &y)
Definition: comparer.h:102
ogdf::VComparer::less
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition: comparer.h:274
ogdf::StlLess::operator()
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:376
ogdf::VComparer
Abstract base class for comparer classes.
Definition: comparer.h:258
ogdf::StdComparer< bool >::equal
static bool equal(const bool &x, const bool &y)
Definition: comparer.h:108
ogdf::StdComparer< bool >::less
static bool less(const bool &x, const bool &y)
Definition: comparer.h:100
ogdf::Prioritized::operator!=
bool operator!=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:340
ogdf::TargetComparer::less
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:122
ogdf::StlLess
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:374
ogdf::Prioritized::x
X x
Definition: comparer.h:296
ogdf::Prioritized::Prioritized
Prioritized()
Constructor of empty element. Be careful!
Definition: comparer.h:301
OGDF_THROW
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:74
ogdf::Prioritized::operator>
bool operator>(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:331
ogdf::StdComparer< Prioritized< X, Priority > >::greater
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:354
ogdf::StdComparer< Prioritized< X, Priority > >::leq
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:350
ogdf::Prioritized::operator<
bool operator<(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:325
ogdf::StlGreater::operator()
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:388
ogdf::VComparer::geq
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition: comparer.h:283
ogdf::StdComparer::greater
static bool greater(const E &x, const E &y)
Definition: comparer.h:69
ogdf::StdComparer< Prioritized< X, Priority > >::equal
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:362
ogdf::VComparer::leq
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition: comparer.h:277
ogdf::VComparer::greater
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition: comparer.h:280
ogdf::GenericComparer::m_mapToValue
const OrderFunction m_mapToValue
Definition: comparer.h:419
ogdf::VComparer::compare
virtual int compare(const E &x, const E &y) const =0
Compares x and y and returns the result as an integer.
ogdf::Prioritized::operator=
Prioritized & operator=(const Prioritized< X, Priority > &P)=default
Copy assignment operator.
ogdf::TargetComparer::geq
static bool geq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:134
ogdf::Prioritized::operator<=
bool operator<=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:328
ogdf::VComparer::~VComparer
virtual ~VComparer()
Definition: comparer.h:263
ogdf::TargetComparer
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition: comparer.h:118
ogdf::StdComparer::leq
static bool leq(const E &x, const E &y)
Definition: comparer.h:67
ogdf::GenericComparer::compare
int compare(const ELEM &x, const ELEM &y) const
See OGDF_AUGMENT_COMPARER.
Definition: comparer.h:409
ogdf::TargetComparer::leq
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:126
ogdf::StdComparer< Prioritized< X, Priority > >::geq
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:358
ogdf::Prioritized::p
Priority p
Definition: comparer.h:297
ogdf::GenericComparer::OrderFunction
std::function< NUM(const ELEM &)> OrderFunction
Definition: comparer.h:403
ogdf::Prioritized::setPriority
void setPriority(Priority pp)
Sets priority.
Definition: comparer.h:316
ogdf::GenericComparer::GenericComparer
GenericComparer(const OrderFunction &mapToValue)
Construct a comparer with mapping mapToValue.
Definition: comparer.h:406
ogdf::StdComparer< bool >::geq
static bool geq(const bool &x, const bool &y)
Definition: comparer.h:106
ogdf::Prioritized
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition: comparer.h:295
ogdf::TargetComparer::greater
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:130
ogdf::TargetComparer::equal
static bool equal(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:138
ogdf::Prioritized::operator==
bool operator==(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition: comparer.h:337
ogdf::GenericComparer
Compare elements based on a single comparable attribute.
Definition: comparer.h:42
ogdf::Prioritized::item
X item() const
Returns the data of the element.
Definition: comparer.h:313
ogdf::StdComparer< Prioritized< X, Priority > >::less
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:346