# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# Date 1222417571 -7200
# Node ID 8ab073e5c4f9475051e71dfb5564c8524241957e
# Parent fe86041c013c5fbc02bf1ac64774204dd415394b
Using from-to parameter order in graph copying tools (ticket #150)
diff --git a/lemon/core.h b/lemon/core.h
a
|
b
|
|
308 | 308 | }; |
309 | 309 | |
310 | 310 | template <typename Digraph, typename Item, typename RefMap, |
311 | | typename ToMap, typename FromMap> |
| 311 | typename FromMap, typename ToMap> |
312 | 312 | class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
313 | 313 | public: |
314 | 314 | |
315 | | MapCopy(ToMap& tmap, const FromMap& map) |
316 | | : _tmap(tmap), _map(map) {} |
| 315 | MapCopy(const FromMap& map, ToMap& tmap) |
| 316 | : _map(map), _tmap(tmap) {} |
317 | 317 | |
318 | 318 | virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
319 | 319 | typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
… |
… |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | private: |
| 326 | const FromMap& _map; |
326 | 327 | ToMap& _tmap; |
327 | | const FromMap& _map; |
328 | 328 | }; |
329 | 329 | |
330 | 330 | template <typename Digraph, typename Item, typename RefMap, typename It> |
331 | 331 | class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
332 | 332 | public: |
333 | 333 | |
334 | | ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
| 334 | ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
335 | 335 | |
336 | 336 | virtual void copy(const Digraph&, const RefMap& refMap) { |
337 | 337 | _it = refMap[_item]; |
338 | 338 | } |
339 | 339 | |
340 | 340 | private: |
| 341 | Item _item; |
341 | 342 | It& _it; |
342 | | Item _item; |
343 | 343 | }; |
344 | 344 | |
345 | 345 | template <typename Digraph, typename Item, typename RefMap, typename Ref> |
… |
… |
|
380 | 380 | template <typename Digraph, typename Enable = void> |
381 | 381 | struct CopyDigraphSelector { |
382 | 382 | template <typename From, typename NodeRefMap, typename ArcRefMap> |
383 | | static void copy(Digraph &to, const From& from, |
| 383 | static void copy(const From& from, Digraph &to, |
384 | 384 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
385 | 385 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
386 | 386 | nodeRefMap[it] = to.addNode(); |
… |
… |
|
398 | 398 | typename enable_if<typename Digraph::BuildTag, void>::type> |
399 | 399 | { |
400 | 400 | template <typename From, typename NodeRefMap, typename ArcRefMap> |
401 | | static void copy(Digraph &to, const From& from, |
| 401 | static void copy(const From& from, Digraph &to, |
402 | 402 | NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
403 | 403 | to.build(from, nodeRefMap, arcRefMap); |
404 | 404 | } |
… |
… |
|
407 | 407 | template <typename Graph, typename Enable = void> |
408 | 408 | struct CopyGraphSelector { |
409 | 409 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
410 | | static void copy(Graph &to, const From& from, |
| 410 | static void copy(const From& from, Graph &to, |
411 | 411 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
412 | 412 | for (typename From::NodeIt it(from); it != INVALID; ++it) { |
413 | 413 | nodeRefMap[it] = to.addNode(); |
… |
… |
|
425 | 425 | typename enable_if<typename Graph::BuildTag, void>::type> |
426 | 426 | { |
427 | 427 | template <typename From, typename NodeRefMap, typename EdgeRefMap> |
428 | | static void copy(Graph &to, const From& from, |
| 428 | static void copy(const From& from, Graph &to, |
429 | 429 | NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
430 | 430 | to.build(from, nodeRefMap, edgeRefMap); |
431 | 431 | } |
… |
… |
|
450 | 450 | /// |
451 | 451 | /// The next code copies a digraph with several data: |
452 | 452 | ///\code |
453 | | /// CopyDigraph<NewGraph, OrigGraph> cg(new_graph, orig_graph); |
| 453 | /// CopyDigraph<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
454 | 454 | /// // Create references for the nodes |
455 | 455 | /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
456 | 456 | /// cg.nodeRef(nr); |
… |
… |
|
460 | 460 | /// // Copy an arc map |
461 | 461 | /// OrigGraph::ArcMap<double> oamap(orig_graph); |
462 | 462 | /// NewGraph::ArcMap<double> namap(new_graph); |
463 | | /// cg.arcMap(namap, oamap); |
| 463 | /// cg.arcMap(oamap, namap); |
464 | 464 | /// // Copy a node |
465 | 465 | /// OrigGraph::Node on; |
466 | 466 | /// NewGraph::Node nn; |
467 | | /// cg.node(nn, on); |
| 467 | /// cg.node(on, nn); |
468 | 468 | /// // Execute copying |
469 | 469 | /// cg.run(); |
470 | 470 | ///\endcode |
471 | | template <typename To, typename From> |
| 471 | template <typename From, typename To> |
472 | 472 | class CopyDigraph { |
473 | 473 | private: |
474 | 474 | |
… |
… |
|
489 | 489 | /// |
490 | 490 | /// Constructor of CopyDigraph for copying the content of the |
491 | 491 | /// \c from digraph into the \c to digraph. |
492 | | CopyDigraph(To& to, const From& from) |
| 492 | CopyDigraph(const From& from, To& to) |
493 | 493 | : _from(from), _to(to) {} |
494 | 494 | |
495 | 495 | /// \brief Destructor of CopyDigraph |
… |
… |
|
538 | 538 | /// The key type of the new map \c tmap should be the Node type of the |
539 | 539 | /// destination digraph, and the key type of the original map \c map |
540 | 540 | /// should be the Node type of the source digraph. |
541 | | template <typename ToMap, typename FromMap> |
542 | | CopyDigraph& nodeMap(ToMap& tmap, const FromMap& map) { |
| 541 | template <typename FromMap, typename ToMap> |
| 542 | CopyDigraph& nodeMap(const FromMap& map, ToMap& tmap) { |
543 | 543 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, |
544 | | NodeRefMap, ToMap, FromMap>(tmap, map)); |
| 544 | NodeRefMap, FromMap, ToMap>(map, tmap)); |
545 | 545 | return *this; |
546 | 546 | } |
547 | 547 | |
548 | 548 | /// \brief Make a copy of the given node. |
549 | 549 | /// |
550 | 550 | /// This function makes a copy of the given node. |
551 | | CopyDigraph& node(TNode& tnode, const Node& node) { |
| 551 | CopyDigraph& node(const Node& node, TNode& tnode) { |
552 | 552 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
553 | | NodeRefMap, TNode>(tnode, node)); |
| 553 | NodeRefMap, TNode>(node, tnode)); |
554 | 554 | return *this; |
555 | 555 | } |
556 | 556 | |
… |
… |
|
587 | 587 | /// The key type of the new map \c tmap should be the Arc type of the |
588 | 588 | /// destination digraph, and the key type of the original map \c map |
589 | 589 | /// should be the Arc type of the source digraph. |
590 | | template <typename ToMap, typename FromMap> |
591 | | CopyDigraph& arcMap(ToMap& tmap, const FromMap& map) { |
| 590 | template <typename FromMap, typename ToMap> |
| 591 | CopyDigraph& arcMap(const FromMap& map, ToMap& tmap) { |
592 | 592 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
593 | | ArcRefMap, ToMap, FromMap>(tmap, map)); |
| 593 | ArcRefMap, FromMap, ToMap>(map, tmap)); |
594 | 594 | return *this; |
595 | 595 | } |
596 | 596 | |
597 | 597 | /// \brief Make a copy of the given arc. |
598 | 598 | /// |
599 | 599 | /// This function makes a copy of the given arc. |
600 | | CopyDigraph& arc(TArc& tarc, const Arc& arc) { |
| 600 | CopyDigraph& arc(const Arc& arc, TArc& tarc) { |
601 | 601 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
602 | | ArcRefMap, TArc>(tarc, arc)); |
| 602 | ArcRefMap, TArc>(arc, tarc)); |
603 | 603 | return *this; |
604 | 604 | } |
605 | 605 | |
… |
… |
|
611 | 611 | NodeRefMap nodeRefMap(_from); |
612 | 612 | ArcRefMap arcRefMap(_from); |
613 | 613 | _core_bits::CopyDigraphSelector<To>:: |
614 | | copy(_to, _from, nodeRefMap, arcRefMap); |
| 614 | copy(_from, _to, nodeRefMap, arcRefMap); |
615 | 615 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
616 | 616 | _node_maps[i]->copy(_from, nodeRefMap); |
617 | 617 | } |
… |
… |
|
639 | 639 | /// The complete usage of it is detailed in the CopyDigraph class, but |
640 | 640 | /// a short example shows a basic work: |
641 | 641 | ///\code |
642 | | /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(acr).run(); |
| 642 | /// copyDigraph(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
643 | 643 | ///\endcode |
644 | 644 | /// |
645 | 645 | /// After the copy the \c nr map will contain the mapping from the |
… |
… |
|
648 | 648 | /// to the arcs of the \c from digraph. |
649 | 649 | /// |
650 | 650 | /// \see CopyDigraph |
651 | | template <typename To, typename From> |
652 | | CopyDigraph<To, From> copyDigraph(To& to, const From& from) { |
653 | | return CopyDigraph<To, From>(to, from); |
| 651 | template <typename From, typename To> |
| 652 | CopyDigraph<From, To> copyDigraph(const From& from, To& to) { |
| 653 | return CopyDigraph<From, To>(from, to); |
654 | 654 | } |
655 | 655 | |
656 | 656 | /// \brief Class to copy a graph. |
… |
… |
|
670 | 670 | /// |
671 | 671 | /// The next code copies a graph with several data: |
672 | 672 | ///\code |
673 | | /// CopyGraph<NewGraph, OrigGraph> cg(new_graph, orig_graph); |
| 673 | /// CopyGraph<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
674 | 674 | /// // Create references for the nodes |
675 | 675 | /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
676 | 676 | /// cg.nodeRef(nr); |
… |
… |
|
680 | 680 | /// // Copy an edge map |
681 | 681 | /// OrigGraph::EdgeMap<double> oemap(orig_graph); |
682 | 682 | /// NewGraph::EdgeMap<double> nemap(new_graph); |
683 | | /// cg.edgeMap(nemap, oemap); |
| 683 | /// cg.edgeMap(oemap, nemap); |
684 | 684 | /// // Copy a node |
685 | 685 | /// OrigGraph::Node on; |
686 | 686 | /// NewGraph::Node nn; |
687 | | /// cg.node(nn, on); |
| 687 | /// cg.node(on, nn); |
688 | 688 | /// // Execute copying |
689 | 689 | /// cg.run(); |
690 | 690 | ///\endcode |
691 | | template <typename To, typename From> |
| 691 | template <typename From, typename To> |
692 | 692 | class CopyGraph { |
693 | 693 | private: |
694 | 694 | |
… |
… |
|
707 | 707 | typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
708 | 708 | |
709 | 709 | struct ArcRefMap { |
710 | | ArcRefMap(const To& to, const From& from, |
| 710 | ArcRefMap(const From& from, const To& to, |
711 | 711 | const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
712 | | : _to(to), _from(from), |
| 712 | : _from(from), _to(to), |
713 | 713 | _edge_ref(edge_ref), _node_ref(node_ref) {} |
714 | 714 | |
715 | 715 | typedef typename From::Arc Key; |
… |
… |
|
723 | 723 | return _to.direct(_edge_ref[key], forward); |
724 | 724 | } |
725 | 725 | |
| 726 | const From& _from; |
726 | 727 | const To& _to; |
727 | | const From& _from; |
728 | 728 | const EdgeRefMap& _edge_ref; |
729 | 729 | const NodeRefMap& _node_ref; |
730 | 730 | }; |
… |
… |
|
735 | 735 | /// |
736 | 736 | /// Constructor of CopyGraph for copying the content of the |
737 | 737 | /// \c from graph into the \c to graph. |
738 | | CopyGraph(To& to, const From& from) |
| 738 | CopyGraph(const From& from, To& to) |
739 | 739 | : _from(from), _to(to) {} |
740 | 740 | |
741 | 741 | /// \brief Destructor of CopyGraph |
… |
… |
|
786 | 786 | /// The key type of the new map \c tmap should be the Node type of the |
787 | 787 | /// destination graph, and the key type of the original map \c map |
788 | 788 | /// should be the Node type of the source graph. |
789 | | template <typename ToMap, typename FromMap> |
790 | | CopyGraph& nodeMap(ToMap& tmap, const FromMap& map) { |
| 789 | template <typename FromMap, typename ToMap> |
| 790 | CopyGraph& nodeMap(const FromMap& map, ToMap& tmap) { |
791 | 791 | _node_maps.push_back(new _core_bits::MapCopy<From, Node, |
792 | | NodeRefMap, ToMap, FromMap>(tmap, map)); |
| 792 | NodeRefMap, FromMap, ToMap>(map, tmap)); |
793 | 793 | return *this; |
794 | 794 | } |
795 | 795 | |
796 | 796 | /// \brief Make a copy of the given node. |
797 | 797 | /// |
798 | 798 | /// This function makes a copy of the given node. |
799 | | CopyGraph& node(TNode& tnode, const Node& node) { |
| 799 | CopyGraph& node(const Node& node, TNode& tnode) { |
800 | 800 | _node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
801 | | NodeRefMap, TNode>(tnode, node)); |
| 801 | NodeRefMap, TNode>(node, tnode)); |
802 | 802 | return *this; |
803 | 803 | } |
804 | 804 | |
… |
… |
|
835 | 835 | /// The key type of the new map \c tmap should be the Arc type of the |
836 | 836 | /// destination graph, and the key type of the original map \c map |
837 | 837 | /// should be the Arc type of the source graph. |
838 | | template <typename ToMap, typename FromMap> |
839 | | CopyGraph& arcMap(ToMap& tmap, const FromMap& map) { |
| 838 | template <typename FromMap, typename ToMap> |
| 839 | CopyGraph& arcMap(const FromMap& map, ToMap& tmap) { |
840 | 840 | _arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
841 | | ArcRefMap, ToMap, FromMap>(tmap, map)); |
| 841 | ArcRefMap, FromMap, ToMap>(map, tmap)); |
842 | 842 | return *this; |
843 | 843 | } |
844 | 844 | |
845 | 845 | /// \brief Make a copy of the given arc. |
846 | 846 | /// |
847 | 847 | /// This function makes a copy of the given arc. |
848 | | CopyGraph& arc(TArc& tarc, const Arc& arc) { |
| 848 | CopyGraph& arc(const Arc& arc, TArc& tarc) { |
849 | 849 | _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
850 | | ArcRefMap, TArc>(tarc, arc)); |
| 850 | ArcRefMap, TArc>(arc, tarc)); |
851 | 851 | return *this; |
852 | 852 | } |
853 | 853 | |
… |
… |
|
884 | 884 | /// The key type of the new map \c tmap should be the Edge type of the |
885 | 885 | /// destination graph, and the key type of the original map \c map |
886 | 886 | /// should be the Edge type of the source graph. |
887 | | template <typename ToMap, typename FromMap> |
888 | | CopyGraph& edgeMap(ToMap& tmap, const FromMap& map) { |
| 887 | template <typename FromMap, typename ToMap> |
| 888 | CopyGraph& edgeMap(const FromMap& map, ToMap& tmap) { |
889 | 889 | _edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
890 | | EdgeRefMap, ToMap, FromMap>(tmap, map)); |
| 890 | EdgeRefMap, FromMap, ToMap>(map, tmap)); |
891 | 891 | return *this; |
892 | 892 | } |
893 | 893 | |
894 | 894 | /// \brief Make a copy of the given edge. |
895 | 895 | /// |
896 | 896 | /// This function makes a copy of the given edge. |
897 | | CopyGraph& edge(TEdge& tedge, const Edge& edge) { |
| 897 | CopyGraph& edge(const Edge& edge, TEdge& tedge) { |
898 | 898 | _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
899 | | EdgeRefMap, TEdge>(tedge, edge)); |
| 899 | EdgeRefMap, TEdge>(edge, tedge)); |
900 | 900 | return *this; |
901 | 901 | } |
902 | 902 | |
… |
… |
|
907 | 907 | void run() { |
908 | 908 | NodeRefMap nodeRefMap(_from); |
909 | 909 | EdgeRefMap edgeRefMap(_from); |
910 | | ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
| 910 | ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
911 | 911 | _core_bits::CopyGraphSelector<To>:: |
912 | | copy(_to, _from, nodeRefMap, edgeRefMap); |
| 912 | copy(_from, _to, nodeRefMap, edgeRefMap); |
913 | 913 | for (int i = 0; i < int(_node_maps.size()); ++i) { |
914 | 914 | _node_maps[i]->copy(_from, nodeRefMap); |
915 | 915 | } |
… |
… |
|
943 | 943 | /// The complete usage of it is detailed in the CopyGraph class, |
944 | 944 | /// but a short example shows a basic work: |
945 | 945 | ///\code |
946 | | /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run(); |
| 946 | /// copyGraph(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
947 | 947 | ///\endcode |
948 | 948 | /// |
949 | 949 | /// After the copy the \c nr map will contain the mapping from the |
… |
… |
|
952 | 952 | /// to the edges of the \c from graph. |
953 | 953 | /// |
954 | 954 | /// \see CopyGraph |
955 | | template <typename To, typename From> |
956 | | CopyGraph<To, From> |
957 | | copyGraph(To& to, const From& from) { |
958 | | return CopyGraph<To, From>(to, from); |
| 955 | template <typename From, typename To> |
| 956 | CopyGraph<From, To> |
| 957 | copyGraph(const From& from, To& to) { |
| 958 | return CopyGraph<From, To>(from, to); |
959 | 959 | } |
960 | 960 | |
961 | 961 | namespace _core_bits { |
diff --git a/test/graph_copy_test.cc b/test/graph_copy_test.cc
a
|
b
|
|
63 | 63 | ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
64 | 64 | ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
65 | 65 | |
66 | | CopyDigraph<ListDigraph, SmartDigraph>(to, from). |
67 | | nodeMap(tnm, fnm).arcMap(tam, fam). |
| 66 | copyDigraph(from, to). |
| 67 | nodeMap(fnm, tnm).arcMap(fam, tam). |
68 | 68 | nodeRef(nr).arcRef(er). |
69 | 69 | nodeCrossRef(ncr).arcCrossRef(ecr). |
70 | | node(tn, fn).arc(ta, fa).run(); |
| 70 | node(fn, tn).arc(fa, ta).run(); |
71 | 71 | |
72 | 72 | for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
73 | 73 | check(ncr[nr[it]] == it, "Wrong copy."); |
… |
… |
|
138 | 138 | ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
139 | 139 | ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
140 | 140 | |
141 | | CopyGraph<ListGraph, SmartGraph>(to, from). |
142 | | nodeMap(tnm, fnm).arcMap(tam, fam).edgeMap(tem, fem). |
| 141 | copyGraph(from, to). |
| 142 | nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
143 | 143 | nodeRef(nr).arcRef(ar).edgeRef(er). |
144 | 144 | nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
145 | | node(tn, fn).arc(ta, fa).edge(te, fe).run(); |
| 145 | node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
146 | 146 | |
147 | 147 | for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
148 | 148 | check(ncr[nr[it]] == it, "Wrong copy."); |