lemon/bits/iterable_graph_extender.h
author alpar
Thu, 17 Nov 2005 10:16:29 +0000
changeset 1812 a6f019fa6e7a
parent 1627 3fd1ba6e9872
child 1820 22099ef840d7
permissions -rw-r--r--
split(Edge) member function added.
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>
alpar@1448
     6
#include <lemon/utility.h>
klao@946
     7
klao@946
     8
namespace lemon {
klao@946
     9
  
klao@946
    10
  template <typename _Base>
klao@946
    11
  class IterableGraphExtender : public _Base {
klao@962
    12
  public:
klao@946
    13
alpar@1448
    14
    /// Indicates that the graph is undirected.
alpar@1448
    15
alpar@1448
    16
    ///\todo Better name?
alpar@1448
    17
    ///
alpar@1448
    18
    ///\bug Should it be here?
alpar@1448
    19
    typedef False UndirTag;
alpar@1448
    20
klao@946
    21
    typedef _Base Parent;
klao@946
    22
    typedef IterableGraphExtender<_Base> Graph;
klao@946
    23
klao@946
    24
    typedef typename Parent::Node Node;
klao@946
    25
    typedef typename Parent::Edge Edge;
klao@946
    26
klao@946
    27
klao@946
    28
    class NodeIt : public Node { 
klao@946
    29
      const Graph* graph;
klao@946
    30
    public:
klao@946
    31
klao@946
    32
      NodeIt() {}
klao@946
    33
klao@946
    34
      NodeIt(Invalid i) : Node(i) { }
klao@946
    35
klao@962
    36
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
klao@946
    37
	_graph.first(*static_cast<Node*>(this));
klao@946
    38
      }
klao@946
    39
klao@946
    40
      NodeIt(const Graph& _graph, const Node& node) 
klao@946
    41
	: Node(node), graph(&_graph) {}
klao@946
    42
klao@946
    43
      NodeIt& operator++() { 
klao@946
    44
	graph->next(*this);
klao@946
    45
	return *this; 
klao@946
    46
      }
klao@946
    47
klao@946
    48
    };
klao@946
    49
klao@946
    50
klao@946
    51
    class EdgeIt : public Edge { 
klao@946
    52
      const Graph* graph;
klao@946
    53
    public:
klao@946
    54
klao@946
    55
      EdgeIt() { }
klao@946
    56
klao@946
    57
      EdgeIt(Invalid i) : Edge(i) { }
klao@946
    58
klao@962
    59
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
klao@946
    60
	_graph.first(*static_cast<Edge*>(this));
klao@946
    61
      }
klao@946
    62
klao@946
    63
      EdgeIt(const Graph& _graph, const Edge& e) : 
klao@946
    64
	Edge(e), graph(&_graph) { }
klao@946
    65
klao@946
    66
      EdgeIt& operator++() { 
klao@946
    67
	graph->next(*this);
klao@946
    68
	return *this; 
klao@946
    69
      }
klao@946
    70
klao@946
    71
    };
klao@946
    72
klao@946
    73
klao@946
    74
    class OutEdgeIt : public Edge { 
klao@946
    75
      const Graph* graph;
klao@946
    76
    public:
klao@946
    77
klao@946
    78
      OutEdgeIt() { }
klao@946
    79
klao@946
    80
      OutEdgeIt(Invalid i) : Edge(i) { }
klao@946
    81
klao@946
    82
      OutEdgeIt(const Graph& _graph, const Node& node) 
klao@962
    83
	: graph(&_graph) {
klao@946
    84
	_graph.firstOut(*this, node);
klao@946
    85
      }
klao@946
    86
klao@946
    87
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
klao@946
    88
	: Edge(edge), graph(&_graph) {}
klao@946
    89
klao@946
    90
      OutEdgeIt& operator++() { 
klao@946
    91
	graph->nextOut(*this);
klao@946
    92
	return *this; 
klao@946
    93
      }
klao@946
    94
klao@946
    95
    };
klao@946
    96
klao@946
    97
klao@946
    98
    class InEdgeIt : public Edge { 
klao@946
    99
      const Graph* graph;
klao@946
   100
    public:
klao@946
   101
klao@946
   102
      InEdgeIt() { }
klao@946
   103
klao@946
   104
      InEdgeIt(Invalid i) : Edge(i) { }
klao@946
   105
klao@946
   106
      InEdgeIt(const Graph& _graph, const Node& node) 
klao@962
   107
	: graph(&_graph) {
klao@946
   108
	_graph.firstIn(*this, node);
klao@946
   109
      }
klao@946
   110
klao@946
   111
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
klao@946
   112
	Edge(edge), graph(&_graph) {}
klao@946
   113
klao@946
   114
      InEdgeIt& operator++() { 
klao@946
   115
	graph->nextIn(*this);
klao@946
   116
	return *this; 
klao@946
   117
      }
klao@946
   118
klao@946
   119
    };
klao@946
   120
deba@1627
   121
    /// \brief Base node of the iterator
klao@1158
   122
    ///
klao@1158
   123
    /// Returns the base node (ie. the source in this case) of the iterator
klao@1158
   124
    Node baseNode(const OutEdgeIt &e) const {
deba@1564
   125
      return Parent::source((Edge)e);
klao@1158
   126
    }
deba@1627
   127
    /// \brief Running node of the iterator
klao@1158
   128
    ///
klao@1158
   129
    /// Returns the running node (ie. the target in this case) of the
klao@1158
   130
    /// iterator
klao@1158
   131
    Node runningNode(const OutEdgeIt &e) const {
deba@1564
   132
      return Parent::target((Edge)e);
klao@1158
   133
    }
klao@1158
   134
deba@1627
   135
    /// \brief Base node of the iterator
klao@1158
   136
    ///
klao@1158
   137
    /// Returns the base node (ie. the target in this case) of the iterator
klao@1158
   138
    Node baseNode(const InEdgeIt &e) const {
deba@1564
   139
      return Parent::target((Edge)e);
klao@1158
   140
    }
deba@1627
   141
    /// \brief Running node of the iterator
klao@1158
   142
    ///
klao@1158
   143
    /// Returns the running node (ie. the source in this case) of the
klao@1158
   144
    /// iterator
klao@1158
   145
    Node runningNode(const InEdgeIt &e) const {
deba@1564
   146
      return Parent::source((Edge)e);
klao@1158
   147
    }
klao@1158
   148
klao@946
   149
    using Parent::first;
klao@946
   150
deba@1627
   151
    /// \brief The opposite node on the given edge.
deba@1627
   152
    ///
deba@1627
   153
    /// Gives back the opposite on the given edge.
deba@1627
   154
    Node oppositeNode(const Node& n, const Edge& e) const {
deba@1627
   155
      if (Parent::source(e) == n) {
deba@1627
   156
	return Parent::target(e);
deba@1627
   157
      } else {
deba@1627
   158
	return Parent::source(e);
deba@1627
   159
      }
deba@1627
   160
    }
deba@1627
   161
klao@946
   162
  private:
klao@946
   163
klao@1230
   164
    // void first(NodeIt &) const;
klao@1230
   165
    // void first(EdgeIt &) const;
klao@1230
   166
    // void first(OutEdgeIt &) const;
klao@1230
   167
    // void first(InEdgeIt &) const;
klao@946
   168
klao@946
   169
  };
klao@1158
   170
klao@1158
   171
klao@1158
   172
klao@1158
   173
klao@1158
   174
klao@946
   175
  
klao@962
   176
  template <typename _Base>
klao@962
   177
  class IterableUndirGraphExtender : public IterableGraphExtender<_Base> {
klao@962
   178
  public:
klao@962
   179
alpar@1448
   180
    /// Indicates that the graph is undirected.
alpar@1448
   181
alpar@1448
   182
    ///\todo Better name?
alpar@1448
   183
    ///
alpar@1448
   184
    ///\bug Should it be here?
alpar@1448
   185
    ///\bug Should be tested in the concept checker whether it is defined
alpar@1448
   186
    ///correctly.
alpar@1448
   187
    typedef True UndirTag;
alpar@1448
   188
klao@962
   189
    typedef IterableGraphExtender<_Base> Parent;
klao@962
   190
    typedef IterableUndirGraphExtender<_Base> Graph;
klao@1021
   191
    typedef typename Parent::Node Node;
deba@1627
   192
    typedef typename Parent::Edge Edge;
klao@962
   193
    typedef typename Parent::UndirEdge UndirEdge;
klao@962
   194
klao@1230
   195
    class UndirEdgeIt : public Parent::UndirEdge { 
klao@962
   196
      const Graph* graph;
klao@962
   197
    public:
klao@962
   198
klao@962
   199
      UndirEdgeIt() { }
klao@962
   200
klao@962
   201
      UndirEdgeIt(Invalid i) : UndirEdge(i) { }
klao@962
   202
klao@962
   203
      explicit UndirEdgeIt(const Graph& _graph) : graph(&_graph) {
klao@962
   204
	_graph.first(*static_cast<UndirEdge*>(this));
klao@962
   205
      }
klao@962
   206
klao@962
   207
      UndirEdgeIt(const Graph& _graph, const UndirEdge& e) : 
klao@962
   208
	UndirEdge(e), graph(&_graph) { }
klao@962
   209
klao@962
   210
      UndirEdgeIt& operator++() { 
klao@962
   211
	graph->next(*this);
klao@962
   212
	return *this; 
klao@962
   213
      }
klao@962
   214
klao@962
   215
    };
klao@962
   216
klao@1230
   217
    class IncEdgeIt : public Parent::UndirEdge { 
klao@1021
   218
      const Graph* graph;
deba@1704
   219
      bool direction;
klao@1021
   220
      friend class IterableUndirGraphExtender;
klao@1021
   221
    public:
klao@1021
   222
klao@1030
   223
      IncEdgeIt() { }
klao@1021
   224
deba@1704
   225
      IncEdgeIt(Invalid i) : UndirEdge(i), direction(false) { }
klao@1021
   226
deba@1704
   227
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@1704
   228
	_graph.firstInc(*this, direction, n);
klao@1021
   229
      }
klao@1021
   230
klao@1158
   231
      IncEdgeIt(const Graph& _graph, const UndirEdge &ue, const Node &n)
deba@1704
   232
	: graph(&_graph), UndirEdge(ue) {
deba@1704
   233
	direction = (_graph.source(ue) == n);
klao@1158
   234
      }
klao@1021
   235
klao@1030
   236
      IncEdgeIt& operator++() {
deba@1704
   237
	graph->nextInc(*this, direction);
klao@1021
   238
	return *this; 
klao@1021
   239
      }
klao@1021
   240
    };
klao@1021
   241
klao@1158
   242
    using Parent::baseNode;
klao@1158
   243
    using Parent::runningNode;
klao@1158
   244
klao@1158
   245
    /// Base node of the iterator
klao@1158
   246
    ///
klao@1158
   247
    /// Returns the base node of the iterator
klao@1158
   248
    Node baseNode(const IncEdgeIt &e) const {
deba@1704
   249
      return e.direction ? source(e) : target(e);
klao@1021
   250
    }
klao@1158
   251
    /// Running node of the iterator
klao@1158
   252
    ///
klao@1158
   253
    /// Returns the running node of the iterator
klao@1158
   254
    Node runningNode(const IncEdgeIt &e) const {
deba@1704
   255
      return e.direction ? target(e) : source(e);
klao@1021
   256
    }
klao@1021
   257
deba@1627
   258
    /// \brief The opposite node on the given undirected edge.
deba@1627
   259
    ///
deba@1627
   260
    /// Gives back the opposite on the given undirected edge.
deba@1627
   261
    Node oppositeNode(const Node& n, const UndirEdge& e) const {
deba@1627
   262
      if (Parent::source(e) == n) {
deba@1627
   263
	return Parent::target(e);
deba@1627
   264
      } else {
deba@1627
   265
	return Parent::source(e);
deba@1627
   266
      }
deba@1627
   267
    }
deba@1627
   268
klao@962
   269
  };
klao@946
   270
}
klao@946
   271
klao@946
   272
#endif // LEMON_GRAPH_EXTENDER_H