[962] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1435] | 3 | * lemon/undir_graph_extender.h - Part of LEMON, a generic C++ |
---|
[962] | 4 | * optimization library |
---|
| 5 | * |
---|
[1164] | 6 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi |
---|
[1359] | 7 | * Kutatocsoport (Egervary Research Group on Combinatorial Optimization, |
---|
[962] | 8 | * EGRES). |
---|
| 9 | * |
---|
| 10 | * Permission to use, modify and distribute this software is granted |
---|
| 11 | * provided that this copyright notice appears in all copies. For |
---|
| 12 | * precise terms see the accompanying LICENSE file. |
---|
| 13 | * |
---|
| 14 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 15 | * express or implied, and with no claim as to its suitability for any |
---|
| 16 | * purpose. |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #ifndef LEMON_UNDIR_GRAPH_EXTENDER_H |
---|
| 21 | #define LEMON_UNDIR_GRAPH_EXTENDER_H |
---|
| 22 | |
---|
| 23 | #include <lemon/invalid.h> |
---|
| 24 | |
---|
| 25 | namespace lemon { |
---|
| 26 | |
---|
| 27 | template <typename _Base> |
---|
| 28 | class UndirGraphExtender : public _Base { |
---|
| 29 | typedef _Base Parent; |
---|
| 30 | typedef UndirGraphExtender Graph; |
---|
| 31 | |
---|
| 32 | public: |
---|
| 33 | |
---|
| 34 | typedef typename Parent::Edge UndirEdge; |
---|
| 35 | typedef typename Parent::Node Node; |
---|
| 36 | |
---|
| 37 | class Edge : public UndirEdge { |
---|
[978] | 38 | friend class UndirGraphExtender; |
---|
[962] | 39 | |
---|
| 40 | protected: |
---|
[1401] | 41 | // FIXME: Marci use opposite logic in his graph adaptors. It would |
---|
[962] | 42 | // be reasonable to syncronize... |
---|
| 43 | bool forward; |
---|
| 44 | |
---|
| 45 | Edge(const UndirEdge &ue, bool _forward) : |
---|
[1158] | 46 | UndirEdge(ue), forward(_forward) {} |
---|
| 47 | |
---|
[1627] | 48 | public: |
---|
| 49 | Edge() {} |
---|
[1158] | 50 | |
---|
[962] | 51 | /// Invalid edge constructor |
---|
[1053] | 52 | Edge(Invalid i) : UndirEdge(i), forward(true) {} |
---|
[962] | 53 | |
---|
| 54 | bool operator==(const Edge &that) const { |
---|
| 55 | return forward==that.forward && UndirEdge(*this)==UndirEdge(that); |
---|
| 56 | } |
---|
| 57 | bool operator!=(const Edge &that) const { |
---|
| 58 | return forward!=that.forward || UndirEdge(*this)!=UndirEdge(that); |
---|
| 59 | } |
---|
| 60 | bool operator<(const Edge &that) const { |
---|
| 61 | return forward<that.forward || |
---|
| 62 | (!(that.forward<forward) && UndirEdge(*this)<UndirEdge(that)); |
---|
| 63 | } |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | |
---|
[1158] | 67 | /// \brief Edge of opposite direction. |
---|
[962] | 68 | /// |
---|
[1158] | 69 | /// Returns the Edge of opposite direction. |
---|
[1627] | 70 | Edge oppositeEdge(const Edge &e) const { |
---|
[962] | 71 | return Edge(e,!e.forward); |
---|
| 72 | } |
---|
| 73 | |
---|
[1021] | 74 | public: |
---|
[986] | 75 | /// \todo Shouldn't the "source" of an undirected edge be called "aNode" |
---|
[962] | 76 | /// or something??? |
---|
[986] | 77 | using Parent::source; |
---|
[962] | 78 | |
---|
[1021] | 79 | /// Source of the given Edge. |
---|
| 80 | Node source(const Edge &e) const { |
---|
[1704] | 81 | return e.forward ? Parent::source(e) : Parent::target(e); |
---|
[962] | 82 | } |
---|
| 83 | |
---|
[986] | 84 | /// \todo Shouldn't the "target" of an undirected edge be called "bNode" |
---|
[962] | 85 | /// or something??? |
---|
[986] | 86 | using Parent::target; |
---|
[962] | 87 | |
---|
[1021] | 88 | /// Target of the given Edge. |
---|
| 89 | Node target(const Edge &e) const { |
---|
[1704] | 90 | return e.forward ? Parent::target(e) : Parent::source(e); |
---|
[1021] | 91 | } |
---|
| 92 | |
---|
[1030] | 93 | Node oppositeNode(const Node &n, const UndirEdge &e) const { |
---|
[986] | 94 | if( n == Parent::source(e)) |
---|
| 95 | return Parent::target(e); |
---|
| 96 | else if( n == Parent::target(e)) |
---|
| 97 | return Parent::source(e); |
---|
[962] | 98 | else |
---|
| 99 | return INVALID; |
---|
| 100 | } |
---|
| 101 | |
---|
[1627] | 102 | /// \brief Directed edge from an undirected edge and a source node. |
---|
[1158] | 103 | /// |
---|
| 104 | /// Returns a (directed) Edge corresponding to the specified UndirEdge |
---|
| 105 | /// and source Node. |
---|
| 106 | /// |
---|
[1627] | 107 | Edge direct(const UndirEdge &ue, const Node &s) const { |
---|
| 108 | return Edge(ue, s == source(ue)); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /// \brief Directed edge from an undirected edge. |
---|
[1158] | 112 | /// |
---|
[1627] | 113 | /// Returns a directed edge corresponding to the specified UndirEdge. |
---|
| 114 | /// If the given bool is true the given undirected edge and the |
---|
| 115 | /// returned edge have the same source node. |
---|
| 116 | Edge direct(const UndirEdge &ue, bool d) const { |
---|
| 117 | return Edge(ue, d); |
---|
[1158] | 118 | } |
---|
[962] | 119 | |
---|
[1627] | 120 | /// Returns whether the given directed edge is same orientation as the |
---|
| 121 | /// corresponding undirected edge. |
---|
| 122 | /// |
---|
| 123 | /// \todo reference to the corresponding point of the undirected graph |
---|
| 124 | /// concept. "What does the direction of an undirected edge mean?" |
---|
| 125 | bool direction(const Edge &e) const { return e.forward; } |
---|
| 126 | |
---|
| 127 | |
---|
[962] | 128 | using Parent::first; |
---|
| 129 | void first(Edge &e) const { |
---|
| 130 | Parent::first(e); |
---|
| 131 | e.forward=true; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | using Parent::next; |
---|
| 135 | void next(Edge &e) const { |
---|
| 136 | if( e.forward ) { |
---|
| 137 | e.forward = false; |
---|
| 138 | } |
---|
| 139 | else { |
---|
| 140 | Parent::next(e); |
---|
| 141 | e.forward = true; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
[1704] | 145 | public: |
---|
[1021] | 146 | |
---|
[1704] | 147 | void firstOut(Edge &e, const Node &n) const { |
---|
[1060] | 148 | Parent::firstIn(e,n); |
---|
[962] | 149 | if( UndirEdge(e) != INVALID ) { |
---|
[1060] | 150 | e.forward = false; |
---|
[962] | 151 | } |
---|
| 152 | else { |
---|
[1060] | 153 | Parent::firstOut(e,n); |
---|
| 154 | e.forward = true; |
---|
[962] | 155 | } |
---|
| 156 | } |
---|
[1704] | 157 | void nextOut(Edge &e) const { |
---|
[1060] | 158 | if( ! e.forward ) { |
---|
| 159 | Node n = Parent::target(e); |
---|
| 160 | Parent::nextIn(e); |
---|
| 161 | if( UndirEdge(e) == INVALID ) { |
---|
| 162 | Parent::firstOut(e, n); |
---|
| 163 | e.forward = true; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | else { |
---|
| 167 | Parent::nextOut(e); |
---|
| 168 | } |
---|
| 169 | } |
---|
[1704] | 170 | |
---|
| 171 | void firstIn(Edge &e, const Node &n) const { |
---|
| 172 | Parent::firstOut(e,n); |
---|
| 173 | if( UndirEdge(e) != INVALID ) { |
---|
| 174 | e.forward = false; |
---|
| 175 | } |
---|
| 176 | else { |
---|
| 177 | Parent::firstIn(e,n); |
---|
| 178 | e.forward = true; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | void nextIn(Edge &e) const { |
---|
[1060] | 182 | if( ! e.forward ) { |
---|
| 183 | Node n = Parent::source(e); |
---|
[962] | 184 | Parent::nextOut(e); |
---|
| 185 | if( UndirEdge(e) == INVALID ) { |
---|
[1060] | 186 | Parent::firstIn(e, n); |
---|
| 187 | e.forward = true; |
---|
[962] | 188 | } |
---|
| 189 | } |
---|
| 190 | else { |
---|
| 191 | Parent::nextIn(e); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
[1704] | 195 | void firstInc(UndirEdge &e, const Node &n) const { |
---|
| 196 | Parent::firstOut(e, n); |
---|
| 197 | if (e != INVALID) return; |
---|
| 198 | Parent::firstIn(e, n); |
---|
[1021] | 199 | } |
---|
[1704] | 200 | void nextInc(UndirEdge &e, const Node &n) const { |
---|
| 201 | if (Parent::source(e) == n) { |
---|
| 202 | Parent::nextOut(e); |
---|
| 203 | if (e != INVALID) return; |
---|
| 204 | Parent::firstIn(e, n); |
---|
| 205 | } else { |
---|
| 206 | Parent::nextIn(e); |
---|
| 207 | } |
---|
[1021] | 208 | } |
---|
| 209 | |
---|
[1704] | 210 | void firstInc(UndirEdge &e, bool &d, const Node &n) const { |
---|
| 211 | d = true; |
---|
| 212 | Parent::firstOut(e, n); |
---|
| 213 | if (e != INVALID) return; |
---|
| 214 | d = false; |
---|
| 215 | Parent::firstIn(e, n); |
---|
[1021] | 216 | } |
---|
[1704] | 217 | void nextInc(UndirEdge &e, bool &d) const { |
---|
| 218 | if (d) { |
---|
| 219 | Node s = Parent::source(e); |
---|
| 220 | Parent::nextOut(e); |
---|
| 221 | if (e != INVALID) return; |
---|
| 222 | d = false; |
---|
| 223 | Parent::firstIn(e, s); |
---|
| 224 | } else { |
---|
| 225 | Parent::nextIn(e); |
---|
| 226 | } |
---|
[1021] | 227 | } |
---|
| 228 | |
---|
[962] | 229 | // Miscellaneous stuff: |
---|
| 230 | |
---|
| 231 | /// \todo these methods (id, maxEdgeId) should be moved into separate |
---|
| 232 | /// Extender |
---|
| 233 | |
---|
[1021] | 234 | // using Parent::id; |
---|
| 235 | // Using "using" is not a good idea, cause it could be that there is |
---|
| 236 | // no "id" in Parent... |
---|
| 237 | |
---|
| 238 | int id(const Node &n) const { |
---|
| 239 | return Parent::id(n); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | int id(const UndirEdge &e) const { |
---|
| 243 | return Parent::id(e); |
---|
| 244 | } |
---|
[962] | 245 | |
---|
| 246 | int id(const Edge &e) const { |
---|
[981] | 247 | return 2 * Parent::id(e) + int(e.forward); |
---|
[962] | 248 | } |
---|
| 249 | |
---|
[1021] | 250 | |
---|
[1060] | 251 | int maxId(Node) const { |
---|
[1021] | 252 | return Parent::maxId(Node()); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | int maxId(Edge) const { |
---|
[981] | 256 | return 2 * Parent::maxId(typename Parent::Edge()) + 1; |
---|
[962] | 257 | } |
---|
[1021] | 258 | int maxId(UndirEdge) const { |
---|
[981] | 259 | return Parent::maxId(typename Parent::Edge()); |
---|
[962] | 260 | } |
---|
| 261 | |
---|
[1054] | 262 | |
---|
| 263 | int edgeNum() const { |
---|
| 264 | return 2 * Parent::edgeNum(); |
---|
| 265 | } |
---|
[1704] | 266 | |
---|
[1054] | 267 | int undirEdgeNum() const { |
---|
| 268 | return Parent::edgeNum(); |
---|
| 269 | } |
---|
| 270 | |
---|
[1704] | 271 | Edge findEdge(Node source, Node target, Edge prev) const { |
---|
| 272 | if (prev == INVALID) { |
---|
| 273 | UndirEdge edge = Parent::findEdge(source, target); |
---|
| 274 | if (edge != INVALID) return direct(edge, true); |
---|
| 275 | edge = Parent::findEdge(target, source); |
---|
| 276 | if (edge != INVALID) return direct(edge, false); |
---|
| 277 | } else if (direction(prev)) { |
---|
| 278 | UndirEdge edge = Parent::findEdge(source, target, prev); |
---|
| 279 | if (edge != INVALID) return direct(edge, true); |
---|
| 280 | edge = Parent::findEdge(target, source); |
---|
| 281 | if (edge != INVALID) return direct(edge, false); |
---|
| 282 | } else { |
---|
| 283 | UndirEdge edge = Parent::findEdge(target, source, prev); |
---|
| 284 | if (edge != INVALID) return direct(edge, false); |
---|
| 285 | } |
---|
| 286 | return INVALID; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | UndirEdge findUndirEdge(Node source, Node target, UndirEdge prev) const { |
---|
| 290 | if (prev == INVALID) { |
---|
| 291 | UndirEdge edge = Parent::findEdge(source, target); |
---|
| 292 | if (edge != INVALID) return edge; |
---|
| 293 | edge = Parent::findEdge(target, source); |
---|
| 294 | if (edge != INVALID) return edge; |
---|
| 295 | } else if (Parent::source(prev) == source) { |
---|
| 296 | UndirEdge edge = Parent::findEdge(source, target, prev); |
---|
| 297 | if (edge != INVALID) return edge; |
---|
| 298 | edge = Parent::findEdge(target, source); |
---|
| 299 | if (edge != INVALID) return edge; |
---|
| 300 | } else { |
---|
| 301 | UndirEdge edge = Parent::findEdge(target, source, prev); |
---|
| 302 | if (edge != INVALID) return edge; |
---|
| 303 | } |
---|
| 304 | return INVALID; |
---|
| 305 | } |
---|
| 306 | |
---|
[962] | 307 | }; |
---|
| 308 | |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | #endif // LEMON_UNDIR_GRAPH_EXTENDER_H |
---|