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