lemon/core.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 22 Oct 2013 12:24:09 +0200
changeset 1128 ee96cd1cad8a
parent 1086 97f1760dcd13
child 1121 1d80ec7d17eb
permissions -rw-r--r--
Turn on LEMON_ENABLE_DEBUG in Debug modes (#477)
deba@220
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@220
     2
 *
deba@220
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@220
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
deba@220
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@220
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@220
     8
 *
deba@220
     9
 * Permission to use, modify and distribute this software is granted
deba@220
    10
 * provided that this copyright notice appears in all copies. For
deba@220
    11
 * precise terms see the accompanying LICENSE file.
deba@220
    12
 *
deba@220
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@220
    14
 * express or implied, and with no claim as to its suitability for any
deba@220
    15
 * purpose.
deba@220
    16
 *
deba@220
    17
 */
deba@220
    18
deba@220
    19
#ifndef LEMON_CORE_H
deba@220
    20
#define LEMON_CORE_H
deba@220
    21
deba@220
    22
#include <vector>
deba@220
    23
#include <algorithm>
deba@220
    24
ladanyi@512
    25
#include <lemon/config.h>
deba@220
    26
#include <lemon/bits/enable_if.h>
deba@220
    27
#include <lemon/bits/traits.h>
alpar@319
    28
#include <lemon/assert.h>
deba@220
    29
ladanyi@671
    30
// Disable the following warnings when compiling with MSVC:
ladanyi@671
    31
// C4250: 'class1' : inherits 'class2::member' via dominance
ladanyi@671
    32
// C4355: 'this' : used in base member initializer list
ladanyi@671
    33
// C4503: 'function' : decorated name length exceeded, name was truncated
ladanyi@671
    34
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
ladanyi@671
    35
// C4996: 'function': was declared deprecated
ladanyi@671
    36
#ifdef _MSC_VER
ladanyi@671
    37
#pragma warning( disable : 4250 4355 4503 4800 4996 )
ladanyi@671
    38
#endif
ladanyi@671
    39
alpar@1068
    40
#ifdef __GNUC__
alpar@1077
    41
#define GCC_VERSION (__GNUC__ * 10000                   \
alpar@1077
    42
                     + __GNUC_MINOR__ * 100             \
alpar@1077
    43
                     + __GNUC_PATCHLEVEL__)
alpar@1077
    44
#endif
alpar@1077
    45
alpar@1077
    46
#if GCC_VERSION >= 40800
alpar@1068
    47
// Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8
alpar@1068
    48
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
alpar@1068
    49
#endif
alpar@1068
    50
deba@220
    51
///\file
deba@220
    52
///\brief LEMON core utilities.
kpeter@229
    53
///
kpeter@229
    54
///This header file contains core utilities for LEMON.
deba@233
    55
///It is automatically included by all graph types, therefore it usually
kpeter@229
    56
///do not have to be included directly.
deba@220
    57
deba@220
    58
namespace lemon {
deba@220
    59
deba@220
    60
  /// \brief Dummy type to make it easier to create invalid iterators.
deba@220
    61
  ///
deba@220
    62
  /// Dummy type to make it easier to create invalid iterators.
deba@220
    63
  /// See \ref INVALID for the usage.
deba@220
    64
  struct Invalid {
deba@220
    65
  public:
deba@220
    66
    bool operator==(Invalid) { return true;  }
deba@220
    67
    bool operator!=(Invalid) { return false; }
deba@220
    68
    bool operator< (Invalid) { return false; }
deba@220
    69
  };
deba@220
    70
deba@220
    71
  /// \brief Invalid iterators.
deba@220
    72
  ///
deba@220
    73
  /// \ref Invalid is a global type that converts to each iterator
deba@220
    74
  /// in such a way that the value of the target iterator will be invalid.
deba@220
    75
#ifdef LEMON_ONLY_TEMPLATES
deba@220
    76
  const Invalid INVALID = Invalid();
deba@220
    77
#else
deba@220
    78
  extern const Invalid INVALID;
deba@220
    79
#endif
deba@220
    80
deba@220
    81
  /// \addtogroup gutils
deba@220
    82
  /// @{
deba@220
    83
kpeter@300
    84
  ///Create convenience typedefs for the digraph types and iterators
deba@220
    85
kpeter@282
    86
  ///This \c \#define creates convenient type definitions for the following
kpeter@282
    87
  ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
deba@220
    88
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
deba@220
    89
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
deba@220
    90
  ///
deba@220
    91
  ///\note If the graph type is a dependent type, ie. the graph type depend
deba@220
    92
  ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
deba@220
    93
  ///macro.
deba@220
    94
#define DIGRAPH_TYPEDEFS(Digraph)                                       \
deba@220
    95
  typedef Digraph::Node Node;                                           \
deba@220
    96
  typedef Digraph::NodeIt NodeIt;                                       \
deba@220
    97
  typedef Digraph::Arc Arc;                                             \
deba@220
    98
  typedef Digraph::ArcIt ArcIt;                                         \
deba@220
    99
  typedef Digraph::InArcIt InArcIt;                                     \
deba@220
   100
  typedef Digraph::OutArcIt OutArcIt;                                   \
deba@220
   101
  typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
deba@220
   102
  typedef Digraph::NodeMap<int> IntNodeMap;                             \
deba@220
   103
  typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
deba@220
   104
  typedef Digraph::ArcMap<bool> BoolArcMap;                             \
deba@220
   105
  typedef Digraph::ArcMap<int> IntArcMap;                               \
kpeter@300
   106
  typedef Digraph::ArcMap<double> DoubleArcMap
deba@220
   107
kpeter@300
   108
  ///Create convenience typedefs for the digraph types and iterators
deba@220
   109
deba@220
   110
  ///\see DIGRAPH_TYPEDEFS
deba@220
   111
  ///
deba@220
   112
  ///\note Use this macro, if the graph type is a dependent type,
deba@220
   113
  ///ie. the graph type depend on a template parameter.
deba@220
   114
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
deba@220
   115
  typedef typename Digraph::Node Node;                                  \
deba@220
   116
  typedef typename Digraph::NodeIt NodeIt;                              \
deba@220
   117
  typedef typename Digraph::Arc Arc;                                    \
deba@220
   118
  typedef typename Digraph::ArcIt ArcIt;                                \
deba@220
   119
  typedef typename Digraph::InArcIt InArcIt;                            \
deba@220
   120
  typedef typename Digraph::OutArcIt OutArcIt;                          \
deba@220
   121
  typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
deba@220
   122
  typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
deba@220
   123
  typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
deba@220
   124
  typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
deba@220
   125
  typedef typename Digraph::template ArcMap<int> IntArcMap;             \
kpeter@300
   126
  typedef typename Digraph::template ArcMap<double> DoubleArcMap
deba@220
   127
kpeter@300
   128
  ///Create convenience typedefs for the graph types and iterators
deba@220
   129
kpeter@282
   130
  ///This \c \#define creates the same convenient type definitions as defined
deba@220
   131
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
deba@220
   132
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
deba@220
   133
  ///\c DoubleEdgeMap.
deba@220
   134
  ///
deba@220
   135
  ///\note If the graph type is a dependent type, ie. the graph type depend
kpeter@282
   136
  ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
deba@220
   137
  ///macro.
deba@220
   138
#define GRAPH_TYPEDEFS(Graph)                                           \
deba@220
   139
  DIGRAPH_TYPEDEFS(Graph);                                              \
deba@220
   140
  typedef Graph::Edge Edge;                                             \
deba@220
   141
  typedef Graph::EdgeIt EdgeIt;                                         \
deba@220
   142
  typedef Graph::IncEdgeIt IncEdgeIt;                                   \
deba@220
   143
  typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
deba@220
   144
  typedef Graph::EdgeMap<int> IntEdgeMap;                               \
kpeter@300
   145
  typedef Graph::EdgeMap<double> DoubleEdgeMap
deba@220
   146
kpeter@300
   147
  ///Create convenience typedefs for the graph types and iterators
deba@220
   148
deba@220
   149
  ///\see GRAPH_TYPEDEFS
deba@220
   150
  ///
deba@220
   151
  ///\note Use this macro, if the graph type is a dependent type,
deba@220
   152
  ///ie. the graph type depend on a template parameter.
deba@220
   153
#define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \
deba@220
   154
  TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \
deba@220
   155
  typedef typename Graph::Edge Edge;                                    \
deba@220
   156
  typedef typename Graph::EdgeIt EdgeIt;                                \
deba@220
   157
  typedef typename Graph::IncEdgeIt IncEdgeIt;                          \
deba@220
   158
  typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \
deba@220
   159
  typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
kpeter@300
   160
  typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
deba@220
   161
deba@1019
   162
  ///Create convenience typedefs for the bipartite graph types and iterators
deba@1019
   163
deba@1026
   164
  ///This \c \#define creates the same convenient type definitions as
deba@1026
   165
  ///defined by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it
deba@1026
   166
  ///creates \c RedNode, \c RedNodeIt, \c BoolRedNodeMap,
deba@1026
   167
  ///\c IntRedNodeMap, \c DoubleRedNodeMap, \c BlueNode, \c BlueNodeIt,
deba@1026
   168
  ///\c BoolBlueNodeMap, \c IntBlueNodeMap, \c DoubleBlueNodeMap.
deba@1019
   169
  ///
deba@1019
   170
  ///\note If the graph type is a dependent type, ie. the graph type depend
deba@1019
   171
  ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS()
deba@1019
   172
  ///macro.
deba@1019
   173
#define BPGRAPH_TYPEDEFS(BpGraph)                                       \
deba@1019
   174
  GRAPH_TYPEDEFS(BpGraph);                                              \
deba@1019
   175
  typedef BpGraph::RedNode RedNode;                                     \
deba@1026
   176
  typedef BpGraph::RedNodeIt RedNodeIt;                                 \
deba@1026
   177
  typedef BpGraph::RedNodeMap<bool> BoolRedNodeMap;                     \
deba@1026
   178
  typedef BpGraph::RedNodeMap<int> IntRedNodeMap;                       \
deba@1026
   179
  typedef BpGraph::RedNodeMap<double> DoubleRedNodeMap;                 \
deba@1019
   180
  typedef BpGraph::BlueNode BlueNode;                                   \
deba@1026
   181
  typedef BpGraph::BlueNodeIt BlueNodeIt;                               \
deba@1026
   182
  typedef BpGraph::BlueNodeMap<bool> BoolBlueNodeMap;                   \
deba@1026
   183
  typedef BpGraph::BlueNodeMap<int> IntBlueNodeMap;                     \
deba@1026
   184
  typedef BpGraph::BlueNodeMap<double> DoubleBlueNodeMap
deba@1019
   185
deba@1019
   186
  ///Create convenience typedefs for the bipartite graph types and iterators
deba@1019
   187
deba@1019
   188
  ///\see BPGRAPH_TYPEDEFS
deba@1019
   189
  ///
deba@1019
   190
  ///\note Use this macro, if the graph type is a dependent type,
deba@1019
   191
  ///ie. the graph type depend on a template parameter.
deba@1026
   192
#define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph)                                  \
deba@1026
   193
  TEMPLATE_GRAPH_TYPEDEFS(BpGraph);                                         \
deba@1026
   194
  typedef typename BpGraph::RedNode RedNode;                                \
deba@1026
   195
  typedef typename BpGraph::RedNodeIt RedNodeIt;                            \
deba@1026
   196
  typedef typename BpGraph::template RedNodeMap<bool> BoolRedNodeMap;       \
deba@1026
   197
  typedef typename BpGraph::template RedNodeMap<int> IntRedNodeMap;         \
deba@1026
   198
  typedef typename BpGraph::template RedNodeMap<double> DoubleRedNodeMap;   \
deba@1026
   199
  typedef typename BpGraph::BlueNode BlueNode;                              \
deba@1026
   200
  typedef typename BpGraph::BlueNodeIt BlueNodeIt;                          \
deba@1026
   201
  typedef typename BpGraph::template BlueNodeMap<bool> BoolBlueNodeMap;     \
deba@1026
   202
  typedef typename BpGraph::template BlueNodeMap<int> IntBlueNodeMap;       \
deba@1026
   203
  typedef typename BpGraph::template BlueNodeMap<double> DoubleBlueNodeMap
deba@1019
   204
kpeter@282
   205
  /// \brief Function to count the items in a graph.
deba@220
   206
  ///
kpeter@282
   207
  /// This function counts the items (nodes, arcs etc.) in a graph.
kpeter@282
   208
  /// The complexity of the function is linear because
deba@220
   209
  /// it iterates on all of the items.
deba@220
   210
  template <typename Graph, typename Item>
deba@220
   211
  inline int countItems(const Graph& g) {
deba@220
   212
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@220
   213
    int num = 0;
deba@220
   214
    for (ItemIt it(g); it != INVALID; ++it) {
deba@220
   215
      ++num;
deba@220
   216
    }
deba@220
   217
    return num;
deba@220
   218
  }
deba@220
   219
deba@220
   220
  // Node counting:
deba@220
   221
deba@220
   222
  namespace _core_bits {
deba@220
   223
deba@220
   224
    template <typename Graph, typename Enable = void>
deba@220
   225
    struct CountNodesSelector {
deba@220
   226
      static int count(const Graph &g) {
deba@220
   227
        return countItems<Graph, typename Graph::Node>(g);
deba@220
   228
      }
deba@220
   229
    };
deba@220
   230
deba@220
   231
    template <typename Graph>
deba@220
   232
    struct CountNodesSelector<
deba@220
   233
      Graph, typename
deba@220
   234
      enable_if<typename Graph::NodeNumTag, void>::type>
deba@220
   235
    {
deba@220
   236
      static int count(const Graph &g) {
deba@220
   237
        return g.nodeNum();
deba@220
   238
      }
deba@220
   239
    };
deba@220
   240
  }
deba@220
   241
deba@220
   242
  /// \brief Function to count the nodes in the graph.
deba@220
   243
  ///
deba@220
   244
  /// This function counts the nodes in the graph.
kpeter@282
   245
  /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
kpeter@282
   246
  /// graph structures it is specialized to run in <em>O</em>(1).
deba@220
   247
  ///
kpeter@282
   248
  /// \note If the graph contains a \c nodeNum() member function and a
kpeter@282
   249
  /// \c NodeNumTag tag then this function calls directly the member
deba@220
   250
  /// function to query the cardinality of the node set.
deba@220
   251
  template <typename Graph>
deba@220
   252
  inline int countNodes(const Graph& g) {
deba@220
   253
    return _core_bits::CountNodesSelector<Graph>::count(g);
deba@220
   254
  }
deba@220
   255
deba@1019
   256
  namespace _graph_utils_bits {
alpar@1092
   257
deba@1019
   258
    template <typename Graph, typename Enable = void>
deba@1019
   259
    struct CountRedNodesSelector {
deba@1019
   260
      static int count(const Graph &g) {
deba@1019
   261
        return countItems<Graph, typename Graph::RedNode>(g);
deba@1019
   262
      }
deba@1019
   263
    };
deba@1019
   264
deba@1019
   265
    template <typename Graph>
deba@1019
   266
    struct CountRedNodesSelector<
alpar@1092
   267
      Graph, typename
alpar@1092
   268
      enable_if<typename Graph::NodeNumTag, void>::type>
deba@1019
   269
    {
deba@1019
   270
      static int count(const Graph &g) {
deba@1019
   271
        return g.redNum();
deba@1019
   272
      }
alpar@1092
   273
    };
deba@1019
   274
  }
deba@1019
   275
deba@1019
   276
  /// \brief Function to count the red nodes in the graph.
deba@1019
   277
  ///
deba@1019
   278
  /// This function counts the red nodes in the graph.
deba@1019
   279
  /// The complexity of the function is O(n) but for some
deba@1019
   280
  /// graph structures it is specialized to run in O(1).
deba@1019
   281
  ///
alpar@1092
   282
  /// If the graph contains a \e redNum() member function and a
deba@1019
   283
  /// \e NodeNumTag tag then this function calls directly the member
deba@1019
   284
  /// function to query the cardinality of the node set.
deba@1019
   285
  template <typename Graph>
deba@1019
   286
  inline int countRedNodes(const Graph& g) {
deba@1019
   287
    return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g);
deba@1019
   288
  }
deba@1019
   289
deba@1019
   290
  namespace _graph_utils_bits {
alpar@1092
   291
deba@1019
   292
    template <typename Graph, typename Enable = void>
deba@1019
   293
    struct CountBlueNodesSelector {
deba@1019
   294
      static int count(const Graph &g) {
deba@1019
   295
        return countItems<Graph, typename Graph::BlueNode>(g);
deba@1019
   296
      }
deba@1019
   297
    };
deba@1019
   298
deba@1019
   299
    template <typename Graph>
deba@1019
   300
    struct CountBlueNodesSelector<
alpar@1092
   301
      Graph, typename
alpar@1092
   302
      enable_if<typename Graph::NodeNumTag, void>::type>
deba@1019
   303
    {
deba@1019
   304
      static int count(const Graph &g) {
deba@1019
   305
        return g.blueNum();
deba@1019
   306
      }
alpar@1092
   307
    };
deba@1019
   308
  }
deba@1019
   309
deba@1019
   310
  /// \brief Function to count the blue nodes in the graph.
deba@1019
   311
  ///
deba@1019
   312
  /// This function counts the blue nodes in the graph.
deba@1019
   313
  /// The complexity of the function is O(n) but for some
deba@1019
   314
  /// graph structures it is specialized to run in O(1).
deba@1019
   315
  ///
alpar@1092
   316
  /// If the graph contains a \e blueNum() member function and a
deba@1019
   317
  /// \e NodeNumTag tag then this function calls directly the member
deba@1019
   318
  /// function to query the cardinality of the node set.
deba@1019
   319
  template <typename Graph>
deba@1019
   320
  inline int countBlueNodes(const Graph& g) {
deba@1019
   321
    return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g);
deba@1019
   322
  }
deba@1019
   323
deba@220
   324
  // Arc counting:
deba@220
   325
deba@220
   326
  namespace _core_bits {
deba@220
   327
deba@220
   328
    template <typename Graph, typename Enable = void>
deba@220
   329
    struct CountArcsSelector {
deba@220
   330
      static int count(const Graph &g) {
deba@220
   331
        return countItems<Graph, typename Graph::Arc>(g);
deba@220
   332
      }
deba@220
   333
    };
deba@220
   334
deba@220
   335
    template <typename Graph>
deba@220
   336
    struct CountArcsSelector<
deba@220
   337
      Graph,
deba@220
   338
      typename enable_if<typename Graph::ArcNumTag, void>::type>
deba@220
   339
    {
deba@220
   340
      static int count(const Graph &g) {
deba@220
   341
        return g.arcNum();
deba@220
   342
      }
deba@220
   343
    };
deba@220
   344
  }
deba@220
   345
deba@220
   346
  /// \brief Function to count the arcs in the graph.
deba@220
   347
  ///
deba@220
   348
  /// This function counts the arcs in the graph.
kpeter@282
   349
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
kpeter@282
   350
  /// graph structures it is specialized to run in <em>O</em>(1).
deba@220
   351
  ///
kpeter@282
   352
  /// \note If the graph contains a \c arcNum() member function and a
kpeter@282
   353
  /// \c ArcNumTag tag then this function calls directly the member
deba@220
   354
  /// function to query the cardinality of the arc set.
deba@220
   355
  template <typename Graph>
deba@220
   356
  inline int countArcs(const Graph& g) {
deba@220
   357
    return _core_bits::CountArcsSelector<Graph>::count(g);
deba@220
   358
  }
deba@220
   359
deba@220
   360
  // Edge counting:
kpeter@282
   361
deba@220
   362
  namespace _core_bits {
deba@220
   363
deba@220
   364
    template <typename Graph, typename Enable = void>
deba@220
   365
    struct CountEdgesSelector {
deba@220
   366
      static int count(const Graph &g) {
deba@220
   367
        return countItems<Graph, typename Graph::Edge>(g);
deba@220
   368
      }
deba@220
   369
    };
deba@220
   370
deba@220
   371
    template <typename Graph>
deba@220
   372
    struct CountEdgesSelector<
deba@220
   373
      Graph,
deba@220
   374
      typename enable_if<typename Graph::EdgeNumTag, void>::type>
deba@220
   375
    {
deba@220
   376
      static int count(const Graph &g) {
deba@220
   377
        return g.edgeNum();
deba@220
   378
      }
deba@220
   379
    };
deba@220
   380
  }
deba@220
   381
deba@220
   382
  /// \brief Function to count the edges in the graph.
deba@220
   383
  ///
deba@220
   384
  /// This function counts the edges in the graph.
kpeter@282
   385
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
kpeter@282
   386
  /// graph structures it is specialized to run in <em>O</em>(1).
deba@220
   387
  ///
kpeter@282
   388
  /// \note If the graph contains a \c edgeNum() member function and a
kpeter@282
   389
  /// \c EdgeNumTag tag then this function calls directly the member
deba@220
   390
  /// function to query the cardinality of the edge set.
deba@220
   391
  template <typename Graph>
deba@220
   392
  inline int countEdges(const Graph& g) {
deba@220
   393
    return _core_bits::CountEdgesSelector<Graph>::count(g);
deba@220
   394
deba@220
   395
  }
deba@220
   396
deba@220
   397
deba@220
   398
  template <typename Graph, typename DegIt>
deba@220
   399
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
deba@220
   400
    int num = 0;
deba@220
   401
    for (DegIt it(_g, _n); it != INVALID; ++it) {
deba@220
   402
      ++num;
deba@220
   403
    }
deba@220
   404
    return num;
deba@220
   405
  }
deba@220
   406
deba@220
   407
  /// \brief Function to count the number of the out-arcs from node \c n.
deba@220
   408
  ///
deba@220
   409
  /// This function counts the number of the out-arcs from node \c n
kpeter@282
   410
  /// in the graph \c g.
deba@220
   411
  template <typename Graph>
kpeter@282
   412
  inline int countOutArcs(const Graph& g,  const typename Graph::Node& n) {
kpeter@282
   413
    return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
deba@220
   414
  }
deba@220
   415
deba@220
   416
  /// \brief Function to count the number of the in-arcs to node \c n.
deba@220
   417
  ///
deba@220
   418
  /// This function counts the number of the in-arcs to node \c n
kpeter@282
   419
  /// in the graph \c g.
deba@220
   420
  template <typename Graph>
kpeter@282
   421
  inline int countInArcs(const Graph& g,  const typename Graph::Node& n) {
kpeter@282
   422
    return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
deba@220
   423
  }
deba@220
   424
deba@220
   425
  /// \brief Function to count the number of the inc-edges to node \c n.
deba@220
   426
  ///
deba@220
   427
  /// This function counts the number of the inc-edges to node \c n
kpeter@282
   428
  /// in the undirected graph \c g.
deba@220
   429
  template <typename Graph>
kpeter@282
   430
  inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) {
kpeter@282
   431
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
deba@220
   432
  }
deba@220
   433
deba@220
   434
  namespace _core_bits {
deba@220
   435
deba@220
   436
    template <typename Digraph, typename Item, typename RefMap>
deba@220
   437
    class MapCopyBase {
deba@220
   438
    public:
deba@220
   439
      virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
deba@220
   440
deba@220
   441
      virtual ~MapCopyBase() {}
deba@220
   442
    };
deba@220
   443
deba@220
   444
    template <typename Digraph, typename Item, typename RefMap,
kpeter@282
   445
              typename FromMap, typename ToMap>
deba@220
   446
    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
deba@220
   447
    public:
deba@220
   448
kpeter@282
   449
      MapCopy(const FromMap& map, ToMap& tmap)
kpeter@282
   450
        : _map(map), _tmap(tmap) {}
deba@220
   451
deba@220
   452
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
deba@220
   453
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
deba@220
   454
        for (ItemIt it(digraph); it != INVALID; ++it) {
deba@220
   455
          _tmap.set(refMap[it], _map[it]);
deba@220
   456
        }
deba@220
   457
      }
deba@220
   458
deba@220
   459
    private:
kpeter@282
   460
      const FromMap& _map;
deba@220
   461
      ToMap& _tmap;
deba@220
   462
    };
deba@220
   463
deba@220
   464
    template <typename Digraph, typename Item, typename RefMap, typename It>
deba@220
   465
    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
deba@220
   466
    public:
deba@220
   467
kpeter@282
   468
      ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
deba@220
   469
deba@220
   470
      virtual void copy(const Digraph&, const RefMap& refMap) {
deba@220
   471
        _it = refMap[_item];
deba@220
   472
      }
deba@220
   473
deba@220
   474
    private:
kpeter@282
   475
      Item _item;
deba@220
   476
      It& _it;
deba@220
   477
    };
deba@220
   478
deba@220
   479
    template <typename Digraph, typename Item, typename RefMap, typename Ref>
deba@220
   480
    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
deba@220
   481
    public:
deba@220
   482
deba@220
   483
      RefCopy(Ref& map) : _map(map) {}
deba@220
   484
deba@220
   485
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
deba@220
   486
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
deba@220
   487
        for (ItemIt it(digraph); it != INVALID; ++it) {
deba@220
   488
          _map.set(it, refMap[it]);
deba@220
   489
        }
deba@220
   490
      }
deba@220
   491
deba@220
   492
    private:
deba@220
   493
      Ref& _map;
deba@220
   494
    };
deba@220
   495
deba@220
   496
    template <typename Digraph, typename Item, typename RefMap,
deba@220
   497
              typename CrossRef>
deba@220
   498
    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
deba@220
   499
    public:
deba@220
   500
deba@220
   501
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
deba@220
   502
deba@220
   503
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
deba@220
   504
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
deba@220
   505
        for (ItemIt it(digraph); it != INVALID; ++it) {
deba@220
   506
          _cmap.set(refMap[it], it);
deba@220
   507
        }
deba@220
   508
      }
deba@220
   509
deba@220
   510
    private:
deba@220
   511
      CrossRef& _cmap;
deba@220
   512
    };
deba@220
   513
deba@220
   514
    template <typename Digraph, typename Enable = void>
deba@220
   515
    struct DigraphCopySelector {
deba@220
   516
      template <typename From, typename NodeRefMap, typename ArcRefMap>
kpeter@282
   517
      static void copy(const From& from, Digraph &to,
deba@220
   518
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
kpeter@890
   519
        to.clear();
deba@220
   520
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@220
   521
          nodeRefMap[it] = to.addNode();
deba@220
   522
        }
deba@220
   523
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
deba@220
   524
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
deba@220
   525
                                    nodeRefMap[from.target(it)]);
deba@220
   526
        }
deba@220
   527
      }
deba@220
   528
    };
deba@220
   529
deba@220
   530
    template <typename Digraph>
deba@220
   531
    struct DigraphCopySelector<
deba@220
   532
      Digraph,
deba@220
   533
      typename enable_if<typename Digraph::BuildTag, void>::type>
deba@220
   534
    {
deba@220
   535
      template <typename From, typename NodeRefMap, typename ArcRefMap>
kpeter@282
   536
      static void copy(const From& from, Digraph &to,
deba@220
   537
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
deba@220
   538
        to.build(from, nodeRefMap, arcRefMap);
deba@220
   539
      }
deba@220
   540
    };
deba@220
   541
deba@220
   542
    template <typename Graph, typename Enable = void>
deba@220
   543
    struct GraphCopySelector {
deba@220
   544
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
kpeter@282
   545
      static void copy(const From& from, Graph &to,
deba@220
   546
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
kpeter@890
   547
        to.clear();
deba@220
   548
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@220
   549
          nodeRefMap[it] = to.addNode();
deba@220
   550
        }
deba@220
   551
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
deba@220
   552
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
deba@220
   553
                                      nodeRefMap[from.v(it)]);
deba@220
   554
        }
deba@220
   555
      }
deba@220
   556
    };
deba@220
   557
deba@220
   558
    template <typename Graph>
deba@220
   559
    struct GraphCopySelector<
deba@220
   560
      Graph,
deba@220
   561
      typename enable_if<typename Graph::BuildTag, void>::type>
deba@220
   562
    {
deba@220
   563
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
kpeter@282
   564
      static void copy(const From& from, Graph &to,
deba@1025
   565
                       NodeRefMap& nodeRefMap,
deba@1025
   566
                       EdgeRefMap& edgeRefMap) {
deba@220
   567
        to.build(from, nodeRefMap, edgeRefMap);
deba@220
   568
      }
deba@220
   569
    };
deba@220
   570
deba@1022
   571
    template <typename BpGraph, typename Enable = void>
deba@1022
   572
    struct BpGraphCopySelector {
deba@1025
   573
      template <typename From, typename RedNodeRefMap,
deba@1025
   574
                typename BlueNodeRefMap, typename EdgeRefMap>
deba@1022
   575
      static void copy(const From& from, BpGraph &to,
deba@1025
   576
                       RedNodeRefMap& redNodeRefMap,
deba@1025
   577
                       BlueNodeRefMap& blueNodeRefMap,
deba@1025
   578
                       EdgeRefMap& edgeRefMap) {
deba@1022
   579
        to.clear();
deba@1026
   580
        for (typename From::RedNodeIt it(from); it != INVALID; ++it) {
deba@1025
   581
          redNodeRefMap[it] = to.addRedNode();
deba@1022
   582
        }
deba@1026
   583
        for (typename From::BlueNodeIt it(from); it != INVALID; ++it) {
deba@1025
   584
          blueNodeRefMap[it] = to.addBlueNode();
deba@1022
   585
        }
deba@1022
   586
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
deba@1025
   587
          edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)],
deba@1025
   588
                                      blueNodeRefMap[from.blueNode(it)]);
deba@1022
   589
        }
deba@1022
   590
      }
deba@1022
   591
    };
deba@1022
   592
deba@1022
   593
    template <typename BpGraph>
deba@1022
   594
    struct BpGraphCopySelector<
deba@1022
   595
      BpGraph,
deba@1022
   596
      typename enable_if<typename BpGraph::BuildTag, void>::type>
deba@1022
   597
    {
deba@1025
   598
      template <typename From, typename RedNodeRefMap,
deba@1025
   599
                typename BlueNodeRefMap, typename EdgeRefMap>
deba@1022
   600
      static void copy(const From& from, BpGraph &to,
deba@1025
   601
                       RedNodeRefMap& redNodeRefMap,
deba@1025
   602
                       BlueNodeRefMap& blueNodeRefMap,
deba@1025
   603
                       EdgeRefMap& edgeRefMap) {
deba@1025
   604
        to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap);
deba@1022
   605
      }
deba@1022
   606
    };
deba@1022
   607
deba@220
   608
  }
deba@220
   609
kpeter@919
   610
  /// \brief Check whether a graph is undirected.
kpeter@883
   611
  ///
kpeter@883
   612
  /// This function returns \c true if the given graph is undirected.
kpeter@883
   613
#ifdef DOXYGEN
kpeter@883
   614
  template <typename GR>
kpeter@883
   615
  bool undirected(const GR& g) { return false; }
kpeter@883
   616
#else
kpeter@883
   617
  template <typename GR>
kpeter@883
   618
  typename enable_if<UndirectedTagIndicator<GR>, bool>::type
kpeter@883
   619
  undirected(const GR&) {
kpeter@883
   620
    return true;
kpeter@883
   621
  }
kpeter@883
   622
  template <typename GR>
kpeter@883
   623
  typename disable_if<UndirectedTagIndicator<GR>, bool>::type
kpeter@883
   624
  undirected(const GR&) {
kpeter@883
   625
    return false;
kpeter@883
   626
  }
kpeter@883
   627
#endif
kpeter@883
   628
deba@220
   629
  /// \brief Class to copy a digraph.
deba@220
   630
  ///
deba@220
   631
  /// Class to copy a digraph to another digraph (duplicate a digraph). The
kpeter@282
   632
  /// simplest way of using it is through the \c digraphCopy() function.
deba@220
   633
  ///
kpeter@282
   634
  /// This class not only make a copy of a digraph, but it can create
deba@220
   635
  /// references and cross references between the nodes and arcs of
kpeter@282
   636
  /// the two digraphs, and it can copy maps to use with the newly created
kpeter@282
   637
  /// digraph.
deba@220
   638
  ///
kpeter@282
   639
  /// To make a copy from a digraph, first an instance of DigraphCopy
kpeter@282
   640
  /// should be created, then the data belongs to the digraph should
deba@220
   641
  /// assigned to copy. In the end, the \c run() member should be
deba@220
   642
  /// called.
deba@220
   643
  ///
kpeter@282
   644
  /// The next code copies a digraph with several data:
deba@220
   645
  ///\code
kpeter@282
   646
  ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
kpeter@282
   647
  ///  // Create references for the nodes
deba@220
   648
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
kpeter@282
   649
  ///  cg.nodeRef(nr);
kpeter@282
   650
  ///  // Create cross references (inverse) for the arcs
deba@220
   651
  ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
kpeter@282
   652
  ///  cg.arcCrossRef(acr);
kpeter@282
   653
  ///  // Copy an arc map
deba@220
   654
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
deba@220
   655
  ///  NewGraph::ArcMap<double> namap(new_graph);
kpeter@282
   656
  ///  cg.arcMap(oamap, namap);
kpeter@282
   657
  ///  // Copy a node
deba@220
   658
  ///  OrigGraph::Node on;
deba@220
   659
  ///  NewGraph::Node nn;
kpeter@282
   660
  ///  cg.node(on, nn);
kpeter@282
   661
  ///  // Execute copying
kpeter@282
   662
  ///  cg.run();
deba@220
   663
  ///\endcode
kpeter@282
   664
  template <typename From, typename To>
deba@220
   665
  class DigraphCopy {
deba@220
   666
  private:
deba@220
   667
deba@220
   668
    typedef typename From::Node Node;
deba@220
   669
    typedef typename From::NodeIt NodeIt;
deba@220
   670
    typedef typename From::Arc Arc;
deba@220
   671
    typedef typename From::ArcIt ArcIt;
deba@220
   672
deba@220
   673
    typedef typename To::Node TNode;
deba@220
   674
    typedef typename To::Arc TArc;
deba@220
   675
deba@220
   676
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@220
   677
    typedef typename From::template ArcMap<TArc> ArcRefMap;
deba@220
   678
deba@220
   679
  public:
deba@220
   680
kpeter@282
   681
    /// \brief Constructor of DigraphCopy.
deba@220
   682
    ///
kpeter@282
   683
    /// Constructor of DigraphCopy for copying the content of the
kpeter@282
   684
    /// \c from digraph into the \c to digraph.
kpeter@282
   685
    DigraphCopy(const From& from, To& to)
deba@220
   686
      : _from(from), _to(to) {}
deba@220
   687
kpeter@282
   688
    /// \brief Destructor of DigraphCopy
deba@220
   689
    ///
kpeter@282
   690
    /// Destructor of DigraphCopy.
deba@220
   691
    ~DigraphCopy() {
deba@220
   692
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   693
        delete _node_maps[i];
deba@220
   694
      }
deba@220
   695
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   696
        delete _arc_maps[i];
deba@220
   697
      }
deba@220
   698
deba@220
   699
    }
deba@220
   700
kpeter@282
   701
    /// \brief Copy the node references into the given map.
deba@220
   702
    ///
kpeter@282
   703
    /// This function copies the node references into the given map.
kpeter@282
   704
    /// The parameter should be a map, whose key type is the Node type of
kpeter@282
   705
    /// the source digraph, while the value type is the Node type of the
kpeter@282
   706
    /// destination digraph.
deba@220
   707
    template <typename NodeRef>
deba@220
   708
    DigraphCopy& nodeRef(NodeRef& map) {
deba@220
   709
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
deba@220
   710
                           NodeRefMap, NodeRef>(map));
deba@220
   711
      return *this;
deba@220
   712
    }
deba@220
   713
kpeter@282
   714
    /// \brief Copy the node cross references into the given map.
deba@220
   715
    ///
kpeter@282
   716
    /// This function copies the node cross references (reverse references)
kpeter@282
   717
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   718
    /// is the Node type of the destination digraph, while the value type is
kpeter@282
   719
    /// the Node type of the source digraph.
deba@220
   720
    template <typename NodeCrossRef>
deba@220
   721
    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@220
   722
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
deba@220
   723
                           NodeRefMap, NodeCrossRef>(map));
deba@220
   724
      return *this;
deba@220
   725
    }
deba@220
   726
kpeter@282
   727
    /// \brief Make a copy of the given node map.
deba@220
   728
    ///
kpeter@282
   729
    /// This function makes a copy of the given node map for the newly
kpeter@282
   730
    /// created digraph.
kpeter@282
   731
    /// The key type of the new map \c tmap should be the Node type of the
kpeter@282
   732
    /// destination digraph, and the key type of the original map \c map
kpeter@282
   733
    /// should be the Node type of the source digraph.
kpeter@282
   734
    template <typename FromMap, typename ToMap>
kpeter@282
   735
    DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
deba@220
   736
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
kpeter@282
   737
                           NodeRefMap, FromMap, ToMap>(map, tmap));
deba@220
   738
      return *this;
deba@220
   739
    }
deba@220
   740
deba@220
   741
    /// \brief Make a copy of the given node.
deba@220
   742
    ///
kpeter@282
   743
    /// This function makes a copy of the given node.
kpeter@282
   744
    DigraphCopy& node(const Node& node, TNode& tnode) {
deba@220
   745
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
kpeter@282
   746
                           NodeRefMap, TNode>(node, tnode));
deba@220
   747
      return *this;
deba@220
   748
    }
deba@220
   749
kpeter@282
   750
    /// \brief Copy the arc references into the given map.
deba@220
   751
    ///
kpeter@282
   752
    /// This function copies the arc references into the given map.
kpeter@282
   753
    /// The parameter should be a map, whose key type is the Arc type of
kpeter@282
   754
    /// the source digraph, while the value type is the Arc type of the
kpeter@282
   755
    /// destination digraph.
deba@220
   756
    template <typename ArcRef>
deba@220
   757
    DigraphCopy& arcRef(ArcRef& map) {
deba@220
   758
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
deba@220
   759
                          ArcRefMap, ArcRef>(map));
deba@220
   760
      return *this;
deba@220
   761
    }
deba@220
   762
kpeter@282
   763
    /// \brief Copy the arc cross references into the given map.
deba@220
   764
    ///
kpeter@282
   765
    /// This function copies the arc cross references (reverse references)
kpeter@282
   766
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   767
    /// is the Arc type of the destination digraph, while the value type is
kpeter@282
   768
    /// the Arc type of the source digraph.
deba@220
   769
    template <typename ArcCrossRef>
deba@220
   770
    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
deba@220
   771
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
deba@220
   772
                          ArcRefMap, ArcCrossRef>(map));
deba@220
   773
      return *this;
deba@220
   774
    }
deba@220
   775
kpeter@282
   776
    /// \brief Make a copy of the given arc map.
deba@220
   777
    ///
kpeter@282
   778
    /// This function makes a copy of the given arc map for the newly
kpeter@282
   779
    /// created digraph.
kpeter@282
   780
    /// The key type of the new map \c tmap should be the Arc type of the
kpeter@282
   781
    /// destination digraph, and the key type of the original map \c map
kpeter@282
   782
    /// should be the Arc type of the source digraph.
kpeter@282
   783
    template <typename FromMap, typename ToMap>
kpeter@282
   784
    DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
deba@220
   785
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
kpeter@282
   786
                          ArcRefMap, FromMap, ToMap>(map, tmap));
deba@220
   787
      return *this;
deba@220
   788
    }
deba@220
   789
deba@220
   790
    /// \brief Make a copy of the given arc.
deba@220
   791
    ///
kpeter@282
   792
    /// This function makes a copy of the given arc.
kpeter@282
   793
    DigraphCopy& arc(const Arc& arc, TArc& tarc) {
deba@220
   794
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
kpeter@282
   795
                          ArcRefMap, TArc>(arc, tarc));
deba@220
   796
      return *this;
deba@220
   797
    }
deba@220
   798
kpeter@282
   799
    /// \brief Execute copying.
deba@220
   800
    ///
kpeter@282
   801
    /// This function executes the copying of the digraph along with the
kpeter@282
   802
    /// copying of the assigned data.
deba@220
   803
    void run() {
deba@220
   804
      NodeRefMap nodeRefMap(_from);
deba@220
   805
      ArcRefMap arcRefMap(_from);
deba@220
   806
      _core_bits::DigraphCopySelector<To>::
kpeter@282
   807
        copy(_from, _to, nodeRefMap, arcRefMap);
deba@220
   808
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   809
        _node_maps[i]->copy(_from, nodeRefMap);
deba@220
   810
      }
deba@220
   811
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   812
        _arc_maps[i]->copy(_from, arcRefMap);
deba@220
   813
      }
deba@220
   814
    }
deba@220
   815
deba@220
   816
  protected:
deba@220
   817
deba@220
   818
    const From& _from;
deba@220
   819
    To& _to;
deba@220
   820
deba@220
   821
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
kpeter@282
   822
      _node_maps;
deba@220
   823
deba@220
   824
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
kpeter@282
   825
      _arc_maps;
deba@220
   826
deba@220
   827
  };
deba@220
   828
deba@220
   829
  /// \brief Copy a digraph to another digraph.
deba@220
   830
  ///
kpeter@282
   831
  /// This function copies a digraph to another digraph.
kpeter@282
   832
  /// The complete usage of it is detailed in the DigraphCopy class, but
kpeter@282
   833
  /// a short example shows a basic work:
deba@220
   834
  ///\code
kpeter@282
   835
  /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
deba@220
   836
  ///\endcode
deba@220
   837
  ///
deba@220
   838
  /// After the copy the \c nr map will contain the mapping from the
deba@220
   839
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
kpeter@282
   840
  /// \c acr will contain the mapping from the arcs of the \c to digraph
deba@220
   841
  /// to the arcs of the \c from digraph.
deba@220
   842
  ///
deba@220
   843
  /// \see DigraphCopy
kpeter@282
   844
  template <typename From, typename To>
kpeter@282
   845
  DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
kpeter@282
   846
    return DigraphCopy<From, To>(from, to);
deba@220
   847
  }
deba@220
   848
deba@220
   849
  /// \brief Class to copy a graph.
deba@220
   850
  ///
deba@220
   851
  /// Class to copy a graph to another graph (duplicate a graph). The
kpeter@282
   852
  /// simplest way of using it is through the \c graphCopy() function.
deba@220
   853
  ///
kpeter@282
   854
  /// This class not only make a copy of a graph, but it can create
deba@220
   855
  /// references and cross references between the nodes, edges and arcs of
kpeter@282
   856
  /// the two graphs, and it can copy maps for using with the newly created
kpeter@282
   857
  /// graph.
deba@220
   858
  ///
deba@220
   859
  /// To make a copy from a graph, first an instance of GraphCopy
deba@220
   860
  /// should be created, then the data belongs to the graph should
deba@220
   861
  /// assigned to copy. In the end, the \c run() member should be
deba@220
   862
  /// called.
deba@220
   863
  ///
deba@220
   864
  /// The next code copies a graph with several data:
deba@220
   865
  ///\code
kpeter@282
   866
  ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
kpeter@282
   867
  ///  // Create references for the nodes
deba@220
   868
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
kpeter@282
   869
  ///  cg.nodeRef(nr);
kpeter@282
   870
  ///  // Create cross references (inverse) for the edges
kpeter@282
   871
  ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
kpeter@282
   872
  ///  cg.edgeCrossRef(ecr);
kpeter@282
   873
  ///  // Copy an edge map
kpeter@282
   874
  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
kpeter@282
   875
  ///  NewGraph::EdgeMap<double> nemap(new_graph);
kpeter@282
   876
  ///  cg.edgeMap(oemap, nemap);
kpeter@282
   877
  ///  // Copy a node
deba@220
   878
  ///  OrigGraph::Node on;
deba@220
   879
  ///  NewGraph::Node nn;
kpeter@282
   880
  ///  cg.node(on, nn);
kpeter@282
   881
  ///  // Execute copying
kpeter@282
   882
  ///  cg.run();
deba@220
   883
  ///\endcode
kpeter@282
   884
  template <typename From, typename To>
deba@220
   885
  class GraphCopy {
deba@220
   886
  private:
deba@220
   887
deba@220
   888
    typedef typename From::Node Node;
deba@220
   889
    typedef typename From::NodeIt NodeIt;
deba@220
   890
    typedef typename From::Arc Arc;
deba@220
   891
    typedef typename From::ArcIt ArcIt;
deba@220
   892
    typedef typename From::Edge Edge;
deba@220
   893
    typedef typename From::EdgeIt EdgeIt;
deba@220
   894
deba@220
   895
    typedef typename To::Node TNode;
deba@220
   896
    typedef typename To::Arc TArc;
deba@220
   897
    typedef typename To::Edge TEdge;
deba@220
   898
deba@220
   899
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@220
   900
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
deba@220
   901
deba@220
   902
    struct ArcRefMap {
kpeter@282
   903
      ArcRefMap(const From& from, const To& to,
deba@220
   904
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
kpeter@282
   905
        : _from(from), _to(to),
deba@220
   906
          _edge_ref(edge_ref), _node_ref(node_ref) {}
deba@220
   907
deba@220
   908
      typedef typename From::Arc Key;
deba@220
   909
      typedef typename To::Arc Value;
deba@220
   910
deba@220
   911
      Value operator[](const Key& key) const {
deba@220
   912
        bool forward = _from.u(key) != _from.v(key) ?
deba@220
   913
          _node_ref[_from.source(key)] ==
deba@220
   914
          _to.source(_to.direct(_edge_ref[key], true)) :
deba@220
   915
          _from.direction(key);
deba@220
   916
        return _to.direct(_edge_ref[key], forward);
deba@220
   917
      }
deba@220
   918
kpeter@282
   919
      const From& _from;
deba@220
   920
      const To& _to;
deba@220
   921
      const EdgeRefMap& _edge_ref;
deba@220
   922
      const NodeRefMap& _node_ref;
deba@220
   923
    };
deba@220
   924
deba@220
   925
  public:
deba@220
   926
kpeter@282
   927
    /// \brief Constructor of GraphCopy.
deba@220
   928
    ///
kpeter@282
   929
    /// Constructor of GraphCopy for copying the content of the
kpeter@282
   930
    /// \c from graph into the \c to graph.
kpeter@282
   931
    GraphCopy(const From& from, To& to)
deba@220
   932
      : _from(from), _to(to) {}
deba@220
   933
kpeter@282
   934
    /// \brief Destructor of GraphCopy
deba@220
   935
    ///
kpeter@282
   936
    /// Destructor of GraphCopy.
deba@220
   937
    ~GraphCopy() {
deba@220
   938
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
   939
        delete _node_maps[i];
deba@220
   940
      }
deba@220
   941
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
   942
        delete _arc_maps[i];
deba@220
   943
      }
deba@220
   944
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@220
   945
        delete _edge_maps[i];
deba@220
   946
      }
deba@220
   947
    }
deba@220
   948
kpeter@282
   949
    /// \brief Copy the node references into the given map.
deba@220
   950
    ///
kpeter@282
   951
    /// This function copies the node references into the given map.
kpeter@282
   952
    /// The parameter should be a map, whose key type is the Node type of
kpeter@282
   953
    /// the source graph, while the value type is the Node type of the
kpeter@282
   954
    /// destination graph.
deba@220
   955
    template <typename NodeRef>
deba@220
   956
    GraphCopy& nodeRef(NodeRef& map) {
deba@220
   957
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
deba@220
   958
                           NodeRefMap, NodeRef>(map));
deba@220
   959
      return *this;
deba@220
   960
    }
deba@220
   961
kpeter@282
   962
    /// \brief Copy the node cross references into the given map.
deba@220
   963
    ///
kpeter@282
   964
    /// This function copies the node cross references (reverse references)
kpeter@282
   965
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
   966
    /// is the Node type of the destination graph, while the value type is
kpeter@282
   967
    /// the Node type of the source graph.
deba@220
   968
    template <typename NodeCrossRef>
deba@220
   969
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@220
   970
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
deba@220
   971
                           NodeRefMap, NodeCrossRef>(map));
deba@220
   972
      return *this;
deba@220
   973
    }
deba@220
   974
kpeter@282
   975
    /// \brief Make a copy of the given node map.
deba@220
   976
    ///
kpeter@282
   977
    /// This function makes a copy of the given node map for the newly
kpeter@282
   978
    /// created graph.
kpeter@282
   979
    /// The key type of the new map \c tmap should be the Node type of the
kpeter@282
   980
    /// destination graph, and the key type of the original map \c map
kpeter@282
   981
    /// should be the Node type of the source graph.
kpeter@282
   982
    template <typename FromMap, typename ToMap>
kpeter@282
   983
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
deba@220
   984
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
kpeter@282
   985
                           NodeRefMap, FromMap, ToMap>(map, tmap));
deba@220
   986
      return *this;
deba@220
   987
    }
deba@220
   988
deba@220
   989
    /// \brief Make a copy of the given node.
deba@220
   990
    ///
kpeter@282
   991
    /// This function makes a copy of the given node.
kpeter@282
   992
    GraphCopy& node(const Node& node, TNode& tnode) {
deba@220
   993
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
kpeter@282
   994
                           NodeRefMap, TNode>(node, tnode));
deba@220
   995
      return *this;
deba@220
   996
    }
deba@220
   997
kpeter@282
   998
    /// \brief Copy the arc references into the given map.
deba@220
   999
    ///
kpeter@282
  1000
    /// This function copies the arc references into the given map.
kpeter@282
  1001
    /// The parameter should be a map, whose key type is the Arc type of
kpeter@282
  1002
    /// the source graph, while the value type is the Arc type of the
kpeter@282
  1003
    /// destination graph.
deba@220
  1004
    template <typename ArcRef>
deba@220
  1005
    GraphCopy& arcRef(ArcRef& map) {
deba@220
  1006
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
deba@220
  1007
                          ArcRefMap, ArcRef>(map));
deba@220
  1008
      return *this;
deba@220
  1009
    }
deba@220
  1010
kpeter@282
  1011
    /// \brief Copy the arc cross references into the given map.
deba@220
  1012
    ///
kpeter@282
  1013
    /// This function copies the arc cross references (reverse references)
kpeter@282
  1014
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
  1015
    /// is the Arc type of the destination graph, while the value type is
kpeter@282
  1016
    /// the Arc type of the source graph.
deba@220
  1017
    template <typename ArcCrossRef>
deba@220
  1018
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
deba@220
  1019
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
deba@220
  1020
                          ArcRefMap, ArcCrossRef>(map));
deba@220
  1021
      return *this;
deba@220
  1022
    }
deba@220
  1023
kpeter@282
  1024
    /// \brief Make a copy of the given arc map.
deba@220
  1025
    ///
kpeter@282
  1026
    /// This function makes a copy of the given arc map for the newly
kpeter@282
  1027
    /// created graph.
kpeter@282
  1028
    /// The key type of the new map \c tmap should be the Arc type of the
kpeter@282
  1029
    /// destination graph, and the key type of the original map \c map
kpeter@282
  1030
    /// should be the Arc type of the source graph.
kpeter@282
  1031
    template <typename FromMap, typename ToMap>
kpeter@282
  1032
    GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
deba@220
  1033
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
kpeter@282
  1034
                          ArcRefMap, FromMap, ToMap>(map, tmap));
deba@220
  1035
      return *this;
deba@220
  1036
    }
deba@220
  1037
deba@220
  1038
    /// \brief Make a copy of the given arc.
deba@220
  1039
    ///
kpeter@282
  1040
    /// This function makes a copy of the given arc.
kpeter@282
  1041
    GraphCopy& arc(const Arc& arc, TArc& tarc) {
deba@220
  1042
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
kpeter@282
  1043
                          ArcRefMap, TArc>(arc, tarc));
deba@220
  1044
      return *this;
deba@220
  1045
    }
deba@220
  1046
kpeter@282
  1047
    /// \brief Copy the edge references into the given map.
deba@220
  1048
    ///
kpeter@282
  1049
    /// This function copies the edge references into the given map.
kpeter@282
  1050
    /// The parameter should be a map, whose key type is the Edge type of
kpeter@282
  1051
    /// the source graph, while the value type is the Edge type of the
kpeter@282
  1052
    /// destination graph.
deba@220
  1053
    template <typename EdgeRef>
deba@220
  1054
    GraphCopy& edgeRef(EdgeRef& map) {
deba@220
  1055
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
deba@220
  1056
                           EdgeRefMap, EdgeRef>(map));
deba@220
  1057
      return *this;
deba@220
  1058
    }
deba@220
  1059
kpeter@282
  1060
    /// \brief Copy the edge cross references into the given map.
deba@220
  1061
    ///
kpeter@282
  1062
    /// This function copies the edge cross references (reverse references)
kpeter@282
  1063
    /// into the given map. The parameter should be a map, whose key type
kpeter@282
  1064
    /// is the Edge type of the destination graph, while the value type is
kpeter@282
  1065
    /// the Edge type of the source graph.
deba@220
  1066
    template <typename EdgeCrossRef>
deba@220
  1067
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@220
  1068
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
deba@220
  1069
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
deba@220
  1070
      return *this;
deba@220
  1071
    }
deba@220
  1072
kpeter@282
  1073
    /// \brief Make a copy of the given edge map.
deba@220
  1074
    ///
kpeter@282
  1075
    /// This function makes a copy of the given edge map for the newly
kpeter@282
  1076
    /// created graph.
kpeter@282
  1077
    /// The key type of the new map \c tmap should be the Edge type of the
kpeter@282
  1078
    /// destination graph, and the key type of the original map \c map
kpeter@282
  1079
    /// should be the Edge type of the source graph.
kpeter@282
  1080
    template <typename FromMap, typename ToMap>
kpeter@282
  1081
    GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
deba@220
  1082
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
kpeter@282
  1083
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
deba@220
  1084
      return *this;
deba@220
  1085
    }
deba@220
  1086
deba@220
  1087
    /// \brief Make a copy of the given edge.
deba@220
  1088
    ///
kpeter@282
  1089
    /// This function makes a copy of the given edge.
kpeter@282
  1090
    GraphCopy& edge(const Edge& edge, TEdge& tedge) {
deba@220
  1091
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
kpeter@282
  1092
                           EdgeRefMap, TEdge>(edge, tedge));
deba@220
  1093
      return *this;
deba@220
  1094
    }
deba@220
  1095
kpeter@282
  1096
    /// \brief Execute copying.
deba@220
  1097
    ///
kpeter@282
  1098
    /// This function executes the copying of the graph along with the
kpeter@282
  1099
    /// copying of the assigned data.
deba@220
  1100
    void run() {
deba@220
  1101
      NodeRefMap nodeRefMap(_from);
deba@220
  1102
      EdgeRefMap edgeRefMap(_from);
kpeter@282
  1103
      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
deba@220
  1104
      _core_bits::GraphCopySelector<To>::
kpeter@282
  1105
        copy(_from, _to, nodeRefMap, edgeRefMap);
deba@220
  1106
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@220
  1107
        _node_maps[i]->copy(_from, nodeRefMap);
deba@220
  1108
      }
deba@220
  1109
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@220
  1110
        _edge_maps[i]->copy(_from, edgeRefMap);
deba@220
  1111
      }
deba@220
  1112
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@220
  1113
        _arc_maps[i]->copy(_from, arcRefMap);
deba@220
  1114
      }
deba@220
  1115
    }
deba@220
  1116
deba@220
  1117
  private:
deba@220
  1118
deba@220
  1119
    const From& _from;
deba@220
  1120
    To& _to;
deba@220
  1121
deba@220
  1122
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
kpeter@282
  1123
      _node_maps;
deba@220
  1124
deba@220
  1125
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
kpeter@282
  1126
      _arc_maps;
deba@220
  1127
deba@220
  1128
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
kpeter@282
  1129
      _edge_maps;
deba@220
  1130
deba@220
  1131
  };
deba@220
  1132
deba@220
  1133
  /// \brief Copy a graph to another graph.
deba@220
  1134
  ///
kpeter@282
  1135
  /// This function copies a graph to another graph.
kpeter@282
  1136
  /// The complete usage of it is detailed in the GraphCopy class,
kpeter@282
  1137
  /// but a short example shows a basic work:
deba@220
  1138
  ///\code
kpeter@282
  1139
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
deba@220
  1140
  ///\endcode
deba@220
  1141
  ///
deba@220
  1142
  /// After the copy the \c nr map will contain the mapping from the
deba@220
  1143
  /// nodes of the \c from graph to the nodes of the \c to graph and
kpeter@282
  1144
  /// \c ecr will contain the mapping from the edges of the \c to graph
kpeter@282
  1145
  /// to the edges of the \c from graph.
deba@220
  1146
  ///
deba@220
  1147
  /// \see GraphCopy
kpeter@282
  1148
  template <typename From, typename To>
kpeter@282
  1149
  GraphCopy<From, To>
kpeter@282
  1150
  graphCopy(const From& from, To& to) {
kpeter@282
  1151
    return GraphCopy<From, To>(from, to);
deba@220
  1152
  }
deba@220
  1153
deba@1022
  1154
  /// \brief Class to copy a bipartite graph.
deba@1022
  1155
  ///
deba@1022
  1156
  /// Class to copy a bipartite graph to another graph (duplicate a
deba@1022
  1157
  /// graph). The simplest way of using it is through the
deba@1022
  1158
  /// \c bpGraphCopy() function.
deba@1022
  1159
  ///
deba@1022
  1160
  /// This class not only make a copy of a bipartite graph, but it can
deba@1022
  1161
  /// create references and cross references between the nodes, edges
deba@1022
  1162
  /// and arcs of the two graphs, and it can copy maps for using with
deba@1022
  1163
  /// the newly created graph.
deba@1022
  1164
  ///
deba@1022
  1165
  /// To make a copy from a graph, first an instance of BpGraphCopy
deba@1022
  1166
  /// should be created, then the data belongs to the graph should
deba@1022
  1167
  /// assigned to copy. In the end, the \c run() member should be
deba@1022
  1168
  /// called.
deba@1022
  1169
  ///
deba@1022
  1170
  /// The next code copies a graph with several data:
deba@1022
  1171
  ///\code
deba@1022
  1172
  ///  BpGraphCopy<OrigBpGraph, NewBpGraph> cg(orig_graph, new_graph);
deba@1022
  1173
  ///  // Create references for the nodes
deba@1022
  1174
  ///  OrigBpGraph::NodeMap<NewBpGraph::Node> nr(orig_graph);
deba@1022
  1175
  ///  cg.nodeRef(nr);
deba@1022
  1176
  ///  // Create cross references (inverse) for the edges
deba@1022
  1177
  ///  NewBpGraph::EdgeMap<OrigBpGraph::Edge> ecr(new_graph);
deba@1022
  1178
  ///  cg.edgeCrossRef(ecr);
deba@1026
  1179
  ///  // Copy a red node map
deba@1026
  1180
  ///  OrigBpGraph::RedNodeMap<double> ormap(orig_graph);
deba@1026
  1181
  ///  NewBpGraph::RedNodeMap<double> nrmap(new_graph);
deba@1026
  1182
  ///  cg.redNodeMap(ormap, nrmap);
deba@1022
  1183
  ///  // Copy a node
deba@1022
  1184
  ///  OrigBpGraph::Node on;
deba@1022
  1185
  ///  NewBpGraph::Node nn;
deba@1022
  1186
  ///  cg.node(on, nn);
deba@1022
  1187
  ///  // Execute copying
deba@1022
  1188
  ///  cg.run();
deba@1022
  1189
  ///\endcode
deba@1022
  1190
  template <typename From, typename To>
deba@1022
  1191
  class BpGraphCopy {
deba@1022
  1192
  private:
deba@1022
  1193
deba@1022
  1194
    typedef typename From::Node Node;
deba@1022
  1195
    typedef typename From::RedNode RedNode;
deba@1022
  1196
    typedef typename From::BlueNode BlueNode;
deba@1022
  1197
    typedef typename From::NodeIt NodeIt;
deba@1022
  1198
    typedef typename From::Arc Arc;
deba@1022
  1199
    typedef typename From::ArcIt ArcIt;
deba@1022
  1200
    typedef typename From::Edge Edge;
deba@1022
  1201
    typedef typename From::EdgeIt EdgeIt;
deba@1022
  1202
deba@1022
  1203
    typedef typename To::Node TNode;
deba@1025
  1204
    typedef typename To::RedNode TRedNode;
deba@1025
  1205
    typedef typename To::BlueNode TBlueNode;
deba@1022
  1206
    typedef typename To::Arc TArc;
deba@1022
  1207
    typedef typename To::Edge TEdge;
deba@1022
  1208
deba@1026
  1209
    typedef typename From::template RedNodeMap<TRedNode> RedNodeRefMap;
deba@1026
  1210
    typedef typename From::template BlueNodeMap<TBlueNode> BlueNodeRefMap;
deba@1022
  1211
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
deba@1022
  1212
deba@1025
  1213
    struct NodeRefMap {
deba@1025
  1214
      NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref,
deba@1025
  1215
                 const BlueNodeRefMap& blue_node_ref)
deba@1025
  1216
        : _from(from), _red_node_ref(red_node_ref),
deba@1025
  1217
          _blue_node_ref(blue_node_ref) {}
deba@1025
  1218
deba@1025
  1219
      typedef typename From::Node Key;
deba@1025
  1220
      typedef typename To::Node Value;
deba@1025
  1221
deba@1025
  1222
      Value operator[](const Key& key) const {
deba@1027
  1223
        if (_from.red(key)) {
deba@1027
  1224
          return _red_node_ref[_from.asRedNodeUnsafe(key)];
deba@1025
  1225
        } else {
deba@1027
  1226
          return _blue_node_ref[_from.asBlueNodeUnsafe(key)];
deba@1025
  1227
        }
deba@1025
  1228
      }
deba@1025
  1229
deba@1025
  1230
      const From& _from;
deba@1025
  1231
      const RedNodeRefMap& _red_node_ref;
deba@1025
  1232
      const BlueNodeRefMap& _blue_node_ref;
deba@1025
  1233
    };
deba@1025
  1234
deba@1022
  1235
    struct ArcRefMap {
deba@1022
  1236
      ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
deba@1022
  1237
        : _from(from), _to(to), _edge_ref(edge_ref) {}
deba@1022
  1238
deba@1022
  1239
      typedef typename From::Arc Key;
deba@1022
  1240
      typedef typename To::Arc Value;
deba@1022
  1241
deba@1022
  1242
      Value operator[](const Key& key) const {
deba@1022
  1243
        return _to.direct(_edge_ref[key], _from.direction(key));
deba@1022
  1244
      }
deba@1022
  1245
deba@1022
  1246
      const From& _from;
deba@1022
  1247
      const To& _to;
deba@1022
  1248
      const EdgeRefMap& _edge_ref;
deba@1022
  1249
    };
deba@1022
  1250
deba@1022
  1251
  public:
deba@1022
  1252
deba@1022
  1253
    /// \brief Constructor of BpGraphCopy.
deba@1022
  1254
    ///
deba@1022
  1255
    /// Constructor of BpGraphCopy for copying the content of the
deba@1022
  1256
    /// \c from graph into the \c to graph.
deba@1022
  1257
    BpGraphCopy(const From& from, To& to)
deba@1022
  1258
      : _from(from), _to(to) {}
deba@1022
  1259
deba@1022
  1260
    /// \brief Destructor of BpGraphCopy
deba@1022
  1261
    ///
deba@1022
  1262
    /// Destructor of BpGraphCopy.
deba@1022
  1263
    ~BpGraphCopy() {
deba@1022
  1264
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@1022
  1265
        delete _node_maps[i];
deba@1022
  1266
      }
deba@1022
  1267
      for (int i = 0; i < int(_red_maps.size()); ++i) {
deba@1022
  1268
        delete _red_maps[i];
deba@1022
  1269
      }
deba@1022
  1270
      for (int i = 0; i < int(_blue_maps.size()); ++i) {
deba@1022
  1271
        delete _blue_maps[i];
deba@1022
  1272
      }
deba@1022
  1273
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@1022
  1274
        delete _arc_maps[i];
deba@1022
  1275
      }
deba@1022
  1276
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@1022
  1277
        delete _edge_maps[i];
deba@1022
  1278
      }
deba@1022
  1279
    }
deba@1022
  1280
deba@1022
  1281
    /// \brief Copy the node references into the given map.
deba@1022
  1282
    ///
deba@1022
  1283
    /// This function copies the node references into the given map.
deba@1022
  1284
    /// The parameter should be a map, whose key type is the Node type of
deba@1022
  1285
    /// the source graph, while the value type is the Node type of the
deba@1022
  1286
    /// destination graph.
deba@1022
  1287
    template <typename NodeRef>
deba@1022
  1288
    BpGraphCopy& nodeRef(NodeRef& map) {
deba@1022
  1289
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
deba@1022
  1290
                           NodeRefMap, NodeRef>(map));
deba@1022
  1291
      return *this;
deba@1022
  1292
    }
deba@1022
  1293
deba@1022
  1294
    /// \brief Copy the node cross references into the given map.
deba@1022
  1295
    ///
deba@1022
  1296
    /// This function copies the node cross references (reverse references)
deba@1022
  1297
    /// into the given map. The parameter should be a map, whose key type
deba@1022
  1298
    /// is the Node type of the destination graph, while the value type is
deba@1022
  1299
    /// the Node type of the source graph.
deba@1022
  1300
    template <typename NodeCrossRef>
deba@1022
  1301
    BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@1022
  1302
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
deba@1022
  1303
                           NodeRefMap, NodeCrossRef>(map));
deba@1022
  1304
      return *this;
deba@1022
  1305
    }
deba@1022
  1306
deba@1022
  1307
    /// \brief Make a copy of the given node map.
deba@1022
  1308
    ///
deba@1022
  1309
    /// This function makes a copy of the given node map for the newly
deba@1022
  1310
    /// created graph.
deba@1022
  1311
    /// The key type of the new map \c tmap should be the Node type of the
deba@1022
  1312
    /// destination graph, and the key type of the original map \c map
deba@1022
  1313
    /// should be the Node type of the source graph.
deba@1022
  1314
    template <typename FromMap, typename ToMap>
deba@1022
  1315
    BpGraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
deba@1022
  1316
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
deba@1022
  1317
                           NodeRefMap, FromMap, ToMap>(map, tmap));
deba@1022
  1318
      return *this;
deba@1022
  1319
    }
deba@1022
  1320
deba@1022
  1321
    /// \brief Make a copy of the given node.
deba@1022
  1322
    ///
deba@1022
  1323
    /// This function makes a copy of the given node.
deba@1022
  1324
    BpGraphCopy& node(const Node& node, TNode& tnode) {
deba@1022
  1325
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
deba@1022
  1326
                           NodeRefMap, TNode>(node, tnode));
deba@1022
  1327
      return *this;
deba@1022
  1328
    }
deba@1022
  1329
deba@1022
  1330
    /// \brief Copy the red node references into the given map.
deba@1022
  1331
    ///
deba@1022
  1332
    /// This function copies the red node references into the given
deba@1022
  1333
    /// map.  The parameter should be a map, whose key type is the
deba@1022
  1334
    /// Node type of the source graph with the red item set, while the
deba@1022
  1335
    /// value type is the Node type of the destination graph.
deba@1022
  1336
    template <typename RedRef>
deba@1022
  1337
    BpGraphCopy& redRef(RedRef& map) {
deba@1022
  1338
      _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
deba@1025
  1339
                          RedNodeRefMap, RedRef>(map));
deba@1022
  1340
      return *this;
deba@1022
  1341
    }
deba@1022
  1342
deba@1022
  1343
    /// \brief Copy the red node cross references into the given map.
deba@1022
  1344
    ///
deba@1022
  1345
    /// This function copies the red node cross references (reverse
deba@1022
  1346
    /// references) into the given map. The parameter should be a map,
deba@1022
  1347
    /// whose key type is the Node type of the destination graph with
deba@1022
  1348
    /// the red item set, while the value type is the Node type of the
deba@1022
  1349
    /// source graph.
deba@1022
  1350
    template <typename RedCrossRef>
deba@1022
  1351
    BpGraphCopy& redCrossRef(RedCrossRef& map) {
deba@1022
  1352
      _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
deba@1025
  1353
                          RedNodeRefMap, RedCrossRef>(map));
deba@1022
  1354
      return *this;
deba@1022
  1355
    }
deba@1022
  1356
deba@1022
  1357
    /// \brief Make a copy of the given red node map.
deba@1022
  1358
    ///
deba@1022
  1359
    /// This function makes a copy of the given red node map for the newly
deba@1022
  1360
    /// created graph.
deba@1022
  1361
    /// The key type of the new map \c tmap should be the Node type of
deba@1022
  1362
    /// the destination graph with the red items, and the key type of
deba@1022
  1363
    /// the original map \c map should be the Node type of the source
deba@1022
  1364
    /// graph.
deba@1022
  1365
    template <typename FromMap, typename ToMap>
deba@1026
  1366
    BpGraphCopy& redNodeMap(const FromMap& map, ToMap& tmap) {
deba@1022
  1367
      _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
deba@1025
  1368
                          RedNodeRefMap, FromMap, ToMap>(map, tmap));
deba@1025
  1369
      return *this;
deba@1025
  1370
    }
deba@1025
  1371
deba@1025
  1372
    /// \brief Make a copy of the given red node.
deba@1025
  1373
    ///
deba@1025
  1374
    /// This function makes a copy of the given red node.
deba@1025
  1375
    BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) {
deba@1025
  1376
      _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode,
deba@1025
  1377
                          RedNodeRefMap, TRedNode>(node, tnode));
deba@1022
  1378
      return *this;
deba@1022
  1379
    }
deba@1022
  1380
deba@1022
  1381
    /// \brief Copy the blue node references into the given map.
deba@1022
  1382
    ///
deba@1022
  1383
    /// This function copies the blue node references into the given
deba@1022
  1384
    /// map.  The parameter should be a map, whose key type is the
deba@1022
  1385
    /// Node type of the source graph with the blue item set, while the
deba@1022
  1386
    /// value type is the Node type of the destination graph.
deba@1022
  1387
    template <typename BlueRef>
deba@1022
  1388
    BpGraphCopy& blueRef(BlueRef& map) {
deba@1022
  1389
      _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
deba@1025
  1390
                           BlueNodeRefMap, BlueRef>(map));
deba@1022
  1391
      return *this;
deba@1022
  1392
    }
deba@1022
  1393
deba@1022
  1394
    /// \brief Copy the blue node cross references into the given map.
deba@1022
  1395
    ///
deba@1022
  1396
    /// This function copies the blue node cross references (reverse
deba@1022
  1397
    /// references) into the given map. The parameter should be a map,
deba@1022
  1398
    /// whose key type is the Node type of the destination graph with
deba@1022
  1399
    /// the blue item set, while the value type is the Node type of the
deba@1022
  1400
    /// source graph.
deba@1022
  1401
    template <typename BlueCrossRef>
deba@1022
  1402
    BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
deba@1022
  1403
      _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
deba@1025
  1404
                           BlueNodeRefMap, BlueCrossRef>(map));
deba@1022
  1405
      return *this;
deba@1022
  1406
    }
deba@1022
  1407
deba@1022
  1408
    /// \brief Make a copy of the given blue node map.
deba@1022
  1409
    ///
deba@1022
  1410
    /// This function makes a copy of the given blue node map for the newly
deba@1022
  1411
    /// created graph.
deba@1022
  1412
    /// The key type of the new map \c tmap should be the Node type of
deba@1022
  1413
    /// the destination graph with the blue items, and the key type of
deba@1022
  1414
    /// the original map \c map should be the Node type of the source
deba@1022
  1415
    /// graph.
deba@1022
  1416
    template <typename FromMap, typename ToMap>
deba@1026
  1417
    BpGraphCopy& blueNodeMap(const FromMap& map, ToMap& tmap) {
deba@1022
  1418
      _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
deba@1025
  1419
                           BlueNodeRefMap, FromMap, ToMap>(map, tmap));
deba@1025
  1420
      return *this;
deba@1025
  1421
    }
deba@1025
  1422
deba@1025
  1423
    /// \brief Make a copy of the given blue node.
deba@1025
  1424
    ///
deba@1025
  1425
    /// This function makes a copy of the given blue node.
deba@1025
  1426
    BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) {
deba@1025
  1427
      _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode,
deba@1025
  1428
                           BlueNodeRefMap, TBlueNode>(node, tnode));
deba@1022
  1429
      return *this;
deba@1022
  1430
    }
deba@1022
  1431
deba@1022
  1432
    /// \brief Copy the arc references into the given map.
deba@1022
  1433
    ///
deba@1022
  1434
    /// This function copies the arc references into the given map.
deba@1022
  1435
    /// The parameter should be a map, whose key type is the Arc type of
deba@1022
  1436
    /// the source graph, while the value type is the Arc type of the
deba@1022
  1437
    /// destination graph.
deba@1022
  1438
    template <typename ArcRef>
deba@1022
  1439
    BpGraphCopy& arcRef(ArcRef& map) {
deba@1022
  1440
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
deba@1022
  1441
                          ArcRefMap, ArcRef>(map));
deba@1022
  1442
      return *this;
deba@1022
  1443
    }
deba@1022
  1444
deba@1022
  1445
    /// \brief Copy the arc cross references into the given map.
deba@1022
  1446
    ///
deba@1022
  1447
    /// This function copies the arc cross references (reverse references)
deba@1022
  1448
    /// into the given map. The parameter should be a map, whose key type
deba@1022
  1449
    /// is the Arc type of the destination graph, while the value type is
deba@1022
  1450
    /// the Arc type of the source graph.
deba@1022
  1451
    template <typename ArcCrossRef>
deba@1022
  1452
    BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
deba@1022
  1453
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
deba@1022
  1454
                          ArcRefMap, ArcCrossRef>(map));
deba@1022
  1455
      return *this;
deba@1022
  1456
    }
deba@1022
  1457
deba@1022
  1458
    /// \brief Make a copy of the given arc map.
deba@1022
  1459
    ///
deba@1022
  1460
    /// This function makes a copy of the given arc map for the newly
deba@1022
  1461
    /// created graph.
deba@1022
  1462
    /// The key type of the new map \c tmap should be the Arc type of the
deba@1022
  1463
    /// destination graph, and the key type of the original map \c map
deba@1022
  1464
    /// should be the Arc type of the source graph.
deba@1022
  1465
    template <typename FromMap, typename ToMap>
deba@1022
  1466
    BpGraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
deba@1022
  1467
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
deba@1022
  1468
                          ArcRefMap, FromMap, ToMap>(map, tmap));
deba@1022
  1469
      return *this;
deba@1022
  1470
    }
deba@1022
  1471
deba@1022
  1472
    /// \brief Make a copy of the given arc.
deba@1022
  1473
    ///
deba@1022
  1474
    /// This function makes a copy of the given arc.
deba@1022
  1475
    BpGraphCopy& arc(const Arc& arc, TArc& tarc) {
deba@1022
  1476
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
deba@1022
  1477
                          ArcRefMap, TArc>(arc, tarc));
deba@1022
  1478
      return *this;
deba@1022
  1479
    }
deba@1022
  1480
deba@1022
  1481
    /// \brief Copy the edge references into the given map.
deba@1022
  1482
    ///
deba@1022
  1483
    /// This function copies the edge references into the given map.
deba@1022
  1484
    /// The parameter should be a map, whose key type is the Edge type of
deba@1022
  1485
    /// the source graph, while the value type is the Edge type of the
deba@1022
  1486
    /// destination graph.
deba@1022
  1487
    template <typename EdgeRef>
deba@1022
  1488
    BpGraphCopy& edgeRef(EdgeRef& map) {
deba@1022
  1489
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
deba@1022
  1490
                           EdgeRefMap, EdgeRef>(map));
deba@1022
  1491
      return *this;
deba@1022
  1492
    }
deba@1022
  1493
deba@1022
  1494
    /// \brief Copy the edge cross references into the given map.
deba@1022
  1495
    ///
deba@1022
  1496
    /// This function copies the edge cross references (reverse references)
deba@1022
  1497
    /// into the given map. The parameter should be a map, whose key type
deba@1022
  1498
    /// is the Edge type of the destination graph, while the value type is
deba@1022
  1499
    /// the Edge type of the source graph.
deba@1022
  1500
    template <typename EdgeCrossRef>
deba@1022
  1501
    BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@1022
  1502
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
deba@1022
  1503
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
deba@1022
  1504
      return *this;
deba@1022
  1505
    }
deba@1022
  1506
deba@1022
  1507
    /// \brief Make a copy of the given edge map.
deba@1022
  1508
    ///
deba@1022
  1509
    /// This function makes a copy of the given edge map for the newly
deba@1022
  1510
    /// created graph.
deba@1022
  1511
    /// The key type of the new map \c tmap should be the Edge type of the
deba@1022
  1512
    /// destination graph, and the key type of the original map \c map
deba@1022
  1513
    /// should be the Edge type of the source graph.
deba@1022
  1514
    template <typename FromMap, typename ToMap>
deba@1022
  1515
    BpGraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
deba@1022
  1516
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
deba@1022
  1517
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
deba@1022
  1518
      return *this;
deba@1022
  1519
    }
deba@1022
  1520
deba@1022
  1521
    /// \brief Make a copy of the given edge.
deba@1022
  1522
    ///
deba@1022
  1523
    /// This function makes a copy of the given edge.
deba@1022
  1524
    BpGraphCopy& edge(const Edge& edge, TEdge& tedge) {
deba@1022
  1525
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
deba@1022
  1526
                           EdgeRefMap, TEdge>(edge, tedge));
deba@1022
  1527
      return *this;
deba@1022
  1528
    }
deba@1022
  1529
deba@1022
  1530
    /// \brief Execute copying.
deba@1022
  1531
    ///
deba@1022
  1532
    /// This function executes the copying of the graph along with the
deba@1022
  1533
    /// copying of the assigned data.
deba@1022
  1534
    void run() {
deba@1025
  1535
      RedNodeRefMap redNodeRefMap(_from);
deba@1025
  1536
      BlueNodeRefMap blueNodeRefMap(_from);
deba@1025
  1537
      NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap);
deba@1022
  1538
      EdgeRefMap edgeRefMap(_from);
deba@1022
  1539
      ArcRefMap arcRefMap(_from, _to, edgeRefMap);
deba@1022
  1540
      _core_bits::BpGraphCopySelector<To>::
deba@1025
  1541
        copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap);
deba@1022
  1542
      for (int i = 0; i < int(_node_maps.size()); ++i) {
deba@1022
  1543
        _node_maps[i]->copy(_from, nodeRefMap);
deba@1022
  1544
      }
deba@1022
  1545
      for (int i = 0; i < int(_red_maps.size()); ++i) {
deba@1025
  1546
        _red_maps[i]->copy(_from, redNodeRefMap);
deba@1022
  1547
      }
deba@1022
  1548
      for (int i = 0; i < int(_blue_maps.size()); ++i) {
deba@1025
  1549
        _blue_maps[i]->copy(_from, blueNodeRefMap);
deba@1022
  1550
      }
deba@1022
  1551
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
deba@1022
  1552
        _edge_maps[i]->copy(_from, edgeRefMap);
deba@1022
  1553
      }
deba@1022
  1554
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
deba@1022
  1555
        _arc_maps[i]->copy(_from, arcRefMap);
deba@1022
  1556
      }
deba@1022
  1557
    }
deba@1022
  1558
deba@1022
  1559
  private:
deba@1022
  1560
deba@1022
  1561
    const From& _from;
deba@1022
  1562
    To& _to;
deba@1022
  1563
deba@1022
  1564
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
deba@1022
  1565
      _node_maps;
deba@1022
  1566
deba@1025
  1567
    std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* >
deba@1022
  1568
      _red_maps;
deba@1022
  1569
deba@1025
  1570
    std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* >
deba@1022
  1571
      _blue_maps;
deba@1022
  1572
deba@1022
  1573
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
deba@1022
  1574
      _arc_maps;
deba@1022
  1575
deba@1022
  1576
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
deba@1022
  1577
      _edge_maps;
deba@1022
  1578
deba@1022
  1579
  };
deba@1022
  1580
deba@1022
  1581
  /// \brief Copy a graph to another graph.
deba@1022
  1582
  ///
deba@1022
  1583
  /// This function copies a graph to another graph.
deba@1022
  1584
  /// The complete usage of it is detailed in the BpGraphCopy class,
deba@1022
  1585
  /// but a short example shows a basic work:
deba@1022
  1586
  ///\code
deba@1022
  1587
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
deba@1022
  1588
  ///\endcode
deba@1022
  1589
  ///
deba@1022
  1590
  /// After the copy the \c nr map will contain the mapping from the
deba@1022
  1591
  /// nodes of the \c from graph to the nodes of the \c to graph and
deba@1022
  1592
  /// \c ecr will contain the mapping from the edges of the \c to graph
deba@1022
  1593
  /// to the edges of the \c from graph.
deba@1022
  1594
  ///
deba@1022
  1595
  /// \see BpGraphCopy
deba@1022
  1596
  template <typename From, typename To>
deba@1022
  1597
  BpGraphCopy<From, To>
deba@1022
  1598
  bpGraphCopy(const From& from, To& to) {
deba@1022
  1599
    return BpGraphCopy<From, To>(from, to);
deba@1022
  1600
  }
deba@1022
  1601
deba@220
  1602
  namespace _core_bits {
deba@220
  1603
deba@220
  1604
    template <typename Graph, typename Enable = void>
deba@220
  1605
    struct FindArcSelector {
deba@220
  1606
      typedef typename Graph::Node Node;
deba@220
  1607
      typedef typename Graph::Arc Arc;
deba@220
  1608
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
deba@220
  1609
        if (e == INVALID) {
deba@220
  1610
          g.firstOut(e, u);
deba@220
  1611
        } else {
deba@220
  1612
          g.nextOut(e);
deba@220
  1613
        }
deba@220
  1614
        while (e != INVALID && g.target(e) != v) {
deba@220
  1615
          g.nextOut(e);
deba@220
  1616
        }
deba@220
  1617
        return e;
deba@220
  1618
      }
deba@220
  1619
    };
deba@220
  1620
deba@220
  1621
    template <typename Graph>
deba@220
  1622
    struct FindArcSelector<
deba@220
  1623
      Graph,
kpeter@282
  1624
      typename enable_if<typename Graph::FindArcTag, void>::type>
deba@220
  1625
    {
deba@220
  1626
      typedef typename Graph::Node Node;
deba@220
  1627
      typedef typename Graph::Arc Arc;
deba@220
  1628
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
deba@220
  1629
        return g.findArc(u, v, prev);
deba@220
  1630
      }
deba@220
  1631
    };
deba@220
  1632
  }
deba@220
  1633
kpeter@282
  1634
  /// \brief Find an arc between two nodes of a digraph.
deba@220
  1635
  ///
kpeter@282
  1636
  /// This function finds an arc from node \c u to node \c v in the
kpeter@282
  1637
  /// digraph \c g.
deba@220
  1638
  ///
deba@220
  1639
  /// If \c prev is \ref INVALID (this is the default value), then
deba@220
  1640
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
deba@220
  1641
  /// the next arc from \c u to \c v after \c prev.
deba@220
  1642
  /// \return The found arc or \ref INVALID if there is no such an arc.
deba@220
  1643
  ///
deba@220
  1644
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
deba@220
  1645
  ///\code
kpeter@282
  1646
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
deba@220
  1647
  ///   ...
deba@220
  1648
  /// }
deba@220
  1649
  ///\endcode
deba@220
  1650
  ///
kpeter@282
  1651
  /// \note \ref ConArcIt provides iterator interface for the same
kpeter@282
  1652
  /// functionality.
kpeter@282
  1653
  ///
deba@220
  1654
  ///\sa ConArcIt
kpeter@282
  1655
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
deba@220
  1656
  template <typename Graph>
deba@220
  1657
  inline typename Graph::Arc
deba@220
  1658
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@220
  1659
          typename Graph::Arc prev = INVALID) {
deba@220
  1660
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
deba@220
  1661
  }
deba@220
  1662
kpeter@282
  1663
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
deba@220
  1664
  ///
kpeter@282
  1665
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
kpeter@282
  1666
  /// a higher level interface for the \ref findArc() function. You can
deba@220
  1667
  /// use it the following way:
deba@220
  1668
  ///\code
deba@220
  1669
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@220
  1670
  ///   ...
deba@220
  1671
  /// }
deba@220
  1672
  ///\endcode
deba@220
  1673
  ///
deba@220
  1674
  ///\sa findArc()
kpeter@282
  1675
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
kpeter@559
  1676
  template <typename GR>
kpeter@559
  1677
  class ConArcIt : public GR::Arc {
kpeter@617
  1678
    typedef typename GR::Arc Parent;
kpeter@617
  1679
deba@220
  1680
  public:
deba@220
  1681
kpeter@617
  1682
    typedef typename GR::Arc Arc;
kpeter@617
  1683
    typedef typename GR::Node Node;
deba@220
  1684
deba@220
  1685
    /// \brief Constructor.
deba@220
  1686
    ///
kpeter@282
  1687
    /// Construct a new ConArcIt iterating on the arcs that
kpeter@282
  1688
    /// connects nodes \c u and \c v.
kpeter@617
  1689
    ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
deba@220
  1690
      Parent::operator=(findArc(_graph, u, v));
deba@220
  1691
    }
deba@220
  1692
deba@220
  1693
    /// \brief Constructor.
deba@220
  1694
    ///
kpeter@282
  1695
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
kpeter@617
  1696
    ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
deba@220
  1697
deba@220
  1698
    /// \brief Increment operator.
deba@220
  1699
    ///
deba@220
  1700
    /// It increments the iterator and gives back the next arc.
deba@220
  1701
    ConArcIt& operator++() {
deba@220
  1702
      Parent::operator=(findArc(_graph, _graph.source(*this),
deba@220
  1703
                                _graph.target(*this), *this));
deba@220
  1704
      return *this;
deba@220
  1705
    }
deba@220
  1706
  private:
kpeter@617
  1707
    const GR& _graph;
deba@220
  1708
  };
deba@220
  1709
deba@220
  1710
  namespace _core_bits {
deba@220
  1711
deba@220
  1712
    template <typename Graph, typename Enable = void>
deba@220
  1713
    struct FindEdgeSelector {
deba@220
  1714
      typedef typename Graph::Node Node;
deba@220
  1715
      typedef typename Graph::Edge Edge;
deba@220
  1716
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
deba@220
  1717
        bool b;
deba@220
  1718
        if (u != v) {
deba@220
  1719
          if (e == INVALID) {
deba@220
  1720
            g.firstInc(e, b, u);
deba@220
  1721
          } else {
deba@220
  1722
            b = g.u(e) == u;
deba@220
  1723
            g.nextInc(e, b);
deba@220
  1724
          }
deba@220
  1725
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
deba@220
  1726
            g.nextInc(e, b);
deba@220
  1727
          }
deba@220
  1728
        } else {
deba@220
  1729
          if (e == INVALID) {
deba@220
  1730
            g.firstInc(e, b, u);
deba@220
  1731
          } else {
deba@220
  1732
            b = true;
deba@220
  1733
            g.nextInc(e, b);
deba@220
  1734
          }
deba@220
  1735
          while (e != INVALID && (!b || g.v(e) != v)) {
deba@220
  1736
            g.nextInc(e, b);
deba@220
  1737
          }
deba@220
  1738
        }
deba@220
  1739
        return e;
deba@220
  1740
      }
deba@220
  1741
    };
deba@220
  1742
deba@220
  1743
    template <typename Graph>
deba@220
  1744
    struct FindEdgeSelector<
deba@220
  1745
      Graph,
deba@220
  1746
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
deba@220
  1747
    {
deba@220
  1748
      typedef typename Graph::Node Node;
deba@220
  1749
      typedef typename Graph::Edge Edge;
deba@220
  1750
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
deba@220
  1751
        return g.findEdge(u, v, prev);
deba@220
  1752
      }
deba@220
  1753
    };
deba@220
  1754
  }
deba@220
  1755
kpeter@282
  1756
  /// \brief Find an edge between two nodes of a graph.
deba@220
  1757
  ///
kpeter@282
  1758
  /// This function finds an edge from node \c u to node \c v in graph \c g.
kpeter@282
  1759
  /// If node \c u and node \c v is equal then each loop edge
deba@220
  1760
  /// will be enumerated once.
deba@220
  1761
  ///
deba@220
  1762
  /// If \c prev is \ref INVALID (this is the default value), then
kpeter@282
  1763
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
kpeter@282
  1764
  /// the next edge from \c u to \c v after \c prev.
kpeter@282
  1765
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@220
  1766
  ///
kpeter@282
  1767
  /// Thus you can iterate through each edge between \c u and \c v
kpeter@282
  1768
  /// as it follows.
deba@220
  1769
  ///\code
kpeter@282
  1770
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
deba@220
  1771
  ///   ...
deba@220
  1772
  /// }
deba@220
  1773
  ///\endcode
deba@220
  1774
  ///
kpeter@282
  1775
  /// \note \ref ConEdgeIt provides iterator interface for the same
kpeter@282
  1776
  /// functionality.
kpeter@282
  1777
  ///
deba@220
  1778
  ///\sa ConEdgeIt
deba@220
  1779
  template <typename Graph>
deba@220
  1780
  inline typename Graph::Edge
deba@220
  1781
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@220
  1782
            typename Graph::Edge p = INVALID) {
deba@220
  1783
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
deba@220
  1784
  }
deba@220
  1785
kpeter@282
  1786
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
deba@220
  1787
  ///
kpeter@282
  1788
  /// Iterator for iterating on parallel edges connecting the same nodes.
kpeter@282
  1789
  /// It is a higher level interface for the findEdge() function. You can
deba@220
  1790
  /// use it the following way:
deba@220
  1791
  ///\code
kpeter@282
  1792
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
deba@220
  1793
  ///   ...
deba@220
  1794
  /// }
deba@220
  1795
  ///\endcode
deba@220
  1796
  ///
deba@220
  1797
  ///\sa findEdge()
kpeter@559
  1798
  template <typename GR>
kpeter@559
  1799
  class ConEdgeIt : public GR::Edge {
kpeter@617
  1800
    typedef typename GR::Edge Parent;
kpeter@617
  1801
deba@220
  1802
  public:
deba@220
  1803
kpeter@617
  1804
    typedef typename GR::Edge Edge;
kpeter@617
  1805
    typedef typename GR::Node Node;
deba@220
  1806
deba@220
  1807
    /// \brief Constructor.
deba@220
  1808
    ///
kpeter@282
  1809
    /// Construct a new ConEdgeIt iterating on the edges that
kpeter@282
  1810
    /// connects nodes \c u and \c v.
kpeter@617
  1811
    ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
kpeter@429
  1812
      Parent::operator=(findEdge(_graph, _u, _v));
deba@220
  1813
    }
deba@220
  1814
deba@220
  1815
    /// \brief Constructor.
deba@220
  1816
    ///
kpeter@282
  1817
    /// Construct a new ConEdgeIt that continues iterating from edge \c e.
kpeter@617
  1818
    ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
deba@220
  1819
deba@220
  1820
    /// \brief Increment operator.
deba@220
  1821
    ///
deba@220
  1822
    /// It increments the iterator and gives back the next edge.
deba@220
  1823
    ConEdgeIt& operator++() {
kpeter@429
  1824
      Parent::operator=(findEdge(_graph, _u, _v, *this));
deba@220
  1825
      return *this;
deba@220
  1826
    }
deba@220
  1827
  private:
kpeter@617
  1828
    const GR& _graph;
kpeter@429
  1829
    Node _u, _v;
deba@220
  1830
  };
deba@220
  1831
deba@220
  1832
kpeter@282
  1833
  ///Dynamic arc look-up between given endpoints.
deba@220
  1834
deba@220
  1835
  ///Using this class, you can find an arc in a digraph from a given
kpeter@282
  1836
  ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
deba@220
  1837
  ///where <em>d</em> is the out-degree of the source node.
deba@220
  1838
  ///
deba@220
  1839
  ///It is possible to find \e all parallel arcs between two nodes with
deba@233
  1840
  ///the \c operator() member.
deba@220
  1841
  ///
kpeter@282
  1842
  ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
kpeter@282
  1843
  ///\ref AllArcLookUp if your digraph is not changed so frequently.
deba@220
  1844
  ///
kpeter@282
  1845
  ///This class uses a self-adjusting binary search tree, the Splay tree
kpeter@282
  1846
  ///of Sleator and Tarjan to guarantee the logarithmic amortized
kpeter@282
  1847
  ///time bound for arc look-ups. This class also guarantees the
deba@220
  1848
  ///optimal time bound in a constant factor for any distribution of
deba@220
  1849
  ///queries.
deba@220
  1850
  ///
kpeter@559
  1851
  ///\tparam GR The type of the underlying digraph.
deba@220
  1852
  ///
deba@220
  1853
  ///\sa ArcLookUp
deba@220
  1854
  ///\sa AllArcLookUp
kpeter@559
  1855
  template <typename GR>
deba@220
  1856
  class DynArcLookUp
kpeter@559
  1857
    : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
deba@220
  1858
  {
kpeter@559
  1859
    typedef typename ItemSetTraits<GR, typename GR::Arc>
deba@220
  1860
    ::ItemNotifier::ObserverBase Parent;
deba@220
  1861
kpeter@559
  1862
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@617
  1863
kpeter@617
  1864
  public:
kpeter@617
  1865
kpeter@617
  1866
    /// The Digraph type
kpeter@559
  1867
    typedef GR Digraph;
alpar@1092
  1868
deba@220
  1869
  protected:
deba@220
  1870
alpar@877
  1871
    class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
alpar@877
  1872
    {
kpeter@617
  1873
      typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
kpeter@617
  1874
deba@220
  1875
    public:
deba@220
  1876
kpeter@559
  1877
      AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
deba@220
  1878
deba@220
  1879
      virtual void add(const Node& node) {
deba@220
  1880
        Parent::add(node);
deba@220
  1881
        Parent::set(node, INVALID);
deba@220
  1882
      }
deba@220
  1883
deba@220
  1884
      virtual void add(const std::vector<Node>& nodes) {
deba@220
  1885
        Parent::add(nodes);
deba@220
  1886
        for (int i = 0; i < int(nodes.size()); ++i) {
deba@220
  1887
          Parent::set(nodes[i], INVALID);
deba@220
  1888
        }
deba@220
  1889
      }
deba@220
  1890
deba@220
  1891
      virtual void build() {
deba@220
  1892
        Parent::build();
deba@220
  1893
        Node it;
deba@220
  1894
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  1895
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  1896
          Parent::set(it, INVALID);
deba@220
  1897
        }
deba@220
  1898
      }
deba@220
  1899
    };
deba@220
  1900
deba@220
  1901
    class ArcLess {
deba@220
  1902
      const Digraph &g;
deba@220
  1903
    public:
deba@220
  1904
      ArcLess(const Digraph &_g) : g(_g) {}
deba@220
  1905
      bool operator()(Arc a,Arc b) const
deba@220
  1906
      {
deba@220
  1907
        return g.target(a)<g.target(b);
deba@220
  1908
      }
deba@220
  1909
    };
deba@220
  1910
alpar@877
  1911
  protected:
kpeter@617
  1912
kpeter@617
  1913
    const Digraph &_g;
kpeter@617
  1914
    AutoNodeMap _head;
kpeter@617
  1915
    typename Digraph::template ArcMap<Arc> _parent;
kpeter@617
  1916
    typename Digraph::template ArcMap<Arc> _left;
kpeter@617
  1917
    typename Digraph::template ArcMap<Arc> _right;
kpeter@617
  1918
deba@220
  1919
  public:
deba@220
  1920
deba@220
  1921
    ///Constructor
deba@220
  1922
deba@220
  1923
    ///Constructor.
deba@220
  1924
    ///
deba@220
  1925
    ///It builds up the search database.
deba@220
  1926
    DynArcLookUp(const Digraph &g)
deba@220
  1927
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
deba@220
  1928
    {
deba@220
  1929
      Parent::attach(_g.notifier(typename Digraph::Arc()));
deba@220
  1930
      refresh();
deba@220
  1931
    }
deba@220
  1932
deba@220
  1933
  protected:
deba@220
  1934
deba@220
  1935
    virtual void add(const Arc& arc) {
deba@220
  1936
      insert(arc);
deba@220
  1937
    }
deba@220
  1938
deba@220
  1939
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  1940
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  1941
        insert(arcs[i]);
deba@220
  1942
      }
deba@220
  1943
    }
deba@220
  1944
deba@220
  1945
    virtual void erase(const Arc& arc) {
deba@220
  1946
      remove(arc);
deba@220
  1947
    }
deba@220
  1948
deba@220
  1949
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  1950
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  1951
        remove(arcs[i]);
deba@220
  1952
      }
deba@220
  1953
    }
deba@220
  1954
deba@220
  1955
    virtual void build() {
deba@220
  1956
      refresh();
deba@220
  1957
    }
deba@220
  1958
deba@220
  1959
    virtual void clear() {
deba@220
  1960
      for(NodeIt n(_g);n!=INVALID;++n) {
kpeter@581
  1961
        _head[n] = INVALID;
deba@220
  1962
      }
deba@220
  1963
    }
deba@220
  1964
deba@220
  1965
    void insert(Arc arc) {
deba@220
  1966
      Node s = _g.source(arc);
deba@220
  1967
      Node t = _g.target(arc);
kpeter@581
  1968
      _left[arc] = INVALID;
kpeter@581
  1969
      _right[arc] = INVALID;
deba@220
  1970
deba@220
  1971
      Arc e = _head[s];
deba@220
  1972
      if (e == INVALID) {
kpeter@581
  1973
        _head[s] = arc;
kpeter@581
  1974
        _parent[arc] = INVALID;
deba@220
  1975
        return;
deba@220
  1976
      }
deba@220
  1977
      while (true) {
deba@220
  1978
        if (t < _g.target(e)) {
deba@220
  1979
          if (_left[e] == INVALID) {
kpeter@581
  1980
            _left[e] = arc;
kpeter@581
  1981
            _parent[arc] = e;
deba@220
  1982
            splay(arc);
deba@220
  1983
            return;
deba@220
  1984
          } else {
deba@220
  1985
            e = _left[e];
deba@220
  1986
          }
deba@220
  1987
        } else {
deba@220
  1988
          if (_right[e] == INVALID) {
kpeter@581
  1989
            _right[e] = arc;
kpeter@581
  1990
            _parent[arc] = e;
deba@220
  1991
            splay(arc);
deba@220
  1992
            return;
deba@220
  1993
          } else {
deba@220
  1994
            e = _right[e];
deba@220
  1995
          }
deba@220
  1996
        }
deba@220
  1997
      }
deba@220
  1998
    }
deba@220
  1999
deba@220
  2000
    void remove(Arc arc) {
deba@220
  2001
      if (_left[arc] == INVALID) {
deba@220
  2002
        if (_right[arc] != INVALID) {
kpeter@581
  2003
          _parent[_right[arc]] = _parent[arc];
deba@220
  2004
        }
deba@220
  2005
        if (_parent[arc] != INVALID) {
deba@220
  2006
          if (_left[_parent[arc]] == arc) {
kpeter@581
  2007
            _left[_parent[arc]] = _right[arc];
deba@220
  2008
          } else {
kpeter@581
  2009
            _right[_parent[arc]] = _right[arc];
deba@220
  2010
          }
deba@220
  2011
        } else {
kpeter@581
  2012
          _head[_g.source(arc)] = _right[arc];
deba@220
  2013
        }
deba@220
  2014
      } else if (_right[arc] == INVALID) {
kpeter@581
  2015
        _parent[_left[arc]] = _parent[arc];
deba@220
  2016
        if (_parent[arc] != INVALID) {
deba@220
  2017
          if (_left[_parent[arc]] == arc) {
kpeter@581
  2018
            _left[_parent[arc]] = _left[arc];
deba@220
  2019
          } else {
kpeter@581
  2020
            _right[_parent[arc]] = _left[arc];
deba@220
  2021
          }
deba@220
  2022
        } else {
kpeter@581
  2023
          _head[_g.source(arc)] = _left[arc];
deba@220
  2024
        }
deba@220
  2025
      } else {
deba@220
  2026
        Arc e = _left[arc];
deba@220
  2027
        if (_right[e] != INVALID) {
deba@220
  2028
          e = _right[e];
deba@220
  2029
          while (_right[e] != INVALID) {
deba@220
  2030
            e = _right[e];
deba@220
  2031
          }
deba@220
  2032
          Arc s = _parent[e];
kpeter@581
  2033
          _right[_parent[e]] = _left[e];
deba@220
  2034
          if (_left[e] != INVALID) {
kpeter@581
  2035
            _parent[_left[e]] = _parent[e];
deba@220
  2036
          }
deba@220
  2037
kpeter@581
  2038
          _left[e] = _left[arc];
kpeter@581
  2039
          _parent[_left[arc]] = e;
kpeter@581
  2040
          _right[e] = _right[arc];
kpeter@581
  2041
          _parent[_right[arc]] = e;
deba@220
  2042
kpeter@581
  2043
          _parent[e] = _parent[arc];
deba@220
  2044
          if (_parent[arc] != INVALID) {
deba@220
  2045
            if (_left[_parent[arc]] == arc) {
kpeter@581
  2046
              _left[_parent[arc]] = e;
deba@220
  2047
            } else {
kpeter@581
  2048
              _right[_parent[arc]] = e;
deba@220
  2049
            }
deba@220
  2050
          }
deba@220
  2051
          splay(s);
deba@220
  2052
        } else {
kpeter@581
  2053
          _right[e] = _right[arc];
kpeter@581
  2054
          _parent[_right[arc]] = e;
kpeter@581
  2055
          _parent[e] = _parent[arc];
deba@220
  2056
deba@220
  2057
          if (_parent[arc] != INVALID) {
deba@220
  2058
            if (_left[_parent[arc]] == arc) {
kpeter@581
  2059
              _left[_parent[arc]] = e;
deba@220
  2060
            } else {
kpeter@581
  2061
              _right[_parent[arc]] = e;
deba@220
  2062
            }
deba@220
  2063
          } else {
kpeter@581
  2064
            _head[_g.source(arc)] = e;
deba@220
  2065
          }
deba@220
  2066
        }
deba@220
  2067
      }
deba@220
  2068
    }
deba@220
  2069
deba@220
  2070
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
deba@220
  2071
    {
deba@220
  2072
      int m=(a+b)/2;
deba@220
  2073
      Arc me=v[m];
deba@220
  2074
      if (a < m) {
deba@220
  2075
        Arc left = refreshRec(v,a,m-1);
kpeter@581
  2076
        _left[me] = left;
kpeter@581
  2077
        _parent[left] = me;
deba@220
  2078
      } else {
kpeter@581
  2079
        _left[me] = INVALID;
deba@220
  2080
      }
deba@220
  2081
      if (m < b) {
deba@220
  2082
        Arc right = refreshRec(v,m+1,b);
kpeter@581
  2083
        _right[me] = right;
kpeter@581
  2084
        _parent[right] = me;
deba@220
  2085
      } else {
kpeter@581
  2086
        _right[me] = INVALID;
deba@220
  2087
      }
deba@220
  2088
      return me;
deba@220
  2089
    }
deba@220
  2090
deba@220
  2091
    void refresh() {
deba@220
  2092
      for(NodeIt n(_g);n!=INVALID;++n) {
deba@220
  2093
        std::vector<Arc> v;
deba@233
  2094
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
deba@233
  2095
        if (!v.empty()) {
deba@220
  2096
          std::sort(v.begin(),v.end(),ArcLess(_g));
deba@220
  2097
          Arc head = refreshRec(v,0,v.size()-1);
kpeter@581
  2098
          _head[n] = head;
kpeter@581
  2099
          _parent[head] = INVALID;
deba@220
  2100
        }
kpeter@581
  2101
        else _head[n] = INVALID;
deba@220
  2102
      }
deba@220
  2103
    }
deba@220
  2104
deba@220
  2105
    void zig(Arc v) {
deba@220
  2106
      Arc w = _parent[v];
kpeter@581
  2107
      _parent[v] = _parent[w];
kpeter@581
  2108
      _parent[w] = v;
kpeter@581
  2109
      _left[w] = _right[v];
kpeter@581
  2110
      _right[v] = w;
deba@220
  2111
      if (_parent[v] != INVALID) {
deba@220
  2112
        if (_right[_parent[v]] == w) {
kpeter@581
  2113
          _right[_parent[v]] = v;
deba@220
  2114
        } else {
kpeter@581
  2115
          _left[_parent[v]] = v;
deba@220
  2116
        }
deba@220
  2117
      }
deba@220
  2118
      if (_left[w] != INVALID){
kpeter@581
  2119
        _parent[_left[w]] = w;
deba@220
  2120
      }
deba@220
  2121
    }
deba@220
  2122
deba@220
  2123
    void zag(Arc v) {
deba@220
  2124
      Arc w = _parent[v];
kpeter@581
  2125
      _parent[v] = _parent[w];
kpeter@581
  2126
      _parent[w] = v;
kpeter@581
  2127
      _right[w] = _left[v];
kpeter@581
  2128
      _left[v] = w;
deba@220
  2129
      if (_parent[v] != INVALID){
deba@220
  2130
        if (_left[_parent[v]] == w) {
kpeter@581
  2131
          _left[_parent[v]] = v;
deba@220
  2132
        } else {
kpeter@581
  2133
          _right[_parent[v]] = v;
deba@220
  2134
        }
deba@220
  2135
      }
deba@220
  2136
      if (_right[w] != INVALID){
kpeter@581
  2137
        _parent[_right[w]] = w;
deba@220
  2138
      }
deba@220
  2139
    }
deba@220
  2140
deba@220
  2141
    void splay(Arc v) {
deba@220
  2142
      while (_parent[v] != INVALID) {
deba@220
  2143
        if (v == _left[_parent[v]]) {
deba@220
  2144
          if (_parent[_parent[v]] == INVALID) {
deba@220
  2145
            zig(v);
deba@220
  2146
          } else {
deba@220
  2147
            if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@220
  2148
              zig(_parent[v]);
deba@220
  2149
              zig(v);
deba@220
  2150
            } else {
deba@220
  2151
              zig(v);
deba@220
  2152
              zag(v);
deba@220
  2153
            }
deba@220
  2154
          }
deba@220
  2155
        } else {
deba@220
  2156
          if (_parent[_parent[v]] == INVALID) {
deba@220
  2157
            zag(v);
deba@220
  2158
          } else {
deba@220
  2159
            if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@220
  2160
              zag(v);
deba@220
  2161
              zig(v);
deba@220
  2162
            } else {
deba@220
  2163
              zag(_parent[v]);
deba@220
  2164
              zag(v);
deba@220
  2165
            }
deba@220
  2166
          }
deba@220
  2167
        }
deba@220
  2168
      }
deba@220
  2169
      _head[_g.source(v)] = v;
deba@220
  2170
    }
deba@220
  2171
deba@220
  2172
deba@220
  2173
  public:
deba@220
  2174
deba@220
  2175
    ///Find an arc between two nodes.
deba@220
  2176
deba@233
  2177
    ///Find an arc between two nodes.
kpeter@282
  2178
    ///\param s The source node.
kpeter@282
  2179
    ///\param t The target node.
deba@233
  2180
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
deba@233
  2181
    ///not given, the operator finds the first appropriate arc.
deba@233
  2182
    ///\return An arc from \c s to \c t after \c p or
deba@233
  2183
    ///\ref INVALID if there is no more.
deba@233
  2184
    ///
deba@233
  2185
    ///For example, you can count the number of arcs from \c u to \c v in the
deba@233
  2186
    ///following way.
deba@233
  2187
    ///\code
deba@233
  2188
    ///DynArcLookUp<ListDigraph> ae(g);
deba@233
  2189
    ///...
kpeter@282
  2190
    ///int n = 0;
kpeter@282
  2191
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
deba@233
  2192
    ///\endcode
deba@233
  2193
    ///
kpeter@282
  2194
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
deba@233
  2195
    ///amortized time, specifically, the time complexity of the lookups
deba@233
  2196
    ///is equal to the optimal search tree implementation for the
deba@233
  2197
    ///current query distribution in a constant factor.
deba@233
  2198
    ///
deba@233
  2199
    ///\note This is a dynamic data structure, therefore the data
kpeter@282
  2200
    ///structure is updated after each graph alteration. Thus although
kpeter@282
  2201
    ///this data structure is theoretically faster than \ref ArcLookUp
kpeter@313
  2202
    ///and \ref AllArcLookUp, it often provides worse performance than
deba@233
  2203
    ///them.
deba@233
  2204
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
deba@233
  2205
      if (p == INVALID) {
deba@233
  2206
        Arc a = _head[s];
deba@233
  2207
        if (a == INVALID) return INVALID;
deba@233
  2208
        Arc r = INVALID;
deba@233
  2209
        while (true) {
deba@233
  2210
          if (_g.target(a) < t) {
deba@233
  2211
            if (_right[a] == INVALID) {
deba@233
  2212
              const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  2213
              return r;
deba@233
  2214
            } else {
deba@233
  2215
              a = _right[a];
deba@233
  2216
            }
deba@233
  2217
          } else {
deba@233
  2218
            if (_g.target(a) == t) {
deba@233
  2219
              r = a;
deba@233
  2220
            }
deba@233
  2221
            if (_left[a] == INVALID) {
deba@233
  2222
              const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  2223
              return r;
deba@233
  2224
            } else {
deba@233
  2225
              a = _left[a];
deba@233
  2226
            }
deba@233
  2227
          }
deba@233
  2228
        }
deba@233
  2229
      } else {
deba@233
  2230
        Arc a = p;
deba@233
  2231
        if (_right[a] != INVALID) {
deba@233
  2232
          a = _right[a];
deba@233
  2233
          while (_left[a] != INVALID) {
deba@233
  2234
            a = _left[a];
deba@233
  2235
          }
deba@220
  2236
          const_cast<DynArcLookUp&>(*this).splay(a);
deba@233
  2237
        } else {
deba@233
  2238
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
deba@233
  2239
            a = _parent[a];
deba@233
  2240
          }
deba@233
  2241
          if (_parent[a] == INVALID) {
deba@220
  2242
            return INVALID;
deba@220
  2243
          } else {
deba@233
  2244
            a = _parent[a];
deba@220
  2245
            const_cast<DynArcLookUp&>(*this).splay(a);
deba@220
  2246
          }
deba@220
  2247
        }
deba@233
  2248
        if (_g.target(a) == t) return a;
deba@233
  2249
        else return INVALID;
deba@220
  2250
      }
deba@220
  2251
    }
deba@220
  2252
deba@220
  2253
  };
deba@220
  2254
kpeter@282
  2255
  ///Fast arc look-up between given endpoints.
deba@220
  2256
deba@220
  2257
  ///Using this class, you can find an arc in a digraph from a given
kpeter@282
  2258
  ///source to a given target in time <em>O</em>(log<em>d</em>),
deba@220
  2259
  ///where <em>d</em> is the out-degree of the source node.
deba@220
  2260
  ///
deba@220
  2261
  ///It is not possible to find \e all parallel arcs between two nodes.
deba@220
  2262
  ///Use \ref AllArcLookUp for this purpose.
deba@220
  2263
  ///
kpeter@282
  2264
  ///\warning This class is static, so you should call refresh() (or at
kpeter@282
  2265
  ///least refresh(Node)) to refresh this data structure whenever the
kpeter@282
  2266
  ///digraph changes. This is a time consuming (superlinearly proportional
kpeter@282
  2267
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
deba@220
  2268
  ///
kpeter@559
  2269
  ///\tparam GR The type of the underlying digraph.
deba@220
  2270
  ///
deba@220
  2271
  ///\sa DynArcLookUp
deba@220
  2272
  ///\sa AllArcLookUp
kpeter@559
  2273
  template<class GR>
deba@220
  2274
  class ArcLookUp
deba@220
  2275
  {
kpeter@617
  2276
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@617
  2277
deba@220
  2278
  public:
kpeter@617
  2279
kpeter@617
  2280
    /// The Digraph type
kpeter@559
  2281
    typedef GR Digraph;
deba@220
  2282
deba@220
  2283
  protected:
deba@220
  2284
    const Digraph &_g;
deba@220
  2285
    typename Digraph::template NodeMap<Arc> _head;
deba@220
  2286
    typename Digraph::template ArcMap<Arc> _left;
deba@220
  2287
    typename Digraph::template ArcMap<Arc> _right;
deba@220
  2288
deba@220
  2289
    class ArcLess {
deba@220
  2290
      const Digraph &g;
deba@220
  2291
    public:
deba@220
  2292
      ArcLess(const Digraph &_g) : g(_g) {}
deba@220
  2293
      bool operator()(Arc a,Arc b) const
deba@220
  2294
      {
deba@220
  2295
        return g.target(a)<g.target(b);
deba@220
  2296
      }
deba@220
  2297
    };
deba@220
  2298
deba@220
  2299
  public:
deba@220
  2300
deba@220
  2301
    ///Constructor
deba@220
  2302
deba@220
  2303
    ///Constructor.
deba@220
  2304
    ///
deba@220
  2305
    ///It builds up the search database, which remains valid until the digraph
deba@220
  2306
    ///changes.
deba@220
  2307
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
deba@220
  2308
deba@220
  2309
  private:
deba@220
  2310
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
deba@220
  2311
    {
deba@220
  2312
      int m=(a+b)/2;
deba@220
  2313
      Arc me=v[m];
deba@220
  2314
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
deba@220
  2315
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
deba@220
  2316
      return me;
deba@220
  2317
    }
deba@220
  2318
  public:
kpeter@282
  2319
    ///Refresh the search data structure at a node.
deba@220
  2320
deba@220
  2321
    ///Build up the search database of node \c n.
deba@220
  2322
    ///
kpeter@282
  2323
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
kpeter@282
  2324
    ///is the number of the outgoing arcs of \c n.
deba@220
  2325
    void refresh(Node n)
deba@220
  2326
    {
deba@220
  2327
      std::vector<Arc> v;
deba@220
  2328
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
deba@220
  2329
      if(v.size()) {
deba@220
  2330
        std::sort(v.begin(),v.end(),ArcLess(_g));
deba@220
  2331
        _head[n]=refreshRec(v,0,v.size()-1);
deba@220
  2332
      }
deba@220
  2333
      else _head[n]=INVALID;
deba@220
  2334
    }
deba@220
  2335
    ///Refresh the full data structure.
deba@220
  2336
deba@220
  2337
    ///Build up the full search database. In fact, it simply calls
deba@220
  2338
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
deba@220
  2339
    ///
kpeter@282
  2340
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
kpeter@282
  2341
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
deba@220
  2342
    ///out-degree of the digraph.
deba@220
  2343
    void refresh()
deba@220
  2344
    {
deba@220
  2345
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
deba@220
  2346
    }
deba@220
  2347
deba@220
  2348
    ///Find an arc between two nodes.
deba@220
  2349
kpeter@313
  2350
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
kpeter@313
  2351
    ///where <em>d</em> is the number of outgoing arcs of \c s.
kpeter@282
  2352
    ///\param s The source node.
kpeter@282
  2353
    ///\param t The target node.
deba@220
  2354
    ///\return An arc from \c s to \c t if there exists,
deba@220
  2355
    ///\ref INVALID otherwise.
deba@220
  2356
    ///
deba@220
  2357
    ///\warning If you change the digraph, refresh() must be called before using
deba@220
  2358
    ///this operator. If you change the outgoing arcs of
kpeter@282
  2359
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
deba@220
  2360
    Arc operator()(Node s, Node t) const
deba@220
  2361
    {
deba@220
  2362
      Arc e;
deba@220
  2363
      for(e=_head[s];
deba@220
  2364
          e!=INVALID&&_g.target(e)!=t;
deba@220
  2365
          e = t < _g.target(e)?_left[e]:_right[e]) ;
deba@220
  2366
      return e;
deba@220
  2367
    }
deba@220
  2368
deba@220
  2369
  };
deba@220
  2370
kpeter@282
  2371
  ///Fast look-up of all arcs between given endpoints.
deba@220
  2372
deba@220
  2373
  ///This class is the same as \ref ArcLookUp, with the addition
kpeter@282
  2374
  ///that it makes it possible to find all parallel arcs between given
kpeter@282
  2375
  ///endpoints.
deba@220
  2376
  ///
kpeter@282
  2377
  ///\warning This class is static, so you should call refresh() (or at
kpeter@282
  2378
  ///least refresh(Node)) to refresh this data structure whenever the
kpeter@282
  2379
  ///digraph changes. This is a time consuming (superlinearly proportional
kpeter@282
  2380
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
deba@220
  2381
  ///
kpeter@559
  2382
  ///\tparam GR The type of the underlying digraph.
deba@220
  2383
  ///
deba@220
  2384
  ///\sa DynArcLookUp
deba@220
  2385
  ///\sa ArcLookUp
kpeter@559
  2386
  template<class GR>
kpeter@559
  2387
  class AllArcLookUp : public ArcLookUp<GR>
deba@220
  2388
  {
kpeter@559
  2389
    using ArcLookUp<GR>::_g;
kpeter@559
  2390
    using ArcLookUp<GR>::_right;
kpeter@559
  2391
    using ArcLookUp<GR>::_left;
kpeter@559
  2392
    using ArcLookUp<GR>::_head;
deba@220
  2393
kpeter@559
  2394
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
deba@220
  2395
kpeter@617
  2396
    typename GR::template ArcMap<Arc> _next;
deba@220
  2397
deba@220
  2398
    Arc refreshNext(Arc head,Arc next=INVALID)
deba@220
  2399
    {
deba@220
  2400
      if(head==INVALID) return next;
deba@220
  2401
      else {
deba@220
  2402
        next=refreshNext(_right[head],next);
deba@220
  2403
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
deba@220
  2404
          ? next : INVALID;
deba@220
  2405
        return refreshNext(_left[head],head);
deba@220
  2406
      }
deba@220
  2407
    }
deba@220
  2408
deba@220
  2409
    void refreshNext()
deba@220
  2410
    {
deba@220
  2411
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
deba@220
  2412
    }
deba@220
  2413
deba@220
  2414
  public:
kpeter@617
  2415
kpeter@617
  2416
    /// The Digraph type
kpeter@617
  2417
    typedef GR Digraph;
kpeter@617
  2418
deba@220
  2419
    ///Constructor
deba@220
  2420
deba@220
  2421
    ///Constructor.
deba@220
  2422
    ///
deba@220
  2423
    ///It builds up the search database, which remains valid until the digraph
deba@220
  2424
    ///changes.
kpeter@559
  2425
    AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
deba@220
  2426
deba@220
  2427
    ///Refresh the data structure at a node.
deba@220
  2428
deba@220
  2429
    ///Build up the search database of node \c n.
deba@220
  2430
    ///
kpeter@282
  2431
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
deba@220
  2432
    ///the number of the outgoing arcs of \c n.
deba@220
  2433
    void refresh(Node n)
deba@220
  2434
    {
kpeter@559
  2435
      ArcLookUp<GR>::refresh(n);
deba@220
  2436
      refreshNext(_head[n]);
deba@220
  2437
    }
deba@220
  2438
deba@220
  2439
    ///Refresh the full data structure.
deba@220
  2440
deba@220
  2441
    ///Build up the full search database. In fact, it simply calls
deba@220
  2442
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
deba@220
  2443
    ///
kpeter@282
  2444
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
kpeter@282
  2445
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
deba@220
  2446
    ///out-degree of the digraph.
deba@220
  2447
    void refresh()
deba@220
  2448
    {
deba@220
  2449
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
deba@220
  2450
    }
deba@220
  2451
deba@220
  2452
    ///Find an arc between two nodes.
deba@220
  2453
deba@220
  2454
    ///Find an arc between two nodes.
kpeter@282
  2455
    ///\param s The source node.
kpeter@282
  2456
    ///\param t The target node.
deba@220
  2457
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
deba@220
  2458
    ///not given, the operator finds the first appropriate arc.
deba@220
  2459
    ///\return An arc from \c s to \c t after \c prev or
deba@220
  2460
    ///\ref INVALID if there is no more.
deba@220
  2461
    ///
deba@220
  2462
    ///For example, you can count the number of arcs from \c u to \c v in the
deba@220
  2463
    ///following way.
deba@220
  2464
    ///\code
deba@220
  2465
    ///AllArcLookUp<ListDigraph> ae(g);
deba@220
  2466
    ///...
kpeter@282
  2467
    ///int n = 0;
kpeter@282
  2468
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
deba@220
  2469
    ///\endcode
deba@220
  2470
    ///
kpeter@313
  2471
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
kpeter@313
  2472
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
deba@220
  2473
    ///consecutive arcs are found in constant time.
deba@220
  2474
    ///
deba@220
  2475
    ///\warning If you change the digraph, refresh() must be called before using
deba@220
  2476
    ///this operator. If you change the outgoing arcs of
kpeter@282
  2477
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
deba@220
  2478
    ///
alpar@993
  2479
    Arc operator()(Node s, Node t, Arc prev=INVALID) const
deba@220
  2480
    {
alpar@993
  2481
      if(prev==INVALID)
alpar@993
  2482
        {
alpar@993
  2483
          Arc f=INVALID;
alpar@993
  2484
          Arc e;
alpar@993
  2485
          for(e=_head[s];
alpar@993
  2486
              e!=INVALID&&_g.target(e)!=t;
alpar@993
  2487
              e = t < _g.target(e)?_left[e]:_right[e]) ;
alpar@993
  2488
          while(e!=INVALID)
alpar@993
  2489
            if(_g.target(e)==t)
alpar@993
  2490
              {
alpar@993
  2491
                f = e;
alpar@993
  2492
                e = _left[e];
alpar@993
  2493
              }
alpar@993
  2494
            else e = _right[e];
alpar@993
  2495
          return f;
alpar@993
  2496
        }
alpar@993
  2497
      else return _next[prev];
deba@220
  2498
    }
deba@220
  2499
deba@220
  2500
  };
deba@220
  2501
deba@220
  2502
  /// @}
deba@220
  2503
deba@220
  2504
} //namespace lemon
deba@220
  2505
deba@220
  2506
#endif