31.01.2014 Views

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

dictionary D0; // default ordering<br />

dictionary D1; // polar ordering<br />

In general the macro call<br />

DEFINE_LINEAR_ORDER(T, cmp, T1)<br />

introduces a new type T 1 equivalent to type T with the linear order defined by the<br />

compare function cmp.<br />

2. As a new feature all order based data types like dictionaries, priority queues, and<br />

sorted sequences offer a constructor which allows a user to set the internally used<br />

ordering at construction time.<br />

dictionary D0;<br />

// default ordering<br />

dictionary D1(pol_cmp); // polar ordering<br />

This alternative handles the cases where two or more different orderings are needed<br />

more elegantly.<br />

3. Instead of passing a compare function cmp(const T &, const T &) to the sorted type<br />

one can also pass an object (a so-called compare object) of a class that is derived<br />

from the class leda cmp base and that overloads the function-call operator<br />

int operator()(const T &, const T &) to define a linear order for T .<br />

This variant is helpful when the compare function depends on a global parameter.<br />

We give an example. More examples can be found in several sections of the <strong>LEDA</strong><br />

book [64]. Assume that we want to compare edges of a graph GRAPH < point, int ><br />

(in this type every node has an associated point in the plane; the point associated<br />

with a node v is accessed as G[v]) according to the distance of their endpoints. We<br />

write<br />

using namespace leda;<br />

class cmp_edges_by_length: public leda_cmp_base {<br />

const GRAPH& G;<br />

public:<br />

cmp_edges_by_length(const GRAPH& g): G(g){}<br />

int operator()(const edge& e, const edge& f) const<br />

{ point pe = G[G.source(e)]; point qe = G[G.target(e)];<br />

point pf = G[G.source(f)]; point qf = G[G.target(f)];<br />

return compare(pe.sqr_dist(qe),pf.sqr_dist(qf));<br />

}<br />

};<br />

int main(){<br />

GRAPH G;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!