lemon/core.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 09 Oct 2008 13:27:35 +0200
changeset 314 2cc60866a0c9
parent 300 8c05947fc107
child 319 5e12d7734036
permissions -rw-r--r--
Doc reorganization + improvements

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