Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
comparer.h
Go to the documentation of this file.
1
32#pragma once
33
35
36#include <functional>
37
38namespace ogdf {
39template<typename E>
40class StdComparer;
41template<typename ELEM, typename NUM, bool ascending = true>
42struct GenericComparer;
43
45
62template<typename E>
64public:
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
95
96
97template<>
98class StdComparer<bool> {
99public:
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
117template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE>>
119 using CONTENTPOINTER = CONTENTTYPE*;
120
121public:
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) \
184public: \
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) \
230public: \
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
257template<class E>
259public:
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
294template<class X, class Priority = double>
296 X x;
297 Priority p;
298
299public:
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
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
343template<class X, class Priority>
344class StdComparer<Prioritized<X, Priority>> {
345public:
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
363 return x == y;
364 }
365};
366
373template<typename TYPE, class COMPARER = StdComparer<TYPE>>
374class StlLess {
375public:
376 bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::less(x, y); }
377};
378
385template<typename TYPE, class COMPARER = StdComparer<TYPE>>
387public:
388 bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::greater(x, y); }
389};
390
401template<typename ELEM, typename NUM, bool ascending>
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
418private:
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}
Exception thrown when a required standard comparer has not been specialized.
Definition exceptions.h:225
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition comparer.h:295
bool operator<=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:328
Prioritized(const Prioritized &P)=default
Copy-constructor.
X item() const
Returns the data of the element.
Definition comparer.h:313
bool operator==(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:337
Priority priority() const
Returns the key of the element.
Definition comparer.h:310
void setItem(X item)
Sets value x.
Definition comparer.h:319
bool operator!=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:340
bool operator>(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:331
void setPriority(Priority pp)
Sets priority.
Definition comparer.h:316
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition comparer.h:304
Prioritized & operator=(const Prioritized< X, Priority > &P)=default
Copy assignment operator.
Prioritized()
Constructor of empty element. Be careful!
Definition comparer.h:301
bool operator<(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:325
bool operator>=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:334
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:354
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:350
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:358
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:362
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:346
static bool equal(const bool &x, const bool &y)
Definition comparer.h:108
static bool less(const bool &x, const bool &y)
Definition comparer.h:100
static bool leq(const bool &x, const bool &y)
Definition comparer.h:102
static bool geq(const bool &x, const bool &y)
Definition comparer.h:106
static bool greater(const bool &x, const bool &y)
Definition comparer.h:104
Standard comparer (valid as a static comparer).
Definition comparer.h:63
static bool less(const E &x, const E &y)
Definition comparer.h:65
static bool equal(const E &x, const E &y)
Definition comparer.h:73
static bool leq(const E &x, const E &y)
Definition comparer.h:67
static bool geq(const E &x, const E &y)
Definition comparer.h:71
static bool greater(const E &x, const E &y)
Definition comparer.h:69
Template for converting any StdComparer into a STL compatible compare functor.
Definition comparer.h:386
bool operator()(const TYPE &x, const TYPE &y) const
Definition comparer.h:388
Template for converting any StdComparer into a STL compatible compare functor.
Definition comparer.h:374
bool operator()(const TYPE &x, const TYPE &y) const
Definition comparer.h:376
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition comparer.h:118
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:122
static bool equal(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:138
static bool geq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:134
CONTENTTYPE * CONTENTPOINTER
Definition comparer.h:119
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:126
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:130
Abstract base class for comparer classes.
Definition comparer.h:258
virtual int compare(const E &x, const E &y) const =0
Compares x and y and returns the result as an integer.
VComparer()
Initializes a comparer.
Definition comparer.h:261
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition comparer.h:283
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition comparer.h:286
virtual ~VComparer()
Definition comparer.h:263
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition comparer.h:277
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition comparer.h:274
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition comparer.h:280
Definition of exception classes.
#define OGDF_AUGMENT_COMPARER(type)
Add this macro to your class to turn it into a full comparer.
Definition comparer.h:183
#define OGDF_STD_COMPARER(type)
Generates a specialization of the standard static comparer for type based on compare operators.
Definition comparer.h:80
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition exceptions.h:67
The namespace for all OGDF objects.
Compare elements based on a single comparable attribute.
Definition comparer.h:402
int compare(const ELEM &x, const ELEM &y) const
See OGDF_AUGMENT_COMPARER.
Definition comparer.h:409
GenericComparer(const OrderFunction &mapToValue)
Construct a comparer with mapping mapToValue.
Definition comparer.h:406
const OrderFunction m_mapToValue
Definition comparer.h:419
std::function< NUM(const ELEM &)> OrderFunction
Definition comparer.h:403