gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Bug fix in GraphCopy (ticket #117)
0 1 0
default
1 file changed with 6 insertions and 5 deletions:
↑ Collapse diff ↑
Show white space 192 line context
... ...
@@ -535,194 +535,194 @@
535 535
        for (ItemIt it(digraph); it != INVALID; ++it) {
536 536
          _tmap.set(refMap[it], _map[it]);
537 537
        }
538 538
      }
539 539

	
540 540
    private:
541 541
      ToMap& _tmap;
542 542
      const FromMap& _map;
543 543
    };
544 544

	
545 545
    template <typename Digraph, typename Item, typename RefMap, typename It>
546 546
    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
547 547
    public:
548 548

	
549 549
      ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
550 550
      
551 551
      virtual void copy(const Digraph&, const RefMap& refMap) {
552 552
        _it = refMap[_item];
553 553
      }
554 554

	
555 555
    private:
556 556
      It& _it;
557 557
      Item _item;
558 558
    };
559 559

	
560 560
    template <typename Digraph, typename Item, typename RefMap, typename Ref>
561 561
    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
562 562
    public:
563 563

	
564 564
      RefCopy(Ref& map) : _map(map) {}
565 565
      
566 566
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
567 567
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
568 568
        for (ItemIt it(digraph); it != INVALID; ++it) {
569 569
          _map.set(it, refMap[it]);
570 570
        }
571 571
      }
572 572

	
573 573
    private:
574 574
      Ref& _map;
575 575
    };
576 576

	
577 577
    template <typename Digraph, typename Item, typename RefMap, 
578 578
              typename CrossRef>
579 579
    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
580 580
    public:
581 581

	
582 582
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
583 583
      
584 584
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
585 585
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
586 586
        for (ItemIt it(digraph); it != INVALID; ++it) {
587 587
          _cmap.set(refMap[it], it);
588 588
        }
589 589
      }
590 590

	
591 591
    private:
592 592
      CrossRef& _cmap;
593 593
    };
594 594

	
595 595
    template <typename Digraph, typename Enable = void>
596 596
    struct DigraphCopySelector {
597 597
      template <typename From, typename NodeRefMap, typename ArcRefMap>
598 598
      static void copy(Digraph &to, const From& from,
599 599
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
600 600
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
601 601
          nodeRefMap[it] = to.addNode();
602 602
        }
603 603
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
604 604
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], 
605 605
                                          nodeRefMap[from.target(it)]);
606 606
        }
607 607
      }
608 608
    };
609 609

	
610 610
    template <typename Digraph>
611 611
    struct DigraphCopySelector<
612 612
      Digraph, 
613 613
      typename enable_if<typename Digraph::BuildTag, void>::type> 
614 614
    {
615 615
      template <typename From, typename NodeRefMap, typename ArcRefMap>
616 616
      static void copy(Digraph &to, const From& from,
617 617
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
618 618
        to.build(from, nodeRefMap, arcRefMap);
619 619
      }
620 620
    };
621 621

	
622 622
    template <typename Graph, typename Enable = void>
623 623
    struct GraphCopySelector {
624 624
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
625 625
      static void copy(Graph &to, const From& from,
626 626
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
627 627
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
628 628
          nodeRefMap[it] = to.addNode();
629 629
        }
630 630
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
631
          edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], 
632
				       nodeRefMap[from.target(it)]);
631
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], 
632
				      nodeRefMap[from.v(it)]);
633 633
        }
634 634
      }
635 635
    };
636 636

	
637 637
    template <typename Graph>
638 638
    struct GraphCopySelector<
639 639
      Graph, 
640 640
      typename enable_if<typename Graph::BuildTag, void>::type> 
641 641
    {
642 642
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
643 643
      static void copy(Graph &to, const From& from,
644 644
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
645 645
        to.build(from, nodeRefMap, edgeRefMap);
646 646
      }
647 647
    };
648 648

	
649 649
  }
650 650

	
651 651
  /// \brief Class to copy a digraph.
652 652
  ///
653 653
  /// Class to copy a digraph to another digraph (duplicate a digraph). The
654 654
  /// simplest way of using it is through the \c copyDigraph() function.
655 655
  ///
656 656
  /// This class not just make a copy of a graph, but it can create
657 657
  /// references and cross references between the nodes and arcs of
658 658
  /// the two graphs, it can copy maps for use with the newly created
659 659
  /// graph and copy nodes and arcs.
660 660
  ///
661 661
  /// To make a copy from a graph, first an instance of DigraphCopy
662 662
  /// should be created, then the data belongs to the graph should
663 663
  /// assigned to copy. In the end, the \c run() member should be
664 664
  /// called.
665 665
  ///
666 666
  /// The next code copies a graph with several data:
667 667
  ///\code
668 668
  ///  DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
669 669
  ///  // create a reference for the nodes
670 670
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
671 671
  ///  dc.nodeRef(nr);
672 672
  ///  // create a cross reference (inverse) for the arcs
673 673
  ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
674 674
  ///  dc.arcCrossRef(acr);
675 675
  ///  // copy an arc map
676 676
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
677 677
  ///  NewGraph::ArcMap<double> namap(new_graph);
678 678
  ///  dc.arcMap(namap, oamap);
679 679
  ///  // copy a node
680 680
  ///  OrigGraph::Node on;
681 681
  ///  NewGraph::Node nn;
682 682
  ///  dc.node(nn, on);
683 683
  ///  // Executions of copy
684 684
  ///  dc.run();
685 685
  ///\endcode
686 686
  template <typename To, typename From>
687 687
  class DigraphCopy {
688 688
  private:
689 689

	
690 690
    typedef typename From::Node Node;
691 691
    typedef typename From::NodeIt NodeIt;
692 692
    typedef typename From::Arc Arc;
693 693
    typedef typename From::ArcIt ArcIt;
694 694

	
695 695
    typedef typename To::Node TNode;
696 696
    typedef typename To::Arc TArc;
697 697

	
698 698
    typedef typename From::template NodeMap<TNode> NodeRefMap;
699 699
    typedef typename From::template ArcMap<TArc> ArcRefMap;
700 700
    
701 701
    
702 702
  public: 
703 703

	
704 704

	
705 705
    /// \brief Constructor for the DigraphCopy.
706 706
    ///
707 707
    /// It copies the content of the \c _from digraph into the
708 708
    /// \c _to digraph.
709 709
    DigraphCopy(To& to, const From& from) 
710 710
      : _from(from), _to(to) {}
711 711

	
712 712
    /// \brief Destructor of the DigraphCopy
713 713
    ///
714 714
    /// Destructor of the DigraphCopy
715 715
    ~DigraphCopy() {
716 716
      for (int i = 0; i < int(_node_maps.size()); ++i) {
717 717
        delete _node_maps[i];
718 718
      }
719 719
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
720 720
        delete _arc_maps[i];
721 721
      }
722 722

	
723 723
    }
724 724

	
725 725
    /// \brief Copies the node references into the given map.
726 726
    ///
727 727
    /// Copies the node references into the given map. The parameter
728 728
    /// should be a map, which key type is the Node type of the source
... ...
@@ -832,195 +832,196 @@
832 832

	
833 833

	
834 834
    const From& _from;
835 835
    To& _to;
836 836

	
837 837
    std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
838 838
    _node_maps;
839 839

	
840 840
    std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > 
841 841
    _arc_maps;
842 842

	
843 843
  };
844 844

	
845 845
  /// \brief Copy a digraph to another digraph.
846 846
  ///
847 847
  /// Copy a digraph to another digraph. The complete usage of the
848 848
  /// function is detailed in the DigraphCopy class, but a short
849 849
  /// example shows a basic work:
850 850
  ///\code
851 851
  /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
852 852
  ///\endcode
853 853
  /// 
854 854
  /// After the copy the \c nr map will contain the mapping from the
855 855
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
856 856
  /// \c ecr will contain the mapping from the arcs of the \c to digraph
857 857
  /// to the arcs of the \c from digraph.
858 858
  ///
859 859
  /// \see DigraphCopy 
860 860
  template <typename To, typename From>
861 861
  DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
862 862
    return DigraphCopy<To, From>(to, from);
863 863
  }
864 864

	
865 865
  /// \brief Class to copy a graph.
866 866
  ///
867 867
  /// Class to copy a graph to another graph (duplicate a graph). The
868 868
  /// simplest way of using it is through the \c copyGraph() function.
869 869
  ///
870 870
  /// This class not just make a copy of a graph, but it can create
871 871
  /// references and cross references between the nodes, edges and arcs of
872 872
  /// the two graphs, it can copy maps for use with the newly created
873 873
  /// graph and copy nodes, edges and arcs.
874 874
  ///
875 875
  /// To make a copy from a graph, first an instance of GraphCopy
876 876
  /// should be created, then the data belongs to the graph should
877 877
  /// assigned to copy. In the end, the \c run() member should be
878 878
  /// called.
879 879
  ///
880 880
  /// The next code copies a graph with several data:
881 881
  ///\code
882 882
  ///  GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
883 883
  ///  // create a reference for the nodes
884 884
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
885 885
  ///  dc.nodeRef(nr);
886 886
  ///  // create a cross reference (inverse) for the edges
887 887
  ///  NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph);
888 888
  ///  dc.edgeCrossRef(ecr);
889 889
  ///  // copy an arc map
890 890
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
891 891
  ///  NewGraph::ArcMap<double> namap(new_graph);
892 892
  ///  dc.arcMap(namap, oamap);
893 893
  ///  // copy a node
894 894
  ///  OrigGraph::Node on;
895 895
  ///  NewGraph::Node nn;
896 896
  ///  dc.node(nn, on);
897 897
  ///  // Executions of copy
898 898
  ///  dc.run();
899 899
  ///\endcode
900 900
  template <typename To, typename From>
901 901
  class GraphCopy {
902 902
  private:
903 903

	
904 904
    typedef typename From::Node Node;
905 905
    typedef typename From::NodeIt NodeIt;
906 906
    typedef typename From::Arc Arc;
907 907
    typedef typename From::ArcIt ArcIt;
908 908
    typedef typename From::Edge Edge;
909 909
    typedef typename From::EdgeIt EdgeIt;
910 910

	
911 911
    typedef typename To::Node TNode;
912 912
    typedef typename To::Arc TArc;
913 913
    typedef typename To::Edge TEdge;
914 914

	
915 915
    typedef typename From::template NodeMap<TNode> NodeRefMap;
916 916
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
917 917

	
918 918
    struct ArcRefMap {
919 919
      ArcRefMap(const To& to, const From& from,
920 920
		const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) 
921 921
        : _to(to), _from(from), 
922 922
          _edge_ref(edge_ref), _node_ref(node_ref) {}
923 923

	
924 924
      typedef typename From::Arc Key;
925 925
      typedef typename To::Arc Value;
926 926

	
927 927
      Value operator[](const Key& key) const {
928
        bool forward = 
929
          (_from.direction(key) == 
930
	   (_node_ref[_from.source(key)] == _to.source(_edge_ref[key])));
928
        bool forward = _from.u(key) != _from.v(key) ?
929
	  _node_ref[_from.source(key)] == 
930
	  _to.source(_to.direct(_edge_ref[key], true)) :
931
	  _from.direction(key);
931 932
	return _to.direct(_edge_ref[key], forward); 
932 933
      }
933 934
      
934 935
      const To& _to;
935 936
      const From& _from;
936 937
      const EdgeRefMap& _edge_ref;
937 938
      const NodeRefMap& _node_ref;
938 939
    };
939 940

	
940 941
    
941 942
  public: 
942 943

	
943 944

	
944 945
    /// \brief Constructor for the GraphCopy.
945 946
    ///
946 947
    /// It copies the content of the \c _from graph into the
947 948
    /// \c _to graph.
948 949
    GraphCopy(To& to, const From& from) 
949 950
      : _from(from), _to(to) {}
950 951

	
951 952
    /// \brief Destructor of the GraphCopy
952 953
    ///
953 954
    /// Destructor of the GraphCopy
954 955
    ~GraphCopy() {
955 956
      for (int i = 0; i < int(_node_maps.size()); ++i) {
956 957
        delete _node_maps[i];
957 958
      }
958 959
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
959 960
        delete _arc_maps[i];
960 961
      }
961 962
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
962 963
        delete _edge_maps[i];
963 964
      }
964 965

	
965 966
    }
966 967

	
967 968
    /// \brief Copies the node references into the given map.
968 969
    ///
969 970
    /// Copies the node references into the given map.
970 971
    template <typename NodeRef>
971 972
    GraphCopy& nodeRef(NodeRef& map) {
972 973
      _node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, 
973 974
			   NodeRefMap, NodeRef>(map));
974 975
      return *this;
975 976
    }
976 977

	
977 978
    /// \brief Copies the node cross references into the given map.
978 979
    ///
979 980
    ///  Copies the node cross references (reverse references) into
980 981
    ///  the given map.
981 982
    template <typename NodeCrossRef>
982 983
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
983 984
      _node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
984 985
			   NodeRefMap, NodeCrossRef>(map));
985 986
      return *this;
986 987
    }
987 988

	
988 989
    /// \brief Make copy of the given map.
989 990
    ///
990 991
    /// Makes copy of the given map for the newly created graph. 
991 992
    /// The new map's key type is the to graph's node type,
992 993
    /// and the copied map's key type is the from graph's node
993 994
    /// type.  
994 995
    template <typename ToMap, typename FromMap>
995 996
    GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
996 997
      _node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, 
997 998
			   NodeRefMap, ToMap, FromMap>(tmap, map));
998 999
      return *this;
999 1000
    }
1000 1001

	
1001 1002
    /// \brief Make a copy of the given node.
1002 1003
    ///
1003 1004
    /// Make a copy of the given node.
1004 1005
    GraphCopy& node(TNode& tnode, const Node& snode) {
1005 1006
      _node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, 
1006 1007
			   NodeRefMap, TNode>(tnode, snode));
1007 1008
      return *this;
1008 1009
    }
1009 1010

	
1010 1011
    /// \brief Copies the arc references into the given map.
1011 1012
    ///
1012 1013
    /// Copies the arc references into the given map.
1013 1014
    template <typename ArcRef>
1014 1015
    GraphCopy& arcRef(ArcRef& map) {
1015 1016
      _arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, 
1016 1017
			  ArcRefMap, ArcRef>(map));
1017 1018
      return *this;
1018 1019
    }
1019 1020

	
1020 1021
    /// \brief Copies the arc cross references into the given map.
1021 1022
    ///
1022 1023
    ///  Copies the arc cross references (reverse references) into
1023 1024
    ///  the given map.
1024 1025
    template <typename ArcCrossRef>
1025 1026
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
1026 1027
      _arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc,
0 comments (0 inline)