[432] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[430] | 2 | * |
---|
[432] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[430] | 4 | * |
---|
[1270] | 5 | * Copyright (C) 2003-2013 |
---|
[430] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
---|
| 20 | #define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
---|
| 21 | |
---|
| 22 | #include <lemon/core.h> |
---|
| 23 | #include <lemon/error.h> |
---|
| 24 | |
---|
| 25 | namespace lemon { |
---|
| 26 | |
---|
| 27 | template <typename _Digraph> |
---|
| 28 | class DigraphAdaptorExtender : public _Digraph { |
---|
[664] | 29 | typedef _Digraph Parent; |
---|
| 30 | |
---|
[430] | 31 | public: |
---|
| 32 | |
---|
| 33 | typedef _Digraph Digraph; |
---|
| 34 | typedef DigraphAdaptorExtender Adaptor; |
---|
| 35 | |
---|
| 36 | // Base extensions |
---|
| 37 | |
---|
| 38 | typedef typename Parent::Node Node; |
---|
| 39 | typedef typename Parent::Arc Arc; |
---|
| 40 | |
---|
| 41 | int maxId(Node) const { |
---|
| 42 | return Parent::maxNodeId(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | int maxId(Arc) const { |
---|
| 46 | return Parent::maxArcId(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | Node fromId(int id, Node) const { |
---|
| 50 | return Parent::nodeFromId(id); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | Arc fromId(int id, Arc) const { |
---|
| 54 | return Parent::arcFromId(id); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | Node oppositeNode(const Node &n, const Arc &e) const { |
---|
| 58 | if (n == Parent::source(e)) |
---|
[432] | 59 | return Parent::target(e); |
---|
[430] | 60 | else if(n==Parent::target(e)) |
---|
[432] | 61 | return Parent::source(e); |
---|
[430] | 62 | else |
---|
[432] | 63 | return INVALID; |
---|
[430] | 64 | } |
---|
| 65 | |
---|
[432] | 66 | class NodeIt : public Node { |
---|
[430] | 67 | const Adaptor* _adaptor; |
---|
| 68 | public: |
---|
| 69 | |
---|
| 70 | NodeIt() {} |
---|
| 71 | |
---|
| 72 | NodeIt(Invalid i) : Node(i) { } |
---|
| 73 | |
---|
| 74 | explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 75 | _adaptor->first(static_cast<Node&>(*this)); |
---|
[430] | 76 | } |
---|
| 77 | |
---|
[432] | 78 | NodeIt(const Adaptor& adaptor, const Node& node) |
---|
| 79 | : Node(node), _adaptor(&adaptor) {} |
---|
[430] | 80 | |
---|
[432] | 81 | NodeIt& operator++() { |
---|
| 82 | _adaptor->next(*this); |
---|
| 83 | return *this; |
---|
[430] | 84 | } |
---|
| 85 | |
---|
| 86 | }; |
---|
| 87 | |
---|
[1337] | 88 | LemonRangeWrapper1<NodeIt, Adaptor> nodes() const { |
---|
[1336] | 89 | return LemonRangeWrapper1<NodeIt, Adaptor>(*this); |
---|
| 90 | } |
---|
[430] | 91 | |
---|
[432] | 92 | class ArcIt : public Arc { |
---|
[430] | 93 | const Adaptor* _adaptor; |
---|
| 94 | public: |
---|
| 95 | |
---|
| 96 | ArcIt() { } |
---|
| 97 | |
---|
| 98 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 99 | |
---|
| 100 | explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 101 | _adaptor->first(static_cast<Arc&>(*this)); |
---|
[430] | 102 | } |
---|
| 103 | |
---|
[432] | 104 | ArcIt(const Adaptor& adaptor, const Arc& e) : |
---|
| 105 | Arc(e), _adaptor(&adaptor) { } |
---|
[430] | 106 | |
---|
[432] | 107 | ArcIt& operator++() { |
---|
| 108 | _adaptor->next(*this); |
---|
| 109 | return *this; |
---|
[430] | 110 | } |
---|
| 111 | |
---|
| 112 | }; |
---|
| 113 | |
---|
[1337] | 114 | LemonRangeWrapper1<ArcIt, Adaptor> arcs() const { |
---|
[1336] | 115 | return LemonRangeWrapper1<ArcIt, Adaptor>(*this); |
---|
| 116 | } |
---|
| 117 | |
---|
[430] | 118 | |
---|
[432] | 119 | class OutArcIt : public Arc { |
---|
[430] | 120 | const Adaptor* _adaptor; |
---|
| 121 | public: |
---|
| 122 | |
---|
| 123 | OutArcIt() { } |
---|
| 124 | |
---|
| 125 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 126 | |
---|
[432] | 127 | OutArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 128 | : _adaptor(&adaptor) { |
---|
| 129 | _adaptor->firstOut(*this, node); |
---|
[430] | 130 | } |
---|
| 131 | |
---|
[432] | 132 | OutArcIt(const Adaptor& adaptor, const Arc& arc) |
---|
| 133 | : Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 134 | |
---|
[432] | 135 | OutArcIt& operator++() { |
---|
| 136 | _adaptor->nextOut(*this); |
---|
| 137 | return *this; |
---|
[430] | 138 | } |
---|
| 139 | |
---|
| 140 | }; |
---|
| 141 | |
---|
[1336] | 142 | LemonRangeWrapper2<OutArcIt, Adaptor, Node> outArcs(const Node& u) const { |
---|
| 143 | return LemonRangeWrapper2<OutArcIt, Adaptor, Node>(*this, u); |
---|
| 144 | } |
---|
| 145 | |
---|
[430] | 146 | |
---|
[432] | 147 | class InArcIt : public Arc { |
---|
[430] | 148 | const Adaptor* _adaptor; |
---|
| 149 | public: |
---|
| 150 | |
---|
| 151 | InArcIt() { } |
---|
| 152 | |
---|
| 153 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 154 | |
---|
[432] | 155 | InArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 156 | : _adaptor(&adaptor) { |
---|
| 157 | _adaptor->firstIn(*this, node); |
---|
[430] | 158 | } |
---|
| 159 | |
---|
[432] | 160 | InArcIt(const Adaptor& adaptor, const Arc& arc) : |
---|
| 161 | Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 162 | |
---|
[432] | 163 | InArcIt& operator++() { |
---|
| 164 | _adaptor->nextIn(*this); |
---|
| 165 | return *this; |
---|
[430] | 166 | } |
---|
| 167 | |
---|
| 168 | }; |
---|
| 169 | |
---|
[1336] | 170 | LemonRangeWrapper2<InArcIt, Adaptor, Node> inArcs(const Node& u) const { |
---|
| 171 | return LemonRangeWrapper2<InArcIt, Adaptor, Node>(*this, u); |
---|
| 172 | } |
---|
| 173 | |
---|
[430] | 174 | Node baseNode(const OutArcIt &e) const { |
---|
| 175 | return Parent::source(e); |
---|
| 176 | } |
---|
| 177 | Node runningNode(const OutArcIt &e) const { |
---|
| 178 | return Parent::target(e); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | Node baseNode(const InArcIt &e) const { |
---|
| 182 | return Parent::target(e); |
---|
| 183 | } |
---|
| 184 | Node runningNode(const InArcIt &e) const { |
---|
| 185 | return Parent::source(e); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | }; |
---|
| 189 | |
---|
[432] | 190 | template <typename _Graph> |
---|
[430] | 191 | class GraphAdaptorExtender : public _Graph { |
---|
[664] | 192 | typedef _Graph Parent; |
---|
| 193 | |
---|
[430] | 194 | public: |
---|
[432] | 195 | |
---|
[430] | 196 | typedef _Graph Graph; |
---|
| 197 | typedef GraphAdaptorExtender Adaptor; |
---|
| 198 | |
---|
[965] | 199 | typedef True UndirectedTag; |
---|
| 200 | |
---|
[430] | 201 | typedef typename Parent::Node Node; |
---|
| 202 | typedef typename Parent::Arc Arc; |
---|
| 203 | typedef typename Parent::Edge Edge; |
---|
| 204 | |
---|
[432] | 205 | // Graph extension |
---|
[430] | 206 | |
---|
| 207 | int maxId(Node) const { |
---|
| 208 | return Parent::maxNodeId(); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | int maxId(Arc) const { |
---|
| 212 | return Parent::maxArcId(); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | int maxId(Edge) const { |
---|
| 216 | return Parent::maxEdgeId(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | Node fromId(int id, Node) const { |
---|
| 220 | return Parent::nodeFromId(id); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | Arc fromId(int id, Arc) const { |
---|
| 224 | return Parent::arcFromId(id); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | Edge fromId(int id, Edge) const { |
---|
| 228 | return Parent::edgeFromId(id); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | Node oppositeNode(const Node &n, const Edge &e) const { |
---|
| 232 | if( n == Parent::u(e)) |
---|
[432] | 233 | return Parent::v(e); |
---|
[430] | 234 | else if( n == Parent::v(e)) |
---|
[432] | 235 | return Parent::u(e); |
---|
[430] | 236 | else |
---|
[432] | 237 | return INVALID; |
---|
[430] | 238 | } |
---|
| 239 | |
---|
| 240 | Arc oppositeArc(const Arc &a) const { |
---|
| 241 | return Parent::direct(a, !Parent::direction(a)); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | using Parent::direct; |
---|
| 245 | Arc direct(const Edge &e, const Node &s) const { |
---|
| 246 | return Parent::direct(e, Parent::u(e) == s); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
[432] | 250 | class NodeIt : public Node { |
---|
[430] | 251 | const Adaptor* _adaptor; |
---|
| 252 | public: |
---|
| 253 | |
---|
| 254 | NodeIt() {} |
---|
| 255 | |
---|
| 256 | NodeIt(Invalid i) : Node(i) { } |
---|
| 257 | |
---|
| 258 | explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 259 | _adaptor->first(static_cast<Node&>(*this)); |
---|
[430] | 260 | } |
---|
| 261 | |
---|
[432] | 262 | NodeIt(const Adaptor& adaptor, const Node& node) |
---|
| 263 | : Node(node), _adaptor(&adaptor) {} |
---|
[430] | 264 | |
---|
[432] | 265 | NodeIt& operator++() { |
---|
| 266 | _adaptor->next(*this); |
---|
| 267 | return *this; |
---|
[430] | 268 | } |
---|
| 269 | |
---|
| 270 | }; |
---|
| 271 | |
---|
[1337] | 272 | LemonRangeWrapper1<NodeIt, Adaptor> nodes() const { |
---|
[1336] | 273 | return LemonRangeWrapper1<NodeIt, Adaptor>(*this); |
---|
| 274 | } |
---|
| 275 | |
---|
[430] | 276 | |
---|
[432] | 277 | class ArcIt : public Arc { |
---|
[430] | 278 | const Adaptor* _adaptor; |
---|
| 279 | public: |
---|
| 280 | |
---|
| 281 | ArcIt() { } |
---|
| 282 | |
---|
| 283 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 284 | |
---|
| 285 | explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 286 | _adaptor->first(static_cast<Arc&>(*this)); |
---|
[430] | 287 | } |
---|
| 288 | |
---|
[432] | 289 | ArcIt(const Adaptor& adaptor, const Arc& e) : |
---|
| 290 | Arc(e), _adaptor(&adaptor) { } |
---|
[430] | 291 | |
---|
[432] | 292 | ArcIt& operator++() { |
---|
| 293 | _adaptor->next(*this); |
---|
| 294 | return *this; |
---|
[430] | 295 | } |
---|
| 296 | |
---|
| 297 | }; |
---|
| 298 | |
---|
[1337] | 299 | LemonRangeWrapper1<ArcIt, Adaptor> arcs() const { |
---|
[1336] | 300 | return LemonRangeWrapper1<ArcIt, Adaptor>(*this); |
---|
| 301 | } |
---|
| 302 | |
---|
[430] | 303 | |
---|
[432] | 304 | class OutArcIt : public Arc { |
---|
[430] | 305 | const Adaptor* _adaptor; |
---|
| 306 | public: |
---|
| 307 | |
---|
| 308 | OutArcIt() { } |
---|
| 309 | |
---|
| 310 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 311 | |
---|
[432] | 312 | OutArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 313 | : _adaptor(&adaptor) { |
---|
| 314 | _adaptor->firstOut(*this, node); |
---|
[430] | 315 | } |
---|
| 316 | |
---|
[432] | 317 | OutArcIt(const Adaptor& adaptor, const Arc& arc) |
---|
| 318 | : Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 319 | |
---|
[432] | 320 | OutArcIt& operator++() { |
---|
| 321 | _adaptor->nextOut(*this); |
---|
| 322 | return *this; |
---|
[430] | 323 | } |
---|
| 324 | |
---|
| 325 | }; |
---|
| 326 | |
---|
[1336] | 327 | LemonRangeWrapper2<OutArcIt, Adaptor, Node> outArcs(const Node& u) const { |
---|
| 328 | return LemonRangeWrapper2<OutArcIt, Adaptor, Node>(*this, u); |
---|
| 329 | } |
---|
| 330 | |
---|
[430] | 331 | |
---|
[432] | 332 | class InArcIt : public Arc { |
---|
[430] | 333 | const Adaptor* _adaptor; |
---|
| 334 | public: |
---|
| 335 | |
---|
| 336 | InArcIt() { } |
---|
| 337 | |
---|
| 338 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 339 | |
---|
[432] | 340 | InArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 341 | : _adaptor(&adaptor) { |
---|
| 342 | _adaptor->firstIn(*this, node); |
---|
[430] | 343 | } |
---|
| 344 | |
---|
[432] | 345 | InArcIt(const Adaptor& adaptor, const Arc& arc) : |
---|
| 346 | Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 347 | |
---|
[432] | 348 | InArcIt& operator++() { |
---|
| 349 | _adaptor->nextIn(*this); |
---|
| 350 | return *this; |
---|
[430] | 351 | } |
---|
| 352 | |
---|
| 353 | }; |
---|
| 354 | |
---|
[1336] | 355 | LemonRangeWrapper2<InArcIt, Adaptor, Node> inArcs(const Node& u) const { |
---|
| 356 | return LemonRangeWrapper2<InArcIt, Adaptor, Node>(*this, u); |
---|
| 357 | } |
---|
| 358 | |
---|
[432] | 359 | class EdgeIt : public Parent::Edge { |
---|
[430] | 360 | const Adaptor* _adaptor; |
---|
| 361 | public: |
---|
| 362 | |
---|
| 363 | EdgeIt() { } |
---|
| 364 | |
---|
| 365 | EdgeIt(Invalid i) : Edge(i) { } |
---|
| 366 | |
---|
| 367 | explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 368 | _adaptor->first(static_cast<Edge&>(*this)); |
---|
[430] | 369 | } |
---|
| 370 | |
---|
[432] | 371 | EdgeIt(const Adaptor& adaptor, const Edge& e) : |
---|
| 372 | Edge(e), _adaptor(&adaptor) { } |
---|
[430] | 373 | |
---|
[432] | 374 | EdgeIt& operator++() { |
---|
| 375 | _adaptor->next(*this); |
---|
| 376 | return *this; |
---|
[430] | 377 | } |
---|
| 378 | |
---|
| 379 | }; |
---|
| 380 | |
---|
[1337] | 381 | LemonRangeWrapper1<EdgeIt, Adaptor> edges() const { |
---|
[1336] | 382 | return LemonRangeWrapper1<EdgeIt, Adaptor>(*this); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | |
---|
[432] | 386 | class IncEdgeIt : public Edge { |
---|
[430] | 387 | friend class GraphAdaptorExtender; |
---|
| 388 | const Adaptor* _adaptor; |
---|
| 389 | bool direction; |
---|
| 390 | public: |
---|
| 391 | |
---|
| 392 | IncEdgeIt() { } |
---|
| 393 | |
---|
| 394 | IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
---|
| 395 | |
---|
| 396 | IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { |
---|
[432] | 397 | _adaptor->firstInc(static_cast<Edge&>(*this), direction, n); |
---|
[430] | 398 | } |
---|
| 399 | |
---|
| 400 | IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) |
---|
[432] | 401 | : _adaptor(&adaptor), Edge(e) { |
---|
| 402 | direction = (_adaptor->u(e) == n); |
---|
[430] | 403 | } |
---|
| 404 | |
---|
| 405 | IncEdgeIt& operator++() { |
---|
[432] | 406 | _adaptor->nextInc(*this, direction); |
---|
| 407 | return *this; |
---|
[430] | 408 | } |
---|
| 409 | }; |
---|
| 410 | |
---|
[1336] | 411 | LemonRangeWrapper2<IncEdgeIt, Adaptor, Node> incEdges(const Node& u) const { |
---|
| 412 | return LemonRangeWrapper2<IncEdgeIt, Adaptor, Node>(*this, u); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | |
---|
[430] | 416 | Node baseNode(const OutArcIt &a) const { |
---|
| 417 | return Parent::source(a); |
---|
| 418 | } |
---|
| 419 | Node runningNode(const OutArcIt &a) const { |
---|
| 420 | return Parent::target(a); |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | Node baseNode(const InArcIt &a) const { |
---|
| 424 | return Parent::target(a); |
---|
| 425 | } |
---|
| 426 | Node runningNode(const InArcIt &a) const { |
---|
| 427 | return Parent::source(a); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | Node baseNode(const IncEdgeIt &e) const { |
---|
| 431 | return e.direction ? Parent::u(e) : Parent::v(e); |
---|
| 432 | } |
---|
| 433 | Node runningNode(const IncEdgeIt &e) const { |
---|
| 434 | return e.direction ? Parent::v(e) : Parent::u(e); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | }; |
---|
| 438 | |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | #endif |
---|