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