lemon/core.h
changeset 1193 c8fa41fcc4a7
parent 1190 523e45e37e52
child 1194 699c7eac2c6d
equal deleted inserted replaced
39:fac391b10d74 40:9db882e09a38
   548       Graph,
   548       Graph,
   549       typename enable_if<typename Graph::BuildTag, void>::type>
   549       typename enable_if<typename Graph::BuildTag, void>::type>
   550     {
   550     {
   551       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   551       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   552       static void copy(const From& from, Graph &to,
   552       static void copy(const From& from, Graph &to,
   553                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   553                        NodeRefMap& nodeRefMap,
       
   554                        EdgeRefMap& edgeRefMap) {
   554         to.build(from, nodeRefMap, edgeRefMap);
   555         to.build(from, nodeRefMap, edgeRefMap);
   555       }
   556       }
   556     };
   557     };
   557 
   558 
   558     template <typename BpGraph, typename Enable = void>
   559     template <typename BpGraph, typename Enable = void>
   559     struct BpGraphCopySelector {
   560     struct BpGraphCopySelector {
   560       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   561       template <typename From, typename RedNodeRefMap,
       
   562                 typename BlueNodeRefMap, typename EdgeRefMap>
   561       static void copy(const From& from, BpGraph &to,
   563       static void copy(const From& from, BpGraph &to,
   562                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   564                        RedNodeRefMap& redNodeRefMap,
       
   565                        BlueNodeRefMap& blueNodeRefMap,
       
   566                        EdgeRefMap& edgeRefMap) {
   563         to.clear();
   567         to.clear();
   564         for (typename From::RedIt it(from); it != INVALID; ++it) {
   568         for (typename From::RedIt it(from); it != INVALID; ++it) {
   565           nodeRefMap[it] = to.addRedNode();
   569           redNodeRefMap[it] = to.addRedNode();
   566         }
   570         }
   567         for (typename From::BlueIt it(from); it != INVALID; ++it) {
   571         for (typename From::BlueIt it(from); it != INVALID; ++it) {
   568           nodeRefMap[it] = to.addBlueNode();
   572           blueNodeRefMap[it] = to.addBlueNode();
   569         }
   573         }
   570         for (typename From::EdgeIt it(from); it != INVALID; ++it) {
   574         for (typename From::EdgeIt it(from); it != INVALID; ++it) {
   571           edgeRefMap[it] = to.addEdge(nodeRefMap[from.redNode(it)],
   575           edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)],
   572                                       nodeRefMap[from.blueNode(it)]);
   576                                       blueNodeRefMap[from.blueNode(it)]);
   573         }
   577         }
   574       }
   578       }
   575     };
   579     };
   576 
   580 
   577     template <typename BpGraph>
   581     template <typename BpGraph>
   578     struct BpGraphCopySelector<
   582     struct BpGraphCopySelector<
   579       BpGraph,
   583       BpGraph,
   580       typename enable_if<typename BpGraph::BuildTag, void>::type>
   584       typename enable_if<typename BpGraph::BuildTag, void>::type>
   581     {
   585     {
   582       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   586       template <typename From, typename RedNodeRefMap,
       
   587                 typename BlueNodeRefMap, typename EdgeRefMap>
   583       static void copy(const From& from, BpGraph &to,
   588       static void copy(const From& from, BpGraph &to,
   584                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   589                        RedNodeRefMap& redNodeRefMap,
   585         to.build(from, nodeRefMap, edgeRefMap);
   590                        BlueNodeRefMap& blueNodeRefMap,
       
   591                        EdgeRefMap& edgeRefMap) {
       
   592         to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap);
   586       }
   593       }
   587     };
   594     };
   588 
   595 
   589   }
   596   }
   590 
   597 
  1180     typedef typename From::ArcIt ArcIt;
  1187     typedef typename From::ArcIt ArcIt;
  1181     typedef typename From::Edge Edge;
  1188     typedef typename From::Edge Edge;
  1182     typedef typename From::EdgeIt EdgeIt;
  1189     typedef typename From::EdgeIt EdgeIt;
  1183 
  1190 
  1184     typedef typename To::Node TNode;
  1191     typedef typename To::Node TNode;
       
  1192     typedef typename To::RedNode TRedNode;
       
  1193     typedef typename To::BlueNode TBlueNode;
  1185     typedef typename To::Arc TArc;
  1194     typedef typename To::Arc TArc;
  1186     typedef typename To::Edge TEdge;
  1195     typedef typename To::Edge TEdge;
  1187 
  1196 
  1188     typedef typename From::template NodeMap<TNode> NodeRefMap;
  1197     typedef typename From::template RedMap<TRedNode> RedNodeRefMap;
       
  1198     typedef typename From::template BlueMap<TBlueNode> BlueNodeRefMap;
  1189     typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
  1199     typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
       
  1200 
       
  1201     struct NodeRefMap {
       
  1202       NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref,
       
  1203                  const BlueNodeRefMap& blue_node_ref)
       
  1204         : _from(from), _red_node_ref(red_node_ref),
       
  1205           _blue_node_ref(blue_node_ref) {}
       
  1206 
       
  1207       typedef typename From::Node Key;
       
  1208       typedef typename To::Node Value;
       
  1209 
       
  1210       Value operator[](const Key& key) const {
       
  1211         std::pair<RedNode, BlueNode> red_blue_pair = _from.asRedBlueNode(key);
       
  1212         if (red_blue_pair.first != INVALID) {
       
  1213           return _red_node_ref[red_blue_pair.first];
       
  1214         } else {
       
  1215           return _blue_node_ref[red_blue_pair.second];
       
  1216         }
       
  1217       }
       
  1218 
       
  1219       const From& _from;
       
  1220       const RedNodeRefMap& _red_node_ref;
       
  1221       const BlueNodeRefMap& _blue_node_ref;
       
  1222     };
  1190 
  1223 
  1191     struct ArcRefMap {
  1224     struct ArcRefMap {
  1192       ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
  1225       ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
  1193         : _from(from), _to(to), _edge_ref(edge_ref) {}
  1226         : _from(from), _to(to), _edge_ref(edge_ref) {}
  1194 
  1227 
  1290     /// Node type of the source graph with the red item set, while the
  1323     /// Node type of the source graph with the red item set, while the
  1291     /// value type is the Node type of the destination graph.
  1324     /// value type is the Node type of the destination graph.
  1292     template <typename RedRef>
  1325     template <typename RedRef>
  1293     BpGraphCopy& redRef(RedRef& map) {
  1326     BpGraphCopy& redRef(RedRef& map) {
  1294       _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
  1327       _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
  1295                           NodeRefMap, RedRef>(map));
  1328                           RedNodeRefMap, RedRef>(map));
  1296       return *this;
  1329       return *this;
  1297     }
  1330     }
  1298 
  1331 
  1299     /// \brief Copy the red node cross references into the given map.
  1332     /// \brief Copy the red node cross references into the given map.
  1300     ///
  1333     ///
  1304     /// the red item set, while the value type is the Node type of the
  1337     /// the red item set, while the value type is the Node type of the
  1305     /// source graph.
  1338     /// source graph.
  1306     template <typename RedCrossRef>
  1339     template <typename RedCrossRef>
  1307     BpGraphCopy& redCrossRef(RedCrossRef& map) {
  1340     BpGraphCopy& redCrossRef(RedCrossRef& map) {
  1308       _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
  1341       _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
  1309                           NodeRefMap, RedCrossRef>(map));
  1342                           RedNodeRefMap, RedCrossRef>(map));
  1310       return *this;
  1343       return *this;
  1311     }
  1344     }
  1312 
  1345 
  1313     /// \brief Make a copy of the given red node map.
  1346     /// \brief Make a copy of the given red node map.
  1314     ///
  1347     ///
  1319     /// the original map \c map should be the Node type of the source
  1352     /// the original map \c map should be the Node type of the source
  1320     /// graph.
  1353     /// graph.
  1321     template <typename FromMap, typename ToMap>
  1354     template <typename FromMap, typename ToMap>
  1322     BpGraphCopy& redMap(const FromMap& map, ToMap& tmap) {
  1355     BpGraphCopy& redMap(const FromMap& map, ToMap& tmap) {
  1323       _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
  1356       _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
  1324                            NodeRefMap, FromMap, ToMap>(map, tmap));
  1357                           RedNodeRefMap, FromMap, ToMap>(map, tmap));
       
  1358       return *this;
       
  1359     }
       
  1360 
       
  1361     /// \brief Make a copy of the given red node.
       
  1362     ///
       
  1363     /// This function makes a copy of the given red node.
       
  1364     BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) {
       
  1365       _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode,
       
  1366                           RedNodeRefMap, TRedNode>(node, tnode));
  1325       return *this;
  1367       return *this;
  1326     }
  1368     }
  1327 
  1369 
  1328     /// \brief Copy the blue node references into the given map.
  1370     /// \brief Copy the blue node references into the given map.
  1329     ///
  1371     ///
  1332     /// Node type of the source graph with the blue item set, while the
  1374     /// Node type of the source graph with the blue item set, while the
  1333     /// value type is the Node type of the destination graph.
  1375     /// value type is the Node type of the destination graph.
  1334     template <typename BlueRef>
  1376     template <typename BlueRef>
  1335     BpGraphCopy& blueRef(BlueRef& map) {
  1377     BpGraphCopy& blueRef(BlueRef& map) {
  1336       _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
  1378       _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
  1337                            NodeRefMap, BlueRef>(map));
  1379                            BlueNodeRefMap, BlueRef>(map));
  1338       return *this;
  1380       return *this;
  1339     }
  1381     }
  1340 
  1382 
  1341     /// \brief Copy the blue node cross references into the given map.
  1383     /// \brief Copy the blue node cross references into the given map.
  1342     ///
  1384     ///
  1346     /// the blue item set, while the value type is the Node type of the
  1388     /// the blue item set, while the value type is the Node type of the
  1347     /// source graph.
  1389     /// source graph.
  1348     template <typename BlueCrossRef>
  1390     template <typename BlueCrossRef>
  1349     BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
  1391     BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
  1350       _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
  1392       _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
  1351                            NodeRefMap, BlueCrossRef>(map));
  1393                            BlueNodeRefMap, BlueCrossRef>(map));
  1352       return *this;
  1394       return *this;
  1353     }
  1395     }
  1354 
  1396 
  1355     /// \brief Make a copy of the given blue node map.
  1397     /// \brief Make a copy of the given blue node map.
  1356     ///
  1398     ///
  1361     /// the original map \c map should be the Node type of the source
  1403     /// the original map \c map should be the Node type of the source
  1362     /// graph.
  1404     /// graph.
  1363     template <typename FromMap, typename ToMap>
  1405     template <typename FromMap, typename ToMap>
  1364     BpGraphCopy& blueMap(const FromMap& map, ToMap& tmap) {
  1406     BpGraphCopy& blueMap(const FromMap& map, ToMap& tmap) {
  1365       _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
  1407       _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
  1366                            NodeRefMap, FromMap, ToMap>(map, tmap));
  1408                            BlueNodeRefMap, FromMap, ToMap>(map, tmap));
       
  1409       return *this;
       
  1410     }
       
  1411 
       
  1412     /// \brief Make a copy of the given blue node.
       
  1413     ///
       
  1414     /// This function makes a copy of the given blue node.
       
  1415     BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) {
       
  1416       _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode,
       
  1417                            BlueNodeRefMap, TBlueNode>(node, tnode));
  1367       return *this;
  1418       return *this;
  1368     }
  1419     }
  1369 
  1420 
  1370     /// \brief Copy the arc references into the given map.
  1421     /// \brief Copy the arc references into the given map.
  1371     ///
  1422     ///
  1468     /// \brief Execute copying.
  1519     /// \brief Execute copying.
  1469     ///
  1520     ///
  1470     /// This function executes the copying of the graph along with the
  1521     /// This function executes the copying of the graph along with the
  1471     /// copying of the assigned data.
  1522     /// copying of the assigned data.
  1472     void run() {
  1523     void run() {
  1473       NodeRefMap nodeRefMap(_from);
  1524       RedNodeRefMap redNodeRefMap(_from);
       
  1525       BlueNodeRefMap blueNodeRefMap(_from);
       
  1526       NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap);
  1474       EdgeRefMap edgeRefMap(_from);
  1527       EdgeRefMap edgeRefMap(_from);
  1475       ArcRefMap arcRefMap(_from, _to, edgeRefMap);
  1528       ArcRefMap arcRefMap(_from, _to, edgeRefMap);
  1476       _core_bits::BpGraphCopySelector<To>::
  1529       _core_bits::BpGraphCopySelector<To>::
  1477         copy(_from, _to, nodeRefMap, edgeRefMap);
  1530         copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap);
  1478       for (int i = 0; i < int(_node_maps.size()); ++i) {
  1531       for (int i = 0; i < int(_node_maps.size()); ++i) {
  1479         _node_maps[i]->copy(_from, nodeRefMap);
  1532         _node_maps[i]->copy(_from, nodeRefMap);
  1480       }
  1533       }
  1481       for (int i = 0; i < int(_red_maps.size()); ++i) {
  1534       for (int i = 0; i < int(_red_maps.size()); ++i) {
  1482         _red_maps[i]->copy(_from, nodeRefMap);
  1535         _red_maps[i]->copy(_from, redNodeRefMap);
  1483       }
  1536       }
  1484       for (int i = 0; i < int(_blue_maps.size()); ++i) {
  1537       for (int i = 0; i < int(_blue_maps.size()); ++i) {
  1485         _blue_maps[i]->copy(_from, nodeRefMap);
  1538         _blue_maps[i]->copy(_from, blueNodeRefMap);
  1486       }
  1539       }
  1487       for (int i = 0; i < int(_edge_maps.size()); ++i) {
  1540       for (int i = 0; i < int(_edge_maps.size()); ++i) {
  1488         _edge_maps[i]->copy(_from, edgeRefMap);
  1541         _edge_maps[i]->copy(_from, edgeRefMap);
  1489       }
  1542       }
  1490       for (int i = 0; i < int(_arc_maps.size()); ++i) {
  1543       for (int i = 0; i < int(_arc_maps.size()); ++i) {
  1498     To& _to;
  1551     To& _to;
  1499 
  1552 
  1500     std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
  1553     std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
  1501       _node_maps;
  1554       _node_maps;
  1502 
  1555 
  1503     std::vector<_core_bits::MapCopyBase<From, RedNode, NodeRefMap>* >
  1556     std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* >
  1504       _red_maps;
  1557       _red_maps;
  1505 
  1558 
  1506     std::vector<_core_bits::MapCopyBase<From, BlueNode, NodeRefMap>* >
  1559     std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* >
  1507       _blue_maps;
  1560       _blue_maps;
  1508 
  1561 
  1509     std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
  1562     std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
  1510       _arc_maps;
  1563       _arc_maps;
  1511 
  1564