COIN-OR::LEMON - Graph Library

Ticket #150: from-to_8ab073e5c4f9.patch

File from-to_8ab073e5c4f9.patch, 15.3 KB (added by Peter Kovacs, 11 years ago)
  • lemon/core.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1222417571 -7200
    # Node ID 8ab073e5c4f9475051e71dfb5564c8524241957e
    # Parent  fe86041c013c5fbc02bf1ac64774204dd415394b
    Using from-to parameter order in graph copying tools (ticket #150)
    
    diff --git a/lemon/core.h b/lemon/core.h
    a b  
    308308    };
    309309
    310310    template <typename Digraph, typename Item, typename RefMap,
    311               typename ToMap, typename FromMap>
     311              typename FromMap, typename ToMap>
    312312    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
    313313    public:
    314314
    315       MapCopy(ToMap& tmap, const FromMap& map)
    316         : _tmap(tmap), _map(map) {}
     315      MapCopy(const FromMap& map, ToMap& tmap)
     316        : _map(map), _tmap(tmap) {}
    317317
    318318      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
    319319        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
     
    323323      }
    324324
    325325    private:
     326      const FromMap& _map;
    326327      ToMap& _tmap;
    327       const FromMap& _map;
    328328    };
    329329
    330330    template <typename Digraph, typename Item, typename RefMap, typename It>
    331331    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
    332332    public:
    333333
    334       ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
     334      ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
    335335
    336336      virtual void copy(const Digraph&, const RefMap& refMap) {
    337337        _it = refMap[_item];
    338338      }
    339339
    340340    private:
     341      Item _item;
    341342      It& _it;
    342       Item _item;
    343343    };
    344344
    345345    template <typename Digraph, typename Item, typename RefMap, typename Ref>
     
    380380    template <typename Digraph, typename Enable = void>
    381381    struct CopyDigraphSelector {
    382382      template <typename From, typename NodeRefMap, typename ArcRefMap>
    383       static void copy(Digraph &to, const From& from,
     383      static void copy(const From& from, Digraph &to,
    384384                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
    385385        for (typename From::NodeIt it(from); it != INVALID; ++it) {
    386386          nodeRefMap[it] = to.addNode();
     
    398398      typename enable_if<typename Digraph::BuildTag, void>::type>
    399399    {
    400400      template <typename From, typename NodeRefMap, typename ArcRefMap>
    401       static void copy(Digraph &to, const From& from,
     401      static void copy(const From& from, Digraph &to,
    402402                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
    403403        to.build(from, nodeRefMap, arcRefMap);
    404404      }
     
    407407    template <typename Graph, typename Enable = void>
    408408    struct CopyGraphSelector {
    409409      template <typename From, typename NodeRefMap, typename EdgeRefMap>
    410       static void copy(Graph &to, const From& from,
     410      static void copy(const From& from, Graph &to,
    411411                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
    412412        for (typename From::NodeIt it(from); it != INVALID; ++it) {
    413413          nodeRefMap[it] = to.addNode();
     
    425425      typename enable_if<typename Graph::BuildTag, void>::type>
    426426    {
    427427      template <typename From, typename NodeRefMap, typename EdgeRefMap>
    428       static void copy(Graph &to, const From& from,
     428      static void copy(const From& from, Graph &to,
    429429                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
    430430        to.build(from, nodeRefMap, edgeRefMap);
    431431      }
     
    450450  ///
    451451  /// The next code copies a digraph with several data:
    452452  ///\code
    453   ///  CopyDigraph<NewGraph, OrigGraph> cg(new_graph, orig_graph);
     453  ///  CopyDigraph<OrigGraph, NewGraph> cg(orig_graph, new_graph);
    454454  ///  // Create references for the nodes
    455455  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
    456456  ///  cg.nodeRef(nr);
     
    460460  ///  // Copy an arc map
    461461  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
    462462  ///  NewGraph::ArcMap<double> namap(new_graph);
    463   ///  cg.arcMap(namap, oamap);
     463  ///  cg.arcMap(oamap, namap);
    464464  ///  // Copy a node
    465465  ///  OrigGraph::Node on;
    466466  ///  NewGraph::Node nn;
    467   ///  cg.node(nn, on);
     467  ///  cg.node(on, nn);
    468468  ///  // Execute copying
    469469  ///  cg.run();
    470470  ///\endcode
    471   template <typename To, typename From>
     471  template <typename From, typename To>
    472472  class CopyDigraph {
    473473  private:
    474474
     
    489489    ///
    490490    /// Constructor of CopyDigraph for copying the content of the
    491491    /// \c from digraph into the \c to digraph.
    492     CopyDigraph(To& to, const From& from)
     492    CopyDigraph(const From& from, To& to)
    493493      : _from(from), _to(to) {}
    494494
    495495    /// \brief Destructor of CopyDigraph
     
    538538    /// The key type of the new map \c tmap should be the Node type of the
    539539    /// destination digraph, and the key type of the original map \c map
    540540    /// should be the Node type of the source digraph.
    541     template <typename ToMap, typename FromMap>
    542     CopyDigraph& nodeMap(ToMap& tmap, const FromMap& map) {
     541    template <typename FromMap, typename ToMap>
     542    CopyDigraph& nodeMap(const FromMap& map, ToMap& tmap) {
    543543      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
    544                            NodeRefMap, ToMap, FromMap>(tmap, map));
     544                           NodeRefMap, FromMap, ToMap>(map, tmap));
    545545      return *this;
    546546    }
    547547
    548548    /// \brief Make a copy of the given node.
    549549    ///
    550550    /// This function makes a copy of the given node.
    551     CopyDigraph& node(TNode& tnode, const Node& node) {
     551    CopyDigraph& node(const Node& node, TNode& tnode) {
    552552      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
    553                            NodeRefMap, TNode>(tnode, node));
     553                           NodeRefMap, TNode>(node, tnode));
    554554      return *this;
    555555    }
    556556
     
    587587    /// The key type of the new map \c tmap should be the Arc type of the
    588588    /// destination digraph, and the key type of the original map \c map
    589589    /// should be the Arc type of the source digraph.
    590     template <typename ToMap, typename FromMap>
    591     CopyDigraph& arcMap(ToMap& tmap, const FromMap& map) {
     590    template <typename FromMap, typename ToMap>
     591    CopyDigraph& arcMap(const FromMap& map, ToMap& tmap) {
    592592      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
    593                           ArcRefMap, ToMap, FromMap>(tmap, map));
     593                          ArcRefMap, FromMap, ToMap>(map, tmap));
    594594      return *this;
    595595    }
    596596
    597597    /// \brief Make a copy of the given arc.
    598598    ///
    599599    /// This function makes a copy of the given arc.
    600     CopyDigraph& arc(TArc& tarc, const Arc& arc) {
     600    CopyDigraph& arc(const Arc& arc, TArc& tarc) {
    601601      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
    602                           ArcRefMap, TArc>(tarc, arc));
     602                          ArcRefMap, TArc>(arc, tarc));
    603603      return *this;
    604604    }
    605605
     
    611611      NodeRefMap nodeRefMap(_from);
    612612      ArcRefMap arcRefMap(_from);
    613613      _core_bits::CopyDigraphSelector<To>::
    614         copy(_to, _from, nodeRefMap, arcRefMap);
     614        copy(_from, _to, nodeRefMap, arcRefMap);
    615615      for (int i = 0; i < int(_node_maps.size()); ++i) {
    616616        _node_maps[i]->copy(_from, nodeRefMap);
    617617      }
     
    639639  /// The complete usage of it is detailed in the CopyDigraph class, but
    640640  /// a short example shows a basic work:
    641641  ///\code
    642   /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(acr).run();
     642  /// copyDigraph(src, trg).nodeRef(nr).arcCrossRef(acr).run();
    643643  ///\endcode
    644644  ///
    645645  /// After the copy the \c nr map will contain the mapping from the
     
    648648  /// to the arcs of the \c from digraph.
    649649  ///
    650650  /// \see CopyDigraph
    651   template <typename To, typename From>
    652   CopyDigraph<To, From> copyDigraph(To& to, const From& from) {
    653     return CopyDigraph<To, From>(to, from);
     651  template <typename From, typename To>
     652  CopyDigraph<From, To> copyDigraph(const From& from, To& to) {
     653    return CopyDigraph<From, To>(from, to);
    654654  }
    655655
    656656  /// \brief Class to copy a graph.
     
    670670  ///
    671671  /// The next code copies a graph with several data:
    672672  ///\code
    673   ///  CopyGraph<NewGraph, OrigGraph> cg(new_graph, orig_graph);
     673  ///  CopyGraph<OrigGraph, NewGraph> cg(orig_graph, new_graph);
    674674  ///  // Create references for the nodes
    675675  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
    676676  ///  cg.nodeRef(nr);
     
    680680  ///  // Copy an edge map
    681681  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
    682682  ///  NewGraph::EdgeMap<double> nemap(new_graph);
    683   ///  cg.edgeMap(nemap, oemap);
     683  ///  cg.edgeMap(oemap, nemap);
    684684  ///  // Copy a node
    685685  ///  OrigGraph::Node on;
    686686  ///  NewGraph::Node nn;
    687   ///  cg.node(nn, on);
     687  ///  cg.node(on, nn);
    688688  ///  // Execute copying
    689689  ///  cg.run();
    690690  ///\endcode
    691   template <typename To, typename From>
     691  template <typename From, typename To>
    692692  class CopyGraph {
    693693  private:
    694694
     
    707707    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
    708708
    709709    struct ArcRefMap {
    710       ArcRefMap(const To& to, const From& from,
     710      ArcRefMap(const From& from, const To& to,
    711711                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
    712         : _to(to), _from(from),
     712        : _from(from), _to(to),
    713713          _edge_ref(edge_ref), _node_ref(node_ref) {}
    714714
    715715      typedef typename From::Arc Key;
     
    723723        return _to.direct(_edge_ref[key], forward);
    724724      }
    725725
     726      const From& _from;
    726727      const To& _to;
    727       const From& _from;
    728728      const EdgeRefMap& _edge_ref;
    729729      const NodeRefMap& _node_ref;
    730730    };
     
    735735    ///
    736736    /// Constructor of CopyGraph for copying the content of the
    737737    /// \c from graph into the \c to graph.
    738     CopyGraph(To& to, const From& from)
     738    CopyGraph(const From& from, To& to)
    739739      : _from(from), _to(to) {}
    740740
    741741    /// \brief Destructor of CopyGraph
     
    786786    /// The key type of the new map \c tmap should be the Node type of the
    787787    /// destination graph, and the key type of the original map \c map
    788788    /// should be the Node type of the source graph.
    789     template <typename ToMap, typename FromMap>
    790     CopyGraph& nodeMap(ToMap& tmap, const FromMap& map) {
     789    template <typename FromMap, typename ToMap>
     790    CopyGraph& nodeMap(const FromMap& map, ToMap& tmap) {
    791791      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
    792                            NodeRefMap, ToMap, FromMap>(tmap, map));
     792                           NodeRefMap, FromMap, ToMap>(map, tmap));
    793793      return *this;
    794794    }
    795795
    796796    /// \brief Make a copy of the given node.
    797797    ///
    798798    /// This function makes a copy of the given node.
    799     CopyGraph& node(TNode& tnode, const Node& node) {
     799    CopyGraph& node(const Node& node, TNode& tnode) {
    800800      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
    801                            NodeRefMap, TNode>(tnode, node));
     801                           NodeRefMap, TNode>(node, tnode));
    802802      return *this;
    803803    }
    804804
     
    835835    /// The key type of the new map \c tmap should be the Arc type of the
    836836    /// destination graph, and the key type of the original map \c map
    837837    /// should be the Arc type of the source graph.
    838     template <typename ToMap, typename FromMap>
    839     CopyGraph& arcMap(ToMap& tmap, const FromMap& map) {
     838    template <typename FromMap, typename ToMap>
     839    CopyGraph& arcMap(const FromMap& map, ToMap& tmap) {
    840840      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
    841                           ArcRefMap, ToMap, FromMap>(tmap, map));
     841                          ArcRefMap, FromMap, ToMap>(map, tmap));
    842842      return *this;
    843843    }
    844844
    845845    /// \brief Make a copy of the given arc.
    846846    ///
    847847    /// This function makes a copy of the given arc.
    848     CopyGraph& arc(TArc& tarc, const Arc& arc) {
     848    CopyGraph& arc(const Arc& arc, TArc& tarc) {
    849849      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
    850                           ArcRefMap, TArc>(tarc, arc));
     850                          ArcRefMap, TArc>(arc, tarc));
    851851      return *this;
    852852    }
    853853
     
    884884    /// The key type of the new map \c tmap should be the Edge type of the
    885885    /// destination graph, and the key type of the original map \c map
    886886    /// should be the Edge type of the source graph.
    887     template <typename ToMap, typename FromMap>
    888     CopyGraph& edgeMap(ToMap& tmap, const FromMap& map) {
     887    template <typename FromMap, typename ToMap>
     888    CopyGraph& edgeMap(const FromMap& map, ToMap& tmap) {
    889889      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
    890                            EdgeRefMap, ToMap, FromMap>(tmap, map));
     890                           EdgeRefMap, FromMap, ToMap>(map, tmap));
    891891      return *this;
    892892    }
    893893
    894894    /// \brief Make a copy of the given edge.
    895895    ///
    896896    /// This function makes a copy of the given edge.
    897     CopyGraph& edge(TEdge& tedge, const Edge& edge) {
     897    CopyGraph& edge(const Edge& edge, TEdge& tedge) {
    898898      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
    899                            EdgeRefMap, TEdge>(tedge, edge));
     899                           EdgeRefMap, TEdge>(edge, tedge));
    900900      return *this;
    901901    }
    902902
     
    907907    void run() {
    908908      NodeRefMap nodeRefMap(_from);
    909909      EdgeRefMap edgeRefMap(_from);
    910       ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap);
     910      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
    911911      _core_bits::CopyGraphSelector<To>::
    912         copy(_to, _from, nodeRefMap, edgeRefMap);
     912        copy(_from, _to, nodeRefMap, edgeRefMap);
    913913      for (int i = 0; i < int(_node_maps.size()); ++i) {
    914914        _node_maps[i]->copy(_from, nodeRefMap);
    915915      }
     
    943943  /// The complete usage of it is detailed in the CopyGraph class,
    944944  /// but a short example shows a basic work:
    945945  ///\code
    946   /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
     946  /// copyGraph(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
    947947  ///\endcode
    948948  ///
    949949  /// After the copy the \c nr map will contain the mapping from the
     
    952952  /// to the edges of the \c from graph.
    953953  ///
    954954  /// \see CopyGraph
    955   template <typename To, typename From>
    956   CopyGraph<To, From>
    957   copyGraph(To& to, const From& from) {
    958     return CopyGraph<To, From>(to, from);
     955  template <typename From, typename To>
     956  CopyGraph<From, To>
     957  copyGraph(const From& from, To& to) {
     958    return CopyGraph<From, To>(from, to);
    959959  }
    960960
    961961  namespace _core_bits {
  • test/graph_copy_test.cc

    diff --git a/test/graph_copy_test.cc b/test/graph_copy_test.cc
    a b  
    6363  ListDigraph::NodeMap<SmartDigraph::Node> ncr(to);
    6464  ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to);
    6565
    66   CopyDigraph<ListDigraph, SmartDigraph>(to, from).
    67     nodeMap(tnm, fnm).arcMap(tam, fam).
     66  copyDigraph(from, to).
     67    nodeMap(fnm, tnm).arcMap(fam, tam).
    6868    nodeRef(nr).arcRef(er).
    6969    nodeCrossRef(ncr).arcCrossRef(ecr).
    70     node(tn, fn).arc(ta, fa).run();
     70    node(fn, tn).arc(fa, ta).run();
    7171
    7272  for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
    7373    check(ncr[nr[it]] == it, "Wrong copy.");
     
    138138  ListGraph::ArcMap<SmartGraph::Arc> acr(to);
    139139  ListGraph::EdgeMap<SmartGraph::Edge> ecr(to);
    140140
    141   CopyGraph<ListGraph, SmartGraph>(to, from).
    142     nodeMap(tnm, fnm).arcMap(tam, fam).edgeMap(tem, fem).
     141  copyGraph(from, to).
     142    nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem).
    143143    nodeRef(nr).arcRef(ar).edgeRef(er).
    144144    nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
    145     node(tn, fn).arc(ta, fa).edge(te, fe).run();
     145    node(fn, tn).arc(fa, ta).edge(fe, te).run();
    146146
    147147  for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
    148148    check(ncr[nr[it]] == it, "Wrong copy.");