lemon/core.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 11 Aug 2013 15:28:12 +0200
changeset 1099 ad40f7d32846
parent 998 7fdaa05a69a1
parent 1074 6039b32a2351
child 1086 97f1760dcd13
permissions -rw-r--r--
Merge >=1.2 branch heads
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_CORE_H
    20 #define LEMON_CORE_H
    21 
    22 #include <vector>
    23 #include <algorithm>
    24 
    25 #include <lemon/config.h>
    26 #include <lemon/bits/enable_if.h>
    27 #include <lemon/bits/traits.h>
    28 #include <lemon/assert.h>
    29 
    30 // Disable the following warnings when compiling with MSVC:
    31 // C4250: 'class1' : inherits 'class2::member' via dominance
    32 // C4355: 'this' : used in base member initializer list
    33 // C4503: 'function' : decorated name length exceeded, name was truncated
    34 // C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
    35 // C4996: 'function': was declared deprecated
    36 #ifdef _MSC_VER
    37 #pragma warning( disable : 4250 4355 4503 4800 4996 )
    38 #endif
    39 
    40 #ifdef __GNUC__
    41 #define GCC_VERSION (__GNUC__ * 10000                   \
    42                      + __GNUC_MINOR__ * 100             \
    43                      + __GNUC_PATCHLEVEL__)
    44 #endif
    45 
    46 #if GCC_VERSION >= 40800
    47 // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8
    48 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
    49 #endif
    50 
    51 ///\file
    52 ///\brief LEMON core utilities.
    53 ///
    54 ///This header file contains core utilities for LEMON.
    55 ///It is automatically included by all graph types, therefore it usually
    56 ///do not have to be included directly.
    57 
    58 namespace lemon {
    59 
    60   /// \brief Dummy type to make it easier to create invalid iterators.
    61   ///
    62   /// Dummy type to make it easier to create invalid iterators.
    63   /// See \ref INVALID for the usage.
    64   struct Invalid {
    65   public:
    66     bool operator==(Invalid) { return true;  }
    67     bool operator!=(Invalid) { return false; }
    68     bool operator< (Invalid) { return false; }
    69   };
    70 
    71   /// \brief Invalid iterators.
    72   ///
    73   /// \ref Invalid is a global type that converts to each iterator
    74   /// in such a way that the value of the target iterator will be invalid.
    75 #ifdef LEMON_ONLY_TEMPLATES
    76   const Invalid INVALID = Invalid();
    77 #else
    78   extern const Invalid INVALID;
    79 #endif
    80 
    81   /// \addtogroup gutils
    82   /// @{
    83 
    84   ///Create convenience typedefs for the digraph types and iterators
    85 
    86   ///This \c \#define creates convenient type definitions for the following
    87   ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
    88   ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
    89   ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
    90   ///
    91   ///\note If the graph type is a dependent type, ie. the graph type depend
    92   ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
    93   ///macro.
    94 #define DIGRAPH_TYPEDEFS(Digraph)                                       \
    95   typedef Digraph::Node Node;                                           \
    96   typedef Digraph::NodeIt NodeIt;                                       \
    97   typedef Digraph::Arc Arc;                                             \
    98   typedef Digraph::ArcIt ArcIt;                                         \
    99   typedef Digraph::InArcIt InArcIt;                                     \
   100   typedef Digraph::OutArcIt OutArcIt;                                   \
   101   typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
   102   typedef Digraph::NodeMap<int> IntNodeMap;                             \
   103   typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
   104   typedef Digraph::ArcMap<bool> BoolArcMap;                             \
   105   typedef Digraph::ArcMap<int> IntArcMap;                               \
   106   typedef Digraph::ArcMap<double> DoubleArcMap
   107 
   108   ///Create convenience typedefs for the digraph types and iterators
   109 
   110   ///\see DIGRAPH_TYPEDEFS
   111   ///
   112   ///\note Use this macro, if the graph type is a dependent type,
   113   ///ie. the graph type depend on a template parameter.
   114 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
   115   typedef typename Digraph::Node Node;                                  \
   116   typedef typename Digraph::NodeIt NodeIt;                              \
   117   typedef typename Digraph::Arc Arc;                                    \
   118   typedef typename Digraph::ArcIt ArcIt;                                \
   119   typedef typename Digraph::InArcIt InArcIt;                            \
   120   typedef typename Digraph::OutArcIt OutArcIt;                          \
   121   typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
   122   typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
   123   typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
   124   typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
   125   typedef typename Digraph::template ArcMap<int> IntArcMap;             \
   126   typedef typename Digraph::template ArcMap<double> DoubleArcMap
   127 
   128   ///Create convenience typedefs for the graph types and iterators
   129 
   130   ///This \c \#define creates the same convenient type definitions as defined
   131   ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
   132   ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
   133   ///\c DoubleEdgeMap.
   134   ///
   135   ///\note If the graph type is a dependent type, ie. the graph type depend
   136   ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
   137   ///macro.
   138 #define GRAPH_TYPEDEFS(Graph)                                           \
   139   DIGRAPH_TYPEDEFS(Graph);                                              \
   140   typedef Graph::Edge Edge;                                             \
   141   typedef Graph::EdgeIt EdgeIt;                                         \
   142   typedef Graph::IncEdgeIt IncEdgeIt;                                   \
   143   typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
   144   typedef Graph::EdgeMap<int> IntEdgeMap;                               \
   145   typedef Graph::EdgeMap<double> DoubleEdgeMap
   146 
   147   ///Create convenience typedefs for the graph types and iterators
   148 
   149   ///\see GRAPH_TYPEDEFS
   150   ///
   151   ///\note Use this macro, if the graph type is a dependent type,
   152   ///ie. the graph type depend on a template parameter.
   153 #define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \
   154   TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \
   155   typedef typename Graph::Edge Edge;                                    \
   156   typedef typename Graph::EdgeIt EdgeIt;                                \
   157   typedef typename Graph::IncEdgeIt IncEdgeIt;                          \
   158   typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \
   159   typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
   160   typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
   161 
   162   /// \brief Function to count the items in a graph.
   163   ///
   164   /// This function counts the items (nodes, arcs etc.) in a graph.
   165   /// The complexity of the function is linear because
   166   /// it iterates on all of the items.
   167   template <typename Graph, typename Item>
   168   inline int countItems(const Graph& g) {
   169     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
   170     int num = 0;
   171     for (ItemIt it(g); it != INVALID; ++it) {
   172       ++num;
   173     }
   174     return num;
   175   }
   176 
   177   // Node counting:
   178 
   179   namespace _core_bits {
   180 
   181     template <typename Graph, typename Enable = void>
   182     struct CountNodesSelector {
   183       static int count(const Graph &g) {
   184         return countItems<Graph, typename Graph::Node>(g);
   185       }
   186     };
   187 
   188     template <typename Graph>
   189     struct CountNodesSelector<
   190       Graph, typename
   191       enable_if<typename Graph::NodeNumTag, void>::type>
   192     {
   193       static int count(const Graph &g) {
   194         return g.nodeNum();
   195       }
   196     };
   197   }
   198 
   199   /// \brief Function to count the nodes in the graph.
   200   ///
   201   /// This function counts the nodes in the graph.
   202   /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
   203   /// graph structures it is specialized to run in <em>O</em>(1).
   204   ///
   205   /// \note If the graph contains a \c nodeNum() member function and a
   206   /// \c NodeNumTag tag then this function calls directly the member
   207   /// function to query the cardinality of the node set.
   208   template <typename Graph>
   209   inline int countNodes(const Graph& g) {
   210     return _core_bits::CountNodesSelector<Graph>::count(g);
   211   }
   212 
   213   // Arc counting:
   214 
   215   namespace _core_bits {
   216 
   217     template <typename Graph, typename Enable = void>
   218     struct CountArcsSelector {
   219       static int count(const Graph &g) {
   220         return countItems<Graph, typename Graph::Arc>(g);
   221       }
   222     };
   223 
   224     template <typename Graph>
   225     struct CountArcsSelector<
   226       Graph,
   227       typename enable_if<typename Graph::ArcNumTag, void>::type>
   228     {
   229       static int count(const Graph &g) {
   230         return g.arcNum();
   231       }
   232     };
   233   }
   234 
   235   /// \brief Function to count the arcs in the graph.
   236   ///
   237   /// This function counts the arcs in the graph.
   238   /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
   239   /// graph structures it is specialized to run in <em>O</em>(1).
   240   ///
   241   /// \note If the graph contains a \c arcNum() member function and a
   242   /// \c ArcNumTag tag then this function calls directly the member
   243   /// function to query the cardinality of the arc set.
   244   template <typename Graph>
   245   inline int countArcs(const Graph& g) {
   246     return _core_bits::CountArcsSelector<Graph>::count(g);
   247   }
   248 
   249   // Edge counting:
   250 
   251   namespace _core_bits {
   252 
   253     template <typename Graph, typename Enable = void>
   254     struct CountEdgesSelector {
   255       static int count(const Graph &g) {
   256         return countItems<Graph, typename Graph::Edge>(g);
   257       }
   258     };
   259 
   260     template <typename Graph>
   261     struct CountEdgesSelector<
   262       Graph,
   263       typename enable_if<typename Graph::EdgeNumTag, void>::type>
   264     {
   265       static int count(const Graph &g) {
   266         return g.edgeNum();
   267       }
   268     };
   269   }
   270 
   271   /// \brief Function to count the edges in the graph.
   272   ///
   273   /// This function counts the edges in the graph.
   274   /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
   275   /// graph structures it is specialized to run in <em>O</em>(1).
   276   ///
   277   /// \note If the graph contains a \c edgeNum() member function and a
   278   /// \c EdgeNumTag tag then this function calls directly the member
   279   /// function to query the cardinality of the edge set.
   280   template <typename Graph>
   281   inline int countEdges(const Graph& g) {
   282     return _core_bits::CountEdgesSelector<Graph>::count(g);
   283 
   284   }
   285 
   286 
   287   template <typename Graph, typename DegIt>
   288   inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
   289     int num = 0;
   290     for (DegIt it(_g, _n); it != INVALID; ++it) {
   291       ++num;
   292     }
   293     return num;
   294   }
   295 
   296   /// \brief Function to count the number of the out-arcs from node \c n.
   297   ///
   298   /// This function counts the number of the out-arcs from node \c n
   299   /// in the graph \c g.
   300   template <typename Graph>
   301   inline int countOutArcs(const Graph& g,  const typename Graph::Node& n) {
   302     return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
   303   }
   304 
   305   /// \brief Function to count the number of the in-arcs to node \c n.
   306   ///
   307   /// This function counts the number of the in-arcs to node \c n
   308   /// in the graph \c g.
   309   template <typename Graph>
   310   inline int countInArcs(const Graph& g,  const typename Graph::Node& n) {
   311     return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
   312   }
   313 
   314   /// \brief Function to count the number of the inc-edges to node \c n.
   315   ///
   316   /// This function counts the number of the inc-edges to node \c n
   317   /// in the undirected graph \c g.
   318   template <typename Graph>
   319   inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) {
   320     return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
   321   }
   322 
   323   namespace _core_bits {
   324 
   325     template <typename Digraph, typename Item, typename RefMap>
   326     class MapCopyBase {
   327     public:
   328       virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
   329 
   330       virtual ~MapCopyBase() {}
   331     };
   332 
   333     template <typename Digraph, typename Item, typename RefMap,
   334               typename FromMap, typename ToMap>
   335     class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
   336     public:
   337 
   338       MapCopy(const FromMap& map, ToMap& tmap)
   339         : _map(map), _tmap(tmap) {}
   340 
   341       virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   342         typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   343         for (ItemIt it(digraph); it != INVALID; ++it) {
   344           _tmap.set(refMap[it], _map[it]);
   345         }
   346       }
   347 
   348     private:
   349       const FromMap& _map;
   350       ToMap& _tmap;
   351     };
   352 
   353     template <typename Digraph, typename Item, typename RefMap, typename It>
   354     class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
   355     public:
   356 
   357       ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
   358 
   359       virtual void copy(const Digraph&, const RefMap& refMap) {
   360         _it = refMap[_item];
   361       }
   362 
   363     private:
   364       Item _item;
   365       It& _it;
   366     };
   367 
   368     template <typename Digraph, typename Item, typename RefMap, typename Ref>
   369     class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
   370     public:
   371 
   372       RefCopy(Ref& map) : _map(map) {}
   373 
   374       virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   375         typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   376         for (ItemIt it(digraph); it != INVALID; ++it) {
   377           _map.set(it, refMap[it]);
   378         }
   379       }
   380 
   381     private:
   382       Ref& _map;
   383     };
   384 
   385     template <typename Digraph, typename Item, typename RefMap,
   386               typename CrossRef>
   387     class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
   388     public:
   389 
   390       CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
   391 
   392       virtual void copy(const Digraph& digraph, const RefMap& refMap) {
   393         typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
   394         for (ItemIt it(digraph); it != INVALID; ++it) {
   395           _cmap.set(refMap[it], it);
   396         }
   397       }
   398 
   399     private:
   400       CrossRef& _cmap;
   401     };
   402 
   403     template <typename Digraph, typename Enable = void>
   404     struct DigraphCopySelector {
   405       template <typename From, typename NodeRefMap, typename ArcRefMap>
   406       static void copy(const From& from, Digraph &to,
   407                        NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
   408         to.clear();
   409         for (typename From::NodeIt it(from); it != INVALID; ++it) {
   410           nodeRefMap[it] = to.addNode();
   411         }
   412         for (typename From::ArcIt it(from); it != INVALID; ++it) {
   413           arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
   414                                     nodeRefMap[from.target(it)]);
   415         }
   416       }
   417     };
   418 
   419     template <typename Digraph>
   420     struct DigraphCopySelector<
   421       Digraph,
   422       typename enable_if<typename Digraph::BuildTag, void>::type>
   423     {
   424       template <typename From, typename NodeRefMap, typename ArcRefMap>
   425       static void copy(const From& from, Digraph &to,
   426                        NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
   427         to.build(from, nodeRefMap, arcRefMap);
   428       }
   429     };
   430 
   431     template <typename Graph, typename Enable = void>
   432     struct GraphCopySelector {
   433       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   434       static void copy(const From& from, Graph &to,
   435                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   436         to.clear();
   437         for (typename From::NodeIt it(from); it != INVALID; ++it) {
   438           nodeRefMap[it] = to.addNode();
   439         }
   440         for (typename From::EdgeIt it(from); it != INVALID; ++it) {
   441           edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
   442                                       nodeRefMap[from.v(it)]);
   443         }
   444       }
   445     };
   446 
   447     template <typename Graph>
   448     struct GraphCopySelector<
   449       Graph,
   450       typename enable_if<typename Graph::BuildTag, void>::type>
   451     {
   452       template <typename From, typename NodeRefMap, typename EdgeRefMap>
   453       static void copy(const From& from, Graph &to,
   454                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
   455         to.build(from, nodeRefMap, edgeRefMap);
   456       }
   457     };
   458 
   459   }
   460 
   461   /// \brief Class to copy a digraph.
   462   ///
   463   /// Class to copy a digraph to another digraph (duplicate a digraph). The
   464   /// simplest way of using it is through the \c digraphCopy() function.
   465   ///
   466   /// This class not only make a copy of a digraph, but it can create
   467   /// references and cross references between the nodes and arcs of
   468   /// the two digraphs, and it can copy maps to use with the newly created
   469   /// digraph.
   470   ///
   471   /// To make a copy from a digraph, first an instance of DigraphCopy
   472   /// should be created, then the data belongs to the digraph should
   473   /// assigned to copy. In the end, the \c run() member should be
   474   /// called.
   475   ///
   476   /// The next code copies a digraph with several data:
   477   ///\code
   478   ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
   479   ///  // Create references for the nodes
   480   ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
   481   ///  cg.nodeRef(nr);
   482   ///  // Create cross references (inverse) for the arcs
   483   ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
   484   ///  cg.arcCrossRef(acr);
   485   ///  // Copy an arc map
   486   ///  OrigGraph::ArcMap<double> oamap(orig_graph);
   487   ///  NewGraph::ArcMap<double> namap(new_graph);
   488   ///  cg.arcMap(oamap, namap);
   489   ///  // Copy a node
   490   ///  OrigGraph::Node on;
   491   ///  NewGraph::Node nn;
   492   ///  cg.node(on, nn);
   493   ///  // Execute copying
   494   ///  cg.run();
   495   ///\endcode
   496   template <typename From, typename To>
   497   class DigraphCopy {
   498   private:
   499 
   500     typedef typename From::Node Node;
   501     typedef typename From::NodeIt NodeIt;
   502     typedef typename From::Arc Arc;
   503     typedef typename From::ArcIt ArcIt;
   504 
   505     typedef typename To::Node TNode;
   506     typedef typename To::Arc TArc;
   507 
   508     typedef typename From::template NodeMap<TNode> NodeRefMap;
   509     typedef typename From::template ArcMap<TArc> ArcRefMap;
   510 
   511   public:
   512 
   513     /// \brief Constructor of DigraphCopy.
   514     ///
   515     /// Constructor of DigraphCopy for copying the content of the
   516     /// \c from digraph into the \c to digraph.
   517     DigraphCopy(const From& from, To& to)
   518       : _from(from), _to(to) {}
   519 
   520     /// \brief Destructor of DigraphCopy
   521     ///
   522     /// Destructor of DigraphCopy.
   523     ~DigraphCopy() {
   524       for (int i = 0; i < int(_node_maps.size()); ++i) {
   525         delete _node_maps[i];
   526       }
   527       for (int i = 0; i < int(_arc_maps.size()); ++i) {
   528         delete _arc_maps[i];
   529       }
   530 
   531     }
   532 
   533     /// \brief Copy the node references into the given map.
   534     ///
   535     /// This function copies the node references into the given map.
   536     /// The parameter should be a map, whose key type is the Node type of
   537     /// the source digraph, while the value type is the Node type of the
   538     /// destination digraph.
   539     template <typename NodeRef>
   540     DigraphCopy& nodeRef(NodeRef& map) {
   541       _node_maps.push_back(new _core_bits::RefCopy<From, Node,
   542                            NodeRefMap, NodeRef>(map));
   543       return *this;
   544     }
   545 
   546     /// \brief Copy the node cross references into the given map.
   547     ///
   548     /// This function copies the node cross references (reverse references)
   549     /// into the given map. The parameter should be a map, whose key type
   550     /// is the Node type of the destination digraph, while the value type is
   551     /// the Node type of the source digraph.
   552     template <typename NodeCrossRef>
   553     DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
   554       _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
   555                            NodeRefMap, NodeCrossRef>(map));
   556       return *this;
   557     }
   558 
   559     /// \brief Make a copy of the given node map.
   560     ///
   561     /// This function makes a copy of the given node map for the newly
   562     /// created digraph.
   563     /// The key type of the new map \c tmap should be the Node type of the
   564     /// destination digraph, and the key type of the original map \c map
   565     /// should be the Node type of the source digraph.
   566     template <typename FromMap, typename ToMap>
   567     DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
   568       _node_maps.push_back(new _core_bits::MapCopy<From, Node,
   569                            NodeRefMap, FromMap, ToMap>(map, tmap));
   570       return *this;
   571     }
   572 
   573     /// \brief Make a copy of the given node.
   574     ///
   575     /// This function makes a copy of the given node.
   576     DigraphCopy& node(const Node& node, TNode& tnode) {
   577       _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
   578                            NodeRefMap, TNode>(node, tnode));
   579       return *this;
   580     }
   581 
   582     /// \brief Copy the arc references into the given map.
   583     ///
   584     /// This function copies the arc references into the given map.
   585     /// The parameter should be a map, whose key type is the Arc type of
   586     /// the source digraph, while the value type is the Arc type of the
   587     /// destination digraph.
   588     template <typename ArcRef>
   589     DigraphCopy& arcRef(ArcRef& map) {
   590       _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
   591                           ArcRefMap, ArcRef>(map));
   592       return *this;
   593     }
   594 
   595     /// \brief Copy the arc cross references into the given map.
   596     ///
   597     /// This function copies the arc cross references (reverse references)
   598     /// into the given map. The parameter should be a map, whose key type
   599     /// is the Arc type of the destination digraph, while the value type is
   600     /// the Arc type of the source digraph.
   601     template <typename ArcCrossRef>
   602     DigraphCopy& arcCrossRef(ArcCrossRef& map) {
   603       _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
   604                           ArcRefMap, ArcCrossRef>(map));
   605       return *this;
   606     }
   607 
   608     /// \brief Make a copy of the given arc map.
   609     ///
   610     /// This function makes a copy of the given arc map for the newly
   611     /// created digraph.
   612     /// The key type of the new map \c tmap should be the Arc type of the
   613     /// destination digraph, and the key type of the original map \c map
   614     /// should be the Arc type of the source digraph.
   615     template <typename FromMap, typename ToMap>
   616     DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
   617       _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
   618                           ArcRefMap, FromMap, ToMap>(map, tmap));
   619       return *this;
   620     }
   621 
   622     /// \brief Make a copy of the given arc.
   623     ///
   624     /// This function makes a copy of the given arc.
   625     DigraphCopy& arc(const Arc& arc, TArc& tarc) {
   626       _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
   627                           ArcRefMap, TArc>(arc, tarc));
   628       return *this;
   629     }
   630 
   631     /// \brief Execute copying.
   632     ///
   633     /// This function executes the copying of the digraph along with the
   634     /// copying of the assigned data.
   635     void run() {
   636       NodeRefMap nodeRefMap(_from);
   637       ArcRefMap arcRefMap(_from);
   638       _core_bits::DigraphCopySelector<To>::
   639         copy(_from, _to, nodeRefMap, arcRefMap);
   640       for (int i = 0; i < int(_node_maps.size()); ++i) {
   641         _node_maps[i]->copy(_from, nodeRefMap);
   642       }
   643       for (int i = 0; i < int(_arc_maps.size()); ++i) {
   644         _arc_maps[i]->copy(_from, arcRefMap);
   645       }
   646     }
   647 
   648   protected:
   649 
   650     const From& _from;
   651     To& _to;
   652 
   653     std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
   654       _node_maps;
   655 
   656     std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
   657       _arc_maps;
   658 
   659   };
   660 
   661   /// \brief Copy a digraph to another digraph.
   662   ///
   663   /// This function copies a digraph to another digraph.
   664   /// The complete usage of it is detailed in the DigraphCopy class, but
   665   /// a short example shows a basic work:
   666   ///\code
   667   /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
   668   ///\endcode
   669   ///
   670   /// After the copy the \c nr map will contain the mapping from the
   671   /// nodes of the \c from digraph to the nodes of the \c to digraph and
   672   /// \c acr will contain the mapping from the arcs of the \c to digraph
   673   /// to the arcs of the \c from digraph.
   674   ///
   675   /// \see DigraphCopy
   676   template <typename From, typename To>
   677   DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
   678     return DigraphCopy<From, To>(from, to);
   679   }
   680 
   681   /// \brief Class to copy a graph.
   682   ///
   683   /// Class to copy a graph to another graph (duplicate a graph). The
   684   /// simplest way of using it is through the \c graphCopy() function.
   685   ///
   686   /// This class not only make a copy of a graph, but it can create
   687   /// references and cross references between the nodes, edges and arcs of
   688   /// the two graphs, and it can copy maps for using with the newly created
   689   /// graph.
   690   ///
   691   /// To make a copy from a graph, first an instance of GraphCopy
   692   /// should be created, then the data belongs to the graph should
   693   /// assigned to copy. In the end, the \c run() member should be
   694   /// called.
   695   ///
   696   /// The next code copies a graph with several data:
   697   ///\code
   698   ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
   699   ///  // Create references for the nodes
   700   ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
   701   ///  cg.nodeRef(nr);
   702   ///  // Create cross references (inverse) for the edges
   703   ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
   704   ///  cg.edgeCrossRef(ecr);
   705   ///  // Copy an edge map
   706   ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
   707   ///  NewGraph::EdgeMap<double> nemap(new_graph);
   708   ///  cg.edgeMap(oemap, nemap);
   709   ///  // Copy a node
   710   ///  OrigGraph::Node on;
   711   ///  NewGraph::Node nn;
   712   ///  cg.node(on, nn);
   713   ///  // Execute copying
   714   ///  cg.run();
   715   ///\endcode
   716   template <typename From, typename To>
   717   class GraphCopy {
   718   private:
   719 
   720     typedef typename From::Node Node;
   721     typedef typename From::NodeIt NodeIt;
   722     typedef typename From::Arc Arc;
   723     typedef typename From::ArcIt ArcIt;
   724     typedef typename From::Edge Edge;
   725     typedef typename From::EdgeIt EdgeIt;
   726 
   727     typedef typename To::Node TNode;
   728     typedef typename To::Arc TArc;
   729     typedef typename To::Edge TEdge;
   730 
   731     typedef typename From::template NodeMap<TNode> NodeRefMap;
   732     typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
   733 
   734     struct ArcRefMap {
   735       ArcRefMap(const From& from, const To& to,
   736                 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
   737         : _from(from), _to(to),
   738           _edge_ref(edge_ref), _node_ref(node_ref) {}
   739 
   740       typedef typename From::Arc Key;
   741       typedef typename To::Arc Value;
   742 
   743       Value operator[](const Key& key) const {
   744         bool forward = _from.u(key) != _from.v(key) ?
   745           _node_ref[_from.source(key)] ==
   746           _to.source(_to.direct(_edge_ref[key], true)) :
   747           _from.direction(key);
   748         return _to.direct(_edge_ref[key], forward);
   749       }
   750 
   751       const From& _from;
   752       const To& _to;
   753       const EdgeRefMap& _edge_ref;
   754       const NodeRefMap& _node_ref;
   755     };
   756 
   757   public:
   758 
   759     /// \brief Constructor of GraphCopy.
   760     ///
   761     /// Constructor of GraphCopy for copying the content of the
   762     /// \c from graph into the \c to graph.
   763     GraphCopy(const From& from, To& to)
   764       : _from(from), _to(to) {}
   765 
   766     /// \brief Destructor of GraphCopy
   767     ///
   768     /// Destructor of GraphCopy.
   769     ~GraphCopy() {
   770       for (int i = 0; i < int(_node_maps.size()); ++i) {
   771         delete _node_maps[i];
   772       }
   773       for (int i = 0; i < int(_arc_maps.size()); ++i) {
   774         delete _arc_maps[i];
   775       }
   776       for (int i = 0; i < int(_edge_maps.size()); ++i) {
   777         delete _edge_maps[i];
   778       }
   779     }
   780 
   781     /// \brief Copy the node references into the given map.
   782     ///
   783     /// This function copies the node references into the given map.
   784     /// The parameter should be a map, whose key type is the Node type of
   785     /// the source graph, while the value type is the Node type of the
   786     /// destination graph.
   787     template <typename NodeRef>
   788     GraphCopy& nodeRef(NodeRef& map) {
   789       _node_maps.push_back(new _core_bits::RefCopy<From, Node,
   790                            NodeRefMap, NodeRef>(map));
   791       return *this;
   792     }
   793 
   794     /// \brief Copy the node cross references into the given map.
   795     ///
   796     /// This function copies the node cross references (reverse references)
   797     /// into the given map. The parameter should be a map, whose key type
   798     /// is the Node type of the destination graph, while the value type is
   799     /// the Node type of the source graph.
   800     template <typename NodeCrossRef>
   801     GraphCopy& nodeCrossRef(NodeCrossRef& map) {
   802       _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
   803                            NodeRefMap, NodeCrossRef>(map));
   804       return *this;
   805     }
   806 
   807     /// \brief Make a copy of the given node map.
   808     ///
   809     /// This function makes a copy of the given node map for the newly
   810     /// created graph.
   811     /// The key type of the new map \c tmap should be the Node type of the
   812     /// destination graph, and the key type of the original map \c map
   813     /// should be the Node type of the source graph.
   814     template <typename FromMap, typename ToMap>
   815     GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
   816       _node_maps.push_back(new _core_bits::MapCopy<From, Node,
   817                            NodeRefMap, FromMap, ToMap>(map, tmap));
   818       return *this;
   819     }
   820 
   821     /// \brief Make a copy of the given node.
   822     ///
   823     /// This function makes a copy of the given node.
   824     GraphCopy& node(const Node& node, TNode& tnode) {
   825       _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
   826                            NodeRefMap, TNode>(node, tnode));
   827       return *this;
   828     }
   829 
   830     /// \brief Copy the arc references into the given map.
   831     ///
   832     /// This function copies the arc references into the given map.
   833     /// The parameter should be a map, whose key type is the Arc type of
   834     /// the source graph, while the value type is the Arc type of the
   835     /// destination graph.
   836     template <typename ArcRef>
   837     GraphCopy& arcRef(ArcRef& map) {
   838       _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
   839                           ArcRefMap, ArcRef>(map));
   840       return *this;
   841     }
   842 
   843     /// \brief Copy the arc cross references into the given map.
   844     ///
   845     /// This function copies the arc cross references (reverse references)
   846     /// into the given map. The parameter should be a map, whose key type
   847     /// is the Arc type of the destination graph, while the value type is
   848     /// the Arc type of the source graph.
   849     template <typename ArcCrossRef>
   850     GraphCopy& arcCrossRef(ArcCrossRef& map) {
   851       _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
   852                           ArcRefMap, ArcCrossRef>(map));
   853       return *this;
   854     }
   855 
   856     /// \brief Make a copy of the given arc map.
   857     ///
   858     /// This function makes a copy of the given arc map for the newly
   859     /// created graph.
   860     /// The key type of the new map \c tmap should be the Arc type of the
   861     /// destination graph, and the key type of the original map \c map
   862     /// should be the Arc type of the source graph.
   863     template <typename FromMap, typename ToMap>
   864     GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
   865       _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
   866                           ArcRefMap, FromMap, ToMap>(map, tmap));
   867       return *this;
   868     }
   869 
   870     /// \brief Make a copy of the given arc.
   871     ///
   872     /// This function makes a copy of the given arc.
   873     GraphCopy& arc(const Arc& arc, TArc& tarc) {
   874       _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
   875                           ArcRefMap, TArc>(arc, tarc));
   876       return *this;
   877     }
   878 
   879     /// \brief Copy the edge references into the given map.
   880     ///
   881     /// This function copies the edge references into the given map.
   882     /// The parameter should be a map, whose key type is the Edge type of
   883     /// the source graph, while the value type is the Edge type of the
   884     /// destination graph.
   885     template <typename EdgeRef>
   886     GraphCopy& edgeRef(EdgeRef& map) {
   887       _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
   888                            EdgeRefMap, EdgeRef>(map));
   889       return *this;
   890     }
   891 
   892     /// \brief Copy the edge cross references into the given map.
   893     ///
   894     /// This function copies the edge cross references (reverse references)
   895     /// into the given map. The parameter should be a map, whose key type
   896     /// is the Edge type of the destination graph, while the value type is
   897     /// the Edge type of the source graph.
   898     template <typename EdgeCrossRef>
   899     GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
   900       _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
   901                            Edge, EdgeRefMap, EdgeCrossRef>(map));
   902       return *this;
   903     }
   904 
   905     /// \brief Make a copy of the given edge map.
   906     ///
   907     /// This function makes a copy of the given edge map for the newly
   908     /// created graph.
   909     /// The key type of the new map \c tmap should be the Edge type of the
   910     /// destination graph, and the key type of the original map \c map
   911     /// should be the Edge type of the source graph.
   912     template <typename FromMap, typename ToMap>
   913     GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
   914       _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
   915                            EdgeRefMap, FromMap, ToMap>(map, tmap));
   916       return *this;
   917     }
   918 
   919     /// \brief Make a copy of the given edge.
   920     ///
   921     /// This function makes a copy of the given edge.
   922     GraphCopy& edge(const Edge& edge, TEdge& tedge) {
   923       _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
   924                            EdgeRefMap, TEdge>(edge, tedge));
   925       return *this;
   926     }
   927 
   928     /// \brief Execute copying.
   929     ///
   930     /// This function executes the copying of the graph along with the
   931     /// copying of the assigned data.
   932     void run() {
   933       NodeRefMap nodeRefMap(_from);
   934       EdgeRefMap edgeRefMap(_from);
   935       ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
   936       _core_bits::GraphCopySelector<To>::
   937         copy(_from, _to, nodeRefMap, edgeRefMap);
   938       for (int i = 0; i < int(_node_maps.size()); ++i) {
   939         _node_maps[i]->copy(_from, nodeRefMap);
   940       }
   941       for (int i = 0; i < int(_edge_maps.size()); ++i) {
   942         _edge_maps[i]->copy(_from, edgeRefMap);
   943       }
   944       for (int i = 0; i < int(_arc_maps.size()); ++i) {
   945         _arc_maps[i]->copy(_from, arcRefMap);
   946       }
   947     }
   948 
   949   private:
   950 
   951     const From& _from;
   952     To& _to;
   953 
   954     std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
   955       _node_maps;
   956 
   957     std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
   958       _arc_maps;
   959 
   960     std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
   961       _edge_maps;
   962 
   963   };
   964 
   965   /// \brief Copy a graph to another graph.
   966   ///
   967   /// This function copies a graph to another graph.
   968   /// The complete usage of it is detailed in the GraphCopy class,
   969   /// but a short example shows a basic work:
   970   ///\code
   971   /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
   972   ///\endcode
   973   ///
   974   /// After the copy the \c nr map will contain the mapping from the
   975   /// nodes of the \c from graph to the nodes of the \c to graph and
   976   /// \c ecr will contain the mapping from the edges of the \c to graph
   977   /// to the edges of the \c from graph.
   978   ///
   979   /// \see GraphCopy
   980   template <typename From, typename To>
   981   GraphCopy<From, To>
   982   graphCopy(const From& from, To& to) {
   983     return GraphCopy<From, To>(from, to);
   984   }
   985 
   986   namespace _core_bits {
   987 
   988     template <typename Graph, typename Enable = void>
   989     struct FindArcSelector {
   990       typedef typename Graph::Node Node;
   991       typedef typename Graph::Arc Arc;
   992       static Arc find(const Graph &g, Node u, Node v, Arc e) {
   993         if (e == INVALID) {
   994           g.firstOut(e, u);
   995         } else {
   996           g.nextOut(e);
   997         }
   998         while (e != INVALID && g.target(e) != v) {
   999           g.nextOut(e);
  1000         }
  1001         return e;
  1002       }
  1003     };
  1004 
  1005     template <typename Graph>
  1006     struct FindArcSelector<
  1007       Graph,
  1008       typename enable_if<typename Graph::FindArcTag, void>::type>
  1009     {
  1010       typedef typename Graph::Node Node;
  1011       typedef typename Graph::Arc Arc;
  1012       static Arc find(const Graph &g, Node u, Node v, Arc prev) {
  1013         return g.findArc(u, v, prev);
  1014       }
  1015     };
  1016   }
  1017 
  1018   /// \brief Find an arc between two nodes of a digraph.
  1019   ///
  1020   /// This function finds an arc from node \c u to node \c v in the
  1021   /// digraph \c g.
  1022   ///
  1023   /// If \c prev is \ref INVALID (this is the default value), then
  1024   /// it finds the first arc from \c u to \c v. Otherwise it looks for
  1025   /// the next arc from \c u to \c v after \c prev.
  1026   /// \return The found arc or \ref INVALID if there is no such an arc.
  1027   ///
  1028   /// Thus you can iterate through each arc from \c u to \c v as it follows.
  1029   ///\code
  1030   /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
  1031   ///   ...
  1032   /// }
  1033   ///\endcode
  1034   ///
  1035   /// \note \ref ConArcIt provides iterator interface for the same
  1036   /// functionality.
  1037   ///
  1038   ///\sa ConArcIt
  1039   ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
  1040   template <typename Graph>
  1041   inline typename Graph::Arc
  1042   findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
  1043           typename Graph::Arc prev = INVALID) {
  1044     return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
  1045   }
  1046 
  1047   /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
  1048   ///
  1049   /// Iterator for iterating on parallel arcs connecting the same nodes. It is
  1050   /// a higher level interface for the \ref findArc() function. You can
  1051   /// use it the following way:
  1052   ///\code
  1053   /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
  1054   ///   ...
  1055   /// }
  1056   ///\endcode
  1057   ///
  1058   ///\sa findArc()
  1059   ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
  1060   template <typename GR>
  1061   class ConArcIt : public GR::Arc {
  1062     typedef typename GR::Arc Parent;
  1063 
  1064   public:
  1065 
  1066     typedef typename GR::Arc Arc;
  1067     typedef typename GR::Node Node;
  1068 
  1069     /// \brief Constructor.
  1070     ///
  1071     /// Construct a new ConArcIt iterating on the arcs that
  1072     /// connects nodes \c u and \c v.
  1073     ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
  1074       Parent::operator=(findArc(_graph, u, v));
  1075     }
  1076 
  1077     /// \brief Constructor.
  1078     ///
  1079     /// Construct a new ConArcIt that continues the iterating from arc \c a.
  1080     ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
  1081 
  1082     /// \brief Increment operator.
  1083     ///
  1084     /// It increments the iterator and gives back the next arc.
  1085     ConArcIt& operator++() {
  1086       Parent::operator=(findArc(_graph, _graph.source(*this),
  1087                                 _graph.target(*this), *this));
  1088       return *this;
  1089     }
  1090   private:
  1091     const GR& _graph;
  1092   };
  1093 
  1094   namespace _core_bits {
  1095 
  1096     template <typename Graph, typename Enable = void>
  1097     struct FindEdgeSelector {
  1098       typedef typename Graph::Node Node;
  1099       typedef typename Graph::Edge Edge;
  1100       static Edge find(const Graph &g, Node u, Node v, Edge e) {
  1101         bool b;
  1102         if (u != v) {
  1103           if (e == INVALID) {
  1104             g.firstInc(e, b, u);
  1105           } else {
  1106             b = g.u(e) == u;
  1107             g.nextInc(e, b);
  1108           }
  1109           while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
  1110             g.nextInc(e, b);
  1111           }
  1112         } else {
  1113           if (e == INVALID) {
  1114             g.firstInc(e, b, u);
  1115           } else {
  1116             b = true;
  1117             g.nextInc(e, b);
  1118           }
  1119           while (e != INVALID && (!b || g.v(e) != v)) {
  1120             g.nextInc(e, b);
  1121           }
  1122         }
  1123         return e;
  1124       }
  1125     };
  1126 
  1127     template <typename Graph>
  1128     struct FindEdgeSelector<
  1129       Graph,
  1130       typename enable_if<typename Graph::FindEdgeTag, void>::type>
  1131     {
  1132       typedef typename Graph::Node Node;
  1133       typedef typename Graph::Edge Edge;
  1134       static Edge find(const Graph &g, Node u, Node v, Edge prev) {
  1135         return g.findEdge(u, v, prev);
  1136       }
  1137     };
  1138   }
  1139 
  1140   /// \brief Find an edge between two nodes of a graph.
  1141   ///
  1142   /// This function finds an edge from node \c u to node \c v in graph \c g.
  1143   /// If node \c u and node \c v is equal then each loop edge
  1144   /// will be enumerated once.
  1145   ///
  1146   /// If \c prev is \ref INVALID (this is the default value), then
  1147   /// it finds the first edge from \c u to \c v. Otherwise it looks for
  1148   /// the next edge from \c u to \c v after \c prev.
  1149   /// \return The found edge or \ref INVALID if there is no such an edge.
  1150   ///
  1151   /// Thus you can iterate through each edge between \c u and \c v
  1152   /// as it follows.
  1153   ///\code
  1154   /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
  1155   ///   ...
  1156   /// }
  1157   ///\endcode
  1158   ///
  1159   /// \note \ref ConEdgeIt provides iterator interface for the same
  1160   /// functionality.
  1161   ///
  1162   ///\sa ConEdgeIt
  1163   template <typename Graph>
  1164   inline typename Graph::Edge
  1165   findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
  1166             typename Graph::Edge p = INVALID) {
  1167     return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
  1168   }
  1169 
  1170   /// \brief Iterator for iterating on parallel edges connecting the same nodes.
  1171   ///
  1172   /// Iterator for iterating on parallel edges connecting the same nodes.
  1173   /// It is a higher level interface for the findEdge() function. You can
  1174   /// use it the following way:
  1175   ///\code
  1176   /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
  1177   ///   ...
  1178   /// }
  1179   ///\endcode
  1180   ///
  1181   ///\sa findEdge()
  1182   template <typename GR>
  1183   class ConEdgeIt : public GR::Edge {
  1184     typedef typename GR::Edge Parent;
  1185 
  1186   public:
  1187 
  1188     typedef typename GR::Edge Edge;
  1189     typedef typename GR::Node Node;
  1190 
  1191     /// \brief Constructor.
  1192     ///
  1193     /// Construct a new ConEdgeIt iterating on the edges that
  1194     /// connects nodes \c u and \c v.
  1195     ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
  1196       Parent::operator=(findEdge(_graph, _u, _v));
  1197     }
  1198 
  1199     /// \brief Constructor.
  1200     ///
  1201     /// Construct a new ConEdgeIt that continues iterating from edge \c e.
  1202     ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
  1203 
  1204     /// \brief Increment operator.
  1205     ///
  1206     /// It increments the iterator and gives back the next edge.
  1207     ConEdgeIt& operator++() {
  1208       Parent::operator=(findEdge(_graph, _u, _v, *this));
  1209       return *this;
  1210     }
  1211   private:
  1212     const GR& _graph;
  1213     Node _u, _v;
  1214   };
  1215 
  1216 
  1217   ///Dynamic arc look-up between given endpoints.
  1218 
  1219   ///Using this class, you can find an arc in a digraph from a given
  1220   ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
  1221   ///where <em>d</em> is the out-degree of the source node.
  1222   ///
  1223   ///It is possible to find \e all parallel arcs between two nodes with
  1224   ///the \c operator() member.
  1225   ///
  1226   ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
  1227   ///\ref AllArcLookUp if your digraph is not changed so frequently.
  1228   ///
  1229   ///This class uses a self-adjusting binary search tree, the Splay tree
  1230   ///of Sleator and Tarjan to guarantee the logarithmic amortized
  1231   ///time bound for arc look-ups. This class also guarantees the
  1232   ///optimal time bound in a constant factor for any distribution of
  1233   ///queries.
  1234   ///
  1235   ///\tparam GR The type of the underlying digraph.
  1236   ///
  1237   ///\sa ArcLookUp
  1238   ///\sa AllArcLookUp
  1239   template <typename GR>
  1240   class DynArcLookUp
  1241     : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
  1242   {
  1243     typedef typename ItemSetTraits<GR, typename GR::Arc>
  1244     ::ItemNotifier::ObserverBase Parent;
  1245 
  1246     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
  1247 
  1248   public:
  1249 
  1250     /// The Digraph type
  1251     typedef GR Digraph;
  1252 
  1253   protected:
  1254 
  1255     class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
  1256     {
  1257       typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
  1258 
  1259     public:
  1260 
  1261       AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
  1262 
  1263       virtual void add(const Node& node) {
  1264         Parent::add(node);
  1265         Parent::set(node, INVALID);
  1266       }
  1267 
  1268       virtual void add(const std::vector<Node>& nodes) {
  1269         Parent::add(nodes);
  1270         for (int i = 0; i < int(nodes.size()); ++i) {
  1271           Parent::set(nodes[i], INVALID);
  1272         }
  1273       }
  1274 
  1275       virtual void build() {
  1276         Parent::build();
  1277         Node it;
  1278         typename Parent::Notifier* nf = Parent::notifier();
  1279         for (nf->first(it); it != INVALID; nf->next(it)) {
  1280           Parent::set(it, INVALID);
  1281         }
  1282       }
  1283     };
  1284 
  1285     class ArcLess {
  1286       const Digraph &g;
  1287     public:
  1288       ArcLess(const Digraph &_g) : g(_g) {}
  1289       bool operator()(Arc a,Arc b) const
  1290       {
  1291         return g.target(a)<g.target(b);
  1292       }
  1293     };
  1294 
  1295   protected:
  1296 
  1297     const Digraph &_g;
  1298     AutoNodeMap _head;
  1299     typename Digraph::template ArcMap<Arc> _parent;
  1300     typename Digraph::template ArcMap<Arc> _left;
  1301     typename Digraph::template ArcMap<Arc> _right;
  1302 
  1303   public:
  1304 
  1305     ///Constructor
  1306 
  1307     ///Constructor.
  1308     ///
  1309     ///It builds up the search database.
  1310     DynArcLookUp(const Digraph &g)
  1311       : _g(g),_head(g),_parent(g),_left(g),_right(g)
  1312     {
  1313       Parent::attach(_g.notifier(typename Digraph::Arc()));
  1314       refresh();
  1315     }
  1316 
  1317   protected:
  1318 
  1319     virtual void add(const Arc& arc) {
  1320       insert(arc);
  1321     }
  1322 
  1323     virtual void add(const std::vector<Arc>& arcs) {
  1324       for (int i = 0; i < int(arcs.size()); ++i) {
  1325         insert(arcs[i]);
  1326       }
  1327     }
  1328 
  1329     virtual void erase(const Arc& arc) {
  1330       remove(arc);
  1331     }
  1332 
  1333     virtual void erase(const std::vector<Arc>& arcs) {
  1334       for (int i = 0; i < int(arcs.size()); ++i) {
  1335         remove(arcs[i]);
  1336       }
  1337     }
  1338 
  1339     virtual void build() {
  1340       refresh();
  1341     }
  1342 
  1343     virtual void clear() {
  1344       for(NodeIt n(_g);n!=INVALID;++n) {
  1345         _head[n] = INVALID;
  1346       }
  1347     }
  1348 
  1349     void insert(Arc arc) {
  1350       Node s = _g.source(arc);
  1351       Node t = _g.target(arc);
  1352       _left[arc] = INVALID;
  1353       _right[arc] = INVALID;
  1354 
  1355       Arc e = _head[s];
  1356       if (e == INVALID) {
  1357         _head[s] = arc;
  1358         _parent[arc] = INVALID;
  1359         return;
  1360       }
  1361       while (true) {
  1362         if (t < _g.target(e)) {
  1363           if (_left[e] == INVALID) {
  1364             _left[e] = arc;
  1365             _parent[arc] = e;
  1366             splay(arc);
  1367             return;
  1368           } else {
  1369             e = _left[e];
  1370           }
  1371         } else {
  1372           if (_right[e] == INVALID) {
  1373             _right[e] = arc;
  1374             _parent[arc] = e;
  1375             splay(arc);
  1376             return;
  1377           } else {
  1378             e = _right[e];
  1379           }
  1380         }
  1381       }
  1382     }
  1383 
  1384     void remove(Arc arc) {
  1385       if (_left[arc] == INVALID) {
  1386         if (_right[arc] != INVALID) {
  1387           _parent[_right[arc]] = _parent[arc];
  1388         }
  1389         if (_parent[arc] != INVALID) {
  1390           if (_left[_parent[arc]] == arc) {
  1391             _left[_parent[arc]] = _right[arc];
  1392           } else {
  1393             _right[_parent[arc]] = _right[arc];
  1394           }
  1395         } else {
  1396           _head[_g.source(arc)] = _right[arc];
  1397         }
  1398       } else if (_right[arc] == INVALID) {
  1399         _parent[_left[arc]] = _parent[arc];
  1400         if (_parent[arc] != INVALID) {
  1401           if (_left[_parent[arc]] == arc) {
  1402             _left[_parent[arc]] = _left[arc];
  1403           } else {
  1404             _right[_parent[arc]] = _left[arc];
  1405           }
  1406         } else {
  1407           _head[_g.source(arc)] = _left[arc];
  1408         }
  1409       } else {
  1410         Arc e = _left[arc];
  1411         if (_right[e] != INVALID) {
  1412           e = _right[e];
  1413           while (_right[e] != INVALID) {
  1414             e = _right[e];
  1415           }
  1416           Arc s = _parent[e];
  1417           _right[_parent[e]] = _left[e];
  1418           if (_left[e] != INVALID) {
  1419             _parent[_left[e]] = _parent[e];
  1420           }
  1421 
  1422           _left[e] = _left[arc];
  1423           _parent[_left[arc]] = e;
  1424           _right[e] = _right[arc];
  1425           _parent[_right[arc]] = e;
  1426 
  1427           _parent[e] = _parent[arc];
  1428           if (_parent[arc] != INVALID) {
  1429             if (_left[_parent[arc]] == arc) {
  1430               _left[_parent[arc]] = e;
  1431             } else {
  1432               _right[_parent[arc]] = e;
  1433             }
  1434           }
  1435           splay(s);
  1436         } else {
  1437           _right[e] = _right[arc];
  1438           _parent[_right[arc]] = e;
  1439           _parent[e] = _parent[arc];
  1440 
  1441           if (_parent[arc] != INVALID) {
  1442             if (_left[_parent[arc]] == arc) {
  1443               _left[_parent[arc]] = e;
  1444             } else {
  1445               _right[_parent[arc]] = e;
  1446             }
  1447           } else {
  1448             _head[_g.source(arc)] = e;
  1449           }
  1450         }
  1451       }
  1452     }
  1453 
  1454     Arc refreshRec(std::vector<Arc> &v,int a,int b)
  1455     {
  1456       int m=(a+b)/2;
  1457       Arc me=v[m];
  1458       if (a < m) {
  1459         Arc left = refreshRec(v,a,m-1);
  1460         _left[me] = left;
  1461         _parent[left] = me;
  1462       } else {
  1463         _left[me] = INVALID;
  1464       }
  1465       if (m < b) {
  1466         Arc right = refreshRec(v,m+1,b);
  1467         _right[me] = right;
  1468         _parent[right] = me;
  1469       } else {
  1470         _right[me] = INVALID;
  1471       }
  1472       return me;
  1473     }
  1474 
  1475     void refresh() {
  1476       for(NodeIt n(_g);n!=INVALID;++n) {
  1477         std::vector<Arc> v;
  1478         for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
  1479         if (!v.empty()) {
  1480           std::sort(v.begin(),v.end(),ArcLess(_g));
  1481           Arc head = refreshRec(v,0,v.size()-1);
  1482           _head[n] = head;
  1483           _parent[head] = INVALID;
  1484         }
  1485         else _head[n] = INVALID;
  1486       }
  1487     }
  1488 
  1489     void zig(Arc v) {
  1490       Arc w = _parent[v];
  1491       _parent[v] = _parent[w];
  1492       _parent[w] = v;
  1493       _left[w] = _right[v];
  1494       _right[v] = w;
  1495       if (_parent[v] != INVALID) {
  1496         if (_right[_parent[v]] == w) {
  1497           _right[_parent[v]] = v;
  1498         } else {
  1499           _left[_parent[v]] = v;
  1500         }
  1501       }
  1502       if (_left[w] != INVALID){
  1503         _parent[_left[w]] = w;
  1504       }
  1505     }
  1506 
  1507     void zag(Arc v) {
  1508       Arc w = _parent[v];
  1509       _parent[v] = _parent[w];
  1510       _parent[w] = v;
  1511       _right[w] = _left[v];
  1512       _left[v] = w;
  1513       if (_parent[v] != INVALID){
  1514         if (_left[_parent[v]] == w) {
  1515           _left[_parent[v]] = v;
  1516         } else {
  1517           _right[_parent[v]] = v;
  1518         }
  1519       }
  1520       if (_right[w] != INVALID){
  1521         _parent[_right[w]] = w;
  1522       }
  1523     }
  1524 
  1525     void splay(Arc v) {
  1526       while (_parent[v] != INVALID) {
  1527         if (v == _left[_parent[v]]) {
  1528           if (_parent[_parent[v]] == INVALID) {
  1529             zig(v);
  1530           } else {
  1531             if (_parent[v] == _left[_parent[_parent[v]]]) {
  1532               zig(_parent[v]);
  1533               zig(v);
  1534             } else {
  1535               zig(v);
  1536               zag(v);
  1537             }
  1538           }
  1539         } else {
  1540           if (_parent[_parent[v]] == INVALID) {
  1541             zag(v);
  1542           } else {
  1543             if (_parent[v] == _left[_parent[_parent[v]]]) {
  1544               zag(v);
  1545               zig(v);
  1546             } else {
  1547               zag(_parent[v]);
  1548               zag(v);
  1549             }
  1550           }
  1551         }
  1552       }
  1553       _head[_g.source(v)] = v;
  1554     }
  1555 
  1556 
  1557   public:
  1558 
  1559     ///Find an arc between two nodes.
  1560 
  1561     ///Find an arc between two nodes.
  1562     ///\param s The source node.
  1563     ///\param t The target node.
  1564     ///\param p The previous arc between \c s and \c t. It it is INVALID or
  1565     ///not given, the operator finds the first appropriate arc.
  1566     ///\return An arc from \c s to \c t after \c p or
  1567     ///\ref INVALID if there is no more.
  1568     ///
  1569     ///For example, you can count the number of arcs from \c u to \c v in the
  1570     ///following way.
  1571     ///\code
  1572     ///DynArcLookUp<ListDigraph> ae(g);
  1573     ///...
  1574     ///int n = 0;
  1575     ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
  1576     ///\endcode
  1577     ///
  1578     ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
  1579     ///amortized time, specifically, the time complexity of the lookups
  1580     ///is equal to the optimal search tree implementation for the
  1581     ///current query distribution in a constant factor.
  1582     ///
  1583     ///\note This is a dynamic data structure, therefore the data
  1584     ///structure is updated after each graph alteration. Thus although
  1585     ///this data structure is theoretically faster than \ref ArcLookUp
  1586     ///and \ref AllArcLookUp, it often provides worse performance than
  1587     ///them.
  1588     Arc operator()(Node s, Node t, Arc p = INVALID) const  {
  1589       if (p == INVALID) {
  1590         Arc a = _head[s];
  1591         if (a == INVALID) return INVALID;
  1592         Arc r = INVALID;
  1593         while (true) {
  1594           if (_g.target(a) < t) {
  1595             if (_right[a] == INVALID) {
  1596               const_cast<DynArcLookUp&>(*this).splay(a);
  1597               return r;
  1598             } else {
  1599               a = _right[a];
  1600             }
  1601           } else {
  1602             if (_g.target(a) == t) {
  1603               r = a;
  1604             }
  1605             if (_left[a] == INVALID) {
  1606               const_cast<DynArcLookUp&>(*this).splay(a);
  1607               return r;
  1608             } else {
  1609               a = _left[a];
  1610             }
  1611           }
  1612         }
  1613       } else {
  1614         Arc a = p;
  1615         if (_right[a] != INVALID) {
  1616           a = _right[a];
  1617           while (_left[a] != INVALID) {
  1618             a = _left[a];
  1619           }
  1620           const_cast<DynArcLookUp&>(*this).splay(a);
  1621         } else {
  1622           while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
  1623             a = _parent[a];
  1624           }
  1625           if (_parent[a] == INVALID) {
  1626             return INVALID;
  1627           } else {
  1628             a = _parent[a];
  1629             const_cast<DynArcLookUp&>(*this).splay(a);
  1630           }
  1631         }
  1632         if (_g.target(a) == t) return a;
  1633         else return INVALID;
  1634       }
  1635     }
  1636 
  1637   };
  1638 
  1639   ///Fast arc look-up between given endpoints.
  1640 
  1641   ///Using this class, you can find an arc in a digraph from a given
  1642   ///source to a given target in time <em>O</em>(log<em>d</em>),
  1643   ///where <em>d</em> is the out-degree of the source node.
  1644   ///
  1645   ///It is not possible to find \e all parallel arcs between two nodes.
  1646   ///Use \ref AllArcLookUp for this purpose.
  1647   ///
  1648   ///\warning This class is static, so you should call refresh() (or at
  1649   ///least refresh(Node)) to refresh this data structure whenever the
  1650   ///digraph changes. This is a time consuming (superlinearly proportional
  1651   ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
  1652   ///
  1653   ///\tparam GR The type of the underlying digraph.
  1654   ///
  1655   ///\sa DynArcLookUp
  1656   ///\sa AllArcLookUp
  1657   template<class GR>
  1658   class ArcLookUp
  1659   {
  1660     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
  1661 
  1662   public:
  1663 
  1664     /// The Digraph type
  1665     typedef GR Digraph;
  1666 
  1667   protected:
  1668     const Digraph &_g;
  1669     typename Digraph::template NodeMap<Arc> _head;
  1670     typename Digraph::template ArcMap<Arc> _left;
  1671     typename Digraph::template ArcMap<Arc> _right;
  1672 
  1673     class ArcLess {
  1674       const Digraph &g;
  1675     public:
  1676       ArcLess(const Digraph &_g) : g(_g) {}
  1677       bool operator()(Arc a,Arc b) const
  1678       {
  1679         return g.target(a)<g.target(b);
  1680       }
  1681     };
  1682 
  1683   public:
  1684 
  1685     ///Constructor
  1686 
  1687     ///Constructor.
  1688     ///
  1689     ///It builds up the search database, which remains valid until the digraph
  1690     ///changes.
  1691     ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
  1692 
  1693   private:
  1694     Arc refreshRec(std::vector<Arc> &v,int a,int b)
  1695     {
  1696       int m=(a+b)/2;
  1697       Arc me=v[m];
  1698       _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
  1699       _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
  1700       return me;
  1701     }
  1702   public:
  1703     ///Refresh the search data structure at a node.
  1704 
  1705     ///Build up the search database of node \c n.
  1706     ///
  1707     ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
  1708     ///is the number of the outgoing arcs of \c n.
  1709     void refresh(Node n)
  1710     {
  1711       std::vector<Arc> v;
  1712       for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
  1713       if(v.size()) {
  1714         std::sort(v.begin(),v.end(),ArcLess(_g));
  1715         _head[n]=refreshRec(v,0,v.size()-1);
  1716       }
  1717       else _head[n]=INVALID;
  1718     }
  1719     ///Refresh the full data structure.
  1720 
  1721     ///Build up the full search database. In fact, it simply calls
  1722     ///\ref refresh(Node) "refresh(n)" for each node \c n.
  1723     ///
  1724     ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
  1725     ///the number of the arcs in the digraph and <em>D</em> is the maximum
  1726     ///out-degree of the digraph.
  1727     void refresh()
  1728     {
  1729       for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
  1730     }
  1731 
  1732     ///Find an arc between two nodes.
  1733 
  1734     ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
  1735     ///where <em>d</em> is the number of outgoing arcs of \c s.
  1736     ///\param s The source node.
  1737     ///\param t The target node.
  1738     ///\return An arc from \c s to \c t if there exists,
  1739     ///\ref INVALID otherwise.
  1740     ///
  1741     ///\warning If you change the digraph, refresh() must be called before using
  1742     ///this operator. If you change the outgoing arcs of
  1743     ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
  1744     Arc operator()(Node s, Node t) const
  1745     {
  1746       Arc e;
  1747       for(e=_head[s];
  1748           e!=INVALID&&_g.target(e)!=t;
  1749           e = t < _g.target(e)?_left[e]:_right[e]) ;
  1750       return e;
  1751     }
  1752 
  1753   };
  1754 
  1755   ///Fast look-up of all arcs between given endpoints.
  1756 
  1757   ///This class is the same as \ref ArcLookUp, with the addition
  1758   ///that it makes it possible to find all parallel arcs between given
  1759   ///endpoints.
  1760   ///
  1761   ///\warning This class is static, so you should call refresh() (or at
  1762   ///least refresh(Node)) to refresh this data structure whenever the
  1763   ///digraph changes. This is a time consuming (superlinearly proportional
  1764   ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
  1765   ///
  1766   ///\tparam GR The type of the underlying digraph.
  1767   ///
  1768   ///\sa DynArcLookUp
  1769   ///\sa ArcLookUp
  1770   template<class GR>
  1771   class AllArcLookUp : public ArcLookUp<GR>
  1772   {
  1773     using ArcLookUp<GR>::_g;
  1774     using ArcLookUp<GR>::_right;
  1775     using ArcLookUp<GR>::_left;
  1776     using ArcLookUp<GR>::_head;
  1777 
  1778     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
  1779 
  1780     typename GR::template ArcMap<Arc> _next;
  1781 
  1782     Arc refreshNext(Arc head,Arc next=INVALID)
  1783     {
  1784       if(head==INVALID) return next;
  1785       else {
  1786         next=refreshNext(_right[head],next);
  1787         _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
  1788           ? next : INVALID;
  1789         return refreshNext(_left[head],head);
  1790       }
  1791     }
  1792 
  1793     void refreshNext()
  1794     {
  1795       for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
  1796     }
  1797 
  1798   public:
  1799 
  1800     /// The Digraph type
  1801     typedef GR Digraph;
  1802 
  1803     ///Constructor
  1804 
  1805     ///Constructor.
  1806     ///
  1807     ///It builds up the search database, which remains valid until the digraph
  1808     ///changes.
  1809     AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
  1810 
  1811     ///Refresh the data structure at a node.
  1812 
  1813     ///Build up the search database of node \c n.
  1814     ///
  1815     ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
  1816     ///the number of the outgoing arcs of \c n.
  1817     void refresh(Node n)
  1818     {
  1819       ArcLookUp<GR>::refresh(n);
  1820       refreshNext(_head[n]);
  1821     }
  1822 
  1823     ///Refresh the full data structure.
  1824 
  1825     ///Build up the full search database. In fact, it simply calls
  1826     ///\ref refresh(Node) "refresh(n)" for each node \c n.
  1827     ///
  1828     ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
  1829     ///the number of the arcs in the digraph and <em>D</em> is the maximum
  1830     ///out-degree of the digraph.
  1831     void refresh()
  1832     {
  1833       for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
  1834     }
  1835 
  1836     ///Find an arc between two nodes.
  1837 
  1838     ///Find an arc between two nodes.
  1839     ///\param s The source node.
  1840     ///\param t The target node.
  1841     ///\param prev The previous arc between \c s and \c t. It it is INVALID or
  1842     ///not given, the operator finds the first appropriate arc.
  1843     ///\return An arc from \c s to \c t after \c prev or
  1844     ///\ref INVALID if there is no more.
  1845     ///
  1846     ///For example, you can count the number of arcs from \c u to \c v in the
  1847     ///following way.
  1848     ///\code
  1849     ///AllArcLookUp<ListDigraph> ae(g);
  1850     ///...
  1851     ///int n = 0;
  1852     ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
  1853     ///\endcode
  1854     ///
  1855     ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
  1856     ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
  1857     ///consecutive arcs are found in constant time.
  1858     ///
  1859     ///\warning If you change the digraph, refresh() must be called before using
  1860     ///this operator. If you change the outgoing arcs of
  1861     ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
  1862     ///
  1863     Arc operator()(Node s, Node t, Arc prev=INVALID) const
  1864     {
  1865       if(prev==INVALID)
  1866         {
  1867           Arc f=INVALID;
  1868           Arc e;
  1869           for(e=_head[s];
  1870               e!=INVALID&&_g.target(e)!=t;
  1871               e = t < _g.target(e)?_left[e]:_right[e]) ;
  1872           while(e!=INVALID)
  1873             if(_g.target(e)==t)
  1874               {
  1875                 f = e;
  1876                 e = _left[e];
  1877               }
  1878             else e = _right[e];
  1879           return f;
  1880         }
  1881       else return _next[prev];
  1882     }
  1883 
  1884   };
  1885 
  1886   /// @}
  1887 
  1888 } //namespace lemon
  1889 
  1890 #endif