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