next up previous contents index
Next: Observer Node Iterator ( Up: Graphs and Iterators Previous: Filter Node Iterator (   Contents   Index


Comparison Predicate ( CompPred )

Definition

An instance cp of class CompPred<Iter,DA,Comp> is a predicate comparator that produces boolean values with the given compare function and the attribute associated with an iterator.

#include < LEDA/graph _iterator.h >

Creation

CompPred<Iter,DA,Comp> cp(DA da, Comp comp, DA::value_type val)
    introduces a variable cp of this class and associates it to the given data accessor da, compare function comp and value val.

Precondition: Comp is a pointer-to-function type which takes two values of type __typename DA::value_type and produces a boolean return value. Comp might also be a class with member function bool operator()(__typename DA::value_type,__typename DA::value_type).

Example

In the following example, a node iterator for red nodes will be created. At first the basic part (see sect. node_array_da for explanation of the data accessor node_array_da):


  graph G;
  NodeIt it(G);
  node_array<color> na_colour(G,black);
  node_array_da<color> da_colour(na_colour);
  assign_some_color_to_each_node();

Now follows the definition of a ``red iterator'' ( Equal<T> yields true, if the given two values are equal):


  template<class T>
  class Equal {
    public:
    bool operator() (T t1, T t2) const {
    return t1==t2; }
  };
  
  typedef CompPred<NodeIt,node_array_da<color>,Equal<color> > Predicate;
  Predicate PredColour(da_colour,Equal<color>(),red);
  FilterNodeIt<Predicate,NodeIt> red_it(PredColour,it);

This simplifies the loop to the following:


  while(red_it.valid()) {
    do_something(red_it);
  ++red_it; }

Equal<T> is a class that compares two items of the template parameter T by means of a method bool operator()(T,T);. There are some classes available for this purpose: Equal<T>, Unequal<T>, LessThan<T>, LessEqual<T>, GreaterThan<T> and GreaterEqual<T> with obvious semantics, where T is the type of the values. Predicates of the STL can be used as well since they have the same interface.


next up previous contents index
Next: Observer Node Iterator ( Up: Graphs and Iterators Previous: Filter Node Iterator (   Contents   Index
LEDA research project
2000-02-09