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