[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[57] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[57] | 4 | * |
---|
[1270] | 5 | * Copyright (C) 2003-2013 |
---|
[57] | 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_EXTENDER_H |
---|
| 20 | #define LEMON_BITS_GRAPH_EXTENDER_H |
---|
| 21 | |
---|
[220] | 22 | #include <lemon/core.h> |
---|
[57] | 23 | |
---|
| 24 | #include <lemon/bits/map_extender.h> |
---|
| 25 | #include <lemon/bits/default_map.h> |
---|
| 26 | |
---|
| 27 | #include <lemon/concept_check.h> |
---|
| 28 | #include <lemon/concepts/maps.h> |
---|
| 29 | |
---|
[1336] | 30 | #include <lemon/bits/stl_iterators.h> |
---|
| 31 | |
---|
[314] | 32 | //\ingroup graphbits |
---|
| 33 | //\file |
---|
[373] | 34 | //\brief Extenders for the graph types |
---|
[57] | 35 | namespace lemon { |
---|
| 36 | |
---|
[314] | 37 | // \ingroup graphbits |
---|
| 38 | // |
---|
[373] | 39 | // \brief Extender for the digraph implementations |
---|
[57] | 40 | template <typename Base> |
---|
| 41 | class DigraphExtender : public Base { |
---|
[664] | 42 | typedef Base Parent; |
---|
| 43 | |
---|
[57] | 44 | public: |
---|
| 45 | |
---|
| 46 | typedef DigraphExtender Digraph; |
---|
| 47 | |
---|
| 48 | // Base extensions |
---|
| 49 | |
---|
| 50 | typedef typename Parent::Node Node; |
---|
| 51 | typedef typename Parent::Arc Arc; |
---|
| 52 | |
---|
| 53 | int maxId(Node) const { |
---|
| 54 | return Parent::maxNodeId(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | int maxId(Arc) const { |
---|
| 58 | return Parent::maxArcId(); |
---|
| 59 | } |
---|
| 60 | |
---|
[825] | 61 | static Node fromId(int id, Node) { |
---|
[57] | 62 | return Parent::nodeFromId(id); |
---|
| 63 | } |
---|
| 64 | |
---|
[825] | 65 | static Arc fromId(int id, Arc) { |
---|
[57] | 66 | return Parent::arcFromId(id); |
---|
| 67 | } |
---|
| 68 | |
---|
[78] | 69 | Node oppositeNode(const Node &node, const Arc &arc) const { |
---|
| 70 | if (node == Parent::source(arc)) |
---|
[209] | 71 | return Parent::target(arc); |
---|
[78] | 72 | else if(node == Parent::target(arc)) |
---|
[209] | 73 | return Parent::source(arc); |
---|
[57] | 74 | else |
---|
[209] | 75 | return INVALID; |
---|
[57] | 76 | } |
---|
| 77 | |
---|
| 78 | // Alterable extension |
---|
| 79 | |
---|
| 80 | typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
---|
| 81 | typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | protected: |
---|
| 85 | |
---|
| 86 | mutable NodeNotifier node_notifier; |
---|
| 87 | mutable ArcNotifier arc_notifier; |
---|
| 88 | |
---|
| 89 | public: |
---|
| 90 | |
---|
| 91 | NodeNotifier& notifier(Node) const { |
---|
| 92 | return node_notifier; |
---|
| 93 | } |
---|
[209] | 94 | |
---|
[57] | 95 | ArcNotifier& notifier(Arc) const { |
---|
| 96 | return arc_notifier; |
---|
| 97 | } |
---|
| 98 | |
---|
[209] | 99 | class NodeIt : public Node { |
---|
[78] | 100 | const Digraph* _digraph; |
---|
[57] | 101 | public: |
---|
| 102 | |
---|
| 103 | NodeIt() {} |
---|
| 104 | |
---|
| 105 | NodeIt(Invalid i) : Node(i) { } |
---|
| 106 | |
---|
[78] | 107 | explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
---|
[209] | 108 | _digraph->first(static_cast<Node&>(*this)); |
---|
[57] | 109 | } |
---|
| 110 | |
---|
[209] | 111 | NodeIt(const Digraph& digraph, const Node& node) |
---|
| 112 | : Node(node), _digraph(&digraph) {} |
---|
[57] | 113 | |
---|
[209] | 114 | NodeIt& operator++() { |
---|
| 115 | _digraph->next(*this); |
---|
| 116 | return *this; |
---|
[57] | 117 | } |
---|
| 118 | |
---|
| 119 | }; |
---|
| 120 | |
---|
[1336] | 121 | LemonRangeWrapper1<NodeIt, Digraph> nodes() const { |
---|
| 122 | return LemonRangeWrapper1<NodeIt, Digraph>(*this); |
---|
| 123 | } |
---|
| 124 | |
---|
[57] | 125 | |
---|
[209] | 126 | class ArcIt : public Arc { |
---|
[78] | 127 | const Digraph* _digraph; |
---|
[57] | 128 | public: |
---|
| 129 | |
---|
| 130 | ArcIt() { } |
---|
| 131 | |
---|
| 132 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 133 | |
---|
[78] | 134 | explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) { |
---|
[209] | 135 | _digraph->first(static_cast<Arc&>(*this)); |
---|
[57] | 136 | } |
---|
| 137 | |
---|
[209] | 138 | ArcIt(const Digraph& digraph, const Arc& arc) : |
---|
| 139 | Arc(arc), _digraph(&digraph) { } |
---|
[57] | 140 | |
---|
[209] | 141 | ArcIt& operator++() { |
---|
| 142 | _digraph->next(*this); |
---|
| 143 | return *this; |
---|
[57] | 144 | } |
---|
| 145 | |
---|
| 146 | }; |
---|
| 147 | |
---|
[1336] | 148 | LemonRangeWrapper1<ArcIt, Digraph> arcs() const { |
---|
| 149 | return LemonRangeWrapper1<ArcIt, Digraph>(*this); |
---|
| 150 | } |
---|
| 151 | |
---|
[57] | 152 | |
---|
[209] | 153 | class OutArcIt : public Arc { |
---|
[78] | 154 | const Digraph* _digraph; |
---|
[57] | 155 | public: |
---|
| 156 | |
---|
| 157 | OutArcIt() { } |
---|
| 158 | |
---|
| 159 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 160 | |
---|
[209] | 161 | OutArcIt(const Digraph& digraph, const Node& node) |
---|
| 162 | : _digraph(&digraph) { |
---|
| 163 | _digraph->firstOut(*this, node); |
---|
[57] | 164 | } |
---|
| 165 | |
---|
[209] | 166 | OutArcIt(const Digraph& digraph, const Arc& arc) |
---|
| 167 | : Arc(arc), _digraph(&digraph) {} |
---|
[57] | 168 | |
---|
[209] | 169 | OutArcIt& operator++() { |
---|
| 170 | _digraph->nextOut(*this); |
---|
| 171 | return *this; |
---|
[57] | 172 | } |
---|
| 173 | |
---|
| 174 | }; |
---|
| 175 | |
---|
[1336] | 176 | LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const { |
---|
| 177 | return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u); |
---|
| 178 | } |
---|
| 179 | |
---|
[57] | 180 | |
---|
[209] | 181 | class InArcIt : public Arc { |
---|
[78] | 182 | const Digraph* _digraph; |
---|
[57] | 183 | public: |
---|
| 184 | |
---|
| 185 | InArcIt() { } |
---|
| 186 | |
---|
| 187 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 188 | |
---|
[209] | 189 | InArcIt(const Digraph& digraph, const Node& node) |
---|
| 190 | : _digraph(&digraph) { |
---|
| 191 | _digraph->firstIn(*this, node); |
---|
[57] | 192 | } |
---|
| 193 | |
---|
[209] | 194 | InArcIt(const Digraph& digraph, const Arc& arc) : |
---|
| 195 | Arc(arc), _digraph(&digraph) {} |
---|
[57] | 196 | |
---|
[209] | 197 | InArcIt& operator++() { |
---|
| 198 | _digraph->nextIn(*this); |
---|
| 199 | return *this; |
---|
[57] | 200 | } |
---|
| 201 | |
---|
| 202 | }; |
---|
| 203 | |
---|
[1336] | 204 | LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const { |
---|
| 205 | return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u); |
---|
| 206 | } |
---|
| 207 | |
---|
[314] | 208 | // \brief Base node of the iterator |
---|
| 209 | // |
---|
| 210 | // Returns the base node (i.e. the source in this case) of the iterator |
---|
[78] | 211 | Node baseNode(const OutArcIt &arc) const { |
---|
| 212 | return Parent::source(arc); |
---|
[57] | 213 | } |
---|
[314] | 214 | // \brief Running node of the iterator |
---|
| 215 | // |
---|
| 216 | // Returns the running node (i.e. the target in this case) of the |
---|
| 217 | // iterator |
---|
[78] | 218 | Node runningNode(const OutArcIt &arc) const { |
---|
| 219 | return Parent::target(arc); |
---|
[57] | 220 | } |
---|
| 221 | |
---|
[314] | 222 | // \brief Base node of the iterator |
---|
| 223 | // |
---|
| 224 | // Returns the base node (i.e. the target in this case) of the iterator |
---|
[78] | 225 | Node baseNode(const InArcIt &arc) const { |
---|
| 226 | return Parent::target(arc); |
---|
[57] | 227 | } |
---|
[314] | 228 | // \brief Running node of the iterator |
---|
| 229 | // |
---|
| 230 | // Returns the running node (i.e. the source in this case) of the |
---|
| 231 | // iterator |
---|
[78] | 232 | Node runningNode(const InArcIt &arc) const { |
---|
| 233 | return Parent::source(arc); |
---|
[57] | 234 | } |
---|
| 235 | |
---|
[209] | 236 | |
---|
[57] | 237 | template <typename _Value> |
---|
[209] | 238 | class NodeMap |
---|
[57] | 239 | : public MapExtender<DefaultMap<Digraph, Node, _Value> > { |
---|
| 240 | typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent; |
---|
| 241 | |
---|
[664] | 242 | public: |
---|
[209] | 243 | explicit NodeMap(const Digraph& digraph) |
---|
| 244 | : Parent(digraph) {} |
---|
| 245 | NodeMap(const Digraph& digraph, const _Value& value) |
---|
| 246 | : Parent(digraph, value) {} |
---|
[57] | 247 | |
---|
[263] | 248 | private: |
---|
[57] | 249 | NodeMap& operator=(const NodeMap& cmap) { |
---|
[209] | 250 | return operator=<NodeMap>(cmap); |
---|
[57] | 251 | } |
---|
| 252 | |
---|
| 253 | template <typename CMap> |
---|
| 254 | NodeMap& operator=(const CMap& cmap) { |
---|
| 255 | Parent::operator=(cmap); |
---|
[209] | 256 | return *this; |
---|
[57] | 257 | } |
---|
| 258 | |
---|
| 259 | }; |
---|
| 260 | |
---|
| 261 | template <typename _Value> |
---|
[209] | 262 | class ArcMap |
---|
[57] | 263 | : public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
---|
| 264 | typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
---|
| 265 | |
---|
[664] | 266 | public: |
---|
[209] | 267 | explicit ArcMap(const Digraph& digraph) |
---|
| 268 | : Parent(digraph) {} |
---|
| 269 | ArcMap(const Digraph& digraph, const _Value& value) |
---|
| 270 | : Parent(digraph, value) {} |
---|
[57] | 271 | |
---|
[263] | 272 | private: |
---|
[57] | 273 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[209] | 274 | return operator=<ArcMap>(cmap); |
---|
[57] | 275 | } |
---|
| 276 | |
---|
| 277 | template <typename CMap> |
---|
| 278 | ArcMap& operator=(const CMap& cmap) { |
---|
| 279 | Parent::operator=(cmap); |
---|
[209] | 280 | return *this; |
---|
[57] | 281 | } |
---|
| 282 | }; |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | Node addNode() { |
---|
| 286 | Node node = Parent::addNode(); |
---|
| 287 | notifier(Node()).add(node); |
---|
| 288 | return node; |
---|
| 289 | } |
---|
[209] | 290 | |
---|
[57] | 291 | Arc addArc(const Node& from, const Node& to) { |
---|
| 292 | Arc arc = Parent::addArc(from, to); |
---|
| 293 | notifier(Arc()).add(arc); |
---|
| 294 | return arc; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void clear() { |
---|
| 298 | notifier(Arc()).clear(); |
---|
| 299 | notifier(Node()).clear(); |
---|
| 300 | Parent::clear(); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
---|
| 304 | void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) { |
---|
| 305 | Parent::build(digraph, nodeRef, arcRef); |
---|
| 306 | notifier(Node()).build(); |
---|
| 307 | notifier(Arc()).build(); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | void erase(const Node& node) { |
---|
| 311 | Arc arc; |
---|
| 312 | Parent::firstOut(arc, node); |
---|
| 313 | while (arc != INVALID ) { |
---|
[209] | 314 | erase(arc); |
---|
| 315 | Parent::firstOut(arc, node); |
---|
| 316 | } |
---|
[57] | 317 | |
---|
| 318 | Parent::firstIn(arc, node); |
---|
| 319 | while (arc != INVALID ) { |
---|
[209] | 320 | erase(arc); |
---|
| 321 | Parent::firstIn(arc, node); |
---|
[57] | 322 | } |
---|
| 323 | |
---|
| 324 | notifier(Node()).erase(node); |
---|
| 325 | Parent::erase(node); |
---|
| 326 | } |
---|
[209] | 327 | |
---|
[57] | 328 | void erase(const Arc& arc) { |
---|
| 329 | notifier(Arc()).erase(arc); |
---|
| 330 | Parent::erase(arc); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | DigraphExtender() { |
---|
| 334 | node_notifier.setContainer(*this); |
---|
| 335 | arc_notifier.setContainer(*this); |
---|
[209] | 336 | } |
---|
| 337 | |
---|
[57] | 338 | |
---|
| 339 | ~DigraphExtender() { |
---|
| 340 | arc_notifier.clear(); |
---|
| 341 | node_notifier.clear(); |
---|
| 342 | } |
---|
| 343 | }; |
---|
| 344 | |
---|
[314] | 345 | // \ingroup _graphbits |
---|
| 346 | // |
---|
| 347 | // \brief Extender for the Graphs |
---|
[209] | 348 | template <typename Base> |
---|
[57] | 349 | class GraphExtender : public Base { |
---|
[664] | 350 | typedef Base Parent; |
---|
| 351 | |
---|
[57] | 352 | public: |
---|
[209] | 353 | |
---|
[78] | 354 | typedef GraphExtender Graph; |
---|
[57] | 355 | |
---|
[77] | 356 | typedef True UndirectedTag; |
---|
| 357 | |
---|
[57] | 358 | typedef typename Parent::Node Node; |
---|
| 359 | typedef typename Parent::Arc Arc; |
---|
| 360 | typedef typename Parent::Edge Edge; |
---|
| 361 | |
---|
[209] | 362 | // Graph extension |
---|
[57] | 363 | |
---|
| 364 | int maxId(Node) const { |
---|
| 365 | return Parent::maxNodeId(); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | int maxId(Arc) const { |
---|
| 369 | return Parent::maxArcId(); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | int maxId(Edge) const { |
---|
| 373 | return Parent::maxEdgeId(); |
---|
| 374 | } |
---|
| 375 | |
---|
[825] | 376 | static Node fromId(int id, Node) { |
---|
[57] | 377 | return Parent::nodeFromId(id); |
---|
| 378 | } |
---|
| 379 | |
---|
[825] | 380 | static Arc fromId(int id, Arc) { |
---|
[57] | 381 | return Parent::arcFromId(id); |
---|
| 382 | } |
---|
| 383 | |
---|
[825] | 384 | static Edge fromId(int id, Edge) { |
---|
[57] | 385 | return Parent::edgeFromId(id); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | Node oppositeNode(const Node &n, const Edge &e) const { |
---|
[125] | 389 | if( n == Parent::u(e)) |
---|
[209] | 390 | return Parent::v(e); |
---|
[125] | 391 | else if( n == Parent::v(e)) |
---|
[209] | 392 | return Parent::u(e); |
---|
[57] | 393 | else |
---|
[209] | 394 | return INVALID; |
---|
[57] | 395 | } |
---|
| 396 | |
---|
[78] | 397 | Arc oppositeArc(const Arc &arc) const { |
---|
| 398 | return Parent::direct(arc, !Parent::direction(arc)); |
---|
[57] | 399 | } |
---|
| 400 | |
---|
| 401 | using Parent::direct; |
---|
[78] | 402 | Arc direct(const Edge &edge, const Node &node) const { |
---|
[125] | 403 | return Parent::direct(edge, Parent::u(edge) == node); |
---|
[57] | 404 | } |
---|
| 405 | |
---|
| 406 | // Alterable extension |
---|
| 407 | |
---|
| 408 | typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
---|
| 409 | typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
---|
| 410 | typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
---|
| 411 | |
---|
| 412 | |
---|
| 413 | protected: |
---|
| 414 | |
---|
| 415 | mutable NodeNotifier node_notifier; |
---|
| 416 | mutable ArcNotifier arc_notifier; |
---|
| 417 | mutable EdgeNotifier edge_notifier; |
---|
| 418 | |
---|
| 419 | public: |
---|
| 420 | |
---|
| 421 | NodeNotifier& notifier(Node) const { |
---|
| 422 | return node_notifier; |
---|
| 423 | } |
---|
[209] | 424 | |
---|
[57] | 425 | ArcNotifier& notifier(Arc) const { |
---|
| 426 | return arc_notifier; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | EdgeNotifier& notifier(Edge) const { |
---|
| 430 | return edge_notifier; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | |
---|
| 434 | |
---|
[209] | 435 | class NodeIt : public Node { |
---|
[78] | 436 | const Graph* _graph; |
---|
[57] | 437 | public: |
---|
| 438 | |
---|
| 439 | NodeIt() {} |
---|
| 440 | |
---|
| 441 | NodeIt(Invalid i) : Node(i) { } |
---|
| 442 | |
---|
[78] | 443 | explicit NodeIt(const Graph& graph) : _graph(&graph) { |
---|
[209] | 444 | _graph->first(static_cast<Node&>(*this)); |
---|
[57] | 445 | } |
---|
| 446 | |
---|
[209] | 447 | NodeIt(const Graph& graph, const Node& node) |
---|
| 448 | : Node(node), _graph(&graph) {} |
---|
[57] | 449 | |
---|
[209] | 450 | NodeIt& operator++() { |
---|
| 451 | _graph->next(*this); |
---|
| 452 | return *this; |
---|
[57] | 453 | } |
---|
| 454 | |
---|
| 455 | }; |
---|
| 456 | |
---|
[1336] | 457 | LemonRangeWrapper1<NodeIt, Graph> nodes() const { |
---|
| 458 | return LemonRangeWrapper1<NodeIt, Graph>(*this); |
---|
| 459 | } |
---|
| 460 | |
---|
[57] | 461 | |
---|
[209] | 462 | class ArcIt : public Arc { |
---|
[78] | 463 | const Graph* _graph; |
---|
[57] | 464 | public: |
---|
| 465 | |
---|
| 466 | ArcIt() { } |
---|
| 467 | |
---|
| 468 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 469 | |
---|
[78] | 470 | explicit ArcIt(const Graph& graph) : _graph(&graph) { |
---|
[209] | 471 | _graph->first(static_cast<Arc&>(*this)); |
---|
[57] | 472 | } |
---|
| 473 | |
---|
[209] | 474 | ArcIt(const Graph& graph, const Arc& arc) : |
---|
| 475 | Arc(arc), _graph(&graph) { } |
---|
[57] | 476 | |
---|
[209] | 477 | ArcIt& operator++() { |
---|
| 478 | _graph->next(*this); |
---|
| 479 | return *this; |
---|
[57] | 480 | } |
---|
| 481 | |
---|
| 482 | }; |
---|
| 483 | |
---|
[1336] | 484 | LemonRangeWrapper1<ArcIt, Graph> arcs() const { |
---|
| 485 | return LemonRangeWrapper1<ArcIt, Graph>(*this); |
---|
| 486 | } |
---|
| 487 | |
---|
[57] | 488 | |
---|
[209] | 489 | class OutArcIt : public Arc { |
---|
[78] | 490 | const Graph* _graph; |
---|
[57] | 491 | public: |
---|
| 492 | |
---|
| 493 | OutArcIt() { } |
---|
| 494 | |
---|
| 495 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 496 | |
---|
[209] | 497 | OutArcIt(const Graph& graph, const Node& node) |
---|
| 498 | : _graph(&graph) { |
---|
| 499 | _graph->firstOut(*this, node); |
---|
[57] | 500 | } |
---|
| 501 | |
---|
[209] | 502 | OutArcIt(const Graph& graph, const Arc& arc) |
---|
| 503 | : Arc(arc), _graph(&graph) {} |
---|
[57] | 504 | |
---|
[209] | 505 | OutArcIt& operator++() { |
---|
| 506 | _graph->nextOut(*this); |
---|
| 507 | return *this; |
---|
[57] | 508 | } |
---|
| 509 | |
---|
| 510 | }; |
---|
| 511 | |
---|
[1336] | 512 | LemonRangeWrapper2<OutArcIt, Graph, Node> outArcs(const Node& u) const { |
---|
| 513 | return LemonRangeWrapper2<OutArcIt, Graph, Node>(*this, u); |
---|
| 514 | } |
---|
| 515 | |
---|
[57] | 516 | |
---|
[209] | 517 | class InArcIt : public Arc { |
---|
[78] | 518 | const Graph* _graph; |
---|
[57] | 519 | public: |
---|
| 520 | |
---|
| 521 | InArcIt() { } |
---|
| 522 | |
---|
| 523 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 524 | |
---|
[209] | 525 | InArcIt(const Graph& graph, const Node& node) |
---|
| 526 | : _graph(&graph) { |
---|
| 527 | _graph->firstIn(*this, node); |
---|
[57] | 528 | } |
---|
| 529 | |
---|
[209] | 530 | InArcIt(const Graph& graph, const Arc& arc) : |
---|
| 531 | Arc(arc), _graph(&graph) {} |
---|
[57] | 532 | |
---|
[209] | 533 | InArcIt& operator++() { |
---|
| 534 | _graph->nextIn(*this); |
---|
| 535 | return *this; |
---|
[57] | 536 | } |
---|
| 537 | |
---|
| 538 | }; |
---|
| 539 | |
---|
[1336] | 540 | LemonRangeWrapper2<InArcIt, Graph, Node> inArcs(const Node& u) const { |
---|
| 541 | return LemonRangeWrapper2<InArcIt, Graph, Node>(*this, u); |
---|
| 542 | } |
---|
| 543 | |
---|
[57] | 544 | |
---|
[209] | 545 | class EdgeIt : public Parent::Edge { |
---|
[78] | 546 | const Graph* _graph; |
---|
[57] | 547 | public: |
---|
| 548 | |
---|
| 549 | EdgeIt() { } |
---|
| 550 | |
---|
| 551 | EdgeIt(Invalid i) : Edge(i) { } |
---|
| 552 | |
---|
[78] | 553 | explicit EdgeIt(const Graph& graph) : _graph(&graph) { |
---|
[209] | 554 | _graph->first(static_cast<Edge&>(*this)); |
---|
[57] | 555 | } |
---|
| 556 | |
---|
[209] | 557 | EdgeIt(const Graph& graph, const Edge& edge) : |
---|
| 558 | Edge(edge), _graph(&graph) { } |
---|
[57] | 559 | |
---|
[209] | 560 | EdgeIt& operator++() { |
---|
| 561 | _graph->next(*this); |
---|
| 562 | return *this; |
---|
[57] | 563 | } |
---|
| 564 | |
---|
| 565 | }; |
---|
| 566 | |
---|
[1336] | 567 | LemonRangeWrapper1<EdgeIt, Graph> edges() const { |
---|
| 568 | return LemonRangeWrapper1<EdgeIt, Graph>(*this); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | |
---|
[78] | 572 | class IncEdgeIt : public Parent::Edge { |
---|
[57] | 573 | friend class GraphExtender; |
---|
[78] | 574 | const Graph* _graph; |
---|
| 575 | bool _direction; |
---|
[57] | 576 | public: |
---|
| 577 | |
---|
[78] | 578 | IncEdgeIt() { } |
---|
[57] | 579 | |
---|
[78] | 580 | IncEdgeIt(Invalid i) : Edge(i), _direction(false) { } |
---|
[57] | 581 | |
---|
[78] | 582 | IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) { |
---|
[209] | 583 | _graph->firstInc(*this, _direction, node); |
---|
[57] | 584 | } |
---|
| 585 | |
---|
[78] | 586 | IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node) |
---|
[209] | 587 | : _graph(&graph), Edge(edge) { |
---|
| 588 | _direction = (_graph->source(edge) == node); |
---|
[57] | 589 | } |
---|
| 590 | |
---|
[78] | 591 | IncEdgeIt& operator++() { |
---|
[209] | 592 | _graph->nextInc(*this, _direction); |
---|
| 593 | return *this; |
---|
[57] | 594 | } |
---|
| 595 | }; |
---|
| 596 | |
---|
[1336] | 597 | LemonRangeWrapper2<IncEdgeIt, Graph, Node> incEdges(const Node& u) const { |
---|
| 598 | return LemonRangeWrapper2<IncEdgeIt, Graph, Node>(*this, u); |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | |
---|
[314] | 602 | // \brief Base node of the iterator |
---|
| 603 | // |
---|
| 604 | // Returns the base node (ie. the source in this case) of the iterator |
---|
[78] | 605 | Node baseNode(const OutArcIt &arc) const { |
---|
| 606 | return Parent::source(static_cast<const Arc&>(arc)); |
---|
[57] | 607 | } |
---|
[314] | 608 | // \brief Running node of the iterator |
---|
| 609 | // |
---|
| 610 | // Returns the running node (ie. the target in this case) of the |
---|
| 611 | // iterator |
---|
[78] | 612 | Node runningNode(const OutArcIt &arc) const { |
---|
| 613 | return Parent::target(static_cast<const Arc&>(arc)); |
---|
[57] | 614 | } |
---|
| 615 | |
---|
[314] | 616 | // \brief Base node of the iterator |
---|
| 617 | // |
---|
| 618 | // Returns the base node (ie. the target in this case) of the iterator |
---|
[78] | 619 | Node baseNode(const InArcIt &arc) const { |
---|
| 620 | return Parent::target(static_cast<const Arc&>(arc)); |
---|
[57] | 621 | } |
---|
[314] | 622 | // \brief Running node of the iterator |
---|
| 623 | // |
---|
| 624 | // Returns the running node (ie. the source in this case) of the |
---|
| 625 | // iterator |
---|
[78] | 626 | Node runningNode(const InArcIt &arc) const { |
---|
| 627 | return Parent::source(static_cast<const Arc&>(arc)); |
---|
[57] | 628 | } |
---|
| 629 | |
---|
[314] | 630 | // Base node of the iterator |
---|
| 631 | // |
---|
| 632 | // Returns the base node of the iterator |
---|
[78] | 633 | Node baseNode(const IncEdgeIt &edge) const { |
---|
[1157] | 634 | return edge._direction ? this->u(edge) : this->v(edge); |
---|
[57] | 635 | } |
---|
[314] | 636 | // Running node of the iterator |
---|
| 637 | // |
---|
| 638 | // Returns the running node of the iterator |
---|
[78] | 639 | Node runningNode(const IncEdgeIt &edge) const { |
---|
[1157] | 640 | return edge._direction ? this->v(edge) : this->u(edge); |
---|
[57] | 641 | } |
---|
| 642 | |
---|
| 643 | // Mappable extension |
---|
| 644 | |
---|
| 645 | template <typename _Value> |
---|
[209] | 646 | class NodeMap |
---|
[78] | 647 | : public MapExtender<DefaultMap<Graph, Node, _Value> > { |
---|
| 648 | typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
---|
[57] | 649 | |
---|
[664] | 650 | public: |
---|
[732] | 651 | explicit NodeMap(const Graph& graph) |
---|
[209] | 652 | : Parent(graph) {} |
---|
| 653 | NodeMap(const Graph& graph, const _Value& value) |
---|
| 654 | : Parent(graph, value) {} |
---|
[57] | 655 | |
---|
[263] | 656 | private: |
---|
[57] | 657 | NodeMap& operator=(const NodeMap& cmap) { |
---|
[209] | 658 | return operator=<NodeMap>(cmap); |
---|
[57] | 659 | } |
---|
| 660 | |
---|
| 661 | template <typename CMap> |
---|
| 662 | NodeMap& operator=(const CMap& cmap) { |
---|
| 663 | Parent::operator=(cmap); |
---|
[209] | 664 | return *this; |
---|
[57] | 665 | } |
---|
| 666 | |
---|
| 667 | }; |
---|
| 668 | |
---|
| 669 | template <typename _Value> |
---|
[209] | 670 | class ArcMap |
---|
[78] | 671 | : public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
---|
| 672 | typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
---|
[57] | 673 | |
---|
[664] | 674 | public: |
---|
[732] | 675 | explicit ArcMap(const Graph& graph) |
---|
[209] | 676 | : Parent(graph) {} |
---|
| 677 | ArcMap(const Graph& graph, const _Value& value) |
---|
| 678 | : Parent(graph, value) {} |
---|
[57] | 679 | |
---|
[263] | 680 | private: |
---|
[57] | 681 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[209] | 682 | return operator=<ArcMap>(cmap); |
---|
[57] | 683 | } |
---|
| 684 | |
---|
| 685 | template <typename CMap> |
---|
| 686 | ArcMap& operator=(const CMap& cmap) { |
---|
| 687 | Parent::operator=(cmap); |
---|
[209] | 688 | return *this; |
---|
[57] | 689 | } |
---|
| 690 | }; |
---|
| 691 | |
---|
| 692 | |
---|
| 693 | template <typename _Value> |
---|
[209] | 694 | class EdgeMap |
---|
[78] | 695 | : public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
---|
| 696 | typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
---|
[57] | 697 | |
---|
[664] | 698 | public: |
---|
[732] | 699 | explicit EdgeMap(const Graph& graph) |
---|
[209] | 700 | : Parent(graph) {} |
---|
[57] | 701 | |
---|
[209] | 702 | EdgeMap(const Graph& graph, const _Value& value) |
---|
| 703 | : Parent(graph, value) {} |
---|
[57] | 704 | |
---|
[263] | 705 | private: |
---|
[57] | 706 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
[209] | 707 | return operator=<EdgeMap>(cmap); |
---|
[57] | 708 | } |
---|
| 709 | |
---|
| 710 | template <typename CMap> |
---|
| 711 | EdgeMap& operator=(const CMap& cmap) { |
---|
| 712 | Parent::operator=(cmap); |
---|
[209] | 713 | return *this; |
---|
[57] | 714 | } |
---|
| 715 | |
---|
| 716 | }; |
---|
| 717 | |
---|
| 718 | // Alteration extension |
---|
| 719 | |
---|
| 720 | Node addNode() { |
---|
| 721 | Node node = Parent::addNode(); |
---|
| 722 | notifier(Node()).add(node); |
---|
| 723 | return node; |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | Edge addEdge(const Node& from, const Node& to) { |
---|
| 727 | Edge edge = Parent::addEdge(from, to); |
---|
| 728 | notifier(Edge()).add(edge); |
---|
| 729 | std::vector<Arc> ev; |
---|
| 730 | ev.push_back(Parent::direct(edge, true)); |
---|
[209] | 731 | ev.push_back(Parent::direct(edge, false)); |
---|
[57] | 732 | notifier(Arc()).add(ev); |
---|
| 733 | return edge; |
---|
| 734 | } |
---|
[209] | 735 | |
---|
[57] | 736 | void clear() { |
---|
| 737 | notifier(Arc()).clear(); |
---|
| 738 | notifier(Edge()).clear(); |
---|
| 739 | notifier(Node()).clear(); |
---|
| 740 | Parent::clear(); |
---|
| 741 | } |
---|
| 742 | |
---|
[78] | 743 | template <typename Graph, typename NodeRefMap, typename EdgeRefMap> |
---|
[209] | 744 | void build(const Graph& graph, NodeRefMap& nodeRef, |
---|
[57] | 745 | EdgeRefMap& edgeRef) { |
---|
[78] | 746 | Parent::build(graph, nodeRef, edgeRef); |
---|
[57] | 747 | notifier(Node()).build(); |
---|
| 748 | notifier(Edge()).build(); |
---|
| 749 | notifier(Arc()).build(); |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | void erase(const Node& node) { |
---|
| 753 | Arc arc; |
---|
| 754 | Parent::firstOut(arc, node); |
---|
| 755 | while (arc != INVALID ) { |
---|
[209] | 756 | erase(arc); |
---|
| 757 | Parent::firstOut(arc, node); |
---|
| 758 | } |
---|
[57] | 759 | |
---|
| 760 | Parent::firstIn(arc, node); |
---|
| 761 | while (arc != INVALID ) { |
---|
[209] | 762 | erase(arc); |
---|
| 763 | Parent::firstIn(arc, node); |
---|
[57] | 764 | } |
---|
| 765 | |
---|
| 766 | notifier(Node()).erase(node); |
---|
| 767 | Parent::erase(node); |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | void erase(const Edge& edge) { |
---|
[78] | 771 | std::vector<Arc> av; |
---|
| 772 | av.push_back(Parent::direct(edge, true)); |
---|
[209] | 773 | av.push_back(Parent::direct(edge, false)); |
---|
[78] | 774 | notifier(Arc()).erase(av); |
---|
[57] | 775 | notifier(Edge()).erase(edge); |
---|
| 776 | Parent::erase(edge); |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | GraphExtender() { |
---|
[209] | 780 | node_notifier.setContainer(*this); |
---|
[57] | 781 | arc_notifier.setContainer(*this); |
---|
| 782 | edge_notifier.setContainer(*this); |
---|
[209] | 783 | } |
---|
[57] | 784 | |
---|
| 785 | ~GraphExtender() { |
---|
| 786 | edge_notifier.clear(); |
---|
| 787 | arc_notifier.clear(); |
---|
[209] | 788 | node_notifier.clear(); |
---|
| 789 | } |
---|
[57] | 790 | |
---|
| 791 | }; |
---|
| 792 | |
---|
[1187] | 793 | // \ingroup _graphbits |
---|
| 794 | // |
---|
| 795 | // \brief Extender for the BpGraphs |
---|
| 796 | template <typename Base> |
---|
| 797 | class BpGraphExtender : public Base { |
---|
| 798 | typedef Base Parent; |
---|
| 799 | |
---|
| 800 | public: |
---|
| 801 | |
---|
| 802 | typedef BpGraphExtender BpGraph; |
---|
| 803 | |
---|
| 804 | typedef True UndirectedTag; |
---|
| 805 | |
---|
| 806 | typedef typename Parent::Node Node; |
---|
[1193] | 807 | typedef typename Parent::RedNode RedNode; |
---|
| 808 | typedef typename Parent::BlueNode BlueNode; |
---|
[1187] | 809 | typedef typename Parent::Arc Arc; |
---|
| 810 | typedef typename Parent::Edge Edge; |
---|
| 811 | |
---|
| 812 | // BpGraph extension |
---|
| 813 | |
---|
| 814 | using Parent::first; |
---|
| 815 | using Parent::next; |
---|
| 816 | using Parent::id; |
---|
| 817 | |
---|
| 818 | int maxId(Node) const { |
---|
| 819 | return Parent::maxNodeId(); |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | int maxId(RedNode) const { |
---|
| 823 | return Parent::maxRedId(); |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | int maxId(BlueNode) const { |
---|
| 827 | return Parent::maxBlueId(); |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | int maxId(Arc) const { |
---|
| 831 | return Parent::maxArcId(); |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | int maxId(Edge) const { |
---|
| 835 | return Parent::maxEdgeId(); |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | static Node fromId(int id, Node) { |
---|
| 839 | return Parent::nodeFromId(id); |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | static Arc fromId(int id, Arc) { |
---|
| 843 | return Parent::arcFromId(id); |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | static Edge fromId(int id, Edge) { |
---|
| 847 | return Parent::edgeFromId(id); |
---|
| 848 | } |
---|
| 849 | |
---|
[1188] | 850 | Node u(Edge e) const { return this->redNode(e); } |
---|
| 851 | Node v(Edge e) const { return this->blueNode(e); } |
---|
| 852 | |
---|
[1187] | 853 | Node oppositeNode(const Node &n, const Edge &e) const { |
---|
[1188] | 854 | if( n == u(e)) |
---|
| 855 | return v(e); |
---|
| 856 | else if( n == v(e)) |
---|
| 857 | return u(e); |
---|
[1187] | 858 | else |
---|
| 859 | return INVALID; |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | Arc oppositeArc(const Arc &arc) const { |
---|
| 863 | return Parent::direct(arc, !Parent::direction(arc)); |
---|
| 864 | } |
---|
| 865 | |
---|
| 866 | using Parent::direct; |
---|
| 867 | Arc direct(const Edge &edge, const Node &node) const { |
---|
[1188] | 868 | return Parent::direct(edge, Parent::redNode(edge) == node); |
---|
[1187] | 869 | } |
---|
| 870 | |
---|
[1193] | 871 | RedNode asRedNode(const Node& node) const { |
---|
| 872 | if (node == INVALID || Parent::blue(node)) { |
---|
| 873 | return INVALID; |
---|
| 874 | } else { |
---|
| 875 | return Parent::asRedNodeUnsafe(node); |
---|
| 876 | } |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | BlueNode asBlueNode(const Node& node) const { |
---|
| 880 | if (node == INVALID || Parent::red(node)) { |
---|
| 881 | return INVALID; |
---|
| 882 | } else { |
---|
| 883 | return Parent::asBlueNodeUnsafe(node); |
---|
| 884 | } |
---|
| 885 | } |
---|
| 886 | |
---|
[1187] | 887 | // Alterable extension |
---|
| 888 | |
---|
| 889 | typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier; |
---|
[1270] | 890 | typedef AlterationNotifier<BpGraphExtender, RedNode> RedNodeNotifier; |
---|
[1187] | 891 | typedef AlterationNotifier<BpGraphExtender, BlueNode> BlueNodeNotifier; |
---|
| 892 | typedef AlterationNotifier<BpGraphExtender, Arc> ArcNotifier; |
---|
| 893 | typedef AlterationNotifier<BpGraphExtender, Edge> EdgeNotifier; |
---|
| 894 | |
---|
| 895 | |
---|
| 896 | protected: |
---|
| 897 | |
---|
| 898 | mutable NodeNotifier node_notifier; |
---|
| 899 | mutable RedNodeNotifier red_node_notifier; |
---|
| 900 | mutable BlueNodeNotifier blue_node_notifier; |
---|
| 901 | mutable ArcNotifier arc_notifier; |
---|
| 902 | mutable EdgeNotifier edge_notifier; |
---|
| 903 | |
---|
| 904 | public: |
---|
| 905 | |
---|
| 906 | NodeNotifier& notifier(Node) const { |
---|
| 907 | return node_notifier; |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | RedNodeNotifier& notifier(RedNode) const { |
---|
| 911 | return red_node_notifier; |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | BlueNodeNotifier& notifier(BlueNode) const { |
---|
| 915 | return blue_node_notifier; |
---|
| 916 | } |
---|
| 917 | |
---|
| 918 | ArcNotifier& notifier(Arc) const { |
---|
| 919 | return arc_notifier; |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | EdgeNotifier& notifier(Edge) const { |
---|
| 923 | return edge_notifier; |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | |
---|
| 927 | |
---|
| 928 | class NodeIt : public Node { |
---|
| 929 | const BpGraph* _graph; |
---|
| 930 | public: |
---|
| 931 | |
---|
| 932 | NodeIt() {} |
---|
| 933 | |
---|
| 934 | NodeIt(Invalid i) : Node(i) { } |
---|
| 935 | |
---|
| 936 | explicit NodeIt(const BpGraph& graph) : _graph(&graph) { |
---|
| 937 | _graph->first(static_cast<Node&>(*this)); |
---|
| 938 | } |
---|
| 939 | |
---|
| 940 | NodeIt(const BpGraph& graph, const Node& node) |
---|
| 941 | : Node(node), _graph(&graph) {} |
---|
| 942 | |
---|
| 943 | NodeIt& operator++() { |
---|
| 944 | _graph->next(*this); |
---|
| 945 | return *this; |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | }; |
---|
| 949 | |
---|
[1336] | 950 | LemonRangeWrapper1<NodeIt, BpGraph> nodes() const { |
---|
| 951 | return LemonRangeWrapper1<NodeIt, BpGraph>(*this); |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | |
---|
[1194] | 955 | class RedNodeIt : public RedNode { |
---|
[1187] | 956 | const BpGraph* _graph; |
---|
| 957 | public: |
---|
| 958 | |
---|
[1194] | 959 | RedNodeIt() {} |
---|
[1187] | 960 | |
---|
[1194] | 961 | RedNodeIt(Invalid i) : RedNode(i) { } |
---|
[1187] | 962 | |
---|
[1194] | 963 | explicit RedNodeIt(const BpGraph& graph) : _graph(&graph) { |
---|
[1193] | 964 | _graph->first(static_cast<RedNode&>(*this)); |
---|
[1187] | 965 | } |
---|
| 966 | |
---|
[1194] | 967 | RedNodeIt(const BpGraph& graph, const RedNode& node) |
---|
[1193] | 968 | : RedNode(node), _graph(&graph) {} |
---|
[1187] | 969 | |
---|
[1194] | 970 | RedNodeIt& operator++() { |
---|
[1193] | 971 | _graph->next(static_cast<RedNode&>(*this)); |
---|
[1187] | 972 | return *this; |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | }; |
---|
| 976 | |
---|
[1336] | 977 | LemonRangeWrapper1<RedNodeIt, BpGraph> redNodes() const { |
---|
| 978 | return LemonRangeWrapper1<RedNodeIt, BpGraph>(*this); |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | |
---|
[1194] | 982 | class BlueNodeIt : public BlueNode { |
---|
[1187] | 983 | const BpGraph* _graph; |
---|
| 984 | public: |
---|
| 985 | |
---|
[1194] | 986 | BlueNodeIt() {} |
---|
[1187] | 987 | |
---|
[1194] | 988 | BlueNodeIt(Invalid i) : BlueNode(i) { } |
---|
[1187] | 989 | |
---|
[1194] | 990 | explicit BlueNodeIt(const BpGraph& graph) : _graph(&graph) { |
---|
[1193] | 991 | _graph->first(static_cast<BlueNode&>(*this)); |
---|
[1187] | 992 | } |
---|
| 993 | |
---|
[1194] | 994 | BlueNodeIt(const BpGraph& graph, const BlueNode& node) |
---|
[1193] | 995 | : BlueNode(node), _graph(&graph) {} |
---|
[1187] | 996 | |
---|
[1194] | 997 | BlueNodeIt& operator++() { |
---|
[1193] | 998 | _graph->next(static_cast<BlueNode&>(*this)); |
---|
[1187] | 999 | return *this; |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | }; |
---|
| 1003 | |
---|
[1336] | 1004 | LemonRangeWrapper1<BlueNodeIt, BpGraph> blueNodes() const { |
---|
| 1005 | return LemonRangeWrapper1<BlueNodeIt, BpGraph>(*this); |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | |
---|
[1187] | 1009 | |
---|
| 1010 | class ArcIt : public Arc { |
---|
| 1011 | const BpGraph* _graph; |
---|
| 1012 | public: |
---|
| 1013 | |
---|
| 1014 | ArcIt() { } |
---|
| 1015 | |
---|
| 1016 | ArcIt(Invalid i) : Arc(i) { } |
---|
| 1017 | |
---|
| 1018 | explicit ArcIt(const BpGraph& graph) : _graph(&graph) { |
---|
| 1019 | _graph->first(static_cast<Arc&>(*this)); |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | ArcIt(const BpGraph& graph, const Arc& arc) : |
---|
| 1023 | Arc(arc), _graph(&graph) { } |
---|
| 1024 | |
---|
| 1025 | ArcIt& operator++() { |
---|
| 1026 | _graph->next(*this); |
---|
| 1027 | return *this; |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | }; |
---|
| 1031 | |
---|
[1336] | 1032 | LemonRangeWrapper1<ArcIt, BpGraph> arcs() const { |
---|
| 1033 | return LemonRangeWrapper1<ArcIt, BpGraph>(*this); |
---|
| 1034 | } |
---|
| 1035 | |
---|
[1187] | 1036 | |
---|
| 1037 | class OutArcIt : public Arc { |
---|
| 1038 | const BpGraph* _graph; |
---|
| 1039 | public: |
---|
| 1040 | |
---|
| 1041 | OutArcIt() { } |
---|
| 1042 | |
---|
| 1043 | OutArcIt(Invalid i) : Arc(i) { } |
---|
| 1044 | |
---|
| 1045 | OutArcIt(const BpGraph& graph, const Node& node) |
---|
| 1046 | : _graph(&graph) { |
---|
| 1047 | _graph->firstOut(*this, node); |
---|
| 1048 | } |
---|
| 1049 | |
---|
| 1050 | OutArcIt(const BpGraph& graph, const Arc& arc) |
---|
| 1051 | : Arc(arc), _graph(&graph) {} |
---|
| 1052 | |
---|
| 1053 | OutArcIt& operator++() { |
---|
| 1054 | _graph->nextOut(*this); |
---|
| 1055 | return *this; |
---|
| 1056 | } |
---|
| 1057 | |
---|
| 1058 | }; |
---|
| 1059 | |
---|
[1336] | 1060 | LemonRangeWrapper2<OutArcIt, BpGraph, Node> outArcs(const Node& u) const { |
---|
| 1061 | return LemonRangeWrapper2<OutArcIt, BpGraph, Node>(*this, u); |
---|
| 1062 | } |
---|
| 1063 | |
---|
[1187] | 1064 | |
---|
| 1065 | class InArcIt : public Arc { |
---|
| 1066 | const BpGraph* _graph; |
---|
| 1067 | public: |
---|
| 1068 | |
---|
| 1069 | InArcIt() { } |
---|
| 1070 | |
---|
| 1071 | InArcIt(Invalid i) : Arc(i) { } |
---|
| 1072 | |
---|
| 1073 | InArcIt(const BpGraph& graph, const Node& node) |
---|
| 1074 | : _graph(&graph) { |
---|
| 1075 | _graph->firstIn(*this, node); |
---|
| 1076 | } |
---|
| 1077 | |
---|
| 1078 | InArcIt(const BpGraph& graph, const Arc& arc) : |
---|
| 1079 | Arc(arc), _graph(&graph) {} |
---|
| 1080 | |
---|
| 1081 | InArcIt& operator++() { |
---|
| 1082 | _graph->nextIn(*this); |
---|
| 1083 | return *this; |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | }; |
---|
| 1087 | |
---|
[1336] | 1088 | LemonRangeWrapper2<InArcIt, BpGraph, Node> inArcs(const Node& u) const { |
---|
| 1089 | return LemonRangeWrapper2<InArcIt, BpGraph, Node>(*this, u); |
---|
| 1090 | } |
---|
| 1091 | |
---|
[1187] | 1092 | |
---|
| 1093 | class EdgeIt : public Parent::Edge { |
---|
| 1094 | const BpGraph* _graph; |
---|
| 1095 | public: |
---|
| 1096 | |
---|
| 1097 | EdgeIt() { } |
---|
| 1098 | |
---|
| 1099 | EdgeIt(Invalid i) : Edge(i) { } |
---|
| 1100 | |
---|
| 1101 | explicit EdgeIt(const BpGraph& graph) : _graph(&graph) { |
---|
| 1102 | _graph->first(static_cast<Edge&>(*this)); |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | EdgeIt(const BpGraph& graph, const Edge& edge) : |
---|
| 1106 | Edge(edge), _graph(&graph) { } |
---|
| 1107 | |
---|
| 1108 | EdgeIt& operator++() { |
---|
| 1109 | _graph->next(*this); |
---|
| 1110 | return *this; |
---|
| 1111 | } |
---|
| 1112 | |
---|
| 1113 | }; |
---|
| 1114 | |
---|
[1336] | 1115 | LemonRangeWrapper1<EdgeIt, BpGraph> edges() const { |
---|
| 1116 | return LemonRangeWrapper1<EdgeIt, BpGraph>(*this); |
---|
| 1117 | } |
---|
| 1118 | |
---|
| 1119 | |
---|
[1187] | 1120 | class IncEdgeIt : public Parent::Edge { |
---|
| 1121 | friend class BpGraphExtender; |
---|
| 1122 | const BpGraph* _graph; |
---|
| 1123 | bool _direction; |
---|
| 1124 | public: |
---|
| 1125 | |
---|
| 1126 | IncEdgeIt() { } |
---|
| 1127 | |
---|
| 1128 | IncEdgeIt(Invalid i) : Edge(i), _direction(false) { } |
---|
| 1129 | |
---|
| 1130 | IncEdgeIt(const BpGraph& graph, const Node &node) : _graph(&graph) { |
---|
| 1131 | _graph->firstInc(*this, _direction, node); |
---|
| 1132 | } |
---|
| 1133 | |
---|
| 1134 | IncEdgeIt(const BpGraph& graph, const Edge &edge, const Node &node) |
---|
| 1135 | : _graph(&graph), Edge(edge) { |
---|
| 1136 | _direction = (_graph->source(edge) == node); |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | IncEdgeIt& operator++() { |
---|
| 1140 | _graph->nextInc(*this, _direction); |
---|
| 1141 | return *this; |
---|
| 1142 | } |
---|
| 1143 | }; |
---|
| 1144 | |
---|
[1336] | 1145 | LemonRangeWrapper2<IncEdgeIt, BpGraph, Node> incEdges(const Node& u) const { |
---|
| 1146 | return LemonRangeWrapper2<IncEdgeIt, BpGraph, Node>(*this, u); |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | |
---|
[1187] | 1150 | // \brief Base node of the iterator |
---|
| 1151 | // |
---|
| 1152 | // Returns the base node (ie. the source in this case) of the iterator |
---|
| 1153 | Node baseNode(const OutArcIt &arc) const { |
---|
| 1154 | return Parent::source(static_cast<const Arc&>(arc)); |
---|
| 1155 | } |
---|
| 1156 | // \brief Running node of the iterator |
---|
| 1157 | // |
---|
| 1158 | // Returns the running node (ie. the target in this case) of the |
---|
| 1159 | // iterator |
---|
| 1160 | Node runningNode(const OutArcIt &arc) const { |
---|
| 1161 | return Parent::target(static_cast<const Arc&>(arc)); |
---|
| 1162 | } |
---|
| 1163 | |
---|
| 1164 | // \brief Base node of the iterator |
---|
| 1165 | // |
---|
| 1166 | // Returns the base node (ie. the target in this case) of the iterator |
---|
| 1167 | Node baseNode(const InArcIt &arc) const { |
---|
| 1168 | return Parent::target(static_cast<const Arc&>(arc)); |
---|
| 1169 | } |
---|
| 1170 | // \brief Running node of the iterator |
---|
| 1171 | // |
---|
| 1172 | // Returns the running node (ie. the source in this case) of the |
---|
| 1173 | // iterator |
---|
| 1174 | Node runningNode(const InArcIt &arc) const { |
---|
| 1175 | return Parent::source(static_cast<const Arc&>(arc)); |
---|
| 1176 | } |
---|
| 1177 | |
---|
| 1178 | // Base node of the iterator |
---|
| 1179 | // |
---|
| 1180 | // Returns the base node of the iterator |
---|
| 1181 | Node baseNode(const IncEdgeIt &edge) const { |
---|
| 1182 | return edge._direction ? this->u(edge) : this->v(edge); |
---|
| 1183 | } |
---|
| 1184 | // Running node of the iterator |
---|
| 1185 | // |
---|
| 1186 | // Returns the running node of the iterator |
---|
| 1187 | Node runningNode(const IncEdgeIt &edge) const { |
---|
| 1188 | return edge._direction ? this->v(edge) : this->u(edge); |
---|
| 1189 | } |
---|
| 1190 | |
---|
| 1191 | // Mappable extension |
---|
| 1192 | |
---|
| 1193 | template <typename _Value> |
---|
| 1194 | class NodeMap |
---|
| 1195 | : public MapExtender<DefaultMap<BpGraph, Node, _Value> > { |
---|
| 1196 | typedef MapExtender<DefaultMap<BpGraph, Node, _Value> > Parent; |
---|
| 1197 | |
---|
| 1198 | public: |
---|
| 1199 | explicit NodeMap(const BpGraph& bpgraph) |
---|
| 1200 | : Parent(bpgraph) {} |
---|
| 1201 | NodeMap(const BpGraph& bpgraph, const _Value& value) |
---|
| 1202 | : Parent(bpgraph, value) {} |
---|
| 1203 | |
---|
| 1204 | private: |
---|
| 1205 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 1206 | return operator=<NodeMap>(cmap); |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | template <typename CMap> |
---|
| 1210 | NodeMap& operator=(const CMap& cmap) { |
---|
| 1211 | Parent::operator=(cmap); |
---|
| 1212 | return *this; |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | }; |
---|
| 1216 | |
---|
| 1217 | template <typename _Value> |
---|
[1194] | 1218 | class RedNodeMap |
---|
[1187] | 1219 | : public MapExtender<DefaultMap<BpGraph, RedNode, _Value> > { |
---|
| 1220 | typedef MapExtender<DefaultMap<BpGraph, RedNode, _Value> > Parent; |
---|
| 1221 | |
---|
| 1222 | public: |
---|
[1194] | 1223 | explicit RedNodeMap(const BpGraph& bpgraph) |
---|
[1187] | 1224 | : Parent(bpgraph) {} |
---|
[1194] | 1225 | RedNodeMap(const BpGraph& bpgraph, const _Value& value) |
---|
[1187] | 1226 | : Parent(bpgraph, value) {} |
---|
| 1227 | |
---|
| 1228 | private: |
---|
[1194] | 1229 | RedNodeMap& operator=(const RedNodeMap& cmap) { |
---|
| 1230 | return operator=<RedNodeMap>(cmap); |
---|
[1187] | 1231 | } |
---|
| 1232 | |
---|
| 1233 | template <typename CMap> |
---|
[1194] | 1234 | RedNodeMap& operator=(const CMap& cmap) { |
---|
[1187] | 1235 | Parent::operator=(cmap); |
---|
| 1236 | return *this; |
---|
| 1237 | } |
---|
| 1238 | |
---|
| 1239 | }; |
---|
| 1240 | |
---|
| 1241 | template <typename _Value> |
---|
[1194] | 1242 | class BlueNodeMap |
---|
[1187] | 1243 | : public MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > { |
---|
| 1244 | typedef MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > Parent; |
---|
| 1245 | |
---|
| 1246 | public: |
---|
[1194] | 1247 | explicit BlueNodeMap(const BpGraph& bpgraph) |
---|
[1187] | 1248 | : Parent(bpgraph) {} |
---|
[1194] | 1249 | BlueNodeMap(const BpGraph& bpgraph, const _Value& value) |
---|
[1187] | 1250 | : Parent(bpgraph, value) {} |
---|
| 1251 | |
---|
| 1252 | private: |
---|
[1194] | 1253 | BlueNodeMap& operator=(const BlueNodeMap& cmap) { |
---|
| 1254 | return operator=<BlueNodeMap>(cmap); |
---|
[1187] | 1255 | } |
---|
| 1256 | |
---|
| 1257 | template <typename CMap> |
---|
[1194] | 1258 | BlueNodeMap& operator=(const CMap& cmap) { |
---|
[1187] | 1259 | Parent::operator=(cmap); |
---|
| 1260 | return *this; |
---|
| 1261 | } |
---|
| 1262 | |
---|
| 1263 | }; |
---|
| 1264 | |
---|
| 1265 | template <typename _Value> |
---|
| 1266 | class ArcMap |
---|
| 1267 | : public MapExtender<DefaultMap<BpGraph, Arc, _Value> > { |
---|
| 1268 | typedef MapExtender<DefaultMap<BpGraph, Arc, _Value> > Parent; |
---|
| 1269 | |
---|
| 1270 | public: |
---|
| 1271 | explicit ArcMap(const BpGraph& graph) |
---|
| 1272 | : Parent(graph) {} |
---|
| 1273 | ArcMap(const BpGraph& graph, const _Value& value) |
---|
| 1274 | : Parent(graph, value) {} |
---|
| 1275 | |
---|
| 1276 | private: |
---|
| 1277 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 1278 | return operator=<ArcMap>(cmap); |
---|
| 1279 | } |
---|
| 1280 | |
---|
| 1281 | template <typename CMap> |
---|
| 1282 | ArcMap& operator=(const CMap& cmap) { |
---|
| 1283 | Parent::operator=(cmap); |
---|
| 1284 | return *this; |
---|
| 1285 | } |
---|
| 1286 | }; |
---|
| 1287 | |
---|
| 1288 | |
---|
| 1289 | template <typename _Value> |
---|
| 1290 | class EdgeMap |
---|
| 1291 | : public MapExtender<DefaultMap<BpGraph, Edge, _Value> > { |
---|
| 1292 | typedef MapExtender<DefaultMap<BpGraph, Edge, _Value> > Parent; |
---|
| 1293 | |
---|
| 1294 | public: |
---|
| 1295 | explicit EdgeMap(const BpGraph& graph) |
---|
| 1296 | : Parent(graph) {} |
---|
| 1297 | |
---|
| 1298 | EdgeMap(const BpGraph& graph, const _Value& value) |
---|
| 1299 | : Parent(graph, value) {} |
---|
| 1300 | |
---|
| 1301 | private: |
---|
| 1302 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 1303 | return operator=<EdgeMap>(cmap); |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | template <typename CMap> |
---|
| 1307 | EdgeMap& operator=(const CMap& cmap) { |
---|
| 1308 | Parent::operator=(cmap); |
---|
| 1309 | return *this; |
---|
| 1310 | } |
---|
| 1311 | |
---|
| 1312 | }; |
---|
| 1313 | |
---|
| 1314 | // Alteration extension |
---|
| 1315 | |
---|
[1193] | 1316 | RedNode addRedNode() { |
---|
| 1317 | RedNode node = Parent::addRedNode(); |
---|
[1187] | 1318 | notifier(RedNode()).add(node); |
---|
| 1319 | notifier(Node()).add(node); |
---|
| 1320 | return node; |
---|
| 1321 | } |
---|
| 1322 | |
---|
[1193] | 1323 | BlueNode addBlueNode() { |
---|
| 1324 | BlueNode node = Parent::addBlueNode(); |
---|
[1187] | 1325 | notifier(BlueNode()).add(node); |
---|
| 1326 | notifier(Node()).add(node); |
---|
| 1327 | return node; |
---|
| 1328 | } |
---|
| 1329 | |
---|
[1193] | 1330 | Edge addEdge(const RedNode& from, const BlueNode& to) { |
---|
[1187] | 1331 | Edge edge = Parent::addEdge(from, to); |
---|
| 1332 | notifier(Edge()).add(edge); |
---|
| 1333 | std::vector<Arc> av; |
---|
| 1334 | av.push_back(Parent::direct(edge, true)); |
---|
| 1335 | av.push_back(Parent::direct(edge, false)); |
---|
| 1336 | notifier(Arc()).add(av); |
---|
| 1337 | return edge; |
---|
| 1338 | } |
---|
| 1339 | |
---|
| 1340 | void clear() { |
---|
| 1341 | notifier(Arc()).clear(); |
---|
| 1342 | notifier(Edge()).clear(); |
---|
| 1343 | notifier(Node()).clear(); |
---|
| 1344 | notifier(BlueNode()).clear(); |
---|
| 1345 | notifier(RedNode()).clear(); |
---|
| 1346 | Parent::clear(); |
---|
| 1347 | } |
---|
| 1348 | |
---|
| 1349 | template <typename BpGraph, typename NodeRefMap, typename EdgeRefMap> |
---|
| 1350 | void build(const BpGraph& graph, NodeRefMap& nodeRef, |
---|
| 1351 | EdgeRefMap& edgeRef) { |
---|
| 1352 | Parent::build(graph, nodeRef, edgeRef); |
---|
| 1353 | notifier(RedNode()).build(); |
---|
| 1354 | notifier(BlueNode()).build(); |
---|
| 1355 | notifier(Node()).build(); |
---|
| 1356 | notifier(Edge()).build(); |
---|
| 1357 | notifier(Arc()).build(); |
---|
| 1358 | } |
---|
| 1359 | |
---|
| 1360 | void erase(const Node& node) { |
---|
| 1361 | Arc arc; |
---|
| 1362 | Parent::firstOut(arc, node); |
---|
| 1363 | while (arc != INVALID ) { |
---|
| 1364 | erase(arc); |
---|
| 1365 | Parent::firstOut(arc, node); |
---|
| 1366 | } |
---|
| 1367 | |
---|
| 1368 | Parent::firstIn(arc, node); |
---|
| 1369 | while (arc != INVALID ) { |
---|
| 1370 | erase(arc); |
---|
| 1371 | Parent::firstIn(arc, node); |
---|
| 1372 | } |
---|
| 1373 | |
---|
| 1374 | if (Parent::red(node)) { |
---|
[1193] | 1375 | notifier(RedNode()).erase(this->asRedNodeUnsafe(node)); |
---|
[1187] | 1376 | } else { |
---|
[1193] | 1377 | notifier(BlueNode()).erase(this->asBlueNodeUnsafe(node)); |
---|
[1187] | 1378 | } |
---|
| 1379 | |
---|
| 1380 | notifier(Node()).erase(node); |
---|
| 1381 | Parent::erase(node); |
---|
| 1382 | } |
---|
| 1383 | |
---|
| 1384 | void erase(const Edge& edge) { |
---|
| 1385 | std::vector<Arc> av; |
---|
| 1386 | av.push_back(Parent::direct(edge, true)); |
---|
| 1387 | av.push_back(Parent::direct(edge, false)); |
---|
| 1388 | notifier(Arc()).erase(av); |
---|
| 1389 | notifier(Edge()).erase(edge); |
---|
| 1390 | Parent::erase(edge); |
---|
| 1391 | } |
---|
| 1392 | |
---|
| 1393 | BpGraphExtender() { |
---|
| 1394 | red_node_notifier.setContainer(*this); |
---|
| 1395 | blue_node_notifier.setContainer(*this); |
---|
| 1396 | node_notifier.setContainer(*this); |
---|
| 1397 | arc_notifier.setContainer(*this); |
---|
| 1398 | edge_notifier.setContainer(*this); |
---|
| 1399 | } |
---|
| 1400 | |
---|
| 1401 | ~BpGraphExtender() { |
---|
| 1402 | edge_notifier.clear(); |
---|
| 1403 | arc_notifier.clear(); |
---|
| 1404 | node_notifier.clear(); |
---|
| 1405 | blue_node_notifier.clear(); |
---|
| 1406 | red_node_notifier.clear(); |
---|
| 1407 | } |
---|
| 1408 | |
---|
| 1409 | }; |
---|
| 1410 | |
---|
[57] | 1411 | } |
---|
| 1412 | |
---|
| 1413 | #endif |
---|