lemon/core.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 01 Apr 2015 18:13:30 +0200
changeset 1325 1d80ec7d17eb
parent 1270 dceba191c00d
child 1327 18c89646185e
permissions -rw-r--r--
Remove GCC_VERSION define (#536)

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