src/lemon/iterable_graph_extender.h
author deba
Mon, 07 Feb 2005 11:28:37 +0000
changeset 1136 8d066154b66a
parent 1022 567f392d1d2e
child 1158 29961fa390a3
permissions -rw-r--r--
Graph documentation
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@1021
   134
    typedef typename Parent::Node Node;
klao@962
   135
klao@962
   136
    typedef typename Parent::UndirEdge UndirEdge;
klao@962
   137
klao@962
   138
    class UndirEdgeIt : public UndirEdge { 
klao@962
   139
      const Graph* graph;
klao@962
   140
    public:
klao@962
   141
klao@962
   142
      UndirEdgeIt() { }
klao@962
   143
klao@962
   144
      UndirEdgeIt(Invalid i) : UndirEdge(i) { }
klao@962
   145
klao@962
   146
      explicit UndirEdgeIt(const Graph& _graph) : graph(&_graph) {
klao@962
   147
	_graph.first(*static_cast<UndirEdge*>(this));
klao@962
   148
      }
klao@962
   149
klao@962
   150
      UndirEdgeIt(const Graph& _graph, const UndirEdge& e) : 
klao@962
   151
	UndirEdge(e), graph(&_graph) { }
klao@962
   152
klao@962
   153
      UndirEdgeIt& operator++() { 
klao@962
   154
	graph->next(*this);
klao@962
   155
	return *this; 
klao@962
   156
      }
klao@962
   157
klao@962
   158
    };
klao@962
   159
klao@1030
   160
    class IncEdgeIt : public UndirEdge { 
klao@1021
   161
      const Graph* graph;
klao@1021
   162
      bool forward;
klao@1021
   163
      friend class IterableUndirGraphExtender;
klao@1021
   164
      template <typename G>
klao@1021
   165
      friend class UndirGraphExtender;
klao@1021
   166
    public:
klao@1021
   167
klao@1030
   168
      IncEdgeIt() { }
klao@1021
   169
klao@1030
   170
      IncEdgeIt(Invalid i) : UndirEdge(i), forward(false) { }
klao@1021
   171
klao@1030
   172
      explicit IncEdgeIt(const Graph& _graph, const Node &n)
klao@1021
   173
	: graph(&_graph)
klao@1021
   174
      {
klao@1021
   175
	_graph._dirFirstOut(*this, n);
klao@1021
   176
      }
klao@1021
   177
klao@1021
   178
      // FIXME: Do we need this type of constructor here?
klao@1030
   179
      // IncEdgeIt(const Graph& _graph, const Edge& e) : 
klao@1022
   180
      //   UndirEdge(e), graph(&_graph), forward(_graph.forward(e)) { }
klao@1022
   181
      // or
klao@1030
   182
      // IncEdgeIt(const Graph& _graph, const Node& n,
klao@1022
   183
      //    Const UndirEdge &e) ... ?
klao@1021
   184
klao@1030
   185
      IncEdgeIt& operator++() {
klao@1021
   186
	graph->_dirNextOut(*this);
klao@1021
   187
	return *this; 
klao@1021
   188
      }
klao@1021
   189
    };
klao@1021
   190
klao@1030
   191
    Node source(const IncEdgeIt &e) const {
klao@1021
   192
      return _dirSource(e);
klao@1021
   193
    }
klao@1021
   194
klao@1021
   195
    /// \todo Shouldn't the "source" of an undirected edge be called "aNode"
klao@1021
   196
    /// or something???
klao@1021
   197
    using Parent::source;
klao@1021
   198
klao@1021
   199
    /// Target of the given Edge.
klao@1030
   200
    Node target(const IncEdgeIt &e) const {
klao@1021
   201
      return _dirTarget(e);
klao@1021
   202
    }
klao@1021
   203
klao@1021
   204
    /// \todo Shouldn't the "target" of an undirected edge be called "bNode"
klao@1021
   205
    /// or something???
klao@1021
   206
    using Parent::target;
klao@962
   207
klao@962
   208
  };
klao@946
   209
}
klao@946
   210
klao@946
   211
#endif // LEMON_GRAPH_EXTENDER_H