| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_EDGE_SET_H |
| 20 | 20 |
#define LEMON_EDGE_SET_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/edge_set_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \ingroup graphs |
| 26 | 26 |
/// \file |
| 27 | 27 |
/// \brief ArcSet and EdgeSet classes. |
| 28 | 28 |
/// |
| 29 | 29 |
/// Graphs which use another graph's node-set as own. |
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
template <typename GR> |
| 33 | 33 |
class ListArcSetBase {
|
| 34 | 34 |
public: |
| 35 | 35 |
|
| 36 | 36 |
typedef typename GR::Node Node; |
| 37 | 37 |
typedef typename GR::NodeIt NodeIt; |
| 38 | 38 |
|
| 39 | 39 |
protected: |
| 40 | 40 |
|
| 41 | 41 |
struct NodeT {
|
| 42 | 42 |
int first_out, first_in; |
| 43 | 43 |
NodeT() : first_out(-1), first_in(-1) {}
|
| 44 | 44 |
}; |
| 45 | 45 |
|
| 46 | 46 |
typedef typename ItemSetTraits<GR, Node>:: |
| 47 | 47 |
template Map<NodeT>::Type NodesImplBase; |
| 48 | 48 |
|
| 49 | 49 |
NodesImplBase* _nodes; |
| 50 | 50 |
|
| 51 | 51 |
struct ArcT {
|
| 52 | 52 |
Node source, target; |
| 53 | 53 |
int next_out, next_in; |
| 54 | 54 |
int prev_out, prev_in; |
| 55 | 55 |
ArcT() : prev_out(-1), prev_in(-1) {}
|
| 56 | 56 |
}; |
| 57 | 57 |
|
| 58 | 58 |
std::vector<ArcT> arcs; |
| 59 | 59 |
|
| 60 | 60 |
int first_arc; |
| 61 | 61 |
int first_free_arc; |
| 62 | 62 |
|
| 63 | 63 |
const GR* _graph; |
| 64 | 64 |
|
| 65 | 65 |
void initalize(const GR& graph, NodesImplBase& nodes) {
|
| 66 | 66 |
_graph = &graph; |
| 67 | 67 |
_nodes = &nodes; |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
|
| 72 | 72 |
class Arc {
|
| 73 | 73 |
friend class ListArcSetBase<GR>; |
| 74 | 74 |
protected: |
| 75 | 75 |
Arc(int _id) : id(_id) {}
|
| 76 | 76 |
int id; |
| 77 | 77 |
public: |
| 78 | 78 |
Arc() {}
|
| 79 | 79 |
Arc(Invalid) : id(-1) {}
|
| 80 | 80 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 81 | 81 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 82 | 82 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 83 | 83 |
}; |
| 84 | 84 |
|
| 85 | 85 |
ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
|
| 86 | 86 |
|
| 87 |
Node addNode() {
|
|
| 88 |
LEMON_ASSERT(false, |
|
| 89 |
"This graph structure does not support node insertion"); |
|
| 90 |
return INVALID; // avoid warning |
|
| 91 |
} |
|
| 92 |
|
|
| 87 | 93 |
Arc addArc(const Node& u, const Node& v) {
|
| 88 | 94 |
int n; |
| 89 | 95 |
if (first_free_arc == -1) {
|
| 90 | 96 |
n = arcs.size(); |
| 91 | 97 |
arcs.push_back(ArcT()); |
| 92 | 98 |
} else {
|
| 93 | 99 |
n = first_free_arc; |
| 94 | 100 |
first_free_arc = arcs[first_free_arc].next_in; |
| 95 | 101 |
} |
| 96 | 102 |
arcs[n].next_in = (*_nodes)[v].first_in; |
| 97 | 103 |
if ((*_nodes)[v].first_in != -1) {
|
| 98 | 104 |
arcs[(*_nodes)[v].first_in].prev_in = n; |
| 99 | 105 |
} |
| 100 | 106 |
(*_nodes)[v].first_in = n; |
| 101 | 107 |
arcs[n].next_out = (*_nodes)[u].first_out; |
| 102 | 108 |
if ((*_nodes)[u].first_out != -1) {
|
| 103 | 109 |
arcs[(*_nodes)[u].first_out].prev_out = n; |
| 104 | 110 |
} |
| 105 | 111 |
(*_nodes)[u].first_out = n; |
| 106 | 112 |
arcs[n].source = u; |
| 107 | 113 |
arcs[n].target = v; |
| 108 | 114 |
return Arc(n); |
| 109 | 115 |
} |
| 110 | 116 |
|
| 111 | 117 |
void erase(const Arc& arc) {
|
| 112 | 118 |
int n = arc.id; |
| 113 | 119 |
if (arcs[n].prev_in != -1) {
|
| 114 | 120 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
| 115 | 121 |
} else {
|
| 116 | 122 |
(*_nodes)[arcs[n].target].first_in = arcs[n].next_in; |
| 117 | 123 |
} |
| 118 | 124 |
if (arcs[n].next_in != -1) {
|
| 119 | 125 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
| 120 | 126 |
} |
| 121 | 127 |
|
| 122 | 128 |
if (arcs[n].prev_out != -1) {
|
| 123 | 129 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 124 | 130 |
} else {
|
| 125 | 131 |
(*_nodes)[arcs[n].source].first_out = arcs[n].next_out; |
| 126 | 132 |
} |
| 127 | 133 |
if (arcs[n].next_out != -1) {
|
| 128 | 134 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 129 | 135 |
} |
| 130 | 136 |
|
| 131 | 137 |
} |
| 132 | 138 |
|
| 133 | 139 |
void clear() {
|
| 134 | 140 |
Node node; |
| 135 | 141 |
for (first(node); node != INVALID; next(node)) {
|
| 136 | 142 |
(*_nodes)[node].first_in = -1; |
| 137 | 143 |
(*_nodes)[node].first_out = -1; |
| 138 | 144 |
} |
| 139 | 145 |
arcs.clear(); |
| 140 | 146 |
first_arc = -1; |
| 141 | 147 |
first_free_arc = -1; |
| 142 | 148 |
} |
| 143 | 149 |
|
| 144 | 150 |
void first(Node& node) const {
|
| 145 | 151 |
_graph->first(node); |
| 146 | 152 |
} |
| 147 | 153 |
|
| 148 | 154 |
void next(Node& node) const {
|
| 149 | 155 |
_graph->next(node); |
| 150 | 156 |
} |
| 151 | 157 |
|
| 152 | 158 |
void first(Arc& arc) const {
|
| 153 | 159 |
Node node; |
| 154 | 160 |
first(node); |
| 155 | 161 |
while (node != INVALID && (*_nodes)[node].first_in == -1) {
|
| 156 | 162 |
next(node); |
| 157 | 163 |
} |
| 158 | 164 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; |
| 159 | 165 |
} |
| 160 | 166 |
|
| 161 | 167 |
void next(Arc& arc) const {
|
| 162 | 168 |
if (arcs[arc.id].next_in != -1) {
|
| 163 | 169 |
arc.id = arcs[arc.id].next_in; |
| 164 | 170 |
} else {
|
| 165 | 171 |
Node node = arcs[arc.id].target; |
| 166 | 172 |
next(node); |
| 167 | 173 |
while (node != INVALID && (*_nodes)[node].first_in == -1) {
|
| 168 | 174 |
next(node); |
| 169 | 175 |
} |
| 170 | 176 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; |
| 171 | 177 |
} |
| 172 | 178 |
} |
| 173 | 179 |
|
| 174 | 180 |
void firstOut(Arc& arc, const Node& node) const {
|
| 175 | 181 |
arc.id = (*_nodes)[node].first_out; |
| 176 | 182 |
} |
| 177 | 183 |
|
| 178 | 184 |
void nextOut(Arc& arc) const {
|
| 179 | 185 |
arc.id = arcs[arc.id].next_out; |
| 180 | 186 |
} |
| 181 | 187 |
|
| 182 | 188 |
void firstIn(Arc& arc, const Node& node) const {
|
| ... | ... |
@@ -323,192 +329,198 @@ |
| 323 | 329 |
/// \brief Constructor of the ArcSet. |
| 324 | 330 |
/// |
| 325 | 331 |
/// Constructor of the ArcSet. |
| 326 | 332 |
ListArcSet(const GR& graph) : _nodes(graph, *this) {
|
| 327 | 333 |
Parent::initalize(graph, _nodes); |
| 328 | 334 |
} |
| 329 | 335 |
|
| 330 | 336 |
/// \brief Add a new arc to the digraph. |
| 331 | 337 |
/// |
| 332 | 338 |
/// Add a new arc to the digraph with source node \c s |
| 333 | 339 |
/// and target node \c t. |
| 334 | 340 |
/// \return The new arc. |
| 335 | 341 |
Arc addArc(const Node& s, const Node& t) {
|
| 336 | 342 |
return Parent::addArc(s, t); |
| 337 | 343 |
} |
| 338 | 344 |
|
| 339 | 345 |
/// \brief Erase an arc from the digraph. |
| 340 | 346 |
/// |
| 341 | 347 |
/// Erase an arc \c a from the digraph. |
| 342 | 348 |
void erase(const Arc& a) {
|
| 343 | 349 |
return Parent::erase(a); |
| 344 | 350 |
} |
| 345 | 351 |
|
| 346 | 352 |
}; |
| 347 | 353 |
|
| 348 | 354 |
template <typename GR> |
| 349 | 355 |
class ListEdgeSetBase {
|
| 350 | 356 |
public: |
| 351 | 357 |
|
| 352 | 358 |
typedef typename GR::Node Node; |
| 353 | 359 |
typedef typename GR::NodeIt NodeIt; |
| 354 | 360 |
|
| 355 | 361 |
protected: |
| 356 | 362 |
|
| 357 | 363 |
struct NodeT {
|
| 358 | 364 |
int first_out; |
| 359 | 365 |
NodeT() : first_out(-1) {}
|
| 360 | 366 |
}; |
| 361 | 367 |
|
| 362 | 368 |
typedef typename ItemSetTraits<GR, Node>:: |
| 363 | 369 |
template Map<NodeT>::Type NodesImplBase; |
| 364 | 370 |
|
| 365 | 371 |
NodesImplBase* _nodes; |
| 366 | 372 |
|
| 367 | 373 |
struct ArcT {
|
| 368 | 374 |
Node target; |
| 369 | 375 |
int prev_out, next_out; |
| 370 | 376 |
ArcT() : prev_out(-1), next_out(-1) {}
|
| 371 | 377 |
}; |
| 372 | 378 |
|
| 373 | 379 |
std::vector<ArcT> arcs; |
| 374 | 380 |
|
| 375 | 381 |
int first_arc; |
| 376 | 382 |
int first_free_arc; |
| 377 | 383 |
|
| 378 | 384 |
const GR* _graph; |
| 379 | 385 |
|
| 380 | 386 |
void initalize(const GR& graph, NodesImplBase& nodes) {
|
| 381 | 387 |
_graph = &graph; |
| 382 | 388 |
_nodes = &nodes; |
| 383 | 389 |
} |
| 384 | 390 |
|
| 385 | 391 |
public: |
| 386 | 392 |
|
| 387 | 393 |
class Edge {
|
| 388 | 394 |
friend class ListEdgeSetBase; |
| 389 | 395 |
protected: |
| 390 | 396 |
|
| 391 | 397 |
int id; |
| 392 | 398 |
explicit Edge(int _id) { id = _id;}
|
| 393 | 399 |
|
| 394 | 400 |
public: |
| 395 | 401 |
Edge() {}
|
| 396 | 402 |
Edge (Invalid) { id = -1; }
|
| 397 | 403 |
bool operator==(const Edge& arc) const {return id == arc.id;}
|
| 398 | 404 |
bool operator!=(const Edge& arc) const {return id != arc.id;}
|
| 399 | 405 |
bool operator<(const Edge& arc) const {return id < arc.id;}
|
| 400 | 406 |
}; |
| 401 | 407 |
|
| 402 | 408 |
class Arc {
|
| 403 | 409 |
friend class ListEdgeSetBase; |
| 404 | 410 |
protected: |
| 405 | 411 |
Arc(int _id) : id(_id) {}
|
| 406 | 412 |
int id; |
| 407 | 413 |
public: |
| 408 | 414 |
operator Edge() const { return edgeFromId(id / 2); }
|
| 409 | 415 |
|
| 410 | 416 |
Arc() {}
|
| 411 | 417 |
Arc(Invalid) : id(-1) {}
|
| 412 | 418 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 413 | 419 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 414 | 420 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 415 | 421 |
}; |
| 416 | 422 |
|
| 417 | 423 |
ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
|
| 418 | 424 |
|
| 425 |
Node addNode() {
|
|
| 426 |
LEMON_ASSERT(false, |
|
| 427 |
"This graph structure does not support node insertion"); |
|
| 428 |
return INVALID; // avoid warning |
|
| 429 |
} |
|
| 430 |
|
|
| 419 | 431 |
Edge addEdge(const Node& u, const Node& v) {
|
| 420 | 432 |
int n; |
| 421 | 433 |
|
| 422 | 434 |
if (first_free_arc == -1) {
|
| 423 | 435 |
n = arcs.size(); |
| 424 | 436 |
arcs.push_back(ArcT()); |
| 425 | 437 |
arcs.push_back(ArcT()); |
| 426 | 438 |
} else {
|
| 427 | 439 |
n = first_free_arc; |
| 428 | 440 |
first_free_arc = arcs[n].next_out; |
| 429 | 441 |
} |
| 430 | 442 |
|
| 431 | 443 |
arcs[n].target = u; |
| 432 | 444 |
arcs[n | 1].target = v; |
| 433 | 445 |
|
| 434 | 446 |
arcs[n].next_out = (*_nodes)[v].first_out; |
| 435 | 447 |
if ((*_nodes)[v].first_out != -1) {
|
| 436 | 448 |
arcs[(*_nodes)[v].first_out].prev_out = n; |
| 437 | 449 |
} |
| 438 | 450 |
(*_nodes)[v].first_out = n; |
| 439 | 451 |
arcs[n].prev_out = -1; |
| 440 | 452 |
|
| 441 | 453 |
if ((*_nodes)[u].first_out != -1) {
|
| 442 | 454 |
arcs[(*_nodes)[u].first_out].prev_out = (n | 1); |
| 443 | 455 |
} |
| 444 | 456 |
arcs[n | 1].next_out = (*_nodes)[u].first_out; |
| 445 | 457 |
(*_nodes)[u].first_out = (n | 1); |
| 446 | 458 |
arcs[n | 1].prev_out = -1; |
| 447 | 459 |
|
| 448 | 460 |
return Edge(n / 2); |
| 449 | 461 |
} |
| 450 | 462 |
|
| 451 | 463 |
void erase(const Edge& arc) {
|
| 452 | 464 |
int n = arc.id * 2; |
| 453 | 465 |
|
| 454 | 466 |
if (arcs[n].next_out != -1) {
|
| 455 | 467 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 456 | 468 |
} |
| 457 | 469 |
|
| 458 | 470 |
if (arcs[n].prev_out != -1) {
|
| 459 | 471 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 460 | 472 |
} else {
|
| 461 | 473 |
(*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out; |
| 462 | 474 |
} |
| 463 | 475 |
|
| 464 | 476 |
if (arcs[n | 1].next_out != -1) {
|
| 465 | 477 |
arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
| 466 | 478 |
} |
| 467 | 479 |
|
| 468 | 480 |
if (arcs[n | 1].prev_out != -1) {
|
| 469 | 481 |
arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
| 470 | 482 |
} else {
|
| 471 | 483 |
(*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out; |
| 472 | 484 |
} |
| 473 | 485 |
|
| 474 | 486 |
arcs[n].next_out = first_free_arc; |
| 475 | 487 |
first_free_arc = n; |
| 476 | 488 |
|
| 477 | 489 |
} |
| 478 | 490 |
|
| 479 | 491 |
void clear() {
|
| 480 | 492 |
Node node; |
| 481 | 493 |
for (first(node); node != INVALID; next(node)) {
|
| 482 | 494 |
(*_nodes)[node].first_out = -1; |
| 483 | 495 |
} |
| 484 | 496 |
arcs.clear(); |
| 485 | 497 |
first_arc = -1; |
| 486 | 498 |
first_free_arc = -1; |
| 487 | 499 |
} |
| 488 | 500 |
|
| 489 | 501 |
void first(Node& node) const {
|
| 490 | 502 |
_graph->first(node); |
| 491 | 503 |
} |
| 492 | 504 |
|
| 493 | 505 |
void next(Node& node) const {
|
| 494 | 506 |
_graph->next(node); |
| 495 | 507 |
} |
| 496 | 508 |
|
| 497 | 509 |
void first(Arc& arc) const {
|
| 498 | 510 |
Node node; |
| 499 | 511 |
first(node); |
| 500 | 512 |
while (node != INVALID && (*_nodes)[node].first_out == -1) {
|
| 501 | 513 |
next(node); |
| 502 | 514 |
} |
| 503 | 515 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out; |
| 504 | 516 |
} |
| 505 | 517 |
|
| 506 | 518 |
void next(Arc& arc) const {
|
| 507 | 519 |
if (arcs[arc.id].next_out != -1) {
|
| 508 | 520 |
arc.id = arcs[arc.id].next_out; |
| 509 | 521 |
} else {
|
| 510 | 522 |
Node node = arcs[arc.id ^ 1].target; |
| 511 | 523 |
next(node); |
| 512 | 524 |
while(node != INVALID && (*_nodes)[node].first_out == -1) {
|
| 513 | 525 |
next(node); |
| 514 | 526 |
} |
| ... | ... |
@@ -723,192 +735,198 @@ |
| 723 | 735 |
} |
| 724 | 736 |
virtual void erase(const std::vector<Node>& nodes) {
|
| 725 | 737 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 726 | 738 |
_arcset.eraseNode(nodes[i]); |
| 727 | 739 |
} |
| 728 | 740 |
Parent::erase(nodes); |
| 729 | 741 |
} |
| 730 | 742 |
virtual void clear() {
|
| 731 | 743 |
_arcset.clearNodes(); |
| 732 | 744 |
Parent::clear(); |
| 733 | 745 |
} |
| 734 | 746 |
|
| 735 | 747 |
private: |
| 736 | 748 |
ListEdgeSet& _arcset; |
| 737 | 749 |
}; |
| 738 | 750 |
|
| 739 | 751 |
NodesImpl _nodes; |
| 740 | 752 |
|
| 741 | 753 |
public: |
| 742 | 754 |
|
| 743 | 755 |
/// \brief Constructor of the EdgeSet. |
| 744 | 756 |
/// |
| 745 | 757 |
/// Constructor of the EdgeSet. |
| 746 | 758 |
ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
|
| 747 | 759 |
Parent::initalize(graph, _nodes); |
| 748 | 760 |
} |
| 749 | 761 |
|
| 750 | 762 |
/// \brief Add a new edge to the graph. |
| 751 | 763 |
/// |
| 752 | 764 |
/// Add a new edge to the graph with node \c u |
| 753 | 765 |
/// and node \c v endpoints. |
| 754 | 766 |
/// \return The new edge. |
| 755 | 767 |
Edge addEdge(const Node& u, const Node& v) {
|
| 756 | 768 |
return Parent::addEdge(u, v); |
| 757 | 769 |
} |
| 758 | 770 |
|
| 759 | 771 |
/// \brief Erase an edge from the graph. |
| 760 | 772 |
/// |
| 761 | 773 |
/// Erase the edge \c e from the graph. |
| 762 | 774 |
void erase(const Edge& e) {
|
| 763 | 775 |
return Parent::erase(e); |
| 764 | 776 |
} |
| 765 | 777 |
|
| 766 | 778 |
}; |
| 767 | 779 |
|
| 768 | 780 |
template <typename GR> |
| 769 | 781 |
class SmartArcSetBase {
|
| 770 | 782 |
public: |
| 771 | 783 |
|
| 772 | 784 |
typedef typename GR::Node Node; |
| 773 | 785 |
typedef typename GR::NodeIt NodeIt; |
| 774 | 786 |
|
| 775 | 787 |
protected: |
| 776 | 788 |
|
| 777 | 789 |
struct NodeT {
|
| 778 | 790 |
int first_out, first_in; |
| 779 | 791 |
NodeT() : first_out(-1), first_in(-1) {}
|
| 780 | 792 |
}; |
| 781 | 793 |
|
| 782 | 794 |
typedef typename ItemSetTraits<GR, Node>:: |
| 783 | 795 |
template Map<NodeT>::Type NodesImplBase; |
| 784 | 796 |
|
| 785 | 797 |
NodesImplBase* _nodes; |
| 786 | 798 |
|
| 787 | 799 |
struct ArcT {
|
| 788 | 800 |
Node source, target; |
| 789 | 801 |
int next_out, next_in; |
| 790 | 802 |
ArcT() {}
|
| 791 | 803 |
}; |
| 792 | 804 |
|
| 793 | 805 |
std::vector<ArcT> arcs; |
| 794 | 806 |
|
| 795 | 807 |
const GR* _graph; |
| 796 | 808 |
|
| 797 | 809 |
void initalize(const GR& graph, NodesImplBase& nodes) {
|
| 798 | 810 |
_graph = &graph; |
| 799 | 811 |
_nodes = &nodes; |
| 800 | 812 |
} |
| 801 | 813 |
|
| 802 | 814 |
public: |
| 803 | 815 |
|
| 804 | 816 |
class Arc {
|
| 805 | 817 |
friend class SmartArcSetBase<GR>; |
| 806 | 818 |
protected: |
| 807 | 819 |
Arc(int _id) : id(_id) {}
|
| 808 | 820 |
int id; |
| 809 | 821 |
public: |
| 810 | 822 |
Arc() {}
|
| 811 | 823 |
Arc(Invalid) : id(-1) {}
|
| 812 | 824 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 813 | 825 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 814 | 826 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 815 | 827 |
}; |
| 816 | 828 |
|
| 817 | 829 |
SmartArcSetBase() {}
|
| 818 | 830 |
|
| 831 |
Node addNode() {
|
|
| 832 |
LEMON_ASSERT(false, |
|
| 833 |
"This graph structure does not support node insertion"); |
|
| 834 |
return INVALID; // avoid warning |
|
| 835 |
} |
|
| 836 |
|
|
| 819 | 837 |
Arc addArc(const Node& u, const Node& v) {
|
| 820 | 838 |
int n = arcs.size(); |
| 821 | 839 |
arcs.push_back(ArcT()); |
| 822 | 840 |
arcs[n].next_in = (*_nodes)[v].first_in; |
| 823 | 841 |
(*_nodes)[v].first_in = n; |
| 824 | 842 |
arcs[n].next_out = (*_nodes)[u].first_out; |
| 825 | 843 |
(*_nodes)[u].first_out = n; |
| 826 | 844 |
arcs[n].source = u; |
| 827 | 845 |
arcs[n].target = v; |
| 828 | 846 |
return Arc(n); |
| 829 | 847 |
} |
| 830 | 848 |
|
| 831 | 849 |
void clear() {
|
| 832 | 850 |
Node node; |
| 833 | 851 |
for (first(node); node != INVALID; next(node)) {
|
| 834 | 852 |
(*_nodes)[node].first_in = -1; |
| 835 | 853 |
(*_nodes)[node].first_out = -1; |
| 836 | 854 |
} |
| 837 | 855 |
arcs.clear(); |
| 838 | 856 |
} |
| 839 | 857 |
|
| 840 | 858 |
void first(Node& node) const {
|
| 841 | 859 |
_graph->first(node); |
| 842 | 860 |
} |
| 843 | 861 |
|
| 844 | 862 |
void next(Node& node) const {
|
| 845 | 863 |
_graph->next(node); |
| 846 | 864 |
} |
| 847 | 865 |
|
| 848 | 866 |
void first(Arc& arc) const {
|
| 849 | 867 |
arc.id = arcs.size() - 1; |
| 850 | 868 |
} |
| 851 | 869 |
|
| 852 | 870 |
void next(Arc& arc) const {
|
| 853 | 871 |
--arc.id; |
| 854 | 872 |
} |
| 855 | 873 |
|
| 856 | 874 |
void firstOut(Arc& arc, const Node& node) const {
|
| 857 | 875 |
arc.id = (*_nodes)[node].first_out; |
| 858 | 876 |
} |
| 859 | 877 |
|
| 860 | 878 |
void nextOut(Arc& arc) const {
|
| 861 | 879 |
arc.id = arcs[arc.id].next_out; |
| 862 | 880 |
} |
| 863 | 881 |
|
| 864 | 882 |
void firstIn(Arc& arc, const Node& node) const {
|
| 865 | 883 |
arc.id = (*_nodes)[node].first_in; |
| 866 | 884 |
} |
| 867 | 885 |
|
| 868 | 886 |
void nextIn(Arc& arc) const {
|
| 869 | 887 |
arc.id = arcs[arc.id].next_in; |
| 870 | 888 |
} |
| 871 | 889 |
|
| 872 | 890 |
int id(const Node& node) const { return _graph->id(node); }
|
| 873 | 891 |
int id(const Arc& arc) const { return arc.id; }
|
| 874 | 892 |
|
| 875 | 893 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
|
| 876 | 894 |
Arc arcFromId(int ix) const { return Arc(ix); }
|
| 877 | 895 |
|
| 878 | 896 |
int maxNodeId() const { return _graph->maxNodeId(); };
|
| 879 | 897 |
int maxArcId() const { return arcs.size() - 1; }
|
| 880 | 898 |
|
| 881 | 899 |
Node source(const Arc& arc) const { return arcs[arc.id].source;}
|
| 882 | 900 |
Node target(const Arc& arc) const { return arcs[arc.id].target;}
|
| 883 | 901 |
|
| 884 | 902 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
| 885 | 903 |
|
| 886 | 904 |
NodeNotifier& notifier(Node) const {
|
| 887 | 905 |
return _graph->notifier(Node()); |
| 888 | 906 |
} |
| 889 | 907 |
|
| 890 | 908 |
template <typename V> |
| 891 | 909 |
class NodeMap : public GR::template NodeMap<V> {
|
| 892 | 910 |
typedef typename GR::template NodeMap<V> Parent; |
| 893 | 911 |
|
| 894 | 912 |
public: |
| 895 | 913 |
|
| 896 | 914 |
explicit NodeMap(const SmartArcSetBase<GR>& arcset) |
| 897 | 915 |
: Parent(*arcset._graph) { }
|
| 898 | 916 |
|
| 899 | 917 |
NodeMap(const SmartArcSetBase<GR>& arcset, const V& value) |
| 900 | 918 |
: Parent(*arcset._graph, value) { }
|
| 901 | 919 |
|
| 902 | 920 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 903 | 921 |
return operator=<NodeMap>(cmap); |
| 904 | 922 |
} |
| 905 | 923 |
|
| 906 | 924 |
template <typename CMap> |
| 907 | 925 |
NodeMap& operator=(const CMap& cmap) {
|
| 908 | 926 |
Parent::operator=(cmap); |
| 909 | 927 |
return *this; |
| 910 | 928 |
} |
| 911 | 929 |
}; |
| 912 | 930 |
|
| 913 | 931 |
}; |
| 914 | 932 |
|
| ... | ... |
@@ -1019,192 +1037,198 @@ |
| 1019 | 1037 |
/// \brief Constructor of the ArcSet. |
| 1020 | 1038 |
/// |
| 1021 | 1039 |
/// Constructor of the ArcSet. |
| 1022 | 1040 |
SmartArcSet(const GR& graph) : _nodes(graph, *this) {
|
| 1023 | 1041 |
Parent::initalize(graph, _nodes); |
| 1024 | 1042 |
} |
| 1025 | 1043 |
|
| 1026 | 1044 |
/// \brief Add a new arc to the digraph. |
| 1027 | 1045 |
/// |
| 1028 | 1046 |
/// Add a new arc to the digraph with source node \c s |
| 1029 | 1047 |
/// and target node \c t. |
| 1030 | 1048 |
/// \return The new arc. |
| 1031 | 1049 |
Arc addArc(const Node& s, const Node& t) {
|
| 1032 | 1050 |
return Parent::addArc(s, t); |
| 1033 | 1051 |
} |
| 1034 | 1052 |
|
| 1035 | 1053 |
/// \brief Validity check |
| 1036 | 1054 |
/// |
| 1037 | 1055 |
/// This functions gives back false if the ArcSet is |
| 1038 | 1056 |
/// invalidated. It occurs when a node in the underlying graph is |
| 1039 | 1057 |
/// erased and it is not isolated in the ArcSet. |
| 1040 | 1058 |
bool valid() const {
|
| 1041 | 1059 |
return _nodes.attached(); |
| 1042 | 1060 |
} |
| 1043 | 1061 |
|
| 1044 | 1062 |
}; |
| 1045 | 1063 |
|
| 1046 | 1064 |
|
| 1047 | 1065 |
template <typename GR> |
| 1048 | 1066 |
class SmartEdgeSetBase {
|
| 1049 | 1067 |
public: |
| 1050 | 1068 |
|
| 1051 | 1069 |
typedef typename GR::Node Node; |
| 1052 | 1070 |
typedef typename GR::NodeIt NodeIt; |
| 1053 | 1071 |
|
| 1054 | 1072 |
protected: |
| 1055 | 1073 |
|
| 1056 | 1074 |
struct NodeT {
|
| 1057 | 1075 |
int first_out; |
| 1058 | 1076 |
NodeT() : first_out(-1) {}
|
| 1059 | 1077 |
}; |
| 1060 | 1078 |
|
| 1061 | 1079 |
typedef typename ItemSetTraits<GR, Node>:: |
| 1062 | 1080 |
template Map<NodeT>::Type NodesImplBase; |
| 1063 | 1081 |
|
| 1064 | 1082 |
NodesImplBase* _nodes; |
| 1065 | 1083 |
|
| 1066 | 1084 |
struct ArcT {
|
| 1067 | 1085 |
Node target; |
| 1068 | 1086 |
int next_out; |
| 1069 | 1087 |
ArcT() {}
|
| 1070 | 1088 |
}; |
| 1071 | 1089 |
|
| 1072 | 1090 |
std::vector<ArcT> arcs; |
| 1073 | 1091 |
|
| 1074 | 1092 |
const GR* _graph; |
| 1075 | 1093 |
|
| 1076 | 1094 |
void initalize(const GR& graph, NodesImplBase& nodes) {
|
| 1077 | 1095 |
_graph = &graph; |
| 1078 | 1096 |
_nodes = &nodes; |
| 1079 | 1097 |
} |
| 1080 | 1098 |
|
| 1081 | 1099 |
public: |
| 1082 | 1100 |
|
| 1083 | 1101 |
class Edge {
|
| 1084 | 1102 |
friend class SmartEdgeSetBase; |
| 1085 | 1103 |
protected: |
| 1086 | 1104 |
|
| 1087 | 1105 |
int id; |
| 1088 | 1106 |
explicit Edge(int _id) { id = _id;}
|
| 1089 | 1107 |
|
| 1090 | 1108 |
public: |
| 1091 | 1109 |
Edge() {}
|
| 1092 | 1110 |
Edge (Invalid) { id = -1; }
|
| 1093 | 1111 |
bool operator==(const Edge& arc) const {return id == arc.id;}
|
| 1094 | 1112 |
bool operator!=(const Edge& arc) const {return id != arc.id;}
|
| 1095 | 1113 |
bool operator<(const Edge& arc) const {return id < arc.id;}
|
| 1096 | 1114 |
}; |
| 1097 | 1115 |
|
| 1098 | 1116 |
class Arc {
|
| 1099 | 1117 |
friend class SmartEdgeSetBase; |
| 1100 | 1118 |
protected: |
| 1101 | 1119 |
Arc(int _id) : id(_id) {}
|
| 1102 | 1120 |
int id; |
| 1103 | 1121 |
public: |
| 1104 | 1122 |
operator Edge() const { return edgeFromId(id / 2); }
|
| 1105 | 1123 |
|
| 1106 | 1124 |
Arc() {}
|
| 1107 | 1125 |
Arc(Invalid) : id(-1) {}
|
| 1108 | 1126 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 1109 | 1127 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 1110 | 1128 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 1111 | 1129 |
}; |
| 1112 | 1130 |
|
| 1113 | 1131 |
SmartEdgeSetBase() {}
|
| 1114 | 1132 |
|
| 1133 |
Node addNode() {
|
|
| 1134 |
LEMON_ASSERT(false, |
|
| 1135 |
"This graph structure does not support node insertion"); |
|
| 1136 |
return INVALID; // avoid warning |
|
| 1137 |
} |
|
| 1138 |
|
|
| 1115 | 1139 |
Edge addEdge(const Node& u, const Node& v) {
|
| 1116 | 1140 |
int n = arcs.size(); |
| 1117 | 1141 |
arcs.push_back(ArcT()); |
| 1118 | 1142 |
arcs.push_back(ArcT()); |
| 1119 | 1143 |
|
| 1120 | 1144 |
arcs[n].target = u; |
| 1121 | 1145 |
arcs[n | 1].target = v; |
| 1122 | 1146 |
|
| 1123 | 1147 |
arcs[n].next_out = (*_nodes)[v].first_out; |
| 1124 | 1148 |
(*_nodes)[v].first_out = n; |
| 1125 | 1149 |
|
| 1126 | 1150 |
arcs[n | 1].next_out = (*_nodes)[u].first_out; |
| 1127 | 1151 |
(*_nodes)[u].first_out = (n | 1); |
| 1128 | 1152 |
|
| 1129 | 1153 |
return Edge(n / 2); |
| 1130 | 1154 |
} |
| 1131 | 1155 |
|
| 1132 | 1156 |
void clear() {
|
| 1133 | 1157 |
Node node; |
| 1134 | 1158 |
for (first(node); node != INVALID; next(node)) {
|
| 1135 | 1159 |
(*_nodes)[node].first_out = -1; |
| 1136 | 1160 |
} |
| 1137 | 1161 |
arcs.clear(); |
| 1138 | 1162 |
} |
| 1139 | 1163 |
|
| 1140 | 1164 |
void first(Node& node) const {
|
| 1141 | 1165 |
_graph->first(node); |
| 1142 | 1166 |
} |
| 1143 | 1167 |
|
| 1144 | 1168 |
void next(Node& node) const {
|
| 1145 | 1169 |
_graph->next(node); |
| 1146 | 1170 |
} |
| 1147 | 1171 |
|
| 1148 | 1172 |
void first(Arc& arc) const {
|
| 1149 | 1173 |
arc.id = arcs.size() - 1; |
| 1150 | 1174 |
} |
| 1151 | 1175 |
|
| 1152 | 1176 |
void next(Arc& arc) const {
|
| 1153 | 1177 |
--arc.id; |
| 1154 | 1178 |
} |
| 1155 | 1179 |
|
| 1156 | 1180 |
void first(Edge& arc) const {
|
| 1157 | 1181 |
arc.id = arcs.size() / 2 - 1; |
| 1158 | 1182 |
} |
| 1159 | 1183 |
|
| 1160 | 1184 |
void next(Edge& arc) const {
|
| 1161 | 1185 |
--arc.id; |
| 1162 | 1186 |
} |
| 1163 | 1187 |
|
| 1164 | 1188 |
void firstOut(Arc& arc, const Node& node) const {
|
| 1165 | 1189 |
arc.id = (*_nodes)[node].first_out; |
| 1166 | 1190 |
} |
| 1167 | 1191 |
|
| 1168 | 1192 |
void nextOut(Arc& arc) const {
|
| 1169 | 1193 |
arc.id = arcs[arc.id].next_out; |
| 1170 | 1194 |
} |
| 1171 | 1195 |
|
| 1172 | 1196 |
void firstIn(Arc& arc, const Node& node) const {
|
| 1173 | 1197 |
arc.id = (((*_nodes)[node].first_out) ^ 1); |
| 1174 | 1198 |
if (arc.id == -2) arc.id = -1; |
| 1175 | 1199 |
} |
| 1176 | 1200 |
|
| 1177 | 1201 |
void nextIn(Arc& arc) const {
|
| 1178 | 1202 |
arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); |
| 1179 | 1203 |
if (arc.id == -2) arc.id = -1; |
| 1180 | 1204 |
} |
| 1181 | 1205 |
|
| 1182 | 1206 |
void firstInc(Edge &arc, bool& dir, const Node& node) const {
|
| 1183 | 1207 |
int de = (*_nodes)[node].first_out; |
| 1184 | 1208 |
if (de != -1 ) {
|
| 1185 | 1209 |
arc.id = de / 2; |
| 1186 | 1210 |
dir = ((de & 1) == 1); |
| 1187 | 1211 |
} else {
|
| 1188 | 1212 |
arc.id = -1; |
| 1189 | 1213 |
dir = true; |
| 1190 | 1214 |
} |
| 1191 | 1215 |
} |
| 1192 | 1216 |
void nextInc(Edge &arc, bool& dir) const {
|
| 1193 | 1217 |
int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); |
| 1194 | 1218 |
if (de != -1 ) {
|
| 1195 | 1219 |
arc.id = de / 2; |
| 1196 | 1220 |
dir = ((de & 1) == 1); |
| 1197 | 1221 |
} else {
|
| 1198 | 1222 |
arc.id = -1; |
| 1199 | 1223 |
dir = true; |
| 1200 | 1224 |
} |
| 1201 | 1225 |
} |
| 1202 | 1226 |
|
| 1203 | 1227 |
static bool direction(Arc arc) {
|
| 1204 | 1228 |
return (arc.id & 1) == 1; |
| 1205 | 1229 |
} |
| 1206 | 1230 |
|
| 1207 | 1231 |
static Arc direct(Edge edge, bool dir) {
|
| 1208 | 1232 |
return Arc(edge.id * 2 + (dir ? 1 : 0)); |
| 1209 | 1233 |
} |
| 1210 | 1234 |
|
0 comments (0 inline)