[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 | * |
---|
| 5 | * Copyright (C) 2003-2008 |
---|
| 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 | #include <lemon/bits/default_map.h> |
---|
| 26 | |
---|
| 27 | namespace lemon { |
---|
| 28 | |
---|
| 29 | template <typename _Digraph> |
---|
| 30 | class DigraphAdaptorExtender : public _Digraph { |
---|
| 31 | public: |
---|
| 32 | |
---|
| 33 | typedef _Digraph Parent; |
---|
| 34 | typedef _Digraph Digraph; |
---|
| 35 | typedef DigraphAdaptorExtender Adaptor; |
---|
| 36 | |
---|
| 37 | // Base extensions |
---|
| 38 | |
---|
| 39 | typedef typename Parent::Node Node; |
---|
| 40 | typedef typename Parent::Arc Arc; |
---|
| 41 | |
---|
| 42 | int maxId(Node) const { |
---|
| 43 | return Parent::maxNodeId(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | int maxId(Arc) const { |
---|
| 47 | return Parent::maxArcId(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | Node fromId(int id, Node) const { |
---|
| 51 | return Parent::nodeFromId(id); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Arc fromId(int id, Arc) const { |
---|
| 55 | return Parent::arcFromId(id); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | Node oppositeNode(const Node &n, const Arc &e) const { |
---|
| 59 | if (n == Parent::source(e)) |
---|
[432] | 60 | return Parent::target(e); |
---|
[430] | 61 | else if(n==Parent::target(e)) |
---|
[432] | 62 | return Parent::source(e); |
---|
[430] | 63 | else |
---|
[432] | 64 | return INVALID; |
---|
[430] | 65 | } |
---|
| 66 | |
---|
[432] | 67 | class NodeIt : public Node { |
---|
[430] | 68 | const Adaptor* _adaptor; |
---|
| 69 | public: |
---|
| 70 | |
---|
| 71 | NodeIt() {} |
---|
| 72 | |
---|
| 73 | NodeIt(Invalid i) : Node(i) { } |
---|
| 74 | |
---|
| 75 | explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 76 | _adaptor->first(static_cast<Node&>(*this)); |
---|
[430] | 77 | } |
---|
| 78 | |
---|
[432] | 79 | NodeIt(const Adaptor& adaptor, const Node& node) |
---|
| 80 | : Node(node), _adaptor(&adaptor) {} |
---|
[430] | 81 | |
---|
[432] | 82 | NodeIt& operator++() { |
---|
| 83 | _adaptor->next(*this); |
---|
| 84 | return *this; |
---|
[430] | 85 | } |
---|
| 86 | |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | |
---|
[432] | 90 | class ArcIt : public Arc { |
---|
[430] | 91 | const Adaptor* _adaptor; |
---|
| 92 | public: |
---|
| 93 | |
---|
| 94 | ArcIt() { } |
---|
| 95 | |
---|
| 96 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 97 | |
---|
| 98 | explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 99 | _adaptor->first(static_cast<Arc&>(*this)); |
---|
[430] | 100 | } |
---|
| 101 | |
---|
[432] | 102 | ArcIt(const Adaptor& adaptor, const Arc& e) : |
---|
| 103 | Arc(e), _adaptor(&adaptor) { } |
---|
[430] | 104 | |
---|
[432] | 105 | ArcIt& operator++() { |
---|
| 106 | _adaptor->next(*this); |
---|
| 107 | return *this; |
---|
[430] | 108 | } |
---|
| 109 | |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | |
---|
[432] | 113 | class OutArcIt : public Arc { |
---|
[430] | 114 | const Adaptor* _adaptor; |
---|
| 115 | public: |
---|
| 116 | |
---|
| 117 | OutArcIt() { } |
---|
| 118 | |
---|
| 119 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 120 | |
---|
[432] | 121 | OutArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 122 | : _adaptor(&adaptor) { |
---|
| 123 | _adaptor->firstOut(*this, node); |
---|
[430] | 124 | } |
---|
| 125 | |
---|
[432] | 126 | OutArcIt(const Adaptor& adaptor, const Arc& arc) |
---|
| 127 | : Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 128 | |
---|
[432] | 129 | OutArcIt& operator++() { |
---|
| 130 | _adaptor->nextOut(*this); |
---|
| 131 | return *this; |
---|
[430] | 132 | } |
---|
| 133 | |
---|
| 134 | }; |
---|
| 135 | |
---|
| 136 | |
---|
[432] | 137 | class InArcIt : public Arc { |
---|
[430] | 138 | const Adaptor* _adaptor; |
---|
| 139 | public: |
---|
| 140 | |
---|
| 141 | InArcIt() { } |
---|
| 142 | |
---|
| 143 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 144 | |
---|
[432] | 145 | InArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 146 | : _adaptor(&adaptor) { |
---|
| 147 | _adaptor->firstIn(*this, node); |
---|
[430] | 148 | } |
---|
| 149 | |
---|
[432] | 150 | InArcIt(const Adaptor& adaptor, const Arc& arc) : |
---|
| 151 | Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 152 | |
---|
[432] | 153 | InArcIt& operator++() { |
---|
| 154 | _adaptor->nextIn(*this); |
---|
| 155 | return *this; |
---|
[430] | 156 | } |
---|
| 157 | |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | Node baseNode(const OutArcIt &e) const { |
---|
| 161 | return Parent::source(e); |
---|
| 162 | } |
---|
| 163 | Node runningNode(const OutArcIt &e) const { |
---|
| 164 | return Parent::target(e); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | Node baseNode(const InArcIt &e) const { |
---|
| 168 | return Parent::target(e); |
---|
| 169 | } |
---|
| 170 | Node runningNode(const InArcIt &e) const { |
---|
| 171 | return Parent::source(e); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | }; |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | /// \ingroup digraphbits |
---|
| 178 | /// |
---|
| 179 | /// \brief Extender for the GraphAdaptors |
---|
[432] | 180 | template <typename _Graph> |
---|
[430] | 181 | class GraphAdaptorExtender : public _Graph { |
---|
| 182 | public: |
---|
[432] | 183 | |
---|
[430] | 184 | typedef _Graph Parent; |
---|
| 185 | typedef _Graph Graph; |
---|
| 186 | typedef GraphAdaptorExtender Adaptor; |
---|
| 187 | |
---|
| 188 | typedef typename Parent::Node Node; |
---|
| 189 | typedef typename Parent::Arc Arc; |
---|
| 190 | typedef typename Parent::Edge Edge; |
---|
| 191 | |
---|
[432] | 192 | // Graph extension |
---|
[430] | 193 | |
---|
| 194 | int maxId(Node) const { |
---|
| 195 | return Parent::maxNodeId(); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int maxId(Arc) const { |
---|
| 199 | return Parent::maxArcId(); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | int maxId(Edge) const { |
---|
| 203 | return Parent::maxEdgeId(); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | Node fromId(int id, Node) const { |
---|
| 207 | return Parent::nodeFromId(id); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | Arc fromId(int id, Arc) const { |
---|
| 211 | return Parent::arcFromId(id); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | Edge fromId(int id, Edge) const { |
---|
| 215 | return Parent::edgeFromId(id); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | Node oppositeNode(const Node &n, const Edge &e) const { |
---|
| 219 | if( n == Parent::u(e)) |
---|
[432] | 220 | return Parent::v(e); |
---|
[430] | 221 | else if( n == Parent::v(e)) |
---|
[432] | 222 | return Parent::u(e); |
---|
[430] | 223 | else |
---|
[432] | 224 | return INVALID; |
---|
[430] | 225 | } |
---|
| 226 | |
---|
| 227 | Arc oppositeArc(const Arc &a) const { |
---|
| 228 | return Parent::direct(a, !Parent::direction(a)); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | using Parent::direct; |
---|
| 232 | Arc direct(const Edge &e, const Node &s) const { |
---|
| 233 | return Parent::direct(e, Parent::u(e) == s); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
[432] | 237 | class NodeIt : public Node { |
---|
[430] | 238 | const Adaptor* _adaptor; |
---|
| 239 | public: |
---|
| 240 | |
---|
| 241 | NodeIt() {} |
---|
| 242 | |
---|
| 243 | NodeIt(Invalid i) : Node(i) { } |
---|
| 244 | |
---|
| 245 | explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 246 | _adaptor->first(static_cast<Node&>(*this)); |
---|
[430] | 247 | } |
---|
| 248 | |
---|
[432] | 249 | NodeIt(const Adaptor& adaptor, const Node& node) |
---|
| 250 | : Node(node), _adaptor(&adaptor) {} |
---|
[430] | 251 | |
---|
[432] | 252 | NodeIt& operator++() { |
---|
| 253 | _adaptor->next(*this); |
---|
| 254 | return *this; |
---|
[430] | 255 | } |
---|
| 256 | |
---|
| 257 | }; |
---|
| 258 | |
---|
| 259 | |
---|
[432] | 260 | class ArcIt : public Arc { |
---|
[430] | 261 | const Adaptor* _adaptor; |
---|
| 262 | public: |
---|
| 263 | |
---|
| 264 | ArcIt() { } |
---|
| 265 | |
---|
| 266 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 267 | |
---|
| 268 | explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 269 | _adaptor->first(static_cast<Arc&>(*this)); |
---|
[430] | 270 | } |
---|
| 271 | |
---|
[432] | 272 | ArcIt(const Adaptor& adaptor, const Arc& e) : |
---|
| 273 | Arc(e), _adaptor(&adaptor) { } |
---|
[430] | 274 | |
---|
[432] | 275 | ArcIt& operator++() { |
---|
| 276 | _adaptor->next(*this); |
---|
| 277 | return *this; |
---|
[430] | 278 | } |
---|
| 279 | |
---|
| 280 | }; |
---|
| 281 | |
---|
| 282 | |
---|
[432] | 283 | class OutArcIt : public Arc { |
---|
[430] | 284 | const Adaptor* _adaptor; |
---|
| 285 | public: |
---|
| 286 | |
---|
| 287 | OutArcIt() { } |
---|
| 288 | |
---|
| 289 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 290 | |
---|
[432] | 291 | OutArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 292 | : _adaptor(&adaptor) { |
---|
| 293 | _adaptor->firstOut(*this, node); |
---|
[430] | 294 | } |
---|
| 295 | |
---|
[432] | 296 | OutArcIt(const Adaptor& adaptor, const Arc& arc) |
---|
| 297 | : Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 298 | |
---|
[432] | 299 | OutArcIt& operator++() { |
---|
| 300 | _adaptor->nextOut(*this); |
---|
| 301 | return *this; |
---|
[430] | 302 | } |
---|
| 303 | |
---|
| 304 | }; |
---|
| 305 | |
---|
| 306 | |
---|
[432] | 307 | class InArcIt : public Arc { |
---|
[430] | 308 | const Adaptor* _adaptor; |
---|
| 309 | public: |
---|
| 310 | |
---|
| 311 | InArcIt() { } |
---|
| 312 | |
---|
| 313 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 314 | |
---|
[432] | 315 | InArcIt(const Adaptor& adaptor, const Node& node) |
---|
| 316 | : _adaptor(&adaptor) { |
---|
| 317 | _adaptor->firstIn(*this, node); |
---|
[430] | 318 | } |
---|
| 319 | |
---|
[432] | 320 | InArcIt(const Adaptor& adaptor, const Arc& arc) : |
---|
| 321 | Arc(arc), _adaptor(&adaptor) {} |
---|
[430] | 322 | |
---|
[432] | 323 | InArcIt& operator++() { |
---|
| 324 | _adaptor->nextIn(*this); |
---|
| 325 | return *this; |
---|
[430] | 326 | } |
---|
| 327 | |
---|
| 328 | }; |
---|
| 329 | |
---|
[432] | 330 | class EdgeIt : public Parent::Edge { |
---|
[430] | 331 | const Adaptor* _adaptor; |
---|
| 332 | public: |
---|
| 333 | |
---|
| 334 | EdgeIt() { } |
---|
| 335 | |
---|
| 336 | EdgeIt(Invalid i) : Edge(i) { } |
---|
| 337 | |
---|
| 338 | explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
---|
[432] | 339 | _adaptor->first(static_cast<Edge&>(*this)); |
---|
[430] | 340 | } |
---|
| 341 | |
---|
[432] | 342 | EdgeIt(const Adaptor& adaptor, const Edge& e) : |
---|
| 343 | Edge(e), _adaptor(&adaptor) { } |
---|
[430] | 344 | |
---|
[432] | 345 | EdgeIt& operator++() { |
---|
| 346 | _adaptor->next(*this); |
---|
| 347 | return *this; |
---|
[430] | 348 | } |
---|
| 349 | |
---|
| 350 | }; |
---|
| 351 | |
---|
[432] | 352 | class IncEdgeIt : public Edge { |
---|
[430] | 353 | friend class GraphAdaptorExtender; |
---|
| 354 | const Adaptor* _adaptor; |
---|
| 355 | bool direction; |
---|
| 356 | public: |
---|
| 357 | |
---|
| 358 | IncEdgeIt() { } |
---|
| 359 | |
---|
| 360 | IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
---|
| 361 | |
---|
| 362 | IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { |
---|
[432] | 363 | _adaptor->firstInc(static_cast<Edge&>(*this), direction, n); |
---|
[430] | 364 | } |
---|
| 365 | |
---|
| 366 | IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) |
---|
[432] | 367 | : _adaptor(&adaptor), Edge(e) { |
---|
| 368 | direction = (_adaptor->u(e) == n); |
---|
[430] | 369 | } |
---|
| 370 | |
---|
| 371 | IncEdgeIt& operator++() { |
---|
[432] | 372 | _adaptor->nextInc(*this, direction); |
---|
| 373 | return *this; |
---|
[430] | 374 | } |
---|
| 375 | }; |
---|
| 376 | |
---|
| 377 | Node baseNode(const OutArcIt &a) const { |
---|
| 378 | return Parent::source(a); |
---|
| 379 | } |
---|
| 380 | Node runningNode(const OutArcIt &a) const { |
---|
| 381 | return Parent::target(a); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | Node baseNode(const InArcIt &a) const { |
---|
| 385 | return Parent::target(a); |
---|
| 386 | } |
---|
| 387 | Node runningNode(const InArcIt &a) const { |
---|
| 388 | return Parent::source(a); |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | Node baseNode(const IncEdgeIt &e) const { |
---|
| 392 | return e.direction ? Parent::u(e) : Parent::v(e); |
---|
| 393 | } |
---|
| 394 | Node runningNode(const IncEdgeIt &e) const { |
---|
| 395 | return e.direction ? Parent::v(e) : Parent::u(e); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | }; |
---|
| 399 | |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | |
---|
| 403 | #endif |
---|