0
2
0
| ... | ... |
@@ -749,193 +749,195 @@ |
| 749 | 749 |
/// Undo the changes until the last snapshot created by save(). |
| 750 | 750 |
void restore() {
|
| 751 | 751 |
detach(); |
| 752 | 752 |
for(std::list<Arc>::iterator it = added_arcs.begin(); |
| 753 | 753 |
it != added_arcs.end(); ++it) {
|
| 754 | 754 |
digraph->erase(*it); |
| 755 | 755 |
} |
| 756 | 756 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
| 757 | 757 |
it != added_nodes.end(); ++it) {
|
| 758 | 758 |
digraph->erase(*it); |
| 759 | 759 |
} |
| 760 | 760 |
clear(); |
| 761 | 761 |
} |
| 762 | 762 |
|
| 763 | 763 |
/// \brief Gives back true when the snapshot is valid. |
| 764 | 764 |
/// |
| 765 | 765 |
/// Gives back true when the snapshot is valid. |
| 766 | 766 |
bool valid() const {
|
| 767 | 767 |
return attached(); |
| 768 | 768 |
} |
| 769 | 769 |
}; |
| 770 | 770 |
|
| 771 | 771 |
}; |
| 772 | 772 |
|
| 773 | 773 |
///@} |
| 774 | 774 |
|
| 775 | 775 |
class ListGraphBase {
|
| 776 | 776 |
|
| 777 | 777 |
protected: |
| 778 | 778 |
|
| 779 | 779 |
struct NodeT {
|
| 780 | 780 |
int first_out; |
| 781 | 781 |
int prev, next; |
| 782 | 782 |
}; |
| 783 | 783 |
|
| 784 | 784 |
struct ArcT {
|
| 785 | 785 |
int target; |
| 786 | 786 |
int prev_out, next_out; |
| 787 | 787 |
}; |
| 788 | 788 |
|
| 789 | 789 |
std::vector<NodeT> nodes; |
| 790 | 790 |
|
| 791 | 791 |
int first_node; |
| 792 | 792 |
|
| 793 | 793 |
int first_free_node; |
| 794 | 794 |
|
| 795 | 795 |
std::vector<ArcT> arcs; |
| 796 | 796 |
|
| 797 | 797 |
int first_free_arc; |
| 798 | 798 |
|
| 799 | 799 |
public: |
| 800 | 800 |
|
| 801 | 801 |
typedef ListGraphBase Digraph; |
| 802 | 802 |
|
| 803 | 803 |
class Node; |
| 804 | 804 |
class Arc; |
| 805 | 805 |
class Edge; |
| 806 | 806 |
|
| 807 | 807 |
class Node {
|
| 808 | 808 |
friend class ListGraphBase; |
| 809 | 809 |
protected: |
| 810 | 810 |
|
| 811 | 811 |
int id; |
| 812 | 812 |
explicit Node(int pid) { id = pid;}
|
| 813 | 813 |
|
| 814 | 814 |
public: |
| 815 | 815 |
Node() {}
|
| 816 | 816 |
Node (Invalid) { id = -1; }
|
| 817 | 817 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 818 | 818 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 819 | 819 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 820 | 820 |
}; |
| 821 | 821 |
|
| 822 | 822 |
class Edge {
|
| 823 | 823 |
friend class ListGraphBase; |
| 824 | 824 |
protected: |
| 825 | 825 |
|
| 826 | 826 |
int id; |
| 827 | 827 |
explicit Edge(int pid) { id = pid;}
|
| 828 | 828 |
|
| 829 | 829 |
public: |
| 830 | 830 |
Edge() {}
|
| 831 | 831 |
Edge (Invalid) { id = -1; }
|
| 832 | 832 |
bool operator==(const Edge& edge) const {return id == edge.id;}
|
| 833 | 833 |
bool operator!=(const Edge& edge) const {return id != edge.id;}
|
| 834 | 834 |
bool operator<(const Edge& edge) const {return id < edge.id;}
|
| 835 | 835 |
}; |
| 836 | 836 |
|
| 837 | 837 |
class Arc {
|
| 838 | 838 |
friend class ListGraphBase; |
| 839 | 839 |
protected: |
| 840 | 840 |
|
| 841 | 841 |
int id; |
| 842 | 842 |
explicit Arc(int pid) { id = pid;}
|
| 843 | 843 |
|
| 844 | 844 |
public: |
| 845 |
operator Edge() const {
|
|
| 845 |
operator Edge() const {
|
|
| 846 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
|
| 847 |
} |
|
| 846 | 848 |
|
| 847 | 849 |
Arc() {}
|
| 848 | 850 |
Arc (Invalid) { id = -1; }
|
| 849 | 851 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 850 | 852 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 851 | 853 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 852 | 854 |
}; |
| 853 | 855 |
|
| 854 | 856 |
|
| 855 | 857 |
|
| 856 | 858 |
ListGraphBase() |
| 857 | 859 |
: nodes(), first_node(-1), |
| 858 | 860 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 859 | 861 |
|
| 860 | 862 |
|
| 861 | 863 |
int maxNodeId() const { return nodes.size()-1; }
|
| 862 | 864 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 863 | 865 |
int maxArcId() const { return arcs.size()-1; }
|
| 864 | 866 |
|
| 865 | 867 |
Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
|
| 866 | 868 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 867 | 869 |
|
| 868 | 870 |
Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
|
| 869 | 871 |
Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
|
| 870 | 872 |
|
| 871 | 873 |
static bool direction(Arc e) {
|
| 872 | 874 |
return (e.id & 1) == 1; |
| 873 | 875 |
} |
| 874 | 876 |
|
| 875 | 877 |
static Arc direct(Edge e, bool d) {
|
| 876 | 878 |
return Arc(e.id * 2 + (d ? 1 : 0)); |
| 877 | 879 |
} |
| 878 | 880 |
|
| 879 | 881 |
void first(Node& node) const {
|
| 880 | 882 |
node.id = first_node; |
| 881 | 883 |
} |
| 882 | 884 |
|
| 883 | 885 |
void next(Node& node) const {
|
| 884 | 886 |
node.id = nodes[node.id].next; |
| 885 | 887 |
} |
| 886 | 888 |
|
| 887 | 889 |
void first(Arc& e) const {
|
| 888 | 890 |
int n = first_node; |
| 889 | 891 |
while (n != -1 && nodes[n].first_out == -1) {
|
| 890 | 892 |
n = nodes[n].next; |
| 891 | 893 |
} |
| 892 | 894 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 893 | 895 |
} |
| 894 | 896 |
|
| 895 | 897 |
void next(Arc& e) const {
|
| 896 | 898 |
if (arcs[e.id].next_out != -1) {
|
| 897 | 899 |
e.id = arcs[e.id].next_out; |
| 898 | 900 |
} else {
|
| 899 | 901 |
int n = nodes[arcs[e.id ^ 1].target].next; |
| 900 | 902 |
while(n != -1 && nodes[n].first_out == -1) {
|
| 901 | 903 |
n = nodes[n].next; |
| 902 | 904 |
} |
| 903 | 905 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 904 | 906 |
} |
| 905 | 907 |
} |
| 906 | 908 |
|
| 907 | 909 |
void first(Edge& e) const {
|
| 908 | 910 |
int n = first_node; |
| 909 | 911 |
while (n != -1) {
|
| 910 | 912 |
e.id = nodes[n].first_out; |
| 911 | 913 |
while ((e.id & 1) != 1) {
|
| 912 | 914 |
e.id = arcs[e.id].next_out; |
| 913 | 915 |
} |
| 914 | 916 |
if (e.id != -1) {
|
| 915 | 917 |
e.id /= 2; |
| 916 | 918 |
return; |
| 917 | 919 |
} |
| 918 | 920 |
n = nodes[n].next; |
| 919 | 921 |
} |
| 920 | 922 |
e.id = -1; |
| 921 | 923 |
} |
| 922 | 924 |
|
| 923 | 925 |
void next(Edge& e) const {
|
| 924 | 926 |
int n = arcs[e.id * 2].target; |
| 925 | 927 |
e.id = arcs[(e.id * 2) | 1].next_out; |
| 926 | 928 |
while ((e.id & 1) != 1) {
|
| 927 | 929 |
e.id = arcs[e.id].next_out; |
| 928 | 930 |
} |
| 929 | 931 |
if (e.id != -1) {
|
| 930 | 932 |
e.id /= 2; |
| 931 | 933 |
return; |
| 932 | 934 |
} |
| 933 | 935 |
n = nodes[n].next; |
| 934 | 936 |
while (n != -1) {
|
| 935 | 937 |
e.id = nodes[n].first_out; |
| 936 | 938 |
while ((e.id & 1) != 1) {
|
| 937 | 939 |
e.id = arcs[e.id].next_out; |
| 938 | 940 |
} |
| 939 | 941 |
if (e.id != -1) {
|
| 940 | 942 |
e.id /= 2; |
| 941 | 943 |
return; |
| ... | ... |
@@ -372,193 +372,195 @@ |
| 372 | 372 |
arc_num=_graph->arcs.size(); |
| 373 | 373 |
} |
| 374 | 374 |
|
| 375 | 375 |
///Make a snapshot. |
| 376 | 376 |
|
| 377 | 377 |
///Make a snapshot of the digraph. |
| 378 | 378 |
/// |
| 379 | 379 |
///This function can be called more than once. In case of a repeated |
| 380 | 380 |
///call, the previous snapshot gets lost. |
| 381 | 381 |
///\param _g The digraph we make the snapshot of. |
| 382 | 382 |
void save(SmartDigraph &graph) |
| 383 | 383 |
{
|
| 384 | 384 |
_graph=&graph; |
| 385 | 385 |
node_num=_graph->nodes.size(); |
| 386 | 386 |
arc_num=_graph->arcs.size(); |
| 387 | 387 |
} |
| 388 | 388 |
|
| 389 | 389 |
///Undo the changes until a snapshot. |
| 390 | 390 |
|
| 391 | 391 |
///Undo the changes until a snapshot created by save(). |
| 392 | 392 |
/// |
| 393 | 393 |
///\note After you restored a state, you cannot restore |
| 394 | 394 |
///a later state, in other word you cannot add again the arcs deleted |
| 395 | 395 |
///by restore(). |
| 396 | 396 |
void restore() |
| 397 | 397 |
{
|
| 398 | 398 |
_graph->restoreSnapshot(*this); |
| 399 | 399 |
} |
| 400 | 400 |
}; |
| 401 | 401 |
}; |
| 402 | 402 |
|
| 403 | 403 |
|
| 404 | 404 |
class SmartGraphBase {
|
| 405 | 405 |
|
| 406 | 406 |
protected: |
| 407 | 407 |
|
| 408 | 408 |
struct NodeT {
|
| 409 | 409 |
int first_out; |
| 410 | 410 |
}; |
| 411 | 411 |
|
| 412 | 412 |
struct ArcT {
|
| 413 | 413 |
int target; |
| 414 | 414 |
int next_out; |
| 415 | 415 |
}; |
| 416 | 416 |
|
| 417 | 417 |
std::vector<NodeT> nodes; |
| 418 | 418 |
std::vector<ArcT> arcs; |
| 419 | 419 |
|
| 420 | 420 |
int first_free_arc; |
| 421 | 421 |
|
| 422 | 422 |
public: |
| 423 | 423 |
|
| 424 | 424 |
typedef SmartGraphBase Digraph; |
| 425 | 425 |
|
| 426 | 426 |
class Node; |
| 427 | 427 |
class Arc; |
| 428 | 428 |
class Edge; |
| 429 | 429 |
|
| 430 | 430 |
class Node {
|
| 431 | 431 |
friend class SmartGraphBase; |
| 432 | 432 |
protected: |
| 433 | 433 |
|
| 434 | 434 |
int _id; |
| 435 | 435 |
explicit Node(int id) { _id = id;}
|
| 436 | 436 |
|
| 437 | 437 |
public: |
| 438 | 438 |
Node() {}
|
| 439 | 439 |
Node (Invalid) { _id = -1; }
|
| 440 | 440 |
bool operator==(const Node& node) const {return _id == node._id;}
|
| 441 | 441 |
bool operator!=(const Node& node) const {return _id != node._id;}
|
| 442 | 442 |
bool operator<(const Node& node) const {return _id < node._id;}
|
| 443 | 443 |
}; |
| 444 | 444 |
|
| 445 | 445 |
class Edge {
|
| 446 | 446 |
friend class SmartGraphBase; |
| 447 | 447 |
protected: |
| 448 | 448 |
|
| 449 | 449 |
int _id; |
| 450 | 450 |
explicit Edge(int id) { _id = id;}
|
| 451 | 451 |
|
| 452 | 452 |
public: |
| 453 | 453 |
Edge() {}
|
| 454 | 454 |
Edge (Invalid) { _id = -1; }
|
| 455 | 455 |
bool operator==(const Edge& arc) const {return _id == arc._id;}
|
| 456 | 456 |
bool operator!=(const Edge& arc) const {return _id != arc._id;}
|
| 457 | 457 |
bool operator<(const Edge& arc) const {return _id < arc._id;}
|
| 458 | 458 |
}; |
| 459 | 459 |
|
| 460 | 460 |
class Arc {
|
| 461 | 461 |
friend class SmartGraphBase; |
| 462 | 462 |
protected: |
| 463 | 463 |
|
| 464 | 464 |
int _id; |
| 465 | 465 |
explicit Arc(int id) { _id = id;}
|
| 466 | 466 |
|
| 467 | 467 |
public: |
| 468 |
operator Edge() const {
|
|
| 468 |
operator Edge() const {
|
|
| 469 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
|
| 470 |
} |
|
| 469 | 471 |
|
| 470 | 472 |
Arc() {}
|
| 471 | 473 |
Arc (Invalid) { _id = -1; }
|
| 472 | 474 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 473 | 475 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 474 | 476 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 475 | 477 |
}; |
| 476 | 478 |
|
| 477 | 479 |
|
| 478 | 480 |
|
| 479 | 481 |
SmartGraphBase() |
| 480 | 482 |
: nodes(), arcs() {}
|
| 481 | 483 |
|
| 482 | 484 |
|
| 483 | 485 |
int maxNodeId() const { return nodes.size()-1; }
|
| 484 | 486 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 485 | 487 |
int maxArcId() const { return arcs.size()-1; }
|
| 486 | 488 |
|
| 487 | 489 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 488 | 490 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 489 | 491 |
|
| 490 | 492 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 491 | 493 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 492 | 494 |
|
| 493 | 495 |
static bool direction(Arc e) {
|
| 494 | 496 |
return (e._id & 1) == 1; |
| 495 | 497 |
} |
| 496 | 498 |
|
| 497 | 499 |
static Arc direct(Edge e, bool d) {
|
| 498 | 500 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
| 499 | 501 |
} |
| 500 | 502 |
|
| 501 | 503 |
void first(Node& node) const {
|
| 502 | 504 |
node._id = nodes.size() - 1; |
| 503 | 505 |
} |
| 504 | 506 |
|
| 505 | 507 |
void next(Node& node) const {
|
| 506 | 508 |
--node._id; |
| 507 | 509 |
} |
| 508 | 510 |
|
| 509 | 511 |
void first(Arc& arc) const {
|
| 510 | 512 |
arc._id = arcs.size() - 1; |
| 511 | 513 |
} |
| 512 | 514 |
|
| 513 | 515 |
void next(Arc& arc) const {
|
| 514 | 516 |
--arc._id; |
| 515 | 517 |
} |
| 516 | 518 |
|
| 517 | 519 |
void first(Edge& arc) const {
|
| 518 | 520 |
arc._id = arcs.size() / 2 - 1; |
| 519 | 521 |
} |
| 520 | 522 |
|
| 521 | 523 |
void next(Edge& arc) const {
|
| 522 | 524 |
--arc._id; |
| 523 | 525 |
} |
| 524 | 526 |
|
| 525 | 527 |
void firstOut(Arc &arc, const Node& v) const {
|
| 526 | 528 |
arc._id = nodes[v._id].first_out; |
| 527 | 529 |
} |
| 528 | 530 |
void nextOut(Arc &arc) const {
|
| 529 | 531 |
arc._id = arcs[arc._id].next_out; |
| 530 | 532 |
} |
| 531 | 533 |
|
| 532 | 534 |
void firstIn(Arc &arc, const Node& v) const {
|
| 533 | 535 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
| 534 | 536 |
if (arc._id == -2) arc._id = -1; |
| 535 | 537 |
} |
| 536 | 538 |
void nextIn(Arc &arc) const {
|
| 537 | 539 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
| 538 | 540 |
if (arc._id == -2) arc._id = -1; |
| 539 | 541 |
} |
| 540 | 542 |
|
| 541 | 543 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 542 | 544 |
int de = nodes[v._id].first_out; |
| 543 | 545 |
if (de != -1) {
|
| 544 | 546 |
arc._id = de / 2; |
| 545 | 547 |
d = ((de & 1) == 1); |
| 546 | 548 |
} else {
|
| 547 | 549 |
arc._id = -1; |
| 548 | 550 |
d = true; |
| 549 | 551 |
} |
| 550 | 552 |
} |
| 551 | 553 |
void nextInc(Edge &arc, bool& d) const {
|
| 552 | 554 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 553 | 555 |
if (de != -1) {
|
| 554 | 556 |
arc._id = de / 2; |
| 555 | 557 |
d = ((de & 1) == 1); |
| 556 | 558 |
} else {
|
| 557 | 559 |
arc._id = -1; |
| 558 | 560 |
d = true; |
| 559 | 561 |
} |
| 560 | 562 |
} |
| 561 | 563 |
|
| 562 | 564 |
static int id(Node v) { return v._id; }
|
| 563 | 565 |
static int id(Arc e) { return e._id; }
|
| 564 | 566 |
static int id(Edge e) { return e._id; }
|
0 comments (0 inline)