gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Converting INVALID arc to INVALID edge
0 2 0
default
2 files changed with 6 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -589,513 +589,515 @@
589 589
            snapshot.addNode(nodes[i]);
590 590
          }
591 591
        }
592 592
        virtual void clear() {
593 593
          Node node;
594 594
          for (notifier()->first(node); node != INVALID;
595 595
               notifier()->next(node)) {
596 596
            snapshot.eraseNode(node);
597 597
          }
598 598
        }
599 599

	
600 600
        Snapshot& snapshot;
601 601
      };
602 602

	
603 603
      class ArcObserverProxy : public ArcNotifier::ObserverBase {
604 604
      public:
605 605

	
606 606
        ArcObserverProxy(Snapshot& _snapshot)
607 607
          : snapshot(_snapshot) {}
608 608

	
609 609
        using ArcNotifier::ObserverBase::attach;
610 610
        using ArcNotifier::ObserverBase::detach;
611 611
        using ArcNotifier::ObserverBase::attached;
612 612

	
613 613
      protected:
614 614

	
615 615
        virtual void add(const Arc& arc) {
616 616
          snapshot.addArc(arc);
617 617
        }
618 618
        virtual void add(const std::vector<Arc>& arcs) {
619 619
          for (int i = arcs.size() - 1; i >= 0; ++i) {
620 620
            snapshot.addArc(arcs[i]);
621 621
          }
622 622
        }
623 623
        virtual void erase(const Arc& arc) {
624 624
          snapshot.eraseArc(arc);
625 625
        }
626 626
        virtual void erase(const std::vector<Arc>& arcs) {
627 627
          for (int i = 0; i < int(arcs.size()); ++i) {
628 628
            snapshot.eraseArc(arcs[i]);
629 629
          }
630 630
        }
631 631
        virtual void build() {
632 632
          Arc arc;
633 633
          std::vector<Arc> arcs;
634 634
          for (notifier()->first(arc); arc != INVALID;
635 635
               notifier()->next(arc)) {
636 636
            arcs.push_back(arc);
637 637
          }
638 638
          for (int i = arcs.size() - 1; i >= 0; --i) {
639 639
            snapshot.addArc(arcs[i]);
640 640
          }
641 641
        }
642 642
        virtual void clear() {
643 643
          Arc arc;
644 644
          for (notifier()->first(arc); arc != INVALID;
645 645
               notifier()->next(arc)) {
646 646
            snapshot.eraseArc(arc);
647 647
          }
648 648
        }
649 649

	
650 650
        Snapshot& snapshot;
651 651
      };
652 652

	
653 653
      ListDigraph *digraph;
654 654

	
655 655
      NodeObserverProxy node_observer_proxy;
656 656
      ArcObserverProxy arc_observer_proxy;
657 657

	
658 658
      std::list<Node> added_nodes;
659 659
      std::list<Arc> added_arcs;
660 660

	
661 661

	
662 662
      void addNode(const Node& node) {
663 663
        added_nodes.push_front(node);
664 664
      }
665 665
      void eraseNode(const Node& node) {
666 666
        std::list<Node>::iterator it =
667 667
          std::find(added_nodes.begin(), added_nodes.end(), node);
668 668
        if (it == added_nodes.end()) {
669 669
          clear();
670 670
          arc_observer_proxy.detach();
671 671
          throw NodeNotifier::ImmediateDetach();
672 672
        } else {
673 673
          added_nodes.erase(it);
674 674
        }
675 675
      }
676 676

	
677 677
      void addArc(const Arc& arc) {
678 678
        added_arcs.push_front(arc);
679 679
      }
680 680
      void eraseArc(const Arc& arc) {
681 681
        std::list<Arc>::iterator it =
682 682
          std::find(added_arcs.begin(), added_arcs.end(), arc);
683 683
        if (it == added_arcs.end()) {
684 684
          clear();
685 685
          node_observer_proxy.detach();
686 686
          throw ArcNotifier::ImmediateDetach();
687 687
        } else {
688 688
          added_arcs.erase(it);
689 689
        }
690 690
      }
691 691

	
692 692
      void attach(ListDigraph &_digraph) {
693 693
        digraph = &_digraph;
694 694
        node_observer_proxy.attach(digraph->notifier(Node()));
695 695
        arc_observer_proxy.attach(digraph->notifier(Arc()));
696 696
      }
697 697

	
698 698
      void detach() {
699 699
        node_observer_proxy.detach();
700 700
        arc_observer_proxy.detach();
701 701
      }
702 702

	
703 703
      bool attached() const {
704 704
        return node_observer_proxy.attached();
705 705
      }
706 706

	
707 707
      void clear() {
708 708
        added_nodes.clear();
709 709
        added_arcs.clear();
710 710
      }
711 711

	
712 712
    public:
713 713

	
714 714
      /// \brief Default constructor.
715 715
      ///
716 716
      /// Default constructor.
717 717
      /// To actually make a snapshot you must call save().
718 718
      Snapshot()
719 719
        : digraph(0), node_observer_proxy(*this),
720 720
          arc_observer_proxy(*this) {}
721 721

	
722 722
      /// \brief Constructor that immediately makes a snapshot.
723 723
      ///
724 724
      /// This constructor immediately makes a snapshot of the digraph.
725 725
      /// \param _digraph The digraph we make a snapshot of.
726 726
      Snapshot(ListDigraph &_digraph)
727 727
        : node_observer_proxy(*this),
728 728
          arc_observer_proxy(*this) {
729 729
        attach(_digraph);
730 730
      }
731 731

	
732 732
      /// \brief Make a snapshot.
733 733
      ///
734 734
      /// Make a snapshot of the digraph.
735 735
      ///
736 736
      /// This function can be called more than once. In case of a repeated
737 737
      /// call, the previous snapshot gets lost.
738 738
      /// \param _digraph The digraph we make the snapshot of.
739 739
      void save(ListDigraph &_digraph) {
740 740
        if (attached()) {
741 741
          detach();
742 742
          clear();
743 743
        }
744 744
        attach(_digraph);
745 745
      }
746 746

	
747 747
      /// \brief Undo the changes until the last snapshot.
748 748
      //
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 { return edgeFromId(id / 2); }
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;
942 944
        }
943 945
        n = nodes[n].next;
944 946
      }
945 947
      e.id = -1;
946 948
    }
947 949

	
948 950
    void firstOut(Arc &e, const Node& v) const {
949 951
      e.id = nodes[v.id].first_out;
950 952
    }
951 953
    void nextOut(Arc &e) const {
952 954
      e.id = arcs[e.id].next_out;
953 955
    }
954 956

	
955 957
    void firstIn(Arc &e, const Node& v) const {
956 958
      e.id = ((nodes[v.id].first_out) ^ 1);
957 959
      if (e.id == -2) e.id = -1;
958 960
    }
959 961
    void nextIn(Arc &e) const {
960 962
      e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
961 963
      if (e.id == -2) e.id = -1;
962 964
    }
963 965

	
964 966
    void firstInc(Edge &e, bool& d, const Node& v) const {
965 967
      int a = nodes[v.id].first_out;
966 968
      if (a != -1 ) {
967 969
        e.id = a / 2;
968 970
        d = ((a & 1) == 1);
969 971
      } else {
970 972
        e.id = -1;
971 973
        d = true;
972 974
      }
973 975
    }
974 976
    void nextInc(Edge &e, bool& d) const {
975 977
      int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
976 978
      if (a != -1 ) {
977 979
        e.id = a / 2;
978 980
        d = ((a & 1) == 1);
979 981
      } else {
980 982
        e.id = -1;
981 983
        d = true;
982 984
      }
983 985
    }
984 986

	
985 987
    static int id(Node v) { return v.id; }
986 988
    static int id(Arc e) { return e.id; }
987 989
    static int id(Edge e) { return e.id; }
988 990

	
989 991
    static Node nodeFromId(int id) { return Node(id);}
990 992
    static Arc arcFromId(int id) { return Arc(id);}
991 993
    static Edge edgeFromId(int id) { return Edge(id);}
992 994

	
993 995
    bool valid(Node n) const {
994 996
      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
995 997
        nodes[n.id].prev != -2;
996 998
    }
997 999

	
998 1000
    bool valid(Arc a) const {
999 1001
      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
1000 1002
        arcs[a.id].prev_out != -2;
1001 1003
    }
1002 1004

	
1003 1005
    bool valid(Edge e) const {
1004 1006
      return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) &&
1005 1007
        arcs[2 * e.id].prev_out != -2;
1006 1008
    }
1007 1009

	
1008 1010
    Node addNode() {
1009 1011
      int n;
1010 1012

	
1011 1013
      if(first_free_node==-1) {
1012 1014
        n = nodes.size();
1013 1015
        nodes.push_back(NodeT());
1014 1016
      } else {
1015 1017
        n = first_free_node;
1016 1018
        first_free_node = nodes[n].next;
1017 1019
      }
1018 1020

	
1019 1021
      nodes[n].next = first_node;
1020 1022
      if (first_node != -1) nodes[first_node].prev = n;
1021 1023
      first_node = n;
1022 1024
      nodes[n].prev = -1;
1023 1025

	
1024 1026
      nodes[n].first_out = -1;
1025 1027

	
1026 1028
      return Node(n);
1027 1029
    }
1028 1030

	
1029 1031
    Edge addEdge(Node u, Node v) {
1030 1032
      int n;
1031 1033

	
1032 1034
      if (first_free_arc == -1) {
1033 1035
        n = arcs.size();
1034 1036
        arcs.push_back(ArcT());
1035 1037
        arcs.push_back(ArcT());
1036 1038
      } else {
1037 1039
        n = first_free_arc;
1038 1040
        first_free_arc = arcs[n].next_out;
1039 1041
      }
1040 1042

	
1041 1043
      arcs[n].target = u.id;
1042 1044
      arcs[n | 1].target = v.id;
1043 1045

	
1044 1046
      arcs[n].next_out = nodes[v.id].first_out;
1045 1047
      if (nodes[v.id].first_out != -1) {
1046 1048
        arcs[nodes[v.id].first_out].prev_out = n;
1047 1049
      }
1048 1050
      arcs[n].prev_out = -1;
1049 1051
      nodes[v.id].first_out = n;
1050 1052

	
1051 1053
      arcs[n | 1].next_out = nodes[u.id].first_out;
1052 1054
      if (nodes[u.id].first_out != -1) {
1053 1055
        arcs[nodes[u.id].first_out].prev_out = (n | 1);
1054 1056
      }
1055 1057
      arcs[n | 1].prev_out = -1;
1056 1058
      nodes[u.id].first_out = (n | 1);
1057 1059

	
1058 1060
      return Edge(n / 2);
1059 1061
    }
1060 1062

	
1061 1063
    void erase(const Node& node) {
1062 1064
      int n = node.id;
1063 1065

	
1064 1066
      if(nodes[n].next != -1) {
1065 1067
        nodes[nodes[n].next].prev = nodes[n].prev;
1066 1068
      }
1067 1069

	
1068 1070
      if(nodes[n].prev != -1) {
1069 1071
        nodes[nodes[n].prev].next = nodes[n].next;
1070 1072
      } else {
1071 1073
        first_node = nodes[n].next;
1072 1074
      }
1073 1075

	
1074 1076
      nodes[n].next = first_free_node;
1075 1077
      first_free_node = n;
1076 1078
      nodes[n].prev = -2;
1077 1079
    }
1078 1080

	
1079 1081
    void erase(const Edge& edge) {
1080 1082
      int n = edge.id * 2;
1081 1083

	
1082 1084
      if (arcs[n].next_out != -1) {
1083 1085
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
1084 1086
      }
1085 1087

	
1086 1088
      if (arcs[n].prev_out != -1) {
1087 1089
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
1088 1090
      } else {
1089 1091
        nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
1090 1092
      }
1091 1093

	
1092 1094
      if (arcs[n | 1].next_out != -1) {
1093 1095
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
1094 1096
      }
1095 1097

	
1096 1098
      if (arcs[n | 1].prev_out != -1) {
1097 1099
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
1098 1100
      } else {
1099 1101
        nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
1100 1102
      }
1101 1103

	
Ignore white space 512 line context
... ...
@@ -212,513 +212,515 @@
212 212
    ///Use DigraphCopy() instead.
213 213

	
214 214
    ///Assignment of SmartDigraph to another one is \e not allowed.
215 215
    ///Use DigraphCopy() instead.
216 216
    void operator=(const SmartDigraph &) {}
217 217

	
218 218
  public:
219 219

	
220 220
    /// Constructor
221 221

	
222 222
    /// Constructor.
223 223
    ///
224 224
    SmartDigraph() {};
225 225

	
226 226
    ///Add a new node to the digraph.
227 227

	
228 228
    /// \return the new node.
229 229
    ///
230 230
    Node addNode() { return Parent::addNode(); }
231 231

	
232 232
    ///Add a new arc to the digraph.
233 233

	
234 234
    ///Add a new arc to the digraph with source node \c s
235 235
    ///and target node \c t.
236 236
    ///\return the new arc.
237 237
    Arc addArc(const Node& s, const Node& t) {
238 238
      return Parent::addArc(s, t);
239 239
    }
240 240

	
241 241
    /// \brief Using this it is possible to avoid the superfluous memory
242 242
    /// allocation.
243 243

	
244 244
    /// Using this it is possible to avoid the superfluous memory
245 245
    /// allocation: if you know that the digraph you want to build will
246 246
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
247 247
    /// then it is worth reserving space for this amount before starting
248 248
    /// to build the digraph.
249 249
    /// \sa reserveArc
250 250
    void reserveNode(int n) { nodes.reserve(n); };
251 251

	
252 252
    /// \brief Using this it is possible to avoid the superfluous memory
253 253
    /// allocation.
254 254

	
255 255
    /// Using this it is possible to avoid the superfluous memory
256 256
    /// allocation: if you know that the digraph you want to build will
257 257
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
258 258
    /// then it is worth reserving space for this amount before starting
259 259
    /// to build the digraph.
260 260
    /// \sa reserveNode
261 261
    void reserveArc(int m) { arcs.reserve(m); };
262 262

	
263 263
    /// \brief Node validity check
264 264
    ///
265 265
    /// This function gives back true if the given node is valid,
266 266
    /// ie. it is a real node of the graph.
267 267
    ///
268 268
    /// \warning A removed node (using Snapshot) could become valid again
269 269
    /// when new nodes are added to the graph.
270 270
    bool valid(Node n) const { return Parent::valid(n); }
271 271

	
272 272
    /// \brief Arc validity check
273 273
    ///
274 274
    /// This function gives back true if the given arc is valid,
275 275
    /// ie. it is a real arc of the graph.
276 276
    ///
277 277
    /// \warning A removed arc (using Snapshot) could become valid again
278 278
    /// when new arcs are added to the graph.
279 279
    bool valid(Arc a) const { return Parent::valid(a); }
280 280

	
281 281
    ///Clear the digraph.
282 282

	
283 283
    ///Erase all the nodes and arcs from the digraph.
284 284
    ///
285 285
    void clear() {
286 286
      Parent::clear();
287 287
    }
288 288

	
289 289
    ///Split a node.
290 290

	
291 291
    ///This function splits a node. First a new node is added to the digraph,
292 292
    ///then the source of each outgoing arc of \c n is moved to this new node.
293 293
    ///If \c connect is \c true (this is the default value), then a new arc
294 294
    ///from \c n to the newly created node is also added.
295 295
    ///\return The newly created node.
296 296
    ///
297 297
    ///\note The <tt>Arc</tt>s
298 298
    ///referencing a moved arc remain
299 299
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
300 300
    ///may be invalidated.
301 301
    ///\warning This functionality cannot be used together with the Snapshot
302 302
    ///feature.
303 303
    ///\todo It could be implemented in a bit faster way.
304 304
    Node split(Node n, bool connect = true)
305 305
    {
306 306
      Node b = addNode();
307 307
      nodes[b._id].first_out=nodes[n._id].first_out;
308 308
      nodes[n._id].first_out=-1;
309 309
      for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
310 310
      if(connect) addArc(n,b);
311 311
      return b;
312 312
    }
313 313

	
314 314
  public:
315 315

	
316 316
    class Snapshot;
317 317

	
318 318
  protected:
319 319

	
320 320
    void restoreSnapshot(const Snapshot &s)
321 321
    {
322 322
      while(s.arc_num<arcs.size()) {
323 323
        Arc arc = arcFromId(arcs.size()-1);
324 324
        Parent::notifier(Arc()).erase(arc);
325 325
        nodes[arcs.back().source].first_out=arcs.back().next_out;
326 326
        nodes[arcs.back().target].first_in=arcs.back().next_in;
327 327
        arcs.pop_back();
328 328
      }
329 329
      while(s.node_num<nodes.size()) {
330 330
        Node node = nodeFromId(nodes.size()-1);
331 331
        Parent::notifier(Node()).erase(node);
332 332
        nodes.pop_back();
333 333
      }
334 334
    }
335 335

	
336 336
  public:
337 337

	
338 338
    ///Class to make a snapshot of the digraph and to restrore to it later.
339 339

	
340 340
    ///Class to make a snapshot of the digraph and to restrore to it later.
341 341
    ///
342 342
    ///The newly added nodes and arcs can be removed using the
343 343
    ///restore() function.
344 344
    ///\note After you restore a state, you cannot restore
345 345
    ///a later state, in other word you cannot add again the arcs deleted
346 346
    ///by restore() using another one Snapshot instance.
347 347
    ///
348 348
    ///\warning If you do not use correctly the snapshot that can cause
349 349
    ///either broken program, invalid state of the digraph, valid but
350 350
    ///not the restored digraph or no change. Because the runtime performance
351 351
    ///the validity of the snapshot is not stored.
352 352
    class Snapshot
353 353
    {
354 354
      SmartDigraph *_graph;
355 355
    protected:
356 356
      friend class SmartDigraph;
357 357
      unsigned int node_num;
358 358
      unsigned int arc_num;
359 359
    public:
360 360
      ///Default constructor.
361 361

	
362 362
      ///Default constructor.
363 363
      ///To actually make a snapshot you must call save().
364 364
      ///
365 365
      Snapshot() : _graph(0) {}
366 366
      ///Constructor that immediately makes a snapshot
367 367

	
368 368
      ///This constructor immediately makes a snapshot of the digraph.
369 369
      ///\param _g The digraph we make a snapshot of.
370 370
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
371 371
        node_num=_graph->nodes.size();
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 { return edgeFromId(_id / 2); }
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; }
565 567

	
566 568
    static Node nodeFromId(int id) { return Node(id);}
567 569
    static Arc arcFromId(int id) { return Arc(id);}
568 570
    static Edge edgeFromId(int id) { return Edge(id);}
569 571

	
570 572
    bool valid(Node n) const {
571 573
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
572 574
    }
573 575
    bool valid(Arc a) const {
574 576
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
575 577
    }
576 578
    bool valid(Edge e) const {
577 579
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
578 580
    }
579 581

	
580 582
    Node addNode() {
581 583
      int n = nodes.size();
582 584
      nodes.push_back(NodeT());
583 585
      nodes[n].first_out = -1;
584 586

	
585 587
      return Node(n);
586 588
    }
587 589

	
588 590
    Edge addEdge(Node u, Node v) {
589 591
      int n = arcs.size();
590 592
      arcs.push_back(ArcT());
591 593
      arcs.push_back(ArcT());
592 594

	
593 595
      arcs[n].target = u._id;
594 596
      arcs[n | 1].target = v._id;
595 597

	
596 598
      arcs[n].next_out = nodes[v._id].first_out;
597 599
      nodes[v._id].first_out = n;
598 600

	
599 601
      arcs[n | 1].next_out = nodes[u._id].first_out;
600 602
      nodes[u._id].first_out = (n | 1);
601 603

	
602 604
      return Edge(n / 2);
603 605
    }
604 606

	
605 607
    void clear() {
606 608
      arcs.clear();
607 609
      nodes.clear();
608 610
    }
609 611

	
610 612
  };
611 613

	
612 614
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
613 615

	
614 616
  /// \ingroup graphs
615 617
  ///
616 618
  /// \brief A smart undirected graph class.
617 619
  ///
618 620
  /// This is a simple and fast graph implementation.
619 621
  /// It is also quite memory efficient, but at the price
620 622
  /// that <b> it does support only limited (only stack-like)
621 623
  /// node and arc deletions</b>.
622 624
  /// Except from this it conforms to
623 625
  /// the \ref concepts::Graph "Graph concept".
624 626
  ///
625 627
  /// It also has an
626 628
  /// important extra feature that
627 629
  /// its maps are real \ref concepts::ReferenceMap "reference map"s.
628 630
  ///
629 631
  /// \sa concepts::Graph.
630 632
  ///
631 633
  class SmartGraph : public ExtendedSmartGraphBase {
632 634
  private:
633 635

	
634 636
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
635 637

	
636 638
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
637 639
    ///
638 640
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
639 641

	
640 642
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
641 643
    ///Use GraphCopy() instead.
642 644

	
643 645
    ///Assignment of SmartGraph to another one is \e not allowed.
644 646
    ///Use GraphCopy() instead.
645 647
    void operator=(const SmartGraph &) {}
646 648

	
647 649
  public:
648 650

	
649 651
    typedef ExtendedSmartGraphBase Parent;
650 652

	
651 653
    /// Constructor
652 654

	
653 655
    /// Constructor.
654 656
    ///
655 657
    SmartGraph() {}
656 658

	
657 659
    ///Add a new node to the graph.
658 660

	
659 661
    /// \return the new node.
660 662
    ///
661 663
    Node addNode() { return Parent::addNode(); }
662 664

	
663 665
    ///Add a new edge to the graph.
664 666

	
665 667
    ///Add a new edge to the graph with node \c s
666 668
    ///and \c t.
667 669
    ///\return the new edge.
668 670
    Edge addEdge(const Node& s, const Node& t) {
669 671
      return Parent::addEdge(s, t);
670 672
    }
671 673

	
672 674
    /// \brief Node validity check
673 675
    ///
674 676
    /// This function gives back true if the given node is valid,
675 677
    /// ie. it is a real node of the graph.
676 678
    ///
677 679
    /// \warning A removed node (using Snapshot) could become valid again
678 680
    /// when new nodes are added to the graph.
679 681
    bool valid(Node n) const { return Parent::valid(n); }
680 682

	
681 683
    /// \brief Arc validity check
682 684
    ///
683 685
    /// This function gives back true if the given arc is valid,
684 686
    /// ie. it is a real arc of the graph.
685 687
    ///
686 688
    /// \warning A removed arc (using Snapshot) could become valid again
687 689
    /// when new edges are added to the graph.
688 690
    bool valid(Arc a) const { return Parent::valid(a); }
689 691

	
690 692
    /// \brief Edge validity check
691 693
    ///
692 694
    /// This function gives back true if the given edge is valid,
693 695
    /// ie. it is a real edge of the graph.
694 696
    ///
695 697
    /// \warning A removed edge (using Snapshot) could become valid again
696 698
    /// when new edges are added to the graph.
697 699
    bool valid(Edge e) const { return Parent::valid(e); }
698 700

	
699 701
    ///Clear the graph.
700 702

	
701 703
    ///Erase all the nodes and edges from the graph.
702 704
    ///
703 705
    void clear() {
704 706
      Parent::clear();
705 707
    }
706 708

	
707 709
  public:
708 710

	
709 711
    class Snapshot;
710 712

	
711 713
  protected:
712 714

	
713 715
    void saveSnapshot(Snapshot &s)
714 716
    {
715 717
      s._graph = this;
716 718
      s.node_num = nodes.size();
717 719
      s.arc_num = arcs.size();
718 720
    }
719 721

	
720 722
    void restoreSnapshot(const Snapshot &s)
721 723
    {
722 724
      while(s.arc_num<arcs.size()) {
723 725
        int n=arcs.size()-1;
724 726
        Edge arc=edgeFromId(n/2);
0 comments (0 inline)