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