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