Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Provides support for comparer classes. More...

Classes

class  ogdf::EdgeComparer
 Compares adjacency entries based on the position of the nodes given by GraphAttribute layout information. More...
 
struct  ogdf::GenericComparer< ELEM, NUM, ascending >
 Compare elements based on a single comparable attribute. More...
 
class  ogdf::StdComparer< E >
 Standard comparer (valid as a static comparer). More...
 
class  ogdf::StlGreater< TYPE, COMPARER >
 Template for converting any StdComparer into a STL compatible compare functor. More...
 
class  ogdf::StlLess< TYPE, COMPARER >
 Template for converting any StdComparer into a STL compatible compare functor. More...
 
class  ogdf::TargetComparer< CONTENTTYPE, STATICCONTENTCOMPARER >
 A static comparer which compares the target of pointers ("content"), instead of the pointer's adresses. More...
 
class  ogdf::VComparer< E >
 Abstract base class for comparer classes. More...
 

Macros

#define OGDF_AUGMENT_COMPARER(type)
 Add this macro to your class to turn it into a full comparer. More...
 
#define OGDF_AUGMENT_STATICCOMPARER(type)
 Add this macro to your class to turn it into a full static comparer. More...
 
#define OGDF_DECLARE_COMPARER(NAME, TYPE, NUMBER, GET_X_ATTR)
 Declares a class NAME that extends from ogdf::GenericComparer. More...
 
#define OGDF_STD_COMPARER(type)
 Generates a specialization of the standard static comparer for type based on compare operators. More...
 

Detailed Description

Provides support for comparer classes.

Macro Definition Documentation

◆ OGDF_AUGMENT_COMPARER

#define OGDF_AUGMENT_COMPARER (   type)
Value:
public: \
bool less(const type& x, const type& y) const { return compare(x, y) < 0; } \
bool leq(const type& x, const type& y) const { return compare(x, y) <= 0; } \
bool greater(const type& x, const type& y) const { return compare(x, y) > 0; } \
bool geq(const type& x, const type& y) const { return compare(x, y) >= 0; } \
bool equal(const type& x, const type& y) const { return compare(x, y) == 0; }

Add this macro to your class to turn it into a full comparer.

It is assumed that your class has a method "compare(const type &x, const type &y)", which returns 0 if the two elements are equal, a negative value if x is smaller, and a positive value if x is greater.

Note: If the compare function of your class requires no additional data other than the two elements to compare, your should usually use the more general OGDF_AUGMENT_STATICCOMPARER: A static comparer is also always valid as a normal comparer.

Usage in Definition:

class MyComparer {
private:
Oracle oracle;
public:
MyComparer(Oracle o) : oracle(o) {}
int compare(const MyStuff& x1, const MyStuff& x2) const {
return ... //compare x1 with x2, using oracle
}
}

Use the Comparer:

MyStuff a=...;
MyStuff b=...;
Oracle or;
MyComparer comp(or);
if( comp.less(a,b) )
... // do something
...
Array<MyStuff> ay(10);
... // fill array
ay.quicksort(comp); // sort the array using the MyComparer comp

Definition at line 179 of file comparer.h.

◆ OGDF_AUGMENT_STATICCOMPARER

#define OGDF_AUGMENT_STATICCOMPARER (   type)
Value:
public: \
static bool less(const type& x, const type& y) { return compare(x, y) < 0; } \
static bool leq(const type& x, const type& y) { return compare(x, y) <= 0; } \
static bool greater(const type& x, const type& y) { return compare(x, y) > 0; } \
static bool geq(const type& x, const type& y) { return compare(x, y) >= 0; } \
static bool equal(const type& x, const type& y) { return compare(x, y) == 0; }

Add this macro to your class to turn it into a full static comparer.

It is assumed that your class has a static method "compare(const type &x, const type &y)", which returns 0 if the two elements are equal, a negative value if x is smaller, and a positive value if x is greater.

Note: You should use this macro instead of OGDF_AUGMENT_COMPARER whenever your compare function requires no additional data stored in the object, other than the two elements to compare. A static comparer is also always valid as a normal comparer.

Usage in Definition:

class MyComparer {
public:
static int comparer(const MyStuff& x1, const MyStuff& x2) {
return ... //compare x1 with x2
}
}

Use the Comparer:

MyStuff a=...;
MyStuff b=...;
MyComparer comp;
if( MyComparer.less(a,b) ) // use it statically on the class
... // do something
if( comp.less(a,b) ) // use it on the object
... // do something
...
Array<MyStuff> ay(10);
... // fill array
ay.quicksort(comp); // sort the array using the MyComparer comp

Definition at line 225 of file comparer.h.

◆ OGDF_DECLARE_COMPARER

#define OGDF_DECLARE_COMPARER (   NAME,
  TYPE,
  NUMBER,
  GET_X_ATTR 
)
Value:
struct NAME : public GenericComparer<TYPE, NUMBER> { \
NAME() : GenericComparer([&](const TYPE& x) { return GET_X_ATTR; }) { } \
}

Declares a class NAME that extends from ogdf::GenericComparer.

The type of compared elements is TYPE, the returned scalar is of type NUMBER. GET_X_ATTR contains the actual element to scalar conversion for any element (denoted by x).

Example use to sort edges by index:

List<edge> edges;
...
OGDF_DECLARE_COMPARER(EdgeIndexCmp, edge, int, x->index());
edges.quicksort(EdgeIndexCmp());

Definition at line 434 of file comparer.h.

◆ OGDF_STD_COMPARER

#define OGDF_STD_COMPARER (   type)
Value:
template<> \
class StdComparer<type> { \
public: \
static bool less(const type& x, const type& y) { return x < y; } \
static bool leq(const type& x, const type& y) { return x <= y; } \
static bool greater(const type& x, const type& y) { return x > y; } \
static bool geq(const type& x, const type& y) { return x >= y; } \
static bool equal(const type& x, const type& y) { return x == y; } \
};

Generates a specialization of the standard static comparer for type based on compare operators.

Definition at line 76 of file comparer.h.

ogdf::internal::gcm::tools::equal
bool equal(const node a, const node b)
Definition: Universal.h:42
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::EdgeElement::index
int index() const
Returns the index of the edge.
Definition: Graph_d.h:388
OGDF_AUGMENT_STATICCOMPARER
#define OGDF_AUGMENT_STATICCOMPARER(type)
Add this macro to your class to turn it into a full static comparer.
Definition: comparer.h:225
ogdf::edge
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:67
backward::Color::type
type
Definition: backward.hpp:1716