lemon/graph_utils.h
changeset 2293 1ee6e8788cc7
parent 2287 16954ac69517
child 2329 3f4a04a9b7bf
equal deleted inserted replaced
57:005459e3ef69 58:321f85b7037c
   613     private:
   613     private:
   614       TargetMap& _tmap;
   614       TargetMap& _tmap;
   615       const SourceMap& _map;
   615       const SourceMap& _map;
   616     };
   616     };
   617 
   617 
       
   618     template <typename Graph, typename Item, typename RefMap, typename It>
       
   619     class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
       
   620     public:
       
   621 
       
   622       ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
       
   623       
       
   624       virtual void copy(const Graph&, const RefMap& refMap) {
       
   625         _it = refMap[_item];
       
   626       }
       
   627 
       
   628     private:
       
   629       It& _it;
       
   630       Item _item;
       
   631     };
       
   632 
   618     template <typename Graph, typename Item, typename RefMap, typename Ref>
   633     template <typename Graph, typename Item, typename RefMap, typename Ref>
   619     class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
   634     class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
   620     public:
   635     public:
   621 
   636 
   622       RefCopy(Ref& map) : _map(map) {}
   637       RefCopy(Ref& map) : _map(map) {}
   648 
   663 
   649     private:
   664     private:
   650       CrossRef& _cmap;
   665       CrossRef& _cmap;
   651     };
   666     };
   652 
   667 
       
   668     template <typename Graph, typename Enable = void>
       
   669     struct GraphCopySelector {
       
   670       template <typename Source, typename NodeRefMap, typename EdgeRefMap>
       
   671       static void copy(Graph &target, const Source& source,
       
   672                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
       
   673         for (typename Source::NodeIt it(source); it != INVALID; ++it) {
       
   674           nodeRefMap[it] = target.addNode();
       
   675         }
       
   676         for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
       
   677           edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
       
   678                                           nodeRefMap[source.target(it)]);
       
   679         }
       
   680       }
       
   681     };
       
   682 
       
   683     template <typename Graph>
       
   684     struct GraphCopySelector<
       
   685       Graph, 
       
   686       typename enable_if<typename Graph::CloneableTag, void>::type> 
       
   687     {
       
   688       template <typename Source, typename NodeRefMap, typename EdgeRefMap>
       
   689       static void copy(Graph &target, const Source& source,
       
   690                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
       
   691         target.clone(source, nodeRefMap, edgeRefMap);
       
   692       }
       
   693     };
       
   694 
       
   695     template <typename UGraph, typename Enable = void>
       
   696     struct UGraphCopySelector {
       
   697       template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
       
   698       static void copy(UGraph &target, const Source& source,
       
   699                        NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
       
   700         for (typename Source::NodeIt it(source); it != INVALID; ++it) {
       
   701           nodeRefMap[it] = target.addNode();
       
   702         }
       
   703         for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
       
   704           uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
       
   705                                           nodeRefMap[source.target(it)]);
       
   706         }
       
   707       }
       
   708     };
       
   709 
       
   710     template <typename UGraph>
       
   711     struct UGraphCopySelector<
       
   712       UGraph, 
       
   713       typename enable_if<typename UGraph::CloneableTag, void>::type> 
       
   714     {
       
   715       template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
       
   716       static void copy(UGraph &target, const Source& source,
       
   717                        NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
       
   718         target.clone(source, nodeRefMap, uEdgeRefMap);
       
   719       }
       
   720     };
       
   721 
       
   722     template <typename BpUGraph, typename Enable = void>
       
   723     struct BpUGraphCopySelector {
       
   724       template <typename Source, typename ANodeRefMap, 
       
   725                 typename BNodeRefMap, typename UEdgeRefMap>
       
   726       static void copy(BpUGraph &target, const Source& source,
       
   727                        ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
       
   728                        UEdgeRefMap& uEdgeRefMap) {
       
   729         for (typename Source::ANodeIt it(source); it != INVALID; ++it) {
       
   730           aNodeRefMap[it] = target.addANode();
       
   731         }
       
   732         for (typename Source::BNodeIt it(source); it != INVALID; ++it) {
       
   733           bNodeRefMap[it] = target.addBNode();
       
   734         }
       
   735         for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
       
   736           uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)], 
       
   737                                            bNodeRefMap[source.bNode(it)]);
       
   738         }
       
   739       }
       
   740     };
       
   741 
       
   742     template <typename BpUGraph>
       
   743     struct BpUGraphCopySelector<
       
   744       BpUGraph, 
       
   745       typename enable_if<typename BpUGraph::CloneableTag, void>::type> 
       
   746     {
       
   747       template <typename Source, typename ANodeRefMap, 
       
   748                 typename BNodeRefMap, typename UEdgeRefMap>
       
   749       static void copy(BpUGraph &target, const Source& source,
       
   750                        ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
       
   751                        UEdgeRefMap& uEdgeRefMap) {
       
   752         target.clone(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
       
   753       }
       
   754     };
       
   755     
       
   756 
   653   }
   757   }
   654 
   758 
   655   /// \brief Class to copy a graph.
   759   /// \brief Class to copy a graph.
   656   ///
   760   ///
   657   /// Class to copy a graph to another graph (duplicate a graph). The
   761   /// Class to copy a graph to another graph (duplicate a graph). The
   703       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
   807       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
   704                               NodeRefMap, NodeRef>(map));
   808                               NodeRefMap, NodeRef>(map));
   705       return *this;
   809       return *this;
   706     }
   810     }
   707 
   811 
   708     /// \brief Reverse and copies the node references into the given map.
   812     /// \brief Copies the node cross references into the given map.
   709     ///
   813     ///
   710     /// Reverse and copies the node references into the given map.
   814     ///  Copies the node cross references (reverse references) into
       
   815     ///  the given map.
   711     template <typename NodeCrossRef>
   816     template <typename NodeCrossRef>
   712     GraphCopy& nodeCrossRef(NodeCrossRef& map) {
   817     GraphCopy& nodeCrossRef(NodeCrossRef& map) {
   713       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
   818       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
   714                               NodeRefMap, NodeCrossRef>(map));
   819                               NodeRefMap, NodeCrossRef>(map));
   715       return *this;
   820       return *this;
   726       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
   831       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
   727                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
   832                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
   728       return *this;
   833       return *this;
   729     }
   834     }
   730 
   835 
       
   836     /// \brief Make a copy of the given node.
       
   837     ///
       
   838     /// Make a copy of the given node.
       
   839     GraphCopy& node(TNode& tnode, const Node& node) {
       
   840       nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
       
   841                               NodeRefMap, TNode>(tnode, node));
       
   842       return *this;
       
   843     }
       
   844 
   731     /// \brief Copies the edge references into the given map.
   845     /// \brief Copies the edge references into the given map.
   732     ///
   846     ///
   733     /// Copies the edge references into the given map.
   847     /// Copies the edge references into the given map.
   734     template <typename EdgeRef>
   848     template <typename EdgeRef>
   735     GraphCopy& edgeRef(EdgeRef& map) {
   849     GraphCopy& edgeRef(EdgeRef& map) {
   736       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
   850       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
   737                               EdgeRefMap, EdgeRef>(map));
   851                               EdgeRefMap, EdgeRef>(map));
   738       return *this;
   852       return *this;
   739     }
   853     }
   740 
   854 
   741     /// \brief Reverse and copies the edge references into the given map.
   855     /// \brief Copies the edge cross references into the given map.
   742     ///
   856     ///
   743     /// Reverse and copies the edge references into the given map.
   857     ///  Copies the edge cross references (reverse references) into
       
   858     ///  the given map.
   744     template <typename EdgeCrossRef>
   859     template <typename EdgeCrossRef>
   745     GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
   860     GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
   746       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
   861       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
   747                               EdgeRefMap, EdgeCrossRef>(map));
   862                               EdgeRefMap, EdgeCrossRef>(map));
   748       return *this;
   863       return *this;
   759       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
   874       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
   760                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
   875                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
   761       return *this;
   876       return *this;
   762     }
   877     }
   763 
   878 
       
   879     /// \brief Make a copy of the given edge.
       
   880     ///
       
   881     /// Make a copy of the given edge.
       
   882     GraphCopy& edge(TEdge& tedge, const Edge& edge) {
       
   883       edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
       
   884                               EdgeRefMap, TEdge>(tedge, edge));
       
   885       return *this;
       
   886     }
       
   887 
   764     /// \brief Executes the copies.
   888     /// \brief Executes the copies.
   765     ///
   889     ///
   766     /// Executes the copies.
   890     /// Executes the copies.
   767     void run() {
   891     void run() {
   768       NodeRefMap nodeRefMap(source);
   892       NodeRefMap nodeRefMap(source);
   769       for (NodeIt it(source); it != INVALID; ++it) {
   893       EdgeRefMap edgeRefMap(source);
   770 	nodeRefMap[it] = target.addNode();
   894       _graph_utils_bits::GraphCopySelector<Target>::
   771       }
   895         copy(target, source, nodeRefMap, edgeRefMap);
   772       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   896       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   773         nodeMapCopies[i]->copy(source, nodeRefMap);
   897         nodeMapCopies[i]->copy(source, nodeRefMap);
   774       }
   898       }
   775       EdgeRefMap edgeRefMap(source);
       
   776       for (EdgeIt it(source); it != INVALID; ++it) {
       
   777 	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
       
   778 					nodeRefMap[source.target(it)]);
       
   779       }
       
   780       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
   899       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
   781         edgeMapCopies[i]->copy(source, edgeRefMap);
   900         edgeMapCopies[i]->copy(source, edgeRefMap);
   782       }
   901       }      
   783     }
   902     }
   784 
   903 
   785   private:
   904   protected:
   786     
   905 
       
   906 
   787     const Source& source;
   907     const Source& source;
   788     Target& target;
   908     Target& target;
   789 
   909 
   790     std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
   910     std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
   791     nodeMapCopies;
   911     nodeMapCopies;
   806   /// 
   926   /// 
   807   /// After the copy the \c nr map will contain the mapping from the
   927   /// After the copy the \c nr map will contain the mapping from the
   808   /// source graph's nodes to the target graph's nodes and the \c ecr will
   928   /// source graph's nodes to the target graph's nodes and the \c ecr will
   809   /// contain the mapping from the target graph's edges to the source's
   929   /// contain the mapping from the target graph's edges to the source's
   810   /// edges.
   930   /// edges.
       
   931   ///
       
   932   /// \see GraphCopy 
   811   template <typename Target, typename Source>
   933   template <typename Target, typename Source>
   812   GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
   934   GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
   813     return GraphCopy<Target, Source>(target, source);
   935     return GraphCopy<Target, Source>(target, source);
   814   }
   936   }
   815 
   937 
   892       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
  1014       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
   893                               NodeRefMap, NodeRef>(map));
  1015                               NodeRefMap, NodeRef>(map));
   894       return *this;
  1016       return *this;
   895     }
  1017     }
   896 
  1018 
   897     /// \brief Reverse and copies the node references into the given map.
  1019     /// \brief Copies the node cross references into the given map.
   898     ///
  1020     ///
   899     /// Reverse and copies the node references into the given map.
  1021     ///  Copies the node cross references (reverse references) into
       
  1022     ///  the given map.
   900     template <typename NodeCrossRef>
  1023     template <typename NodeCrossRef>
   901     UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
  1024     UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
   902       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
  1025       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
   903                               NodeRefMap, NodeCrossRef>(map));
  1026                               NodeRefMap, NodeCrossRef>(map));
   904       return *this;
  1027       return *this;
   915       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
  1038       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
   916                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
  1039                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
   917       return *this;
  1040       return *this;
   918     }
  1041     }
   919 
  1042 
       
  1043     /// \brief Make a copy of the given node.
       
  1044     ///
       
  1045     /// Make a copy of the given node.
       
  1046     UGraphCopy& node(TNode& tnode, const Node& node) {
       
  1047       nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
       
  1048                               NodeRefMap, TNode>(tnode, node));
       
  1049       return *this;
       
  1050     }
       
  1051 
   920     /// \brief Copies the edge references into the given map.
  1052     /// \brief Copies the edge references into the given map.
   921     ///
  1053     ///
   922     /// Copies the edge references into the given map.
  1054     /// Copies the edge references into the given map.
   923     template <typename EdgeRef>
  1055     template <typename EdgeRef>
   924     UGraphCopy& edgeRef(EdgeRef& map) {
  1056     UGraphCopy& edgeRef(EdgeRef& map) {
   925       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
  1057       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
   926                               EdgeRefMap, EdgeRef>(map));
  1058                               EdgeRefMap, EdgeRef>(map));
   927       return *this;
  1059       return *this;
   928     }
  1060     }
   929 
  1061 
   930     /// \brief Reverse and copies the edge references into the given map.
  1062     /// \brief Copies the edge cross references into the given map.
   931     ///
  1063     ///
   932     /// Reverse and copies the edge references into the given map.
  1064     ///  Copies the edge cross references (reverse references) into
       
  1065     ///  the given map.
   933     template <typename EdgeCrossRef>
  1066     template <typename EdgeCrossRef>
   934     UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
  1067     UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
   935       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
  1068       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
   936                               EdgeRefMap, EdgeCrossRef>(map));
  1069                               EdgeRefMap, EdgeCrossRef>(map));
   937       return *this;
  1070       return *this;
   948       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
  1081       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
   949                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
  1082                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
   950       return *this;
  1083       return *this;
   951     }
  1084     }
   952 
  1085 
   953     /// \brief Copies the uEdge references into the given map.
  1086     /// \brief Make a copy of the given edge.
   954     ///
  1087     ///
   955     /// Copies the uEdge references into the given map.
  1088     /// Make a copy of the given edge.
       
  1089     UGraphCopy& edge(TEdge& tedge, const Edge& edge) {
       
  1090       edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
       
  1091                               EdgeRefMap, TEdge>(tedge, edge));
       
  1092       return *this;
       
  1093     }
       
  1094 
       
  1095     /// \brief Copies the undirected edge references into the given map.
       
  1096     ///
       
  1097     /// Copies the undirected edge references into the given map.
   956     template <typename UEdgeRef>
  1098     template <typename UEdgeRef>
   957     UGraphCopy& uEdgeRef(UEdgeRef& map) {
  1099     UGraphCopy& uEdgeRef(UEdgeRef& map) {
   958       uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
  1100       uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
   959                                UEdgeRefMap, UEdgeRef>(map));
  1101                                UEdgeRefMap, UEdgeRef>(map));
   960       return *this;
  1102       return *this;
   961     }
  1103     }
   962 
  1104 
   963     /// \brief Reverse and copies the uEdge references into the given map.
  1105     /// \brief Copies the undirected edge cross references into the given map.
   964     ///
  1106     ///
   965     /// Reverse and copies the uEdge references into the given map.
  1107     /// Copies the undirected edge cross references (reverse
       
  1108     /// references) into the given map.
   966     template <typename UEdgeCrossRef>
  1109     template <typename UEdgeCrossRef>
   967     UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
  1110     UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
   968       uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
  1111       uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
   969                                UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
  1112                                UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
   970       return *this;
  1113       return *this;
   971     }
  1114     }
   972 
  1115 
   973     /// \brief Make copy of the given map.
  1116     /// \brief Make copy of the given map.
   974     ///
  1117     ///
   975     /// Makes copy of the given map for the newly created graph. 
  1118     /// Makes copy of the given map for the newly created graph. 
   976     /// The new map's key type is the target graph's uEdge type,
  1119     /// The new map's key type is the target graph's undirected edge type,
   977     /// and the copied map's key type is the source graph's uEdge
  1120     /// and the copied map's key type is the source graph's undirected edge
   978     /// type.  
  1121     /// type.  
   979     template <typename TargetMap, typename SourceMap>
  1122     template <typename TargetMap, typename SourceMap>
   980     UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
  1123     UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
   981       uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
  1124       uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
   982                                UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
  1125                                UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
   983       return *this;
  1126       return *this;
   984     }
  1127     }
   985 
  1128 
       
  1129     /// \brief Make a copy of the given undirected edge.
       
  1130     ///
       
  1131     /// Make a copy of the given undirected edge.
       
  1132     UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
       
  1133       uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
       
  1134                                UEdgeRefMap, TUEdge>(tuedge, uedge));
       
  1135       return *this;
       
  1136     }
       
  1137 
   986     /// \brief Executes the copies.
  1138     /// \brief Executes the copies.
   987     ///
  1139     ///
   988     /// Executes the copies.
  1140     /// Executes the copies.
   989     void run() {
  1141     void run() {
   990       NodeRefMap nodeRefMap(source);
  1142       NodeRefMap nodeRefMap(source);
   991       for (NodeIt it(source); it != INVALID; ++it) {
  1143       UEdgeRefMap uEdgeRefMap(source);
   992 	nodeRefMap[it] = target.addNode();
  1144       EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
   993       }
  1145       _graph_utils_bits::UGraphCopySelector<Target>::
       
  1146         copy(target, source, nodeRefMap, uEdgeRefMap);
   994       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
  1147       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
   995         nodeMapCopies[i]->copy(source, nodeRefMap);
  1148         nodeMapCopies[i]->copy(source, nodeRefMap);
   996       }
  1149       }
   997       UEdgeRefMap uEdgeRefMap(source);
       
   998       EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
       
   999       for (UEdgeIt it(source); it != INVALID; ++it) {
       
  1000 	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
       
  1001                                          nodeRefMap[source.target(it)]);
       
  1002       }
       
  1003       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
  1150       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
  1004         uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
  1151         uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
  1005       }
  1152       }
  1006       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
  1153       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
  1007         edgeMapCopies[i]->copy(source, edgeRefMap);
  1154         edgeMapCopies[i]->copy(source, edgeRefMap);
  1022     std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
  1169     std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
  1023     uEdgeMapCopies;
  1170     uEdgeMapCopies;
  1024 
  1171 
  1025   };
  1172   };
  1026 
  1173 
  1027   /// \brief Copy a graph to another graph.
  1174   /// \brief Copy an undirected graph to another graph.
  1028   ///
  1175   ///
  1029   /// Copy a graph to another graph.
  1176   /// Copy an undirected graph to another graph.
  1030   /// The usage of the function:
  1177   /// The usage of the function:
  1031   /// 
  1178   /// 
  1032   ///\code
  1179   ///\code
  1033   /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
  1180   /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
  1034   ///\endcode
  1181   ///\endcode
  1035   /// 
  1182   /// 
  1036   /// After the copy the \c nr map will contain the mapping from the
  1183   /// After the copy the \c nr map will contain the mapping from the
  1037   /// source graph's nodes to the target graph's nodes and the \c ecr will
  1184   /// source graph's nodes to the target graph's nodes and the \c ecr will
  1038   /// contain the mapping from the target graph's edges to the source's
  1185   /// contain the mapping from the target graph's edges to the source's
  1039   /// edges.
  1186   /// edges.
       
  1187   ///
       
  1188   /// \see UGraphCopy 
  1040   template <typename Target, typename Source>
  1189   template <typename Target, typename Source>
  1041   UGraphCopy<Target, Source> 
  1190   UGraphCopy<Target, Source> 
  1042   copyUGraph(Target& target, const Source& source) {
  1191   copyUGraph(Target& target, const Source& source) {
  1043     return UGraphCopy<Target, Source>(target, source);
  1192     return UGraphCopy<Target, Source>(target, source);
       
  1193   }
       
  1194 
       
  1195   /// \brief Class to copy a bipartite undirected graph.
       
  1196   ///
       
  1197   /// Class to copy a bipartite undirected graph to another graph
       
  1198   /// (duplicate a graph).  The simplest way of using it is through
       
  1199   /// the \c copyBpUGraph() function.
       
  1200   template <typename Target, typename Source>
       
  1201   class BpUGraphCopy {
       
  1202   private:
       
  1203 
       
  1204     typedef typename Source::Node Node;
       
  1205     typedef typename Source::ANode ANode;
       
  1206     typedef typename Source::BNode BNode;
       
  1207     typedef typename Source::NodeIt NodeIt;
       
  1208     typedef typename Source::Edge Edge;
       
  1209     typedef typename Source::EdgeIt EdgeIt;
       
  1210     typedef typename Source::UEdge UEdge;
       
  1211     typedef typename Source::UEdgeIt UEdgeIt;
       
  1212 
       
  1213     typedef typename Target::Node TNode;
       
  1214     typedef typename Target::Edge TEdge;
       
  1215     typedef typename Target::UEdge TUEdge;
       
  1216 
       
  1217     typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
       
  1218     typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
       
  1219     typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
       
  1220 
       
  1221     struct NodeRefMap {
       
  1222       NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
       
  1223                  const BNodeRefMap& _bnode_ref)
       
  1224         : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
       
  1225 
       
  1226       typedef typename Source::Node Key;
       
  1227       typedef typename Target::Node Value;
       
  1228 
       
  1229       Value operator[](const Key& key) const {
       
  1230 	return source.aNode(key) ? anode_ref[key] : bnode_ref[key]; 
       
  1231       }
       
  1232       
       
  1233       const Source& source;
       
  1234       const ANodeRefMap& anode_ref;
       
  1235       const BNodeRefMap& bnode_ref;
       
  1236     };
       
  1237 
       
  1238     struct EdgeRefMap {
       
  1239       EdgeRefMap(const Target& _target, const Source& _source,
       
  1240                  const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
       
  1241         : target(_target), source(_source), 
       
  1242           uedge_ref(_uedge_ref), node_ref(_node_ref) {}
       
  1243 
       
  1244       typedef typename Source::Edge Key;
       
  1245       typedef typename Target::Edge Value;
       
  1246 
       
  1247       Value operator[](const Key& key) const {
       
  1248         bool forward = (source.direction(key) == 
       
  1249                         (node_ref[source.source((UEdge)key)] == 
       
  1250                          target.source(uedge_ref[(UEdge)key])));
       
  1251 	return target.direct(uedge_ref[key], forward); 
       
  1252       }
       
  1253       
       
  1254       const Target& target;
       
  1255       const Source& source;
       
  1256       const UEdgeRefMap& uedge_ref;
       
  1257       const NodeRefMap& node_ref;
       
  1258     };
       
  1259     
       
  1260   public: 
       
  1261 
       
  1262 
       
  1263     /// \brief Constructor for the GraphCopy.
       
  1264     ///
       
  1265     /// It copies the content of the \c _source graph into the
       
  1266     /// \c _target graph.
       
  1267     BpUGraphCopy(Target& _target, const Source& _source) 
       
  1268       : source(_source), target(_target) {}
       
  1269 
       
  1270     /// \brief Destructor of the GraphCopy
       
  1271     ///
       
  1272     /// Destructor of the GraphCopy
       
  1273     ~BpUGraphCopy() {
       
  1274       for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
       
  1275         delete aNodeMapCopies[i];
       
  1276       }
       
  1277       for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
       
  1278         delete bNodeMapCopies[i];
       
  1279       }
       
  1280       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
       
  1281         delete nodeMapCopies[i];
       
  1282       }
       
  1283       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
       
  1284         delete edgeMapCopies[i];
       
  1285       }
       
  1286       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
       
  1287         delete uEdgeMapCopies[i];
       
  1288       }
       
  1289 
       
  1290     }
       
  1291 
       
  1292     /// \brief Copies the A-node references into the given map.
       
  1293     ///
       
  1294     /// Copies the A-node references into the given map.
       
  1295     template <typename ANodeRef>
       
  1296     BpUGraphCopy& aNodeRef(ANodeRef& map) {
       
  1297       aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode, 
       
  1298                                ANodeRefMap, ANodeRef>(map));
       
  1299       return *this;
       
  1300     }
       
  1301 
       
  1302     /// \brief Copies the A-node cross references into the given map.
       
  1303     ///
       
  1304     /// Copies the A-node cross references (reverse references) into
       
  1305     /// the given map.
       
  1306     template <typename ANodeCrossRef>
       
  1307     BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
       
  1308       aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
       
  1309                                ANode, ANodeRefMap, ANodeCrossRef>(map));
       
  1310       return *this;
       
  1311     }
       
  1312 
       
  1313     /// \brief Make copy of the given A-node map.
       
  1314     ///
       
  1315     /// Makes copy of the given map for the newly created graph. 
       
  1316     /// The new map's key type is the target graph's node type,
       
  1317     /// and the copied map's key type is the source graph's node
       
  1318     /// type.  
       
  1319     template <typename TargetMap, typename SourceMap>
       
  1320     BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
       
  1321       aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode, 
       
  1322                                ANodeRefMap, TargetMap, SourceMap>(tmap, map));
       
  1323       return *this;
       
  1324     }
       
  1325 
       
  1326     /// \brief Copies the B-node references into the given map.
       
  1327     ///
       
  1328     /// Copies the B-node references into the given map.
       
  1329     template <typename BNodeRef>
       
  1330     BpUGraphCopy& bNodeRef(BNodeRef& map) {
       
  1331       bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode, 
       
  1332                                BNodeRefMap, BNodeRef>(map));
       
  1333       return *this;
       
  1334     }
       
  1335 
       
  1336     /// \brief Copies the B-node cross references into the given map.
       
  1337     ///
       
  1338     ///  Copies the B-node cross references (reverse references) into
       
  1339     ///  the given map.
       
  1340     template <typename BNodeCrossRef>
       
  1341     BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
       
  1342       bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
       
  1343                               BNode, BNodeRefMap, BNodeCrossRef>(map));
       
  1344       return *this;
       
  1345     }
       
  1346 
       
  1347     /// \brief Make copy of the given B-node map.
       
  1348     ///
       
  1349     /// Makes copy of the given map for the newly created graph. 
       
  1350     /// The new map's key type is the target graph's node type,
       
  1351     /// and the copied map's key type is the source graph's node
       
  1352     /// type.  
       
  1353     template <typename TargetMap, typename SourceMap>
       
  1354     BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
       
  1355       bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode, 
       
  1356                                BNodeRefMap, TargetMap, SourceMap>(tmap, map));
       
  1357       return *this;
       
  1358     }
       
  1359     /// \brief Copies the node references into the given map.
       
  1360     ///
       
  1361     /// Copies the node references into the given map.
       
  1362     template <typename NodeRef>
       
  1363     BpUGraphCopy& nodeRef(NodeRef& map) {
       
  1364       nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
       
  1365                               NodeRefMap, NodeRef>(map));
       
  1366       return *this;
       
  1367     }
       
  1368 
       
  1369     /// \brief Copies the node cross references into the given map.
       
  1370     ///
       
  1371     ///  Copies the node cross references (reverse references) into
       
  1372     ///  the given map.
       
  1373     template <typename NodeCrossRef>
       
  1374     BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
       
  1375       nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
       
  1376                               NodeRefMap, NodeCrossRef>(map));
       
  1377       return *this;
       
  1378     }
       
  1379 
       
  1380     /// \brief Make copy of the given map.
       
  1381     ///
       
  1382     /// Makes copy of the given map for the newly created graph. 
       
  1383     /// The new map's key type is the target graph's node type,
       
  1384     /// and the copied map's key type is the source graph's node
       
  1385     /// type.  
       
  1386     template <typename TargetMap, typename SourceMap>
       
  1387     BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
       
  1388       nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
       
  1389                               NodeRefMap, TargetMap, SourceMap>(tmap, map));
       
  1390       return *this;
       
  1391     }
       
  1392 
       
  1393     /// \brief Make a copy of the given node.
       
  1394     ///
       
  1395     /// Make a copy of the given node.
       
  1396     BpUGraphCopy& node(TNode& tnode, const Node& node) {
       
  1397       nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
       
  1398                               NodeRefMap, TNode>(tnode, node));
       
  1399       return *this;
       
  1400     }
       
  1401 
       
  1402     /// \brief Copies the edge references into the given map.
       
  1403     ///
       
  1404     /// Copies the edge references into the given map.
       
  1405     template <typename EdgeRef>
       
  1406     BpUGraphCopy& edgeRef(EdgeRef& map) {
       
  1407       edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
       
  1408                               EdgeRefMap, EdgeRef>(map));
       
  1409       return *this;
       
  1410     }
       
  1411 
       
  1412     /// \brief Copies the edge cross references into the given map.
       
  1413     ///
       
  1414     ///  Copies the edge cross references (reverse references) into
       
  1415     ///  the given map.
       
  1416     template <typename EdgeCrossRef>
       
  1417     BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
       
  1418       edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
       
  1419                               EdgeRefMap, EdgeCrossRef>(map));
       
  1420       return *this;
       
  1421     }
       
  1422 
       
  1423     /// \brief Make copy of the given map.
       
  1424     ///
       
  1425     /// Makes copy of the given map for the newly created graph. 
       
  1426     /// The new map's key type is the target graph's edge type,
       
  1427     /// and the copied map's key type is the source graph's edge
       
  1428     /// type.  
       
  1429     template <typename TargetMap, typename SourceMap>
       
  1430     BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
       
  1431       edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
       
  1432                               EdgeRefMap, TargetMap, SourceMap>(tmap, map));
       
  1433       return *this;
       
  1434     }
       
  1435 
       
  1436     /// \brief Make a copy of the given edge.
       
  1437     ///
       
  1438     /// Make a copy of the given edge.
       
  1439     BpUGraphCopy& edge(TEdge& tedge, const Edge& edge) {
       
  1440       edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
       
  1441                               EdgeRefMap, TEdge>(tedge, edge));
       
  1442       return *this;
       
  1443     }
       
  1444 
       
  1445     /// \brief Copies the undirected edge references into the given map.
       
  1446     ///
       
  1447     /// Copies the undirected edge references into the given map.
       
  1448     template <typename UEdgeRef>
       
  1449     BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
       
  1450       uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
       
  1451                                UEdgeRefMap, UEdgeRef>(map));
       
  1452       return *this;
       
  1453     }
       
  1454 
       
  1455     /// \brief Copies the undirected edge cross references into the given map.
       
  1456     ///
       
  1457     /// Copies the undirected edge cross references (reverse
       
  1458     /// references) into the given map.
       
  1459     template <typename UEdgeCrossRef>
       
  1460     BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
       
  1461       uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
       
  1462                                UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
       
  1463       return *this;
       
  1464     }
       
  1465 
       
  1466     /// \brief Make copy of the given map.
       
  1467     ///
       
  1468     /// Makes copy of the given map for the newly created graph. 
       
  1469     /// The new map's key type is the target graph's undirected edge type,
       
  1470     /// and the copied map's key type is the source graph's undirected edge
       
  1471     /// type.  
       
  1472     template <typename TargetMap, typename SourceMap>
       
  1473     BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
       
  1474       uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
       
  1475                                UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
       
  1476       return *this;
       
  1477     }
       
  1478 
       
  1479     /// \brief Make a copy of the given undirected edge.
       
  1480     ///
       
  1481     /// Make a copy of the given undirected edge.
       
  1482     BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
       
  1483       uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
       
  1484                                UEdgeRefMap, TUEdge>(tuedge, uedge));
       
  1485       return *this;
       
  1486     }
       
  1487 
       
  1488     /// \brief Executes the copies.
       
  1489     ///
       
  1490     /// Executes the copies.
       
  1491     void run() {
       
  1492       ANodeRefMap aNodeRefMap(source);
       
  1493       BNodeRefMap bNodeRefMap(source);
       
  1494       NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
       
  1495       UEdgeRefMap uEdgeRefMap(source);
       
  1496       EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
       
  1497       _graph_utils_bits::BpUGraphCopySelector<Target>::
       
  1498         copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
       
  1499       for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
       
  1500         aNodeMapCopies[i]->copy(source, aNodeRefMap);
       
  1501       }
       
  1502       for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
       
  1503         bNodeMapCopies[i]->copy(source, bNodeRefMap);
       
  1504       }
       
  1505       for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
       
  1506         nodeMapCopies[i]->copy(source, nodeRefMap);
       
  1507       }
       
  1508       for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
       
  1509         uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
       
  1510       }
       
  1511       for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
       
  1512         edgeMapCopies[i]->copy(source, edgeRefMap);
       
  1513       }
       
  1514     }
       
  1515 
       
  1516   private:
       
  1517     
       
  1518     const Source& source;
       
  1519     Target& target;
       
  1520 
       
  1521     std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* > 
       
  1522     aNodeMapCopies;
       
  1523 
       
  1524     std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* > 
       
  1525     bNodeMapCopies;
       
  1526 
       
  1527     std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
       
  1528     nodeMapCopies;
       
  1529 
       
  1530     std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
       
  1531     edgeMapCopies;
       
  1532 
       
  1533     std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
       
  1534     uEdgeMapCopies;
       
  1535 
       
  1536   };
       
  1537 
       
  1538   /// \brief Copy a bipartite undirected graph to another graph.
       
  1539   ///
       
  1540   /// Copy a bipartite undirected graph to another graph.
       
  1541   /// The usage of the function:
       
  1542   /// 
       
  1543   ///\code
       
  1544   /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
       
  1545   ///\endcode
       
  1546   /// 
       
  1547   /// After the copy the \c nr map will contain the mapping from the
       
  1548   /// source graph's nodes to the target graph's nodes and the \c ecr will
       
  1549   /// contain the mapping from the target graph's edges to the source's
       
  1550   /// edges.
       
  1551   ///
       
  1552   /// \see BpUGraphCopy
       
  1553   template <typename Target, typename Source>
       
  1554   BpUGraphCopy<Target, Source> 
       
  1555   copyBpUGraph(Target& target, const Source& source) {
       
  1556     return BpUGraphCopy<Target, Source>(target, source);
  1044   }
  1557   }
  1045 
  1558 
  1046 
  1559 
  1047   /// @}
  1560   /// @}
  1048 
  1561