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