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