next up previous contents index
Next: Constant Accessors ( constant_da Up: Graphs and Iterators Previous: STL Iterator Wrapper (   Contents   Index


Node Array Data Accessor ( node_array_da )

Definition

An instance da of class node_array_da<T> is instantiated with a LEDA node_array<T>.

The data in the node array can be accessed by the functions get(da,it) and set(da,it,value) that take as parameters an instance of node_array_da<T> and an iterator, see below.

node_array_da<T>::value_type is a type and equals T.

For node_map<T> there is the variant node_map_da<T> which is defined completely analogous to node_array_da<T>. Classes edge_array_da<T> and edge_map_da<T> are defined analogously, as well.

#include < LEDA/graph/graph_iterator.h >

Creation

node_array_da<T> da introduces a variable da of this class that is not bound.

node_array_da<T> da(leda::node_array<T>& na)
    introduces a variable da of this class bound to na.

Operations

T get(const node_array_da<T>& da, const Iter& it)
    returns the associated value of it for this accessor.

void set(node_array_da<T>& da, const Iter& it, T val)
    sets the associated value of it for this accessor to the given value.

Implementation

Constant Overhead.

Example

We count the number of 'red nodes' in a parameterized graph G.

  int count_red(graph G, node_array<color> COL) {
    node_array_da<color> Color(COL);
    int counter=0;
    NodeIt it(G);
    while (it.valid()) {
      if (get(Color,it)==red) counter++;
      it++; }
    return counter;
  }

Suppose we want to make this 'algorithm' flexible in the representation of colors. Then we could write this version:

  template<class DA>
  int count_red_t(graph G, DA Color) {
    int counter=0;
    NodeIt it(G);
    while (it.valid()) {
      if (get(Color,it)==red) counter++;
      it++; }
    return counter;
  }

With the templatized version it is easily to customize it to match the interface of the version:

  int count_red(graph G, node_array<color> COL) {
    node_array_da<color> Color(COL);
    return count_red_t(G,Color); }


next up previous contents index
Next: Constant Accessors ( constant_da Up: Graphs and Iterators Previous: STL Iterator Wrapper (   Contents   Index
root 2008-01-09