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 {
39
41
58template<typename E>
60public:
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
91
92
93template<>
95public:
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
113template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE>>
116
117public:
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) \
180public: \
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) \
226public: \
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
253template<class E>
255public:
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
290template<class X, class Priority = double>
292 X x;
294
295public:
297 Prioritized() : x(0), p(0) { }
298
301
303 Prioritized(const Prioritized& P) = default;
304
306 Priority priority() const { return p; }
307
309 X item() const { return x; }
310
313
315 void setItem(X item) { x = item; }
316
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
339template<class X, class Priority>
341public:
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
359 return x == y;
360 }
361};
362
369template<typename TYPE, class COMPARER = StdComparer<TYPE>>
370class StlLess {
371public:
372 bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::less(x, y); }
373};
374
381template<typename TYPE, class COMPARER = StdComparer<TYPE>>
383public:
384 bool operator()(const TYPE& x, const TYPE& y) const { return COMPARER::greater(x, y); }
385};
386
397template<typename ELEM, typename NUM, bool ascending = true>
399 using OrderFunction = std::function<NUM(const ELEM&)>;
400
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
414private:
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}
Exception thrown when a required standard comparer has not been specialized.
Definition exceptions.h:219
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition comparer.h:291
bool operator<=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:324
Prioritized(const Prioritized &P)=default
Copy-constructor.
X item() const
Returns the data of the element.
Definition comparer.h:309
bool operator==(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:333
Priority priority() const
Returns the key of the element.
Definition comparer.h:306
void setItem(X item)
Sets value x.
Definition comparer.h:315
bool operator!=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:336
bool operator>(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:327
void setPriority(Priority pp)
Sets priority.
Definition comparer.h:312
Prioritized(X xt, Priority pt)
Constructor using a key/value pair.
Definition comparer.h:300
Prioritized & operator=(const Prioritized< X, Priority > &P)=default
Copy assignment operator.
Prioritized()
Constructor of empty element. Be careful!
Definition comparer.h:297
bool operator<(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:321
bool operator>=(const Prioritized< X, Priority > &P) const
Comparison oprator based on the compare-operator for the key type (Priority)
Definition comparer.h:330
static bool greater(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:350
static bool leq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:346
static bool geq(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:354
static bool equal(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:358
static bool less(const Prioritized< X, Priority > &x, const Prioritized< X, Priority > &y)
Definition comparer.h:342
static bool equal(const bool &x, const bool &y)
Definition comparer.h:104
static bool less(const bool &x, const bool &y)
Definition comparer.h:96
static bool leq(const bool &x, const bool &y)
Definition comparer.h:98
static bool geq(const bool &x, const bool &y)
Definition comparer.h:102
static bool greater(const bool &x, const bool &y)
Definition comparer.h:100
Standard comparer (valid as a static comparer).
Definition comparer.h:59
static bool less(const E &x, const E &y)
Definition comparer.h:61
static bool equal(const E &x, const E &y)
Definition comparer.h:69
static bool leq(const E &x, const E &y)
Definition comparer.h:63
static bool geq(const E &x, const E &y)
Definition comparer.h:67
static bool greater(const E &x, const E &y)
Definition comparer.h:65
Template for converting any StdComparer into a STL compatible compare functor.
Definition comparer.h:382
bool operator()(const TYPE &x, const TYPE &y) const
Definition comparer.h:384
Template for converting any StdComparer into a STL compatible compare functor.
Definition comparer.h:370
bool operator()(const TYPE &x, const TYPE &y) const
Definition comparer.h:372
A static comparer which compares the target of pointers ("content"), instead of the pointer's adresse...
Definition comparer.h:114
static bool less(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:118
static bool equal(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:134
static bool geq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:130
CONTENTTYPE * CONTENTPOINTER
Definition comparer.h:115
static bool leq(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:122
static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y)
Definition comparer.h:126
Abstract base class for comparer classes.
Definition comparer.h:254
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:257
virtual bool geq(const E &x, const E &y) const
Returns true iff x >= y.
Definition comparer.h:279
virtual bool equal(const E &x, const E &y) const
Returns true iff x = y.
Definition comparer.h:282
virtual ~VComparer()
Definition comparer.h:259
virtual bool leq(const E &x, const E &y) const
Returns true iff x <= y.
Definition comparer.h:273
virtual bool less(const E &x, const E &y) const
Returns true iff x < y.
Definition comparer.h:270
virtual bool greater(const E &x, const E &y) const
Returns true iff x > y.
Definition comparer.h:276
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:179
#define OGDF_STD_COMPARER(type)
Generates a specialization of the standard static comparer for type based on compare operators.
Definition comparer.h:76
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition exceptions.h:63
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.
Compare elements based on a single comparable attribute.
Definition comparer.h:398
int compare(const ELEM &x, const ELEM &y) const
See OGDF_AUGMENT_COMPARER.
Definition comparer.h:405
GenericComparer(const OrderFunction &mapToValue)
Construct a comparer with mapping mapToValue.
Definition comparer.h:402
const OrderFunction m_mapToValue
Definition comparer.h:415
std::function< NUM(const ELEM &)> OrderFunction
Definition comparer.h:399