src/lemon/iterable_graph_extender.h
changeset 951 0f1fe84ff36c
child 962 1a770e9f80b2
equal deleted inserted replaced
-1:000000000000 0:8d7e97ede627
       
     1 // -*- c++ -*-
       
     2 #ifndef LEMON_ITERABLE_GRAPH_EXTENDER_H
       
     3 #define LEMON_ITERABLE_GRAPH_EXTENDER_H
       
     4 
       
     5 #include <lemon/invalid.h>
       
     6 
       
     7 namespace lemon {
       
     8   
       
     9   template <typename _Base>
       
    10   class IterableGraphExtender : public _Base {
       
    11 
       
    12     typedef _Base Parent;
       
    13     typedef IterableGraphExtender<_Base> Graph;
       
    14 
       
    15   public:
       
    16 
       
    17     typedef typename Parent::Node Node;
       
    18     typedef typename Parent::Edge Edge;
       
    19 
       
    20 
       
    21     class NodeIt : public Node { 
       
    22       const Graph* graph;
       
    23     public:
       
    24 
       
    25       NodeIt() {}
       
    26 
       
    27       NodeIt(Invalid i) : Node(i) { }
       
    28 
       
    29       explicit NodeIt(const Graph& _graph) : Node(), graph(&_graph) {
       
    30 	_graph.first(*static_cast<Node*>(this));
       
    31       }
       
    32 
       
    33       NodeIt(const Graph& _graph, const Node& node) 
       
    34 	: Node(node), graph(&_graph) {}
       
    35 
       
    36       NodeIt& operator++() { 
       
    37 	graph->next(*this);
       
    38 	return *this; 
       
    39       }
       
    40 
       
    41     };
       
    42 
       
    43 
       
    44     class EdgeIt : public Edge { 
       
    45       const Graph* graph;
       
    46     public:
       
    47 
       
    48       EdgeIt() { }
       
    49 
       
    50       EdgeIt(Invalid i) : Edge(i) { }
       
    51 
       
    52       explicit EdgeIt(const Graph& _graph) : Edge(), graph(&_graph) {
       
    53 	_graph.first(*static_cast<Edge*>(this));
       
    54       }
       
    55 
       
    56       EdgeIt(const Graph& _graph, const Edge& e) : 
       
    57 	Edge(e), graph(&_graph) { }
       
    58 
       
    59       EdgeIt& operator++() { 
       
    60 	graph->next(*this);
       
    61 	return *this; 
       
    62       }
       
    63 
       
    64     };
       
    65 
       
    66 
       
    67     class OutEdgeIt : public Edge { 
       
    68       const Graph* graph;
       
    69     public:
       
    70 
       
    71       OutEdgeIt() { }
       
    72 
       
    73       OutEdgeIt(Invalid i) : Edge(i) { }
       
    74 
       
    75       OutEdgeIt(const Graph& _graph, const Node& node) 
       
    76 	: Edge(), graph(&_graph) {
       
    77 	_graph.firstOut(*this, node);
       
    78       }
       
    79 
       
    80       OutEdgeIt(const Graph& _graph, const Edge& edge) 
       
    81 	: Edge(edge), graph(&_graph) {}
       
    82 
       
    83       OutEdgeIt& operator++() { 
       
    84 	graph->nextOut(*this);
       
    85 	return *this; 
       
    86       }
       
    87 
       
    88     };
       
    89 
       
    90 
       
    91     class InEdgeIt : public Edge { 
       
    92       const Graph* graph;
       
    93     public:
       
    94 
       
    95       InEdgeIt() { }
       
    96 
       
    97       InEdgeIt(Invalid i) : Edge(i) { }
       
    98 
       
    99       InEdgeIt(const Graph& _graph, const Node& node) 
       
   100 	: Edge(), graph(&_graph) {
       
   101 	_graph.firstIn(*this, node);
       
   102       }
       
   103 
       
   104       InEdgeIt(const Graph& _graph, const Edge& edge) : 
       
   105 	Edge(edge), graph(&_graph) {}
       
   106 
       
   107       InEdgeIt& operator++() { 
       
   108 	graph->nextIn(*this);
       
   109 	return *this; 
       
   110       }
       
   111 
       
   112     };
       
   113 
       
   114     using Parent::first;
       
   115 
       
   116   private:
       
   117 
       
   118     /// \todo When (and if) we change the iterators concept to use operator*,
       
   119     /// then the following shadowed methods will become superfluous.
       
   120     /// But for now these are important safety measures.
       
   121 
       
   122     void first(NodeIt &) const;
       
   123     void first(EdgeIt &) const;
       
   124     void first(OutEdgeIt &) const;
       
   125     void first(InEdgeIt &) const;
       
   126 
       
   127   };
       
   128   
       
   129 }
       
   130 
       
   131 #endif // LEMON_GRAPH_EXTENDER_H