src/lemon/iterable_graph_extender.h
author alpar
Tue, 09 Nov 2004 17:48:52 +0000
changeset 973 6a6f3ac07b20
parent 946 c94ef40a22ce
child 1021 fd1d073b6557
permissions -rw-r--r--
- Add makeSnapshot()/rollBack() functionality
- Remove an unnecessary #include
klao@946
     1
// -*- c++ -*-
klao@946
     2
#ifndef LEMON_ITERABLE_GRAPH_EXTENDER_H
klao@946
     3
#define LEMON_ITERABLE_GRAPH_EXTENDER_H
klao@946
     4
klao@946
     5
#include <lemon/invalid.h>
klao@946
     6
klao@946
     7
namespace lemon {
klao@946
     8
  
klao@946
     9
  template <typename _Base>
klao@946
    10
  class IterableGraphExtender : public _Base {
klao@962
    11
  public:
klao@946
    12
klao@946
    13
    typedef _Base Parent;
klao@946
    14
    typedef IterableGraphExtender<_Base> Graph;
klao@946
    15
klao@946
    16
    typedef typename Parent::Node Node;
klao@946
    17
    typedef typename Parent::Edge Edge;
klao@946
    18
klao@946
    19
klao@946
    20
    class NodeIt : public Node { 
klao@946
    21
      const Graph* graph;
klao@946
    22
    public:
klao@946
    23
klao@946
    24
      NodeIt() {}
klao@946
    25
klao@946
    26
      NodeIt(Invalid i) : Node(i) { }
klao@946
    27
klao@962
    28
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
klao@946
    29
	_graph.first(*static_cast<Node*>(this));
klao@946
    30
      }
klao@946
    31
klao@946
    32
      NodeIt(const Graph& _graph, const Node& node) 
klao@946
    33
	: Node(node), graph(&_graph) {}
klao@946
    34
klao@946
    35
      NodeIt& operator++() { 
klao@946
    36
	graph->next(*this);
klao@946
    37
	return *this; 
klao@946
    38
      }
klao@946
    39
klao@946
    40
    };
klao@946
    41
klao@946
    42
klao@946
    43
    class EdgeIt : public Edge { 
klao@946
    44
      const Graph* graph;
klao@946
    45
    public:
klao@946
    46
klao@946
    47
      EdgeIt() { }
klao@946
    48
klao@946
    49
      EdgeIt(Invalid i) : Edge(i) { }
klao@946
    50
klao@962
    51
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
klao@946
    52
	_graph.first(*static_cast<Edge*>(this));
klao@946
    53
      }
klao@946
    54
klao@946
    55
      EdgeIt(const Graph& _graph, const Edge& e) : 
klao@946
    56
	Edge(e), graph(&_graph) { }
klao@946
    57
klao@946
    58
      EdgeIt& operator++() { 
klao@946
    59
	graph->next(*this);
klao@946
    60
	return *this; 
klao@946
    61
      }
klao@946
    62
klao@946
    63
    };
klao@946
    64
klao@946
    65
klao@946
    66
    class OutEdgeIt : public Edge { 
klao@946
    67
      const Graph* graph;
klao@946
    68
    public:
klao@946
    69
klao@946
    70
      OutEdgeIt() { }
klao@946
    71
klao@946
    72
      OutEdgeIt(Invalid i) : Edge(i) { }
klao@946
    73
klao@946
    74
      OutEdgeIt(const Graph& _graph, const Node& node) 
klao@962
    75
	: graph(&_graph) {
klao@946
    76
	_graph.firstOut(*this, node);
klao@946
    77
      }
klao@946
    78
klao@946
    79
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
klao@946
    80
	: Edge(edge), graph(&_graph) {}
klao@946
    81
klao@946
    82
      OutEdgeIt& operator++() { 
klao@946
    83
	graph->nextOut(*this);
klao@946
    84
	return *this; 
klao@946
    85
      }
klao@946
    86
klao@946
    87
    };
klao@946
    88
klao@946
    89
klao@946
    90
    class InEdgeIt : public Edge { 
klao@946
    91
      const Graph* graph;
klao@946
    92
    public:
klao@946
    93
klao@946
    94
      InEdgeIt() { }
klao@946
    95
klao@946
    96
      InEdgeIt(Invalid i) : Edge(i) { }
klao@946
    97
klao@946
    98
      InEdgeIt(const Graph& _graph, const Node& node) 
klao@962
    99
	: graph(&_graph) {
klao@946
   100
	_graph.firstIn(*this, node);
klao@946
   101
      }
klao@946
   102
klao@946
   103
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
klao@946
   104
	Edge(edge), graph(&_graph) {}
klao@946
   105
klao@946
   106
      InEdgeIt& operator++() { 
klao@946
   107
	graph->nextIn(*this);
klao@946
   108
	return *this; 
klao@946
   109
      }
klao@946
   110
klao@946
   111
    };
klao@946
   112
klao@946
   113
    using Parent::first;
klao@946
   114
klao@946
   115
  private:
klao@946
   116
klao@946
   117
    /// \todo When (and if) we change the iterators concept to use operator*,
klao@946
   118
    /// then the following shadowed methods will become superfluous.
klao@946
   119
    /// But for now these are important safety measures.
klao@946
   120
klao@946
   121
    void first(NodeIt &) const;
klao@946
   122
    void first(EdgeIt &) const;
klao@946
   123
    void first(OutEdgeIt &) const;
klao@946
   124
    void first(InEdgeIt &) const;
klao@946
   125
klao@946
   126
  };
klao@946
   127
  
klao@962
   128
  template <typename _Base>
klao@962
   129
  class IterableUndirGraphExtender : public IterableGraphExtender<_Base> {
klao@962
   130
  public:
klao@962
   131
klao@962
   132
    typedef IterableGraphExtender<_Base> Parent;
klao@962
   133
    typedef IterableUndirGraphExtender<_Base> Graph;
klao@962
   134
klao@962
   135
    typedef typename Parent::UndirEdge UndirEdge;
klao@962
   136
klao@962
   137
    class UndirEdgeIt : public UndirEdge { 
klao@962
   138
      const Graph* graph;
klao@962
   139
    public:
klao@962
   140
klao@962
   141
      UndirEdgeIt() { }
klao@962
   142
klao@962
   143
      UndirEdgeIt(Invalid i) : UndirEdge(i) { }
klao@962
   144
klao@962
   145
      explicit UndirEdgeIt(const Graph& _graph) : graph(&_graph) {
klao@962
   146
	_graph.first(*static_cast<UndirEdge*>(this));
klao@962
   147
      }
klao@962
   148
klao@962
   149
      UndirEdgeIt(const Graph& _graph, const UndirEdge& e) : 
klao@962
   150
	UndirEdge(e), graph(&_graph) { }
klao@962
   151
klao@962
   152
      UndirEdgeIt& operator++() { 
klao@962
   153
	graph->next(*this);
klao@962
   154
	return *this; 
klao@962
   155
      }
klao@962
   156
klao@962
   157
    };
klao@962
   158
klao@962
   159
klao@962
   160
  };
klao@946
   161
}
klao@946
   162
klao@946
   163
#endif // LEMON_GRAPH_EXTENDER_H