COIN-OR::LEMON - Graph Library

Changeset 2290:f30867b359a8 in lemon-0.x for lemon/graph_utils.h


Ignore:
Timestamp:
11/03/06 15:20:24 (13 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3055
Message:

GraphCopy? and UGraphCopy modifications
Preliminary support for static graphs

=> cloning graphs

Added BpUGraphCopy

Tests for graph copies

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/graph_utils.h

    r2287 r2290  
    616616    };
    617617
     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
    618633    template <typename Graph, typename Item, typename RefMap, typename Ref>
    619634    class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
     
    651666    };
    652667
     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
    653757  }
    654758
     
    706810    }
    707811
    708     /// \brief Reverse and copies the node references into the given map.
    709     ///
    710     /// Reverse and copies the node references into the given map.
     812    /// \brief Copies the node cross references into the given map.
     813    ///
     814    ///  Copies the node cross references (reverse references) into
     815    ///  the given map.
    711816    template <typename NodeCrossRef>
    712817    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
     
    729834    }
    730835
     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
    731845    /// \brief Copies the edge references into the given map.
    732846    ///
     
    739853    }
    740854
    741     /// \brief Reverse and copies the edge references into the given map.
    742     ///
    743     /// Reverse and copies the edge references into the given map.
     855    /// \brief Copies the edge cross references into the given map.
     856    ///
     857    ///  Copies the edge cross references (reverse references) into
     858    ///  the given map.
    744859    template <typename EdgeCrossRef>
    745860    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
     
    762877    }
    763878
     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
    764888    /// \brief Executes the copies.
    765889    ///
     
    767891    void run() {
    768892      NodeRefMap nodeRefMap(source);
    769       for (NodeIt it(source); it != INVALID; ++it) {
    770         nodeRefMap[it] = target.addNode();
    771       }
     893      EdgeRefMap edgeRefMap(source);
     894      _graph_utils_bits::GraphCopySelector<Target>::
     895        copy(target, source, nodeRefMap, edgeRefMap);
    772896      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
    773897        nodeMapCopies[i]->copy(source, nodeRefMap);
    774898      }
    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       }
    780899      for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
    781900        edgeMapCopies[i]->copy(source, edgeRefMap);
    782       }
    783     }
    784 
    785   private:
    786    
     901      }     
     902    }
     903
     904  protected:
     905
     906
    787907    const Source& source;
    788908    Target& target;
     
    809929  /// contain the mapping from the target graph's edges to the source's
    810930  /// edges.
     931  ///
     932  /// \see GraphCopy
    811933  template <typename Target, typename Source>
    812934  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
     
    8951017    }
    8961018
    897     /// \brief Reverse and copies the node references into the given map.
    898     ///
    899     /// Reverse and copies the node references into the given map.
     1019    /// \brief Copies the node cross references into the given map.
     1020    ///
     1021    ///  Copies the node cross references (reverse references) into
     1022    ///  the given map.
    9001023    template <typename NodeCrossRef>
    9011024    UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
     
    9181041    }
    9191042
     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
    9201052    /// \brief Copies the edge references into the given map.
    9211053    ///
     
    9281060    }
    9291061
    930     /// \brief Reverse and copies the edge references into the given map.
    931     ///
    932     /// Reverse and copies the edge references into the given map.
     1062    /// \brief Copies the edge cross references into the given map.
     1063    ///
     1064    ///  Copies the edge cross references (reverse references) into
     1065    ///  the given map.
    9331066    template <typename EdgeCrossRef>
    9341067    UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
     
    9511084    }
    9521085
    953     /// \brief Copies the uEdge references into the given map.
    954     ///
    955     /// Copies the uEdge references into the given map.
     1086    /// \brief Make a copy of the given edge.
     1087    ///
     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.
    9561098    template <typename UEdgeRef>
    9571099    UGraphCopy& uEdgeRef(UEdgeRef& map) {
     
    9611103    }
    9621104
    963     /// \brief Reverse and copies the uEdge references into the given map.
    964     ///
    965     /// Reverse and copies the uEdge references into the given map.
     1105    /// \brief Copies the undirected edge cross references into the given map.
     1106    ///
     1107    /// Copies the undirected edge cross references (reverse
     1108    /// references) into the given map.
    9661109    template <typename UEdgeCrossRef>
    9671110    UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
     
    9741117    ///
    9751118    /// 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,
    977     /// and the copied map's key type is the source graph's uEdge
     1119    /// The new map's key type is the target graph's undirected edge type,
     1120    /// and the copied map's key type is the source graph's undirected edge
    9781121    /// type. 
    9791122    template <typename TargetMap, typename SourceMap>
     
    9841127    }
    9851128
     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
    9861138    /// \brief Executes the copies.
    9871139    ///
     
    9891141    void run() {
    9901142      NodeRefMap nodeRefMap(source);
    991       for (NodeIt it(source); it != INVALID; ++it) {
    992         nodeRefMap[it] = target.addNode();
    993       }
     1143      UEdgeRefMap uEdgeRefMap(source);
     1144      EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
     1145      _graph_utils_bits::UGraphCopySelector<Target>::
     1146        copy(target, source, nodeRefMap, uEdgeRefMap);
    9941147      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
    9951148        nodeMapCopies[i]->copy(source, nodeRefMap);
    9961149      }
    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       }
    10031150      for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
    10041151        uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
     
    10251172  };
    10261173
    1027   /// \brief Copy a graph to another graph.
    1028   ///
    1029   /// Copy a graph to another graph.
     1174  /// \brief Copy an undirected graph to another graph.
     1175  ///
     1176  /// Copy an undirected graph to another graph.
    10301177  /// The usage of the function:
    10311178  ///
     
    10381185  /// contain the mapping from the target graph's edges to the source's
    10391186  /// edges.
     1187  ///
     1188  /// \see UGraphCopy
    10401189  template <typename Target, typename Source>
    10411190  UGraphCopy<Target, Source>
    10421191  copyUGraph(Target& target, const Source& source) {
    10431192    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);
    10441557  }
    10451558
Note: See TracChangeset for help on using the changeset viewer.