[946] | 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 { |
---|
[962] | 11 | public: |
---|
[946] | 12 | |
---|
| 13 | typedef _Base Parent; |
---|
| 14 | typedef IterableGraphExtender<_Base> Graph; |
---|
| 15 | |
---|
| 16 | typedef typename Parent::Node Node; |
---|
| 17 | typedef typename Parent::Edge Edge; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | class NodeIt : public Node { |
---|
| 21 | const Graph* graph; |
---|
| 22 | public: |
---|
| 23 | |
---|
| 24 | NodeIt() {} |
---|
| 25 | |
---|
| 26 | NodeIt(Invalid i) : Node(i) { } |
---|
| 27 | |
---|
[962] | 28 | explicit NodeIt(const Graph& _graph) : graph(&_graph) { |
---|
[946] | 29 | _graph.first(*static_cast<Node*>(this)); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | NodeIt(const Graph& _graph, const Node& node) |
---|
| 33 | : Node(node), graph(&_graph) {} |
---|
| 34 | |
---|
| 35 | NodeIt& operator++() { |
---|
| 36 | graph->next(*this); |
---|
| 37 | return *this; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | class EdgeIt : public Edge { |
---|
| 44 | const Graph* graph; |
---|
| 45 | public: |
---|
| 46 | |
---|
| 47 | EdgeIt() { } |
---|
| 48 | |
---|
| 49 | EdgeIt(Invalid i) : Edge(i) { } |
---|
| 50 | |
---|
[962] | 51 | explicit EdgeIt(const Graph& _graph) : graph(&_graph) { |
---|
[946] | 52 | _graph.first(*static_cast<Edge*>(this)); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | EdgeIt(const Graph& _graph, const Edge& e) : |
---|
| 56 | Edge(e), graph(&_graph) { } |
---|
| 57 | |
---|
| 58 | EdgeIt& operator++() { |
---|
| 59 | graph->next(*this); |
---|
| 60 | return *this; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | class OutEdgeIt : public Edge { |
---|
| 67 | const Graph* graph; |
---|
| 68 | public: |
---|
| 69 | |
---|
| 70 | OutEdgeIt() { } |
---|
| 71 | |
---|
| 72 | OutEdgeIt(Invalid i) : Edge(i) { } |
---|
| 73 | |
---|
| 74 | OutEdgeIt(const Graph& _graph, const Node& node) |
---|
[962] | 75 | : graph(&_graph) { |
---|
[946] | 76 | _graph.firstOut(*this, node); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | OutEdgeIt(const Graph& _graph, const Edge& edge) |
---|
| 80 | : Edge(edge), graph(&_graph) {} |
---|
| 81 | |
---|
| 82 | OutEdgeIt& operator++() { |
---|
| 83 | graph->nextOut(*this); |
---|
| 84 | return *this; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | class InEdgeIt : public Edge { |
---|
| 91 | const Graph* graph; |
---|
| 92 | public: |
---|
| 93 | |
---|
| 94 | InEdgeIt() { } |
---|
| 95 | |
---|
| 96 | InEdgeIt(Invalid i) : Edge(i) { } |
---|
| 97 | |
---|
| 98 | InEdgeIt(const Graph& _graph, const Node& node) |
---|
[962] | 99 | : graph(&_graph) { |
---|
[946] | 100 | _graph.firstIn(*this, node); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | InEdgeIt(const Graph& _graph, const Edge& edge) : |
---|
| 104 | Edge(edge), graph(&_graph) {} |
---|
| 105 | |
---|
| 106 | InEdgeIt& operator++() { |
---|
| 107 | graph->nextIn(*this); |
---|
| 108 | return *this; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | using Parent::first; |
---|
| 114 | |
---|
| 115 | private: |
---|
| 116 | |
---|
| 117 | /// \todo When (and if) we change the iterators concept to use operator*, |
---|
| 118 | /// then the following shadowed methods will become superfluous. |
---|
| 119 | /// But for now these are important safety measures. |
---|
| 120 | |
---|
| 121 | void first(NodeIt &) const; |
---|
| 122 | void first(EdgeIt &) const; |
---|
| 123 | void first(OutEdgeIt &) const; |
---|
| 124 | void first(InEdgeIt &) const; |
---|
| 125 | |
---|
| 126 | }; |
---|
| 127 | |
---|
[962] | 128 | template <typename _Base> |
---|
| 129 | class IterableUndirGraphExtender : public IterableGraphExtender<_Base> { |
---|
| 130 | public: |
---|
| 131 | |
---|
| 132 | typedef IterableGraphExtender<_Base> Parent; |
---|
| 133 | typedef IterableUndirGraphExtender<_Base> Graph; |
---|
[1021] | 134 | typedef typename Parent::Node Node; |
---|
[962] | 135 | |
---|
| 136 | typedef typename Parent::UndirEdge UndirEdge; |
---|
| 137 | |
---|
| 138 | class UndirEdgeIt : public UndirEdge { |
---|
| 139 | const Graph* graph; |
---|
| 140 | public: |
---|
| 141 | |
---|
| 142 | UndirEdgeIt() { } |
---|
| 143 | |
---|
| 144 | UndirEdgeIt(Invalid i) : UndirEdge(i) { } |
---|
| 145 | |
---|
| 146 | explicit UndirEdgeIt(const Graph& _graph) : graph(&_graph) { |
---|
| 147 | _graph.first(*static_cast<UndirEdge*>(this)); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | UndirEdgeIt(const Graph& _graph, const UndirEdge& e) : |
---|
| 151 | UndirEdge(e), graph(&_graph) { } |
---|
| 152 | |
---|
| 153 | UndirEdgeIt& operator++() { |
---|
| 154 | graph->next(*this); |
---|
| 155 | return *this; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | }; |
---|
| 159 | |
---|
[1030] | 160 | class IncEdgeIt : public UndirEdge { |
---|
[1021] | 161 | const Graph* graph; |
---|
| 162 | bool forward; |
---|
| 163 | friend class IterableUndirGraphExtender; |
---|
| 164 | template <typename G> |
---|
| 165 | friend class UndirGraphExtender; |
---|
| 166 | public: |
---|
| 167 | |
---|
[1030] | 168 | IncEdgeIt() { } |
---|
[1021] | 169 | |
---|
[1030] | 170 | IncEdgeIt(Invalid i) : UndirEdge(i), forward(false) { } |
---|
[1021] | 171 | |
---|
[1030] | 172 | explicit IncEdgeIt(const Graph& _graph, const Node &n) |
---|
[1021] | 173 | : graph(&_graph) |
---|
| 174 | { |
---|
| 175 | _graph._dirFirstOut(*this, n); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | // FIXME: Do we need this type of constructor here? |
---|
[1030] | 179 | // IncEdgeIt(const Graph& _graph, const Edge& e) : |
---|
[1022] | 180 | // UndirEdge(e), graph(&_graph), forward(_graph.forward(e)) { } |
---|
| 181 | // or |
---|
[1030] | 182 | // IncEdgeIt(const Graph& _graph, const Node& n, |
---|
[1022] | 183 | // Const UndirEdge &e) ... ? |
---|
[1021] | 184 | |
---|
[1030] | 185 | IncEdgeIt& operator++() { |
---|
[1021] | 186 | graph->_dirNextOut(*this); |
---|
| 187 | return *this; |
---|
| 188 | } |
---|
| 189 | }; |
---|
| 190 | |
---|
[1030] | 191 | Node source(const IncEdgeIt &e) const { |
---|
[1021] | 192 | return _dirSource(e); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /// \todo Shouldn't the "source" of an undirected edge be called "aNode" |
---|
| 196 | /// or something??? |
---|
| 197 | using Parent::source; |
---|
| 198 | |
---|
| 199 | /// Target of the given Edge. |
---|
[1030] | 200 | Node target(const IncEdgeIt &e) const { |
---|
[1021] | 201 | return _dirTarget(e); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /// \todo Shouldn't the "target" of an undirected edge be called "bNode" |
---|
| 205 | /// or something??? |
---|
| 206 | using Parent::target; |
---|
[962] | 207 | |
---|
| 208 | }; |
---|
[946] | 209 | } |
---|
| 210 | |
---|
| 211 | #endif // LEMON_GRAPH_EXTENDER_H |
---|