lemon/core.h
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Wed, 29 Apr 2009 16:15:29 +0100
changeset 645 cb8270a98660
parent 581 aa1804409f29
child 639 72ac25ad276e
permissions -rw-r--r--
Add the solver libraries to lemon.pc.in (#281)
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
 *
alpar@440
     5
 * Copyright (C) 2003-2009
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
alpar@502
    25
#include <lemon/core.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) {
deba@220
   387
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@220
   388
          nodeRefMap[it] = to.addNode();
deba@220
   389
        }
deba@220
   390
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
deba@220
   391
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
deba@220
   392
                                    nodeRefMap[from.target(it)]);
deba@220
   393
        }
deba@220
   394
      }
deba@220
   395
    };
deba@220
   396
deba@220
   397
    template <typename Digraph>
deba@220
   398
    struct DigraphCopySelector<
deba@220
   399
      Digraph,
deba@220
   400
      typename enable_if<typename Digraph::BuildTag, void>::type>
deba@220
   401
    {
deba@220
   402
      template <typename From, typename NodeRefMap, typename ArcRefMap>
kpeter@282
   403
      static void copy(const From& from, Digraph &to,
deba@220
   404
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
deba@220
   405
        to.build(from, nodeRefMap, arcRefMap);
deba@220
   406
      }
deba@220
   407
    };
deba@220
   408
deba@220
   409
    template <typename Graph, typename Enable = void>
deba@220
   410
    struct GraphCopySelector {
deba@220
   411
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
kpeter@282
   412
      static void copy(const From& from, Graph &to,
deba@220
   413
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@220
   414
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@220
   415
          nodeRefMap[it] = to.addNode();
deba@220
   416
        }
deba@220
   417
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
deba@220
   418
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
deba@220
   419
                                      nodeRefMap[from.v(it)]);
deba@220
   420
        }
deba@220
   421
      }
deba@220
   422
    };
deba@220
   423
deba@220
   424
    template <typename Graph>
deba@220
   425
    struct GraphCopySelector<
deba@220
   426
      Graph,
deba@220
   427
      typename enable_if<typename Graph::BuildTag, void>::type>
deba@220
   428
    {
deba@220
   429
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
kpeter@282
   430
      static void copy(const From& from, Graph &to,
deba@220
   431
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@220
   432
        to.build(from, nodeRefMap, edgeRefMap);
deba@220
   433
      }
deba@220
   434
    };
deba@220
   435
deba@220
   436
  }
deba@220
   437
deba@220
   438
  /// \brief Class to copy a digraph.
deba@220
   439
  ///
deba@220
   440
  /// Class to copy a digraph to another digraph (duplicate a digraph). The
kpeter@282
   441
  /// simplest way of using it is through the \c digraphCopy() function.
deba@220
   442
  ///
kpeter@282
   443
  /// This class not only make a copy of a digraph, but it can create
deba@220
   444
  /// references and cross references between the nodes and arcs of
kpeter@282
   445
  /// the two digraphs, and it can copy maps to use with the newly created
kpeter@282
   446
  /// digraph.
deba@220
   447
  ///
kpeter@282
   448
  /// To make a copy from a digraph, first an instance of DigraphCopy
kpeter@282
   449
  /// should be created, then the data belongs to the digraph should
deba@220
   450
  /// assigned to copy. In the end, the \c run() member should be
deba@220
   451
  /// called.
deba@220
   452
  ///
kpeter@282
   453
  /// The next code copies a digraph with several data:
deba@220
   454
  ///\code
kpeter@282
   455
  ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
kpeter@282
   456
  ///  // Create references for the nodes
deba@220
   457
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
kpeter@282
   458
  ///  cg.nodeRef(nr);
kpeter@282
   459
  ///  // Create cross references (inverse) for the arcs
deba@220
   460
  ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
kpeter@282
   461
  ///  cg.arcCrossRef(acr);
kpeter@282
   462
  ///  // Copy an arc map
deba@220
   463
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
deba@220
   464
  ///  NewGraph::ArcMap<double> namap(new_graph);
kpeter@282
   465
  ///  cg.arcMap(oamap, namap);
kpeter@282
   466
  ///  // Copy a node
deba@220
   467
  ///  OrigGraph::Node on;
deba@220
   468
  ///  NewGraph::Node nn;
kpeter@282
   469
  ///  cg.node(on, nn);
kpeter@282
   470
  ///  // Execute copying
kpeter@282
   471
  ///  cg.run();
deba@220
   472
  ///\endcode
kpeter@282
   473
  template <typename From, typename To>
deba@220
   474
  class DigraphCopy {
deba@220
   475
  private:
deba@220
   476
deba@220
   477
    typedef typename From::Node Node;
deba@220
   478
    typedef typename From::NodeIt NodeIt;
deba@220
   479
    typedef typename From::Arc Arc;
deba@220
   480
    typedef typename From::ArcIt ArcIt;
deba@220
   481
deba@220
   482
    typedef typename To::Node TNode;
deba@220
   483
    typedef typename To::Arc TArc;
deba@220
   484
deba@220
   485
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@220
   486
    typedef typename From::template ArcMap<TArc> ArcRefMap;
deba@220
   487
deba@220
   488
  public:
deba@220
   489
kpeter@282
   490
    /// \brief Constructor of DigraphCopy.
deba@220
   491
    ///
kpeter@282
   492
    /// Constructor of DigraphCopy for copying the content of the
kpeter@282
   493
    /// \c from digraph into the \c to digraph.
kpeter@282
   494
    DigraphCopy(const From& from, To& to)
deba@220
   495
      : _from(from), _to(to) {}
deba@220
   496
kpeter@282
   497
    /// \brief Destructor of DigraphCopy
deba@220
   498
    ///
kpeter@282
   499
    /// Destructor of DigraphCopy.
deba@220
   500
    ~DigraphCopy() {
deba@220
   501
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   502
        delete _node_maps[i];
deba@220
   503
      }
deba@220
   504
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   505
        delete _arc_maps[i];
deba@220
   506
      }
deba@220
   507
deba@220
   508
    }
deba@220
   509
kpeter@282
   510
    /// \brief Copy the node references into the given map.
deba@220
   511
    ///
kpeter@282
   512
    /// This function copies the node references into the given map.
kpeter@282
   513
    /// The parameter should be a map, whose key type is the Node type of
kpeter@282
   514
    /// the source digraph, while the value type is the Node type of the
kpeter@282
   515
    /// destination digraph.
deba@220
   516
    template <typename NodeRef>
deba@220
   517
    DigraphCopy& nodeRef(NodeRef& map) {
deba@220
   518
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
deba@220
   519
                           NodeRefMap, NodeRef>(map));
deba@220
   520
      return *this;
deba@220
   521
    }
deba@220
   522
kpeter@282
   523
    /// \brief Copy the node cross references into the given map.
deba@220
   524
    ///
kpeter@282
   525
    /// This function copies the node cross references (reverse references)
kpeter@282
   526
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   527
    /// is the Node type of the destination digraph, while the value type is
kpeter@282
   528
    /// the Node type of the source digraph.
deba@220
   529
    template <typename NodeCrossRef>
deba@220
   530
    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@220
   531
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
deba@220
   532
                           NodeRefMap, NodeCrossRef>(map));
deba@220
   533
      return *this;
deba@220
   534
    }
deba@220
   535
kpeter@282
   536
    /// \brief Make a copy of the given node map.
deba@220
   537
    ///
kpeter@282
   538
    /// This function makes a copy of the given node map for the newly
kpeter@282
   539
    /// created digraph.
kpeter@282
   540
    /// The key type of the new map \c tmap should be the Node type of the
kpeter@282
   541
    /// destination digraph, and the key type of the original map \c map
kpeter@282
   542
    /// should be the Node type of the source digraph.
kpeter@282
   543
    template <typename FromMap, typename ToMap>
kpeter@282
   544
    DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
deba@220
   545
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
kpeter@282
   546
                           NodeRefMap, FromMap, ToMap>(map, tmap));
deba@220
   547
      return *this;
deba@220
   548
    }
deba@220
   549
deba@220
   550
    /// \brief Make a copy of the given node.
deba@220
   551
    ///
kpeter@282
   552
    /// This function makes a copy of the given node.
kpeter@282
   553
    DigraphCopy& node(const Node& node, TNode& tnode) {
deba@220
   554
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
kpeter@282
   555
                           NodeRefMap, TNode>(node, tnode));
deba@220
   556
      return *this;
deba@220
   557
    }
deba@220
   558
kpeter@282
   559
    /// \brief Copy the arc references into the given map.
deba@220
   560
    ///
kpeter@282
   561
    /// This function copies the arc references into the given map.
kpeter@282
   562
    /// The parameter should be a map, whose key type is the Arc type of
kpeter@282
   563
    /// the source digraph, while the value type is the Arc type of the
kpeter@282
   564
    /// destination digraph.
deba@220
   565
    template <typename ArcRef>
deba@220
   566
    DigraphCopy& arcRef(ArcRef& map) {
deba@220
   567
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
deba@220
   568
                          ArcRefMap, ArcRef>(map));
deba@220
   569
      return *this;
deba@220
   570
    }
deba@220
   571
kpeter@282
   572
    /// \brief Copy the arc cross references into the given map.
deba@220
   573
    ///
kpeter@282
   574
    /// This function copies the arc cross references (reverse references)
kpeter@282
   575
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   576
    /// is the Arc type of the destination digraph, while the value type is
kpeter@282
   577
    /// the Arc type of the source digraph.
deba@220
   578
    template <typename ArcCrossRef>
deba@220
   579
    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
deba@220
   580
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
deba@220
   581
                          ArcRefMap, ArcCrossRef>(map));
deba@220
   582
      return *this;
deba@220
   583
    }
deba@220
   584
kpeter@282
   585
    /// \brief Make a copy of the given arc map.
deba@220
   586
    ///
kpeter@282
   587
    /// This function makes a copy of the given arc map for the newly
kpeter@282
   588
    /// created digraph.
kpeter@282
   589
    /// The key type of the new map \c tmap should be the Arc type of the
kpeter@282
   590
    /// destination digraph, and the key type of the original map \c map
kpeter@282
   591
    /// should be the Arc type of the source digraph.
kpeter@282
   592
    template <typename FromMap, typename ToMap>
kpeter@282
   593
    DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
deba@220
   594
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
kpeter@282
   595
                          ArcRefMap, FromMap, ToMap>(map, tmap));
deba@220
   596
      return *this;
deba@220
   597
    }
deba@220
   598
deba@220
   599
    /// \brief Make a copy of the given arc.
deba@220
   600
    ///
kpeter@282
   601
    /// This function makes a copy of the given arc.
kpeter@282
   602
    DigraphCopy& arc(const Arc& arc, TArc& tarc) {
deba@220
   603
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
kpeter@282
   604
                          ArcRefMap, TArc>(arc, tarc));
deba@220
   605
      return *this;
deba@220
   606
    }
deba@220
   607
kpeter@282
   608
    /// \brief Execute copying.
deba@220
   609
    ///
kpeter@282
   610
    /// This function executes the copying of the digraph along with the
kpeter@282
   611
    /// copying of the assigned data.
deba@220
   612
    void run() {
deba@220
   613
      NodeRefMap nodeRefMap(_from);
deba@220
   614
      ArcRefMap arcRefMap(_from);
deba@220
   615
      _core_bits::DigraphCopySelector<To>::
kpeter@282
   616
        copy(_from, _to, nodeRefMap, arcRefMap);
deba@220
   617
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   618
        _node_maps[i]->copy(_from, nodeRefMap);
deba@220
   619
      }
deba@220
   620
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   621
        _arc_maps[i]->copy(_from, arcRefMap);
deba@220
   622
      }
deba@220
   623
    }
deba@220
   624
deba@220
   625
  protected:
deba@220
   626
deba@220
   627
    const From& _from;
deba@220
   628
    To& _to;
deba@220
   629
deba@220
   630
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
kpeter@282
   631
      _node_maps;
deba@220
   632
deba@220
   633
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
kpeter@282
   634
      _arc_maps;
deba@220
   635
deba@220
   636
  };
deba@220
   637
deba@220
   638
  /// \brief Copy a digraph to another digraph.
deba@220
   639
  ///
kpeter@282
   640
  /// This function copies a digraph to another digraph.
kpeter@282
   641
  /// The complete usage of it is detailed in the DigraphCopy class, but
kpeter@282
   642
  /// a short example shows a basic work:
deba@220
   643
  ///\code
kpeter@282
   644
  /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
deba@220
   645
  ///\endcode
deba@220
   646
  ///
deba@220
   647
  /// After the copy the \c nr map will contain the mapping from the
deba@220
   648
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
kpeter@282
   649
  /// \c acr will contain the mapping from the arcs of the \c to digraph
deba@220
   650
  /// to the arcs of the \c from digraph.
deba@220
   651
  ///
deba@220
   652
  /// \see DigraphCopy
kpeter@282
   653
  template <typename From, typename To>
kpeter@282
   654
  DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
kpeter@282
   655
    return DigraphCopy<From, To>(from, to);
deba@220
   656
  }
deba@220
   657
deba@220
   658
  /// \brief Class to copy a graph.
deba@220
   659
  ///
deba@220
   660
  /// Class to copy a graph to another graph (duplicate a graph). The
kpeter@282
   661
  /// simplest way of using it is through the \c graphCopy() function.
deba@220
   662
  ///
kpeter@282
   663
  /// This class not only make a copy of a graph, but it can create
deba@220
   664
  /// references and cross references between the nodes, edges and arcs of
kpeter@282
   665
  /// the two graphs, and it can copy maps for using with the newly created
kpeter@282
   666
  /// graph.
deba@220
   667
  ///
deba@220
   668
  /// To make a copy from a graph, first an instance of GraphCopy
deba@220
   669
  /// should be created, then the data belongs to the graph should
deba@220
   670
  /// assigned to copy. In the end, the \c run() member should be
deba@220
   671
  /// called.
deba@220
   672
  ///
deba@220
   673
  /// The next code copies a graph with several data:
deba@220
   674
  ///\code
kpeter@282
   675
  ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
kpeter@282
   676
  ///  // Create references for the nodes
deba@220
   677
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
kpeter@282
   678
  ///  cg.nodeRef(nr);
kpeter@282
   679
  ///  // Create cross references (inverse) for the edges
kpeter@282
   680
  ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
kpeter@282
   681
  ///  cg.edgeCrossRef(ecr);
kpeter@282
   682
  ///  // Copy an edge map
kpeter@282
   683
  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
kpeter@282
   684
  ///  NewGraph::EdgeMap<double> nemap(new_graph);
kpeter@282
   685
  ///  cg.edgeMap(oemap, nemap);
kpeter@282
   686
  ///  // Copy a node
deba@220
   687
  ///  OrigGraph::Node on;
deba@220
   688
  ///  NewGraph::Node nn;
kpeter@282
   689
  ///  cg.node(on, nn);
kpeter@282
   690
  ///  // Execute copying
kpeter@282
   691
  ///  cg.run();
deba@220
   692
  ///\endcode
kpeter@282
   693
  template <typename From, typename To>
deba@220
   694
  class GraphCopy {
deba@220
   695
  private:
deba@220
   696
deba@220
   697
    typedef typename From::Node Node;
deba@220
   698
    typedef typename From::NodeIt NodeIt;
deba@220
   699
    typedef typename From::Arc Arc;
deba@220
   700
    typedef typename From::ArcIt ArcIt;
deba@220
   701
    typedef typename From::Edge Edge;
deba@220
   702
    typedef typename From::EdgeIt EdgeIt;
deba@220
   703
deba@220
   704
    typedef typename To::Node TNode;
deba@220
   705
    typedef typename To::Arc TArc;
deba@220
   706
    typedef typename To::Edge TEdge;
deba@220
   707
deba@220
   708
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@220
   709
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
deba@220
   710
deba@220
   711
    struct ArcRefMap {
kpeter@282
   712
      ArcRefMap(const From& from, const To& to,
deba@220
   713
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
kpeter@282
   714
        : _from(from), _to(to),
deba@220
   715
          _edge_ref(edge_ref), _node_ref(node_ref) {}
deba@220
   716
deba@220
   717
      typedef typename From::Arc Key;
deba@220
   718
      typedef typename To::Arc Value;
deba@220
   719
deba@220
   720
      Value operator[](const Key& key) const {
deba@220
   721
        bool forward = _from.u(key) != _from.v(key) ?
deba@220
   722
          _node_ref[_from.source(key)] ==
deba@220
   723
          _to.source(_to.direct(_edge_ref[key], true)) :
deba@220
   724
          _from.direction(key);
deba@220
   725
        return _to.direct(_edge_ref[key], forward);
deba@220
   726
      }
deba@220
   727
kpeter@282
   728
      const From& _from;
deba@220
   729
      const To& _to;
deba@220
   730
      const EdgeRefMap& _edge_ref;
deba@220
   731
      const NodeRefMap& _node_ref;
deba@220
   732
    };
deba@220
   733
deba@220
   734
  public:
deba@220
   735
kpeter@282
   736
    /// \brief Constructor of GraphCopy.
deba@220
   737
    ///
kpeter@282
   738
    /// Constructor of GraphCopy for copying the content of the
kpeter@282
   739
    /// \c from graph into the \c to graph.
kpeter@282
   740
    GraphCopy(const From& from, To& to)
deba@220
   741
      : _from(from), _to(to) {}
deba@220
   742
kpeter@282
   743
    /// \brief Destructor of GraphCopy
deba@220
   744
    ///
kpeter@282
   745
    /// Destructor of GraphCopy.
deba@220
   746
    ~GraphCopy() {
deba@220
   747
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   748
        delete _node_maps[i];
deba@220
   749
      }
deba@220
   750
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   751
        delete _arc_maps[i];
deba@220
   752
      }
deba@220
   753
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@220
   754
        delete _edge_maps[i];
deba@220
   755
      }
deba@220
   756
    }
deba@220
   757
kpeter@282
   758
    /// \brief Copy the node references into the given map.
deba@220
   759
    ///
kpeter@282
   760
    /// This function copies the node references into the given map.
kpeter@282
   761
    /// The parameter should be a map, whose key type is the Node type of
kpeter@282
   762
    /// the source graph, while the value type is the Node type of the
kpeter@282
   763
    /// destination graph.
deba@220
   764
    template <typename NodeRef>
deba@220
   765
    GraphCopy& nodeRef(NodeRef& map) {
deba@220
   766
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
deba@220
   767
                           NodeRefMap, NodeRef>(map));
deba@220
   768
      return *this;
deba@220
   769
    }
deba@220
   770
kpeter@282
   771
    /// \brief Copy the node cross references into the given map.
deba@220
   772
    ///
kpeter@282
   773
    /// This function copies the node cross references (reverse references)
kpeter@282
   774
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   775
    /// is the Node type of the destination graph, while the value type is
kpeter@282
   776
    /// the Node type of the source graph.
deba@220
   777
    template <typename NodeCrossRef>
deba@220
   778
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@220
   779
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
deba@220
   780
                           NodeRefMap, NodeCrossRef>(map));
deba@220
   781
      return *this;
deba@220
   782
    }
deba@220
   783
kpeter@282
   784
    /// \brief Make a copy of the given node map.
deba@220
   785
    ///
kpeter@282
   786
    /// This function makes a copy of the given node map for the newly
kpeter@282
   787
    /// created graph.
kpeter@282
   788
    /// The key type of the new map \c tmap should be the Node type of the
kpeter@282
   789
    /// destination graph, and the key type of the original map \c map
kpeter@282
   790
    /// should be the Node type of the source graph.
kpeter@282
   791
    template <typename FromMap, typename ToMap>
kpeter@282
   792
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
deba@220
   793
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
kpeter@282
   794
                           NodeRefMap, FromMap, ToMap>(map, tmap));
deba@220
   795
      return *this;
deba@220
   796
    }
deba@220
   797
deba@220
   798
    /// \brief Make a copy of the given node.
deba@220
   799
    ///
kpeter@282
   800
    /// This function makes a copy of the given node.
kpeter@282
   801
    GraphCopy& node(const Node& node, TNode& tnode) {
deba@220
   802
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
kpeter@282
   803
                           NodeRefMap, TNode>(node, tnode));
deba@220
   804
      return *this;
deba@220
   805
    }
deba@220
   806
kpeter@282
   807
    /// \brief Copy the arc references into the given map.
deba@220
   808
    ///
kpeter@282
   809
    /// This function copies the arc references into the given map.
kpeter@282
   810
    /// The parameter should be a map, whose key type is the Arc type of
kpeter@282
   811
    /// the source graph, while the value type is the Arc type of the
kpeter@282
   812
    /// destination graph.
deba@220
   813
    template <typename ArcRef>
deba@220
   814
    GraphCopy& arcRef(ArcRef& map) {
deba@220
   815
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
deba@220
   816
                          ArcRefMap, ArcRef>(map));
deba@220
   817
      return *this;
deba@220
   818
    }
deba@220
   819
kpeter@282
   820
    /// \brief Copy the arc cross references into the given map.
deba@220
   821
    ///
kpeter@282
   822
    /// This function copies the arc cross references (reverse references)
kpeter@282
   823
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   824
    /// is the Arc type of the destination graph, while the value type is
kpeter@282
   825
    /// the Arc type of the source graph.
deba@220
   826
    template <typename ArcCrossRef>
deba@220
   827
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
deba@220
   828
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
deba@220
   829
                          ArcRefMap, ArcCrossRef>(map));
deba@220
   830
      return *this;
deba@220
   831
    }
deba@220
   832
kpeter@282
   833
    /// \brief Make a copy of the given arc map.
deba@220
   834
    ///
kpeter@282
   835
    /// This function makes a copy of the given arc map for the newly
kpeter@282
   836
    /// created graph.
kpeter@282
   837
    /// The key type of the new map \c tmap should be the Arc type of the
kpeter@282
   838
    /// destination graph, and the key type of the original map \c map
kpeter@282
   839
    /// should be the Arc type of the source graph.
kpeter@282
   840
    template <typename FromMap, typename ToMap>
kpeter@282
   841
    GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
deba@220
   842
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
kpeter@282
   843
                          ArcRefMap, FromMap, ToMap>(map, tmap));
deba@220
   844
      return *this;
deba@220
   845
    }
deba@220
   846
deba@220
   847
    /// \brief Make a copy of the given arc.
deba@220
   848
    ///
kpeter@282
   849
    /// This function makes a copy of the given arc.
kpeter@282
   850
    GraphCopy& arc(const Arc& arc, TArc& tarc) {
deba@220
   851
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
kpeter@282
   852
                          ArcRefMap, TArc>(arc, tarc));
deba@220
   853
      return *this;
deba@220
   854
    }
deba@220
   855
kpeter@282
   856
    /// \brief Copy the edge references into the given map.
deba@220
   857
    ///
kpeter@282
   858
    /// This function copies the edge references into the given map.
kpeter@282
   859
    /// The parameter should be a map, whose key type is the Edge type of
kpeter@282
   860
    /// the source graph, while the value type is the Edge type of the
kpeter@282
   861
    /// destination graph.
deba@220
   862
    template <typename EdgeRef>
deba@220
   863
    GraphCopy& edgeRef(EdgeRef& map) {
deba@220
   864
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
deba@220
   865
                           EdgeRefMap, EdgeRef>(map));
deba@220
   866
      return *this;
deba@220
   867
    }
deba@220
   868
kpeter@282
   869
    /// \brief Copy the edge cross references into the given map.
deba@220
   870
    ///
kpeter@282
   871
    /// This function copies the edge cross references (reverse references)
kpeter@282
   872
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   873
    /// is the Edge type of the destination graph, while the value type is
kpeter@282
   874
    /// the Edge type of the source graph.
deba@220
   875
    template <typename EdgeCrossRef>
deba@220
   876
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@220
   877
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
deba@220
   878
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
deba@220
   879
      return *this;
deba@220
   880
    }
deba@220
   881
kpeter@282
   882
    /// \brief Make a copy of the given edge map.
deba@220
   883
    ///
kpeter@282
   884
    /// This function makes a copy of the given edge map for the newly
kpeter@282
   885
    /// created graph.
kpeter@282
   886
    /// The key type of the new map \c tmap should be the Edge type of the
kpeter@282
   887
    /// destination graph, and the key type of the original map \c map
kpeter@282
   888
    /// should be the Edge type of the source graph.
kpeter@282
   889
    template <typename FromMap, typename ToMap>
kpeter@282
   890
    GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
deba@220
   891
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
kpeter@282
   892
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
deba@220
   893
      return *this;
deba@220
   894
    }
deba@220
   895
deba@220
   896
    /// \brief Make a copy of the given edge.
deba@220
   897
    ///
kpeter@282
   898
    /// This function makes a copy of the given edge.
kpeter@282
   899
    GraphCopy& edge(const Edge& edge, TEdge& tedge) {
deba@220
   900
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
kpeter@282
   901
                           EdgeRefMap, TEdge>(edge, tedge));
deba@220
   902
      return *this;
deba@220
   903
    }
deba@220
   904
kpeter@282
   905
    /// \brief Execute copying.
deba@220
   906
    ///
kpeter@282
   907
    /// This function executes the copying of the graph along with the
kpeter@282
   908
    /// copying of the assigned data.
deba@220
   909
    void run() {
deba@220
   910
      NodeRefMap nodeRefMap(_from);
deba@220
   911
      EdgeRefMap edgeRefMap(_from);
kpeter@282
   912
      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
deba@220
   913
      _core_bits::GraphCopySelector<To>::
kpeter@282
   914
        copy(_from, _to, nodeRefMap, edgeRefMap);
deba@220
   915
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   916
        _node_maps[i]->copy(_from, nodeRefMap);
deba@220
   917
      }
deba@220
   918
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@220
   919
        _edge_maps[i]->copy(_from, edgeRefMap);
deba@220
   920
      }
deba@220
   921
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   922
        _arc_maps[i]->copy(_from, arcRefMap);
deba@220
   923
      }
deba@220
   924
    }
deba@220
   925
deba@220
   926
  private:
deba@220
   927
deba@220
   928
    const From& _from;
deba@220
   929
    To& _to;
deba@220
   930
deba@220
   931
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
kpeter@282
   932
      _node_maps;
deba@220
   933
deba@220
   934
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
kpeter@282
   935
      _arc_maps;
deba@220
   936
deba@220
   937
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
kpeter@282
   938
      _edge_maps;
deba@220
   939
deba@220
   940
  };
deba@220
   941
deba@220
   942
  /// \brief Copy a graph to another graph.
deba@220
   943
  ///
kpeter@282
   944
  /// This function copies a graph to another graph.
kpeter@282
   945
  /// The complete usage of it is detailed in the GraphCopy class,
kpeter@282
   946
  /// but a short example shows a basic work:
deba@220
   947
  ///\code
kpeter@282
   948
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
deba@220
   949
  ///\endcode
deba@220
   950
  ///
deba@220
   951
  /// After the copy the \c nr map will contain the mapping from the
deba@220
   952
  /// nodes of the \c from graph to the nodes of the \c to graph and
kpeter@282
   953
  /// \c ecr will contain the mapping from the edges of the \c to graph
kpeter@282
   954
  /// to the edges of the \c from graph.
deba@220
   955
  ///
deba@220
   956
  /// \see GraphCopy
kpeter@282
   957
  template <typename From, typename To>
kpeter@282
   958
  GraphCopy<From, To>
kpeter@282
   959
  graphCopy(const From& from, To& to) {
kpeter@282
   960
    return GraphCopy<From, To>(from, to);
deba@220
   961
  }
deba@220
   962
deba@220
   963
  namespace _core_bits {
deba@220
   964
deba@220
   965
    template <typename Graph, typename Enable = void>
deba@220
   966
    struct FindArcSelector {
deba@220
   967
      typedef typename Graph::Node Node;
deba@220
   968
      typedef typename Graph::Arc Arc;
deba@220
   969
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
deba@220
   970
        if (e == INVALID) {
deba@220
   971
          g.firstOut(e, u);
deba@220
   972
        } else {
deba@220
   973
          g.nextOut(e);
deba@220
   974
        }
deba@220
   975
        while (e != INVALID && g.target(e) != v) {
deba@220
   976
          g.nextOut(e);
deba@220
   977
        }
deba@220
   978
        return e;
deba@220
   979
      }
deba@220
   980
    };
deba@220
   981
deba@220
   982
    template <typename Graph>
deba@220
   983
    struct FindArcSelector<
deba@220
   984
      Graph,
kpeter@282
   985
      typename enable_if<typename Graph::FindArcTag, void>::type>
deba@220
   986
    {
deba@220
   987
      typedef typename Graph::Node Node;
deba@220
   988
      typedef typename Graph::Arc Arc;
deba@220
   989
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
deba@220
   990
        return g.findArc(u, v, prev);
deba@220
   991
      }
deba@220
   992
    };
deba@220
   993
  }
deba@220
   994
kpeter@282
   995
  /// \brief Find an arc between two nodes of a digraph.
deba@220
   996
  ///
kpeter@282
   997
  /// This function finds an arc from node \c u to node \c v in the
kpeter@282
   998
  /// digraph \c g.
deba@220
   999
  ///
deba@220
  1000
  /// If \c prev is \ref INVALID (this is the default value), then
deba@220
  1001
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
deba@220
  1002
  /// the next arc from \c u to \c v after \c prev.
deba@220
  1003
  /// \return The found arc or \ref INVALID if there is no such an arc.
deba@220
  1004
  ///
deba@220
  1005
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
deba@220
  1006
  ///\code
kpeter@282
  1007
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
deba@220
  1008
  ///   ...
deba@220
  1009
  /// }
deba@220
  1010
  ///\endcode
deba@220
  1011
  ///
kpeter@282
  1012
  /// \note \ref ConArcIt provides iterator interface for the same
kpeter@282
  1013
  /// functionality.
kpeter@282
  1014
  ///
deba@220
  1015
  ///\sa ConArcIt
kpeter@282
  1016
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
deba@220
  1017
  template <typename Graph>
deba@220
  1018
  inline typename Graph::Arc
deba@220
  1019
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@220
  1020
          typename Graph::Arc prev = INVALID) {
deba@220
  1021
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
deba@220
  1022
  }
deba@220
  1023
kpeter@282
  1024
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
deba@220
  1025
  ///
kpeter@282
  1026
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
kpeter@282
  1027
  /// a higher level interface for the \ref findArc() function. You can
deba@220
  1028
  /// use it the following way:
deba@220
  1029
  ///\code
deba@220
  1030
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@220
  1031
  ///   ...
deba@220
  1032
  /// }
deba@220
  1033
  ///\endcode
deba@220
  1034
  ///
deba@220
  1035
  ///\sa findArc()
kpeter@282
  1036
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
kpeter@559
  1037
  template <typename GR>
kpeter@559
  1038
  class ConArcIt : public GR::Arc {
kpeter@617
  1039
    typedef typename GR::Arc Parent;
kpeter@617
  1040
deba@220
  1041
  public:
deba@220
  1042
kpeter@617
  1043
    typedef typename GR::Arc Arc;
kpeter@617
  1044
    typedef typename GR::Node Node;
deba@220
  1045
deba@220
  1046
    /// \brief Constructor.
deba@220
  1047
    ///
kpeter@282
  1048
    /// Construct a new ConArcIt iterating on the arcs that
kpeter@282
  1049
    /// connects nodes \c u and \c v.
kpeter@617
  1050
    ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
deba@220
  1051
      Parent::operator=(findArc(_graph, u, v));
deba@220
  1052
    }
deba@220
  1053
deba@220
  1054
    /// \brief Constructor.
deba@220
  1055
    ///
kpeter@282
  1056
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
kpeter@617
  1057
    ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
deba@220
  1058
deba@220
  1059
    /// \brief Increment operator.
deba@220
  1060
    ///
deba@220
  1061
    /// It increments the iterator and gives back the next arc.
deba@220
  1062
    ConArcIt& operator++() {
deba@220
  1063
      Parent::operator=(findArc(_graph, _graph.source(*this),
deba@220
  1064
                                _graph.target(*this), *this));
deba@220
  1065
      return *this;
deba@220
  1066
    }
deba@220
  1067
  private:
kpeter@617
  1068
    const GR& _graph;
deba@220
  1069
  };
deba@220
  1070
deba@220
  1071
  namespace _core_bits {
deba@220
  1072
deba@220
  1073
    template <typename Graph, typename Enable = void>
deba@220
  1074
    struct FindEdgeSelector {
deba@220
  1075
      typedef typename Graph::Node Node;
deba@220
  1076
      typedef typename Graph::Edge Edge;
deba@220
  1077
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
deba@220
  1078
        bool b;
deba@220
  1079
        if (u != v) {
deba@220
  1080
          if (e == INVALID) {
deba@220
  1081
            g.firstInc(e, b, u);
deba@220
  1082
          } else {
deba@220
  1083
            b = g.u(e) == u;
deba@220
  1084
            g.nextInc(e, b);
deba@220
  1085
          }
deba@220
  1086
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
deba@220
  1087
            g.nextInc(e, b);
deba@220
  1088
          }
deba@220
  1089
        } else {
deba@220
  1090
          if (e == INVALID) {
deba@220
  1091
            g.firstInc(e, b, u);
deba@220
  1092
          } else {
deba@220
  1093
            b = true;
deba@220
  1094
            g.nextInc(e, b);
deba@220
  1095
          }
deba@220
  1096
          while (e != INVALID && (!b || g.v(e) != v)) {
deba@220
  1097
            g.nextInc(e, b);
deba@220
  1098
          }
deba@220
  1099
        }
deba@220
  1100
        return e;
deba@220
  1101
      }
deba@220
  1102
    };
deba@220
  1103
deba@220
  1104
    template <typename Graph>
deba@220
  1105
    struct FindEdgeSelector<
deba@220
  1106
      Graph,
deba@220
  1107
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
deba@220
  1108
    {
deba@220
  1109
      typedef typename Graph::Node Node;
deba@220
  1110
      typedef typename Graph::Edge Edge;
deba@220
  1111
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
deba@220
  1112
        return g.findEdge(u, v, prev);
deba@220
  1113
      }
deba@220
  1114
    };
deba@220
  1115
  }
deba@220
  1116
kpeter@282
  1117
  /// \brief Find an edge between two nodes of a graph.
deba@220
  1118
  ///
kpeter@282
  1119
  /// This function finds an edge from node \c u to node \c v in graph \c g.
kpeter@282
  1120
  /// If node \c u and node \c v is equal then each loop edge
deba@220
  1121
  /// will be enumerated once.
deba@220
  1122
  ///
deba@220
  1123
  /// If \c prev is \ref INVALID (this is the default value), then
kpeter@282
  1124
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
kpeter@282
  1125
  /// the next edge from \c u to \c v after \c prev.
kpeter@282
  1126
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@220
  1127
  ///
kpeter@282
  1128
  /// Thus you can iterate through each edge between \c u and \c v
kpeter@282
  1129
  /// as it follows.
deba@220
  1130
  ///\code
kpeter@282
  1131
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
deba@220
  1132
  ///   ...
deba@220
  1133
  /// }
deba@220
  1134
  ///\endcode
deba@220
  1135
  ///
kpeter@282
  1136
  /// \note \ref ConEdgeIt provides iterator interface for the same
kpeter@282
  1137
  /// functionality.
kpeter@282
  1138
  ///
deba@220
  1139
  ///\sa ConEdgeIt
deba@220
  1140
  template <typename Graph>
deba@220
  1141
  inline typename Graph::Edge
deba@220
  1142
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@220
  1143
            typename Graph::Edge p = INVALID) {
deba@220
  1144
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
deba@220
  1145
  }
deba@220
  1146
kpeter@282
  1147
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
deba@220
  1148
  ///
kpeter@282
  1149
  /// Iterator for iterating on parallel edges connecting the same nodes.
kpeter@282
  1150
  /// It is a higher level interface for the findEdge() function. You can
deba@220
  1151
  /// use it the following way:
deba@220
  1152
  ///\code
kpeter@282
  1153
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
deba@220
  1154
  ///   ...
deba@220
  1155
  /// }
deba@220
  1156
  ///\endcode
deba@220
  1157
  ///
deba@220
  1158
  ///\sa findEdge()
kpeter@559
  1159
  template <typename GR>
kpeter@559
  1160
  class ConEdgeIt : public GR::Edge {
kpeter@617
  1161
    typedef typename GR::Edge Parent;
kpeter@617
  1162
deba@220
  1163
  public:
deba@220
  1164
kpeter@617
  1165
    typedef typename GR::Edge Edge;
kpeter@617
  1166
    typedef typename GR::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.
kpeter@617
  1172
    ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
kpeter@429
  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.
kpeter@617
  1179
    ConEdgeIt(const GR& 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++() {
kpeter@429
  1185
      Parent::operator=(findEdge(_graph, _u, _v, *this));
deba@220
  1186
      return *this;
deba@220
  1187
    }
deba@220
  1188
  private:
kpeter@617
  1189
    const GR& _graph;
kpeter@429
  1190
    Node _u, _v;
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
  ///
kpeter@559
  1212
  ///\tparam GR The type of the underlying digraph.
deba@220
  1213
  ///
deba@220
  1214
  ///\sa ArcLookUp
deba@220
  1215
  ///\sa AllArcLookUp
kpeter@559
  1216
  template <typename GR>
deba@220
  1217
  class DynArcLookUp
kpeter@559
  1218
    : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
deba@220
  1219
  {
kpeter@559
  1220
    typedef typename ItemSetTraits<GR, typename GR::Arc>
deba@220
  1221
    ::ItemNotifier::ObserverBase Parent;
deba@220
  1222
kpeter@559
  1223
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@617
  1224
kpeter@617
  1225
  public:
kpeter@617
  1226
kpeter@617
  1227
    /// The Digraph type
kpeter@559
  1228
    typedef GR Digraph;
deba@220
  1229
deba@220
  1230
  protected:
deba@220
  1231
kpeter@559
  1232
    class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
kpeter@617
  1233
      typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
kpeter@617
  1234
deba@220
  1235
    public:
deba@220
  1236
kpeter@559
  1237
      AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
deba@220
  1238
deba@220
  1239
      virtual void add(const Node& node) {
deba@220
  1240
        Parent::add(node);
deba@220
  1241
        Parent::set(node, INVALID);
deba@220
  1242
      }
deba@220
  1243
deba@220
  1244
      virtual void add(const std::vector<Node>& nodes) {
deba@220
  1245
        Parent::add(nodes);
deba@220
  1246
        for (int i = 0; i < int(nodes.size()); ++i) {
deba@220
  1247
          Parent::set(nodes[i], INVALID);
deba@220
  1248
        }
deba@220
  1249
      }
deba@220
  1250
deba@220
  1251
      virtual void build() {
deba@220
  1252
        Parent::build();
deba@220
  1253
        Node it;
deba@220
  1254
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  1255
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  1256
          Parent::set(it, INVALID);
deba@220
  1257
        }
deba@220
  1258
      }
deba@220
  1259
    };
deba@220
  1260
deba@220
  1261
    class ArcLess {
deba@220
  1262
      const Digraph &g;
deba@220
  1263
    public:
deba@220
  1264
      ArcLess(const Digraph &_g) : g(_g) {}
deba@220
  1265
      bool operator()(Arc a,Arc b) const
deba@220
  1266
      {
deba@220
  1267
        return g.target(a)<g.target(b);
deba@220
  1268
      }
deba@220
  1269
    };
deba@220
  1270
kpeter@617
  1271
  protected: 
kpeter@617
  1272
kpeter@617
  1273
    const Digraph &_g;
kpeter@617
  1274
    AutoNodeMap _head;
kpeter@617
  1275
    typename Digraph::template ArcMap<Arc> _parent;
kpeter@617
  1276
    typename Digraph::template ArcMap<Arc> _left;
kpeter@617
  1277
    typename Digraph::template ArcMap<Arc> _right;
kpeter@617
  1278
deba@220
  1279
  public:
deba@220
  1280
deba@220
  1281
    ///Constructor
deba@220
  1282
deba@220
  1283
    ///Constructor.
deba@220
  1284
    ///
deba@220
  1285
    ///It builds up the search database.
deba@220
  1286
    DynArcLookUp(const Digraph &g)
deba@220
  1287
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
deba@220
  1288
    {
deba@220
  1289
      Parent::attach(_g.notifier(typename Digraph::Arc()));
deba@220
  1290
      refresh();
deba@220
  1291
    }
deba@220
  1292
deba@220
  1293
  protected:
deba@220
  1294
deba@220
  1295
    virtual void add(const Arc& arc) {
deba@220
  1296
      insert(arc);
deba@220
  1297
    }
deba@220
  1298
deba@220
  1299
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  1300
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  1301
        insert(arcs[i]);
deba@220
  1302
      }
deba@220
  1303
    }
deba@220
  1304
deba@220
  1305
    virtual void erase(const Arc& arc) {
deba@220
  1306
      remove(arc);
deba@220
  1307
    }
deba@220
  1308
deba@220
  1309
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  1310
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  1311
        remove(arcs[i]);
deba@220
  1312
      }
deba@220
  1313
    }
deba@220
  1314
deba@220
  1315
    virtual void build() {
deba@220
  1316
      refresh();
deba@220
  1317
    }
deba@220
  1318
deba@220
  1319
    virtual void clear() {
deba@220
  1320
      for(NodeIt n(_g);n!=INVALID;++n) {
kpeter@581
  1321
        _head[n] = INVALID;
deba@220
  1322
      }
deba@220
  1323
    }
deba@220
  1324
deba@220
  1325
    void insert(Arc arc) {
deba@220
  1326
      Node s = _g.source(arc);
deba@220
  1327
      Node t = _g.target(arc);
kpeter@581
  1328
      _left[arc] = INVALID;
kpeter@581
  1329
      _right[arc] = INVALID;
deba@220
  1330
deba@220
  1331
      Arc e = _head[s];
deba@220
  1332
      if (e == INVALID) {
kpeter@581
  1333
        _head[s] = arc;
kpeter@581
  1334
        _parent[arc] = INVALID;
deba@220
  1335
        return;
deba@220
  1336
      }
deba@220
  1337
      while (true) {
deba@220
  1338
        if (t < _g.target(e)) {
deba@220
  1339
          if (_left[e] == INVALID) {
kpeter@581
  1340
            _left[e] = arc;
kpeter@581
  1341
            _parent[arc] = e;
deba@220
  1342
            splay(arc);
deba@220
  1343
            return;
deba@220
  1344
          } else {
deba@220
  1345
            e = _left[e];
deba@220
  1346
          }
deba@220
  1347
        } else {
deba@220
  1348
          if (_right[e] == INVALID) {
kpeter@581
  1349
            _right[e] = arc;
kpeter@581
  1350
            _parent[arc] = e;
deba@220
  1351
            splay(arc);
deba@220
  1352
            return;
deba@220
  1353
          } else {
deba@220
  1354
            e = _right[e];
deba@220
  1355
          }
deba@220
  1356
        }
deba@220
  1357
      }
deba@220
  1358
    }
deba@220
  1359
deba@220
  1360
    void remove(Arc arc) {
deba@220
  1361
      if (_left[arc] == INVALID) {
deba@220
  1362
        if (_right[arc] != INVALID) {
kpeter@581
  1363
          _parent[_right[arc]] = _parent[arc];
deba@220
  1364
        }
deba@220
  1365
        if (_parent[arc] != INVALID) {
deba@220
  1366
          if (_left[_parent[arc]] == arc) {
kpeter@581
  1367
            _left[_parent[arc]] = _right[arc];
deba@220
  1368
          } else {
kpeter@581
  1369
            _right[_parent[arc]] = _right[arc];
deba@220
  1370
          }
deba@220
  1371
        } else {
kpeter@581
  1372
          _head[_g.source(arc)] = _right[arc];
deba@220
  1373
        }
deba@220
  1374
      } else if (_right[arc] == INVALID) {
kpeter@581
  1375
        _parent[_left[arc]] = _parent[arc];
deba@220
  1376
        if (_parent[arc] != INVALID) {
deba@220
  1377
          if (_left[_parent[arc]] == arc) {
kpeter@581
  1378
            _left[_parent[arc]] = _left[arc];
deba@220
  1379
          } else {
kpeter@581
  1380
            _right[_parent[arc]] = _left[arc];
deba@220
  1381
          }
deba@220
  1382
        } else {
kpeter@581
  1383
          _head[_g.source(arc)] = _left[arc];
deba@220
  1384
        }
deba@220
  1385
      } else {
deba@220
  1386
        Arc e = _left[arc];
deba@220
  1387
        if (_right[e] != INVALID) {
deba@220
  1388
          e = _right[e];
deba@220
  1389
          while (_right[e] != INVALID) {
deba@220
  1390
            e = _right[e];
deba@220
  1391
          }
deba@220
  1392
          Arc s = _parent[e];
kpeter@581
  1393
          _right[_parent[e]] = _left[e];
deba@220
  1394
          if (_left[e] != INVALID) {
kpeter@581
  1395
            _parent[_left[e]] = _parent[e];
deba@220
  1396
          }
deba@220
  1397
kpeter@581
  1398
          _left[e] = _left[arc];
kpeter@581
  1399
          _parent[_left[arc]] = e;
kpeter@581
  1400
          _right[e] = _right[arc];
kpeter@581
  1401
          _parent[_right[arc]] = e;
deba@220
  1402
kpeter@581
  1403
          _parent[e] = _parent[arc];
deba@220
  1404
          if (_parent[arc] != INVALID) {
deba@220
  1405
            if (_left[_parent[arc]] == arc) {
kpeter@581
  1406
              _left[_parent[arc]] = e;
deba@220
  1407
            } else {
kpeter@581
  1408
              _right[_parent[arc]] = e;
deba@220
  1409
            }
deba@220
  1410
          }
deba@220
  1411
          splay(s);
deba@220
  1412
        } else {
kpeter@581
  1413
          _right[e] = _right[arc];
kpeter@581
  1414
          _parent[_right[arc]] = e;
kpeter@581
  1415
          _parent[e] = _parent[arc];
deba@220
  1416
deba@220
  1417
          if (_parent[arc] != INVALID) {
deba@220
  1418
            if (_left[_parent[arc]] == arc) {
kpeter@581
  1419
              _left[_parent[arc]] = e;
deba@220
  1420
            } else {
kpeter@581
  1421
              _right[_parent[arc]] = e;
deba@220
  1422
            }
deba@220
  1423
          } else {
kpeter@581
  1424
            _head[_g.source(arc)] = e;
deba@220
  1425
          }
deba@220
  1426
        }
deba@220
  1427
      }
deba@220
  1428
    }
deba@220
  1429
deba@220
  1430
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
deba@220
  1431
    {
deba@220
  1432
      int m=(a+b)/2;
deba@220
  1433
      Arc me=v[m];
deba@220
  1434
      if (a < m) {
deba@220
  1435
        Arc left = refreshRec(v,a,m-1);
kpeter@581
  1436
        _left[me] = left;
kpeter@581
  1437
        _parent[left] = me;
deba@220
  1438
      } else {
kpeter@581
  1439
        _left[me] = INVALID;
deba@220
  1440
      }
deba@220
  1441
      if (m < b) {
deba@220
  1442
        Arc right = refreshRec(v,m+1,b);
kpeter@581
  1443
        _right[me] = right;
kpeter@581
  1444
        _parent[right] = me;
deba@220
  1445
      } else {
kpeter@581
  1446
        _right[me] = INVALID;
deba@220
  1447
      }
deba@220
  1448
      return me;
deba@220
  1449
    }
deba@220
  1450
deba@220
  1451
    void refresh() {
deba@220
  1452
      for(NodeIt n(_g);n!=INVALID;++n) {
deba@220
  1453
        std::vector<Arc> v;
deba@233
  1454
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
deba@233
  1455
        if (!v.empty()) {
deba@220
  1456
          std::sort(v.begin(),v.end(),ArcLess(_g));
deba@220
  1457
          Arc head = refreshRec(v,0,v.size()-1);
kpeter@581
  1458
          _head[n] = head;
kpeter@581
  1459
          _parent[head] = INVALID;
deba@220
  1460
        }
kpeter@581
  1461
        else _head[n] = INVALID;
deba@220
  1462
      }
deba@220
  1463
    }
deba@220
  1464
deba@220
  1465
    void zig(Arc v) {
deba@220
  1466
      Arc w = _parent[v];
kpeter@581
  1467
      _parent[v] = _parent[w];
kpeter@581
  1468
      _parent[w] = v;
kpeter@581
  1469
      _left[w] = _right[v];
kpeter@581
  1470
      _right[v] = w;
deba@220
  1471
      if (_parent[v] != INVALID) {
deba@220
  1472
        if (_right[_parent[v]] == w) {
kpeter@581
  1473
          _right[_parent[v]] = v;
deba@220
  1474
        } else {
kpeter@581
  1475
          _left[_parent[v]] = v;
deba@220
  1476
        }
deba@220
  1477
      }
deba@220
  1478
      if (_left[w] != INVALID){
kpeter@581
  1479
        _parent[_left[w]] = w;
deba@220
  1480
      }
deba@220
  1481
    }
deba@220
  1482
deba@220
  1483
    void zag(Arc v) {
deba@220
  1484
      Arc w = _parent[v];
kpeter@581
  1485
      _parent[v] = _parent[w];
kpeter@581
  1486
      _parent[w] = v;
kpeter@581
  1487
      _right[w] = _left[v];
kpeter@581
  1488
      _left[v] = w;
deba@220
  1489
      if (_parent[v] != INVALID){
deba@220
  1490
        if (_left[_parent[v]] == w) {
kpeter@581
  1491
          _left[_parent[v]] = v;
deba@220
  1492
        } else {
kpeter@581
  1493
          _right[_parent[v]] = v;
deba@220
  1494
        }
deba@220
  1495
      }
deba@220
  1496
      if (_right[w] != INVALID){
kpeter@581
  1497
        _parent[_right[w]] = w;
deba@220
  1498
      }
deba@220
  1499
    }
deba@220
  1500
deba@220
  1501
    void splay(Arc v) {
deba@220
  1502
      while (_parent[v] != INVALID) {
deba@220
  1503
        if (v == _left[_parent[v]]) {
deba@220
  1504
          if (_parent[_parent[v]] == INVALID) {
deba@220
  1505
            zig(v);
deba@220
  1506
          } else {
deba@220
  1507
            if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@220
  1508
              zig(_parent[v]);
deba@220
  1509
              zig(v);
deba@220
  1510
            } else {
deba@220
  1511
              zig(v);
deba@220
  1512
              zag(v);
deba@220
  1513
            }
deba@220
  1514
          }
deba@220
  1515
        } else {
deba@220
  1516
          if (_parent[_parent[v]] == INVALID) {
deba@220
  1517
            zag(v);
deba@220
  1518
          } else {
deba@220
  1519
            if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@220
  1520
              zag(v);
deba@220
  1521
              zig(v);
deba@220
  1522
            } else {
deba@220
  1523
              zag(_parent[v]);
deba@220
  1524
              zag(v);
deba@220
  1525
            }
deba@220
  1526
          }
deba@220
  1527
        }
deba@220
  1528
      }
deba@220
  1529
      _head[_g.source(v)] = v;
deba@220
  1530
    }
deba@220
  1531
deba@220
  1532
deba@220
  1533
  public:
deba@220
  1534
deba@220
  1535
    ///Find an arc between two nodes.
deba@220
  1536
deba@233
  1537
    ///Find an arc between two nodes.
kpeter@282
  1538
    ///\param s The source node.
kpeter@282
  1539
    ///\param t The target node.
deba@233
  1540
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
deba@233
  1541
    ///not given, the operator finds the first appropriate arc.
deba@233
  1542
    ///\return An arc from \c s to \c t after \c p or
deba@233
  1543
    ///\ref INVALID if there is no more.
deba@233
  1544
    ///
deba@233
  1545
    ///For example, you can count the number of arcs from \c u to \c v in the
deba@233
  1546
    ///following way.
deba@233
  1547
    ///\code
deba@233
  1548
    ///DynArcLookUp<ListDigraph> ae(g);
deba@233
  1549
    ///...
kpeter@282
  1550
    ///int n = 0;
kpeter@282
  1551
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
deba@233
  1552
    ///\endcode
deba@233
  1553
    ///
kpeter@282
  1554
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
deba@233
  1555
    ///amortized time, specifically, the time complexity of the lookups
deba@233
  1556
    ///is equal to the optimal search tree implementation for the
deba@233
  1557
    ///current query distribution in a constant factor.
deba@233
  1558
    ///
deba@233
  1559
    ///\note This is a dynamic data structure, therefore the data
kpeter@282
  1560
    ///structure is updated after each graph alteration. Thus although
kpeter@282
  1561
    ///this data structure is theoretically faster than \ref ArcLookUp
kpeter@313
  1562
    ///and \ref AllArcLookUp, it often provides worse performance than
deba@233
  1563
    ///them.
deba@233
  1564
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
deba@233
  1565
      if (p == INVALID) {
deba@233
  1566
        Arc a = _head[s];
deba@233
  1567
        if (a == INVALID) return INVALID;
deba@233
  1568
        Arc r = INVALID;
deba@233
  1569
        while (true) {
deba@233
  1570
          if (_g.target(a) < t) {
deba@233
  1571
            if (_right[a] == INVALID) {
deba@233
  1572
              const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  1573
              return r;
deba@233
  1574
            } else {
deba@233
  1575
              a = _right[a];
deba@233
  1576
            }
deba@233
  1577
          } else {
deba@233
  1578
            if (_g.target(a) == t) {
deba@233
  1579
              r = a;
deba@233
  1580
            }
deba@233
  1581
            if (_left[a] == INVALID) {
deba@233
  1582
              const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  1583
              return r;
deba@233
  1584
            } else {
deba@233
  1585
              a = _left[a];
deba@233
  1586
            }
deba@233
  1587
          }
deba@233
  1588
        }
deba@233
  1589
      } else {
deba@233
  1590
        Arc a = p;
deba@233
  1591
        if (_right[a] != INVALID) {
deba@233
  1592
          a = _right[a];
deba@233
  1593
          while (_left[a] != INVALID) {
deba@233
  1594
            a = _left[a];
deba@233
  1595
          }
deba@220
  1596
          const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  1597
        } else {
deba@233
  1598
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
deba@233
  1599
            a = _parent[a];
deba@233
  1600
          }
deba@233
  1601
          if (_parent[a] == INVALID) {
deba@220
  1602
            return INVALID;
deba@220
  1603
          } else {
deba@233
  1604
            a = _parent[a];
deba@220
  1605
            const_cast<DynArcLookUp&>(*this).splay(a);
deba@220
  1606
          }
deba@220
  1607
        }
deba@233
  1608
        if (_g.target(a) == t) return a;
deba@233
  1609
        else return INVALID;
deba@220
  1610
      }
deba@220
  1611
    }
deba@220
  1612
deba@220
  1613
  };
deba@220
  1614
kpeter@282
  1615
  ///Fast arc look-up between given endpoints.
deba@220
  1616
deba@220
  1617
  ///Using this class, you can find an arc in a digraph from a given
kpeter@282
  1618
  ///source to a given target in time <em>O</em>(log<em>d</em>),
deba@220
  1619
  ///where <em>d</em> is the out-degree of the source node.
deba@220
  1620
  ///
deba@220
  1621
  ///It is not possible to find \e all parallel arcs between two nodes.
deba@220
  1622
  ///Use \ref AllArcLookUp for this purpose.
deba@220
  1623
  ///
kpeter@282
  1624
  ///\warning This class is static, so you should call refresh() (or at
kpeter@282
  1625
  ///least refresh(Node)) to refresh this data structure whenever the
kpeter@282
  1626
  ///digraph changes. This is a time consuming (superlinearly proportional
kpeter@282
  1627
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
deba@220
  1628
  ///
kpeter@559
  1629
  ///\tparam GR The type of the underlying digraph.
deba@220
  1630
  ///
deba@220
  1631
  ///\sa DynArcLookUp
deba@220
  1632
  ///\sa AllArcLookUp
kpeter@559
  1633
  template<class GR>
deba@220
  1634
  class ArcLookUp
deba@220
  1635
  {
kpeter@617
  1636
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@617
  1637
deba@220
  1638
  public:
kpeter@617
  1639
kpeter@617
  1640
    /// The Digraph type
kpeter@559
  1641
    typedef GR Digraph;
deba@220
  1642
deba@220
  1643
  protected:
deba@220
  1644
    const Digraph &_g;
deba@220
  1645
    typename Digraph::template NodeMap<Arc> _head;
deba@220
  1646
    typename Digraph::template ArcMap<Arc> _left;
deba@220
  1647
    typename Digraph::template ArcMap<Arc> _right;
deba@220
  1648
deba@220
  1649
    class ArcLess {
deba@220
  1650
      const Digraph &g;
deba@220
  1651
    public:
deba@220
  1652
      ArcLess(const Digraph &_g) : g(_g) {}
deba@220
  1653
      bool operator()(Arc a,Arc b) const
deba@220
  1654
      {
deba@220
  1655
        return g.target(a)<g.target(b);
deba@220
  1656
      }
deba@220
  1657
    };
deba@220
  1658
deba@220
  1659
  public:
deba@220
  1660
deba@220
  1661
    ///Constructor
deba@220
  1662
deba@220
  1663
    ///Constructor.
deba@220
  1664
    ///
deba@220
  1665
    ///It builds up the search database, which remains valid until the digraph
deba@220
  1666
    ///changes.
deba@220
  1667
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
deba@220
  1668
deba@220
  1669
  private:
deba@220
  1670
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
deba@220
  1671
    {
deba@220
  1672
      int m=(a+b)/2;
deba@220
  1673
      Arc me=v[m];
deba@220
  1674
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
deba@220
  1675
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
deba@220
  1676
      return me;
deba@220
  1677
    }
deba@220
  1678
  public:
kpeter@282
  1679
    ///Refresh the search data structure at a node.
deba@220
  1680
deba@220
  1681
    ///Build up the search database of node \c n.
deba@220
  1682
    ///
kpeter@282
  1683
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
kpeter@282
  1684
    ///is the number of the outgoing arcs of \c n.
deba@220
  1685
    void refresh(Node n)
deba@220
  1686
    {
deba@220
  1687
      std::vector<Arc> v;
deba@220
  1688
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
deba@220
  1689
      if(v.size()) {
deba@220
  1690
        std::sort(v.begin(),v.end(),ArcLess(_g));
deba@220
  1691
        _head[n]=refreshRec(v,0,v.size()-1);
deba@220
  1692
      }
deba@220
  1693
      else _head[n]=INVALID;
deba@220
  1694
    }
deba@220
  1695
    ///Refresh the full data structure.
deba@220
  1696
deba@220
  1697
    ///Build up the full search database. In fact, it simply calls
deba@220
  1698
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
deba@220
  1699
    ///
kpeter@282
  1700
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
kpeter@282
  1701
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
deba@220
  1702
    ///out-degree of the digraph.
deba@220
  1703
    void refresh()
deba@220
  1704
    {
deba@220
  1705
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
deba@220
  1706
    }
deba@220
  1707
deba@220
  1708
    ///Find an arc between two nodes.
deba@220
  1709
kpeter@313
  1710
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
kpeter@313
  1711
    ///where <em>d</em> is the number of outgoing arcs of \c s.
kpeter@282
  1712
    ///\param s The source node.
kpeter@282
  1713
    ///\param t The target node.
deba@220
  1714
    ///\return An arc from \c s to \c t if there exists,
deba@220
  1715
    ///\ref INVALID otherwise.
deba@220
  1716
    ///
deba@220
  1717
    ///\warning If you change the digraph, refresh() must be called before using
deba@220
  1718
    ///this operator. If you change the outgoing arcs of
kpeter@282
  1719
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
deba@220
  1720
    Arc operator()(Node s, Node t) const
deba@220
  1721
    {
deba@220
  1722
      Arc e;
deba@220
  1723
      for(e=_head[s];
deba@220
  1724
          e!=INVALID&&_g.target(e)!=t;
deba@220
  1725
          e = t < _g.target(e)?_left[e]:_right[e]) ;
deba@220
  1726
      return e;
deba@220
  1727
    }
deba@220
  1728
deba@220
  1729
  };
deba@220
  1730
kpeter@282
  1731
  ///Fast look-up of all arcs between given endpoints.
deba@220
  1732
deba@220
  1733
  ///This class is the same as \ref ArcLookUp, with the addition
kpeter@282
  1734
  ///that it makes it possible to find all parallel arcs between given
kpeter@282
  1735
  ///endpoints.
deba@220
  1736
  ///
kpeter@282
  1737
  ///\warning This class is static, so you should call refresh() (or at
kpeter@282
  1738
  ///least refresh(Node)) to refresh this data structure whenever the
kpeter@282
  1739
  ///digraph changes. This is a time consuming (superlinearly proportional
kpeter@282
  1740
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
deba@220
  1741
  ///
kpeter@559
  1742
  ///\tparam GR The type of the underlying digraph.
deba@220
  1743
  ///
deba@220
  1744
  ///\sa DynArcLookUp
deba@220
  1745
  ///\sa ArcLookUp
kpeter@559
  1746
  template<class GR>
kpeter@559
  1747
  class AllArcLookUp : public ArcLookUp<GR>
deba@220
  1748
  {
kpeter@559
  1749
    using ArcLookUp<GR>::_g;
kpeter@559
  1750
    using ArcLookUp<GR>::_right;
kpeter@559
  1751
    using ArcLookUp<GR>::_left;
kpeter@559
  1752
    using ArcLookUp<GR>::_head;
deba@220
  1753
kpeter@559
  1754
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
deba@220
  1755
kpeter@617
  1756
    typename GR::template ArcMap<Arc> _next;
deba@220
  1757
deba@220
  1758
    Arc refreshNext(Arc head,Arc next=INVALID)
deba@220
  1759
    {
deba@220
  1760
      if(head==INVALID) return next;
deba@220
  1761
      else {
deba@220
  1762
        next=refreshNext(_right[head],next);
deba@220
  1763
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
deba@220
  1764
          ? next : INVALID;
deba@220
  1765
        return refreshNext(_left[head],head);
deba@220
  1766
      }
deba@220
  1767
    }
deba@220
  1768
deba@220
  1769
    void refreshNext()
deba@220
  1770
    {
deba@220
  1771
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
deba@220
  1772
    }
deba@220
  1773
deba@220
  1774
  public:
kpeter@617
  1775
kpeter@617
  1776
    /// The Digraph type
kpeter@617
  1777
    typedef GR Digraph;
kpeter@617
  1778
deba@220
  1779
    ///Constructor
deba@220
  1780
deba@220
  1781
    ///Constructor.
deba@220
  1782
    ///
deba@220
  1783
    ///It builds up the search database, which remains valid until the digraph
deba@220
  1784
    ///changes.
kpeter@559
  1785
    AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
deba@220
  1786
deba@220
  1787
    ///Refresh the data structure at a node.
deba@220
  1788
deba@220
  1789
    ///Build up the search database of node \c n.
deba@220
  1790
    ///
kpeter@282
  1791
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
deba@220
  1792
    ///the number of the outgoing arcs of \c n.
deba@220
  1793
    void refresh(Node n)
deba@220
  1794
    {
kpeter@559
  1795
      ArcLookUp<GR>::refresh(n);
deba@220
  1796
      refreshNext(_head[n]);
deba@220
  1797
    }
deba@220
  1798
deba@220
  1799
    ///Refresh the full data structure.
deba@220
  1800
deba@220
  1801
    ///Build up the full search database. In fact, it simply calls
deba@220
  1802
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
deba@220
  1803
    ///
kpeter@282
  1804
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
kpeter@282
  1805
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
deba@220
  1806
    ///out-degree of the digraph.
deba@220
  1807
    void refresh()
deba@220
  1808
    {
deba@220
  1809
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
deba@220
  1810
    }
deba@220
  1811
deba@220
  1812
    ///Find an arc between two nodes.
deba@220
  1813
deba@220
  1814
    ///Find an arc between two nodes.
kpeter@282
  1815
    ///\param s The source node.
kpeter@282
  1816
    ///\param t The target node.
deba@220
  1817
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
deba@220
  1818
    ///not given, the operator finds the first appropriate arc.
deba@220
  1819
    ///\return An arc from \c s to \c t after \c prev or
deba@220
  1820
    ///\ref INVALID if there is no more.
deba@220
  1821
    ///
deba@220
  1822
    ///For example, you can count the number of arcs from \c u to \c v in the
deba@220
  1823
    ///following way.
deba@220
  1824
    ///\code
deba@220
  1825
    ///AllArcLookUp<ListDigraph> ae(g);
deba@220
  1826
    ///...
kpeter@282
  1827
    ///int n = 0;
kpeter@282
  1828
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
deba@220
  1829
    ///\endcode
deba@220
  1830
    ///
kpeter@313
  1831
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
kpeter@313
  1832
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
deba@220
  1833
    ///consecutive arcs are found in constant time.
deba@220
  1834
    ///
deba@220
  1835
    ///\warning If you change the digraph, refresh() must be called before using
deba@220
  1836
    ///this operator. If you change the outgoing arcs of
kpeter@282
  1837
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
deba@220
  1838
    ///
deba@220
  1839
#ifdef DOXYGEN
deba@220
  1840
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
deba@220
  1841
#else
kpeter@559
  1842
    using ArcLookUp<GR>::operator() ;
deba@220
  1843
    Arc operator()(Node s, Node t, Arc prev) const
deba@220
  1844
    {
deba@220
  1845
      return prev==INVALID?(*this)(s,t):_next[prev];
deba@220
  1846
    }
deba@220
  1847
#endif
deba@220
  1848
deba@220
  1849
  };
deba@220
  1850
deba@220
  1851
  /// @}
deba@220
  1852
deba@220
  1853
} //namespace lemon
deba@220
  1854
deba@220
  1855
#endif