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 
41 
58 template<typename E>
59 class StdComparer {
60 public:
61  static bool less(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
62 
63  static bool leq(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
64 
65  static bool greater(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
66 
67  static bool geq(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
68 
69  static bool equal(const E& x, const E& y) { OGDF_THROW(NoStdComparerException); }
70 };
71 
73 
76 #define OGDF_STD_COMPARER(type) \
77  template<> \
78  class StdComparer<type> { \
79  public: \
80  static bool less(const type& x, const type& y) { return x < y; } \
81  static bool leq(const type& x, const type& y) { return x <= y; } \
82  static bool greater(const type& x, const type& y) { return x > y; } \
83  static bool geq(const type& x, const type& y) { return x >= y; } \
84  static bool equal(const type& x, const type& y) { return x == y; } \
85  };
86 
87 OGDF_STD_COMPARER(short)
89 OGDF_STD_COMPARER(float)
90 OGDF_STD_COMPARER(double)
91 
92 template<>
94 class StdComparer<bool> {
95 public:
96  static bool less(const bool& x, const bool& y) { return !x && y; }
97 
98  static bool leq(const bool& x, const bool& y) { return !x || y; }
99 
100  static bool greater(const bool& x, const bool& y) { return x && !y; }
101 
102  static bool geq(const bool& x, const bool& y) { return x || !y; }
103 
104  static bool equal(const bool& x, const bool& y) { return x == y; }
105 };
106 
108 
113 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE>>
115  using CONTENTPOINTER = CONTENTTYPE*;
116 
117 public:
118  static bool less(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
119  return STATICCONTENTCOMPARER::less(*x, *y);
120  }
121 
122  static bool leq(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
123  return STATICCONTENTCOMPARER::leq(*x, *y);
124  }
125 
126  static bool greater(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
127  return STATICCONTENTCOMPARER::greater(*x, *y);
128  }
129 
130  static bool geq(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
131  return STATICCONTENTCOMPARER::geq(*x, *y);
132  }
133 
134  static bool equal(const CONTENTPOINTER& x, const CONTENTPOINTER& y) {
135  return STATICCONTENTCOMPARER::equal(*x, *y);
136  }
137 };
138 
140 
179 #define OGDF_AUGMENT_COMPARER(type) \
180 public: \
181  bool less(const type& x, const type& y) const { return compare(x, y) < 0; } \
182  bool leq(const type& x, const type& y) const { return compare(x, y) <= 0; } \
183  bool greater(const type& x, const type& y) const { return compare(x, y) > 0; } \
184  bool geq(const type& x, const type& y) const { return compare(x, y) >= 0; } \
185  bool equal(const type& x, const type& y) const { return compare(x, y) == 0; }
186 
188 
225 #define OGDF_AUGMENT_STATICCOMPARER(type) \
226 public: \
227  static bool less(const type& x, const type& y) { return compare(x, y) < 0; } \
228  static bool leq(const type& x, const type& y) { return compare(x, y) <= 0; } \
229  static bool greater(const type& x, const type& y) { return compare(x, y) > 0; } \
230  static bool geq(const type& x, const type& y) { return compare(x, y) >= 0; } \
231  static bool equal(const type& x, const type& y) { return compare(x, y) == 0; }
232 
234 
253 template<class E>
254 class VComparer {
255 public:
257  VComparer() { }
258 
259  virtual ~VComparer() { }
260 
262 
267  virtual int compare(const E& x, const E& y) const = 0;
268 
270  virtual bool less(const E& x, const E& y) const { return compare(x, y) < 0; }
271 
273  virtual bool leq(const E& x, const E& y) const { return compare(x, y) <= 0; }
274 
276  virtual bool greater(const E& x, const E& y) const { return compare(x, y) > 0; }
277 
279  virtual bool geq(const E& x, const E& y) const { return compare(x, y) >= 0; }
280 
282  virtual bool equal(const E& x, const E& y) const { return compare(x, y) == 0; }
283 };
284 
286 
290 template<class X, class Priority = double>
291 class Prioritized {
292  X x;
293  Priority p;
294 
295 public:
297  Prioritized() : x(0), p(0) { }
298 
300  Prioritized(X xt, Priority pt) : x(xt), p(pt) { }
301 
303  Prioritized(const Prioritized& P) = default;
304 
306  Priority priority() const { return p; }
307 
309  X item() const { return x; }
310 
312  void setPriority(Priority pp) { p = pp; }
313 
315  void setItem(X item) { x = item; }
316 
318  Prioritized& operator=(const Prioritized<X, Priority>& P) = default;
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 
327  bool operator>(const Prioritized<X, Priority>& P) const { return p > P.p; }
328 
330  bool operator>=(const Prioritized<X, Priority>& P) const { return p >= P.p; }
331 
333  bool operator==(const Prioritized<X, Priority>& P) const { return p == P.p; }
334 
336  bool operator!=(const Prioritized<X, Priority>& P) const { return p != P.p; }
337 };
338 
339 template<class X, class Priority>
340 class StdComparer<Prioritized<X, Priority>> {
341 public:
342  static bool less(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
343  return x < y;
344  }
345 
346  static bool leq(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
347  return x <= y;
348  }
349 
351  return x > y;
352  }
353 
354  static bool geq(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
355  return x >= y;
356  }
357 
358  static bool equal(const Prioritized<X, Priority>& x, const Prioritized<X, Priority>& y) {
359  return x == y;
360  }
361 };
362 
369 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
370 class StlLess {
371 public:
372  bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::less(x, y); }
373 };
374 
381 template<typename TYPE, class COMPARER = StdComparer<TYPE>>
382 class StlGreater {
383 public:
384  bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::greater(x, y); }
385 };
386 
397 template<typename ELEM, typename NUM, bool ascending = true>
399  using OrderFunction = std::function<NUM(const ELEM&)>;
400 
402  GenericComparer(const OrderFunction& mapToValue) : m_mapToValue(mapToValue) { }
403 
405  int compare(const ELEM& x, const ELEM& y) const {
406  NUM a = m_mapToValue(x);
407  NUM b = m_mapToValue(y);
408 
409  return a == b ? 0 : ((a < b) == ascending ? -1 : 1);
410  }
411 
413 
414 private:
416 };
417 
434 #define OGDF_DECLARE_COMPARER(NAME, TYPE, NUMBER, GET_X_ATTR) \
435  struct NAME : public GenericComparer<TYPE, NUMBER> { \
436  NAME() : GenericComparer([&](const TYPE& x) { return GET_X_ATTR; }) { } \
437  }
438 
439 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::VComparer::equal
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition: comparer.h:282
ogdf::Prioritized::setItem
void setItem(X item)
Sets value x.
Definition: comparer.h:315
ogdf::NoStdComparerException
Exception thrown when a required standard comparer has not been specialized.
Definition: exceptions.h:221
ogdf::StdComparer< bool >::greater
static bool greater(const bool &x, const bool &y)
Definition: comparer.h:100
exceptions.h
Definition of exception classes.
ogdf::internal::gcm::tools::equal
bool equal(const node a, const node b)
Definition: Universal.h:42
ogdf::Prioritized::Prioritized
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition: comparer.h:300
ogdf::StdComparer
Standard comparer (valid as a static comparer).
Definition: comparer.h:59
OGDF_AUGMENT_COMPARER
#define OGDF_AUGMENT_COMPARER(type)
Add this macro to your class to turn it into a full comparer.
Definition: comparer.h:179
ogdf::StdComparer::less
static bool less(const E &x, const E &y)
Definition: comparer.h:61
ogdf::TargetComparer::CONTENTPOINTER
CONTENTTYPE * CONTENTPOINTER
Definition: comparer.h:115
ogdf::StdComparer::geq
static bool geq(const E &x, const E &y)
Definition: comparer.h:67
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:76
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:330
ogdf::StlGreater
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:382
ogdf::VComparer::VComparer
VComparer()
Initializes a comparer.
Definition: comparer.h:257
ogdf::StdComparer::equal
static bool equal(const E &x, const E &y)
Definition: comparer.h:69
ogdf::Prioritized::priority
Priority priority() const
Returns the key of the element.
Definition: comparer.h:306
ogdf::StdComparer< bool >::leq
static bool leq(const bool &x, const bool &y)
Definition: comparer.h:98
ogdf::VComparer::less
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition: comparer.h:270
ogdf::StlLess::operator()
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:372
ogdf::VComparer
Abstract base class for comparer classes.
Definition: comparer.h:254
ogdf::StdComparer< bool >::equal
static bool equal(const bool &x, const bool &y)
Definition: comparer.h:104
ogdf::StdComparer< bool >::less
static bool less(const bool &x, const bool &y)
Definition: comparer.h:96
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:336
ogdf::TargetComparer::less
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:118
ogdf::StlLess
Template for converting any StdComparer into a STL compatible compare functor.
Definition: comparer.h:370
ogdf::Prioritized::x
X x
Definition: comparer.h:292
ogdf::Prioritized::Prioritized
Prioritized()
Constructor of empty element. Be careful!
Definition: comparer.h:297
OGDF_THROW
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:70
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:327
ogdf::StdComparer< Prioritized< X, Priority > >::greater
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:350
ogdf::StdComparer< Prioritized< X, Priority > >::leq
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:346
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:321
ogdf::StlGreater::operator()
bool operator()(const TYPE &x, const TYPE &y) const
Definition: comparer.h:384
ogdf::VComparer::geq
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition: comparer.h:279
ogdf::StdComparer::greater
static bool greater(const E &x, const E &y)
Definition: comparer.h:65
ogdf::StdComparer< Prioritized< X, Priority > >::equal
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:358
ogdf::VComparer::leq
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition: comparer.h:273
ogdf::VComparer::greater
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition: comparer.h:276
ogdf::GenericComparer::m_mapToValue
const OrderFunction m_mapToValue
Definition: comparer.h:415
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:130
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:324
ogdf::VComparer::~VComparer
virtual ~VComparer()
Definition: comparer.h:259
ogdf::TargetComparer
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition: comparer.h:114
ogdf::StdComparer::leq
static bool leq(const E &x, const E &y)
Definition: comparer.h:63
ogdf::GenericComparer::compare
int compare(const ELEM &x, const ELEM &y) const
See OGDF_AUGMENT_COMPARER.
Definition: comparer.h:405
ogdf::TargetComparer::leq
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:122
ogdf::StdComparer< Prioritized< X, Priority > >::geq
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:354
ogdf::Prioritized::p
Priority p
Definition: comparer.h:293
ogdf::GenericComparer::OrderFunction
std::function< NUM(const ELEM &)> OrderFunction
Definition: comparer.h:399
ogdf::Prioritized::setPriority
void setPriority(Priority pp)
Sets priority.
Definition: comparer.h:312
ogdf::GenericComparer::GenericComparer
GenericComparer(const OrderFunction &mapToValue)
Construct a comparer with mapping mapToValue.
Definition: comparer.h:402
ogdf::StdComparer< bool >::geq
static bool geq(const bool &x, const bool &y)
Definition: comparer.h:102
ogdf::Prioritized
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition: comparer.h:291
ogdf::TargetComparer::greater
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition: comparer.h:126
ogdf::TargetComparer::equal
static bool equal(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:333
ogdf::GenericComparer
Compare elements based on a single comparable attribute.
Definition: comparer.h:398
ogdf::Prioritized::item
X item() const
Returns the data of the element.
Definition: comparer.h:309
ogdf::StdComparer< Prioritized< X, Priority > >::less
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition: comparer.h:342