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