lemon/graph_utils.h
author deba
Fri, 15 Jun 2007 14:36:24 +0000
changeset 2457 8c791ee69a45
parent 2386 81b47fc5c444
child 2474 e6368948d5f7
permissions -rw-r--r--
Improvments in min cost flow algorithms
- improved cycle cancelling
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     8
 *
klao@946
     9
 * Permission to use, modify and distribute this software is granted
klao@946
    10
 * provided that this copyright notice appears in all copies. For
klao@946
    11
 * precise terms see the accompanying LICENSE file.
klao@946
    12
 *
klao@946
    13
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    14
 * express or implied, and with no claim as to its suitability for any
klao@946
    15
 * purpose.
klao@946
    16
 *
klao@946
    17
 */
klao@946
    18
klao@946
    19
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    20
#define LEMON_GRAPH_UTILS_H
klao@946
    21
klao@946
    22
#include <iterator>
deba@1419
    23
#include <vector>
alpar@1402
    24
#include <map>
deba@1695
    25
#include <cmath>
alpar@2235
    26
#include <algorithm>
klao@946
    27
deba@1993
    28
#include <lemon/bits/invalid.h>
deba@1993
    29
#include <lemon/bits/utility.h>
deba@1413
    30
#include <lemon/maps.h>
deba@1993
    31
#include <lemon/bits/traits.h>
deba@1990
    32
alpar@1459
    33
#include <lemon/bits/alteration_notifier.h>
deba@1990
    34
#include <lemon/bits/default_map.h>
klao@946
    35
alpar@947
    36
///\ingroup gutils
klao@946
    37
///\file
alpar@947
    38
///\brief Graph utilities.
klao@946
    39
///
alpar@964
    40
///
klao@946
    41
klao@946
    42
klao@946
    43
namespace lemon {
klao@946
    44
deba@1267
    45
  /// \addtogroup gutils
deba@1267
    46
  /// @{
alpar@947
    47
alpar@1756
    48
  ///Creates convenience typedefs for the graph types and iterators
alpar@1756
    49
alpar@1756
    50
  ///This \c \#define creates convenience typedefs for the following types
alpar@1756
    51
  ///of \c Graph: \c Node,  \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
deba@2031
    52
  ///\c OutEdgeIt
alpar@1756
    53
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    54
  ///\code
alpar@1756
    55
  ///  GRAPH_TYPEDEFS(typename G)
alpar@1756
    56
  ///\endcode
alpar@1756
    57
  ///
alpar@1756
    58
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    59
  ///template typedefs in C++.
alpar@1804
    60
#define GRAPH_TYPEDEFS(Graph)				\
alpar@1804
    61
  typedef Graph::     Node      Node;			\
alpar@1804
    62
    typedef Graph::   NodeIt    NodeIt;			\
alpar@1804
    63
    typedef Graph::   Edge      Edge;			\
alpar@1804
    64
    typedef Graph::   EdgeIt    EdgeIt;			\
alpar@1804
    65
    typedef Graph:: InEdgeIt  InEdgeIt;			\
alpar@1811
    66
    typedef Graph::OutEdgeIt OutEdgeIt;			
deba@2031
    67
alpar@1756
    68
  ///Creates convenience typedefs for the undirected graph types and iterators
alpar@1756
    69
alpar@1756
    70
  ///This \c \#define creates the same convenience typedefs as defined by
alpar@1756
    71
  ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
klao@1909
    72
  ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
alpar@1756
    73
  ///
alpar@1756
    74
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    75
  ///\code
deba@1992
    76
  ///  UGRAPH_TYPEDEFS(typename G)
alpar@1756
    77
  ///\endcode
alpar@1756
    78
  ///
alpar@1756
    79
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    80
  ///template typedefs in C++.
deba@1992
    81
#define UGRAPH_TYPEDEFS(Graph)				\
alpar@1804
    82
  GRAPH_TYPEDEFS(Graph)						\
klao@1909
    83
    typedef Graph:: UEdge   UEdge;			\
klao@1909
    84
    typedef Graph:: UEdgeIt UEdgeIt;			\
alpar@1811
    85
    typedef Graph:: IncEdgeIt   IncEdgeIt;		       
alpar@1756
    86
deba@2031
    87
  ///\brief Creates convenience typedefs for the bipartite undirected graph 
deba@2031
    88
  ///types and iterators
deba@2031
    89
deba@2031
    90
  ///This \c \#define creates the same convenience typedefs as defined by
deba@2031
    91
  ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
deba@2031
    92
  ///\c ANodeIt, \c BNodeIt, 
deba@2031
    93
  ///
deba@2031
    94
  ///\note If \c G it a template parameter, it should be used in this way.
deba@2031
    95
  ///\code
deba@2031
    96
  ///  BPUGRAPH_TYPEDEFS(typename G)
deba@2031
    97
  ///\endcode
deba@2031
    98
  ///
deba@2031
    99
  ///\warning There are no typedefs for the graph maps because of the lack of
deba@2031
   100
  ///template typedefs in C++.
deba@2031
   101
#define BPUGRAPH_TYPEDEFS(Graph)            \
deba@2031
   102
  UGRAPH_TYPEDEFS(Graph)                    \
deba@2286
   103
    typedef Graph::ANode ANode;             \
deba@2286
   104
    typedef Graph::BNode BNode;             \
deba@2031
   105
    typedef Graph::ANodeIt ANodeIt;	    \
deba@2031
   106
    typedef Graph::BNodeIt BNodeIt;
alpar@1756
   107
klao@946
   108
  /// \brief Function to count the items in the graph.
klao@946
   109
  ///
athos@1540
   110
  /// This function counts the items (nodes, edges etc) in the graph.
klao@946
   111
  /// The complexity of the function is O(n) because
klao@946
   112
  /// it iterates on all of the items.
klao@946
   113
deba@2020
   114
  template <typename Graph, typename Item>
klao@977
   115
  inline int countItems(const Graph& g) {
deba@2020
   116
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
klao@946
   117
    int num = 0;
klao@977
   118
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
   119
      ++num;
klao@946
   120
    }
klao@946
   121
    return num;
klao@946
   122
  }
klao@946
   123
klao@977
   124
  // Node counting:
klao@977
   125
deba@2020
   126
  namespace _graph_utils_bits {
deba@2020
   127
    
deba@2020
   128
    template <typename Graph, typename Enable = void>
deba@2020
   129
    struct CountNodesSelector {
deba@2020
   130
      static int count(const Graph &g) {
deba@2020
   131
        return countItems<Graph, typename Graph::Node>(g);
deba@2020
   132
      }
deba@2020
   133
    };
klao@977
   134
deba@2020
   135
    template <typename Graph>
deba@2020
   136
    struct CountNodesSelector<
deba@2020
   137
      Graph, typename 
deba@2020
   138
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2020
   139
    {
deba@2020
   140
      static int count(const Graph &g) {
deba@2020
   141
        return g.nodeNum();
deba@2020
   142
      }
deba@2020
   143
    };    
klao@977
   144
  }
klao@977
   145
klao@946
   146
  /// \brief Function to count the nodes in the graph.
klao@946
   147
  ///
klao@946
   148
  /// This function counts the nodes in the graph.
klao@946
   149
  /// The complexity of the function is O(n) but for some
athos@1526
   150
  /// graph structures it is specialized to run in O(1).
klao@977
   151
  ///
klao@977
   152
  /// \todo refer how to specialize it
klao@946
   153
klao@946
   154
  template <typename Graph>
klao@977
   155
  inline int countNodes(const Graph& g) {
deba@2020
   156
    return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
klao@977
   157
  }
klao@977
   158
deba@2029
   159
  namespace _graph_utils_bits {
deba@2029
   160
    
deba@2029
   161
    template <typename Graph, typename Enable = void>
deba@2029
   162
    struct CountANodesSelector {
deba@2029
   163
      static int count(const Graph &g) {
deba@2029
   164
        return countItems<Graph, typename Graph::ANode>(g);
deba@2029
   165
      }
deba@2029
   166
    };
deba@2029
   167
deba@2029
   168
    template <typename Graph>
deba@2029
   169
    struct CountANodesSelector<
deba@2029
   170
      Graph, typename 
deba@2029
   171
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2029
   172
    {
deba@2029
   173
      static int count(const Graph &g) {
deba@2186
   174
        return g.aNodeNum();
deba@2029
   175
      }
deba@2029
   176
    };    
deba@2029
   177
  }
deba@2029
   178
deba@2029
   179
  /// \brief Function to count the anodes in the graph.
deba@2029
   180
  ///
deba@2029
   181
  /// This function counts the anodes in the graph.
deba@2029
   182
  /// The complexity of the function is O(an) but for some
deba@2029
   183
  /// graph structures it is specialized to run in O(1).
deba@2029
   184
  ///
deba@2029
   185
  /// \todo refer how to specialize it
deba@2029
   186
deba@2029
   187
  template <typename Graph>
deba@2029
   188
  inline int countANodes(const Graph& g) {
deba@2029
   189
    return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
deba@2029
   190
  }
deba@2029
   191
deba@2029
   192
  namespace _graph_utils_bits {
deba@2029
   193
    
deba@2029
   194
    template <typename Graph, typename Enable = void>
deba@2029
   195
    struct CountBNodesSelector {
deba@2029
   196
      static int count(const Graph &g) {
deba@2029
   197
        return countItems<Graph, typename Graph::BNode>(g);
deba@2029
   198
      }
deba@2029
   199
    };
deba@2029
   200
deba@2029
   201
    template <typename Graph>
deba@2029
   202
    struct CountBNodesSelector<
deba@2029
   203
      Graph, typename 
deba@2029
   204
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2029
   205
    {
deba@2029
   206
      static int count(const Graph &g) {
deba@2186
   207
        return g.bNodeNum();
deba@2029
   208
      }
deba@2029
   209
    };    
deba@2029
   210
  }
deba@2029
   211
deba@2029
   212
  /// \brief Function to count the bnodes in the graph.
deba@2029
   213
  ///
deba@2029
   214
  /// This function counts the bnodes in the graph.
deba@2029
   215
  /// The complexity of the function is O(bn) but for some
deba@2029
   216
  /// graph structures it is specialized to run in O(1).
deba@2029
   217
  ///
deba@2029
   218
  /// \todo refer how to specialize it
deba@2029
   219
deba@2029
   220
  template <typename Graph>
deba@2029
   221
  inline int countBNodes(const Graph& g) {
deba@2029
   222
    return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
deba@2029
   223
  }
deba@2029
   224
deba@2020
   225
klao@977
   226
  // Edge counting:
klao@977
   227
deba@2020
   228
  namespace _graph_utils_bits {
deba@2020
   229
    
deba@2020
   230
    template <typename Graph, typename Enable = void>
deba@2020
   231
    struct CountEdgesSelector {
deba@2020
   232
      static int count(const Graph &g) {
deba@2020
   233
        return countItems<Graph, typename Graph::Edge>(g);
deba@2020
   234
      }
deba@2020
   235
    };
klao@977
   236
deba@2020
   237
    template <typename Graph>
deba@2020
   238
    struct CountEdgesSelector<
deba@2020
   239
      Graph, 
deba@2020
   240
      typename enable_if<typename Graph::EdgeNumTag, void>::type> 
deba@2020
   241
    {
deba@2020
   242
      static int count(const Graph &g) {
deba@2020
   243
        return g.edgeNum();
deba@2020
   244
      }
deba@2020
   245
    };    
klao@946
   246
  }
klao@946
   247
klao@946
   248
  /// \brief Function to count the edges in the graph.
klao@946
   249
  ///
klao@946
   250
  /// This function counts the edges in the graph.
klao@946
   251
  /// The complexity of the function is O(e) but for some
athos@1526
   252
  /// graph structures it is specialized to run in O(1).
klao@977
   253
klao@946
   254
  template <typename Graph>
klao@977
   255
  inline int countEdges(const Graph& g) {
deba@2020
   256
    return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
klao@946
   257
  }
klao@946
   258
klao@1053
   259
  // Undirected edge counting:
deba@2020
   260
  namespace _graph_utils_bits {
deba@2020
   261
    
deba@2020
   262
    template <typename Graph, typename Enable = void>
deba@2020
   263
    struct CountUEdgesSelector {
deba@2020
   264
      static int count(const Graph &g) {
deba@2020
   265
        return countItems<Graph, typename Graph::UEdge>(g);
deba@2020
   266
      }
deba@2020
   267
    };
klao@1053
   268
deba@2020
   269
    template <typename Graph>
deba@2020
   270
    struct CountUEdgesSelector<
deba@2020
   271
      Graph, 
deba@2020
   272
      typename enable_if<typename Graph::EdgeNumTag, void>::type> 
deba@2020
   273
    {
deba@2020
   274
      static int count(const Graph &g) {
deba@2020
   275
        return g.uEdgeNum();
deba@2020
   276
      }
deba@2020
   277
    };    
klao@1053
   278
  }
klao@1053
   279
athos@1526
   280
  /// \brief Function to count the undirected edges in the graph.
klao@946
   281
  ///
athos@1526
   282
  /// This function counts the undirected edges in the graph.
klao@946
   283
  /// The complexity of the function is O(e) but for some
athos@1540
   284
  /// graph structures it is specialized to run in O(1).
klao@1053
   285
klao@946
   286
  template <typename Graph>
klao@1909
   287
  inline int countUEdges(const Graph& g) {
deba@2020
   288
    return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
deba@2020
   289
klao@946
   290
  }
klao@946
   291
klao@977
   292
klao@946
   293
  template <typename Graph, typename DegIt>
klao@946
   294
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   295
    int num = 0;
klao@946
   296
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   297
      ++num;
klao@946
   298
    }
klao@946
   299
    return num;
klao@946
   300
  }
alpar@967
   301
deba@1531
   302
  /// \brief Function to count the number of the out-edges from node \c n.
deba@1531
   303
  ///
deba@1531
   304
  /// This function counts the number of the out-edges from node \c n
deba@1531
   305
  /// in the graph.  
deba@1531
   306
  template <typename Graph>
deba@1531
   307
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   308
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
deba@1531
   309
  }
deba@1531
   310
deba@1531
   311
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1531
   312
  ///
deba@1531
   313
  /// This function counts the number of the in-edges to node \c n
deba@1531
   314
  /// in the graph.  
deba@1531
   315
  template <typename Graph>
deba@1531
   316
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   317
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
deba@1531
   318
  }
deba@1531
   319
deba@1704
   320
  /// \brief Function to count the number of the inc-edges to node \c n.
deba@1679
   321
  ///
deba@1704
   322
  /// This function counts the number of the inc-edges to node \c n
deba@1679
   323
  /// in the graph.  
deba@1679
   324
  template <typename Graph>
deba@1679
   325
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1679
   326
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
deba@1679
   327
  }
deba@1679
   328
deba@2020
   329
  namespace _graph_utils_bits {
deba@2020
   330
    
deba@2020
   331
    template <typename Graph, typename Enable = void>
deba@2020
   332
    struct FindEdgeSelector {
deba@2020
   333
      typedef typename Graph::Node Node;
deba@2020
   334
      typedef typename Graph::Edge Edge;
deba@2020
   335
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
deba@2020
   336
        if (e == INVALID) {
deba@2020
   337
          g.firstOut(e, u);
deba@2020
   338
        } else {
deba@2020
   339
          g.nextOut(e);
deba@2020
   340
        }
deba@2020
   341
        while (e != INVALID && g.target(e) != v) {
deba@2020
   342
          g.nextOut(e);
deba@2020
   343
        }
deba@2020
   344
        return e;
deba@2020
   345
      }
deba@2020
   346
    };
deba@1531
   347
deba@2020
   348
    template <typename Graph>
deba@2020
   349
    struct FindEdgeSelector<
deba@2020
   350
      Graph, 
deba@2020
   351
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
deba@2020
   352
    {
deba@2020
   353
      typedef typename Graph::Node Node;
deba@2020
   354
      typedef typename Graph::Edge Edge;
deba@2020
   355
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
deba@2020
   356
        return g.findEdge(u, v, prev);
deba@2020
   357
      }
deba@2020
   358
    };    
deba@1565
   359
  }
deba@1565
   360
deba@1565
   361
  /// \brief Finds an edge between two nodes of a graph.
deba@1565
   362
  ///
alpar@967
   363
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   364
  ///
alpar@967
   365
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   366
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   367
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   368
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   369
  ///
alpar@967
   370
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   371
  ///\code
alpar@967
   372
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   373
  ///   ...
alpar@967
   374
  /// }
alpar@1946
   375
  ///\endcode
alpar@2155
   376
  ///
alpar@2235
   377
  ///\sa EdgeLookUp
alpar@2235
   378
  ///\se AllEdgeLookup
alpar@2155
   379
  ///\sa ConEdgeIt
alpar@967
   380
  template <typename Graph>
deba@2286
   381
  inline typename Graph::Edge 
deba@2286
   382
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@2286
   383
           typename Graph::Edge prev = INVALID) {
deba@2020
   384
    return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
alpar@967
   385
  }
deba@1531
   386
deba@1565
   387
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   388
  ///
deba@1565
   389
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   390
  /// higher level interface for the findEdge() function. You can
alpar@1591
   391
  /// use it the following way:
alpar@1946
   392
  ///\code
deba@1565
   393
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   394
  ///   ...
deba@1565
   395
  /// }
alpar@1946
   396
  ///\endcode
alpar@2155
   397
  /// 
alpar@2155
   398
  ///\sa findEdge()
alpar@2235
   399
  ///\sa EdgeLookUp
alpar@2350
   400
  ///\sa AllEdgeLookup
deba@1565
   401
  ///
deba@1565
   402
  /// \author Balazs Dezso 
deba@1565
   403
  template <typename _Graph>
deba@1565
   404
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   405
  public:
deba@1565
   406
deba@1565
   407
    typedef _Graph Graph;
deba@1565
   408
    typedef typename Graph::Edge Parent;
deba@1565
   409
deba@1565
   410
    typedef typename Graph::Edge Edge;
deba@1565
   411
    typedef typename Graph::Node Node;
deba@1565
   412
deba@1565
   413
    /// \brief Constructor.
deba@1565
   414
    ///
deba@1565
   415
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   416
    /// connects the \c u and \c v node.
deba@1565
   417
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   418
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   419
    }
deba@1565
   420
deba@1565
   421
    /// \brief Constructor.
deba@1565
   422
    ///
deba@1565
   423
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   424
    /// the \c e edge.
deba@1565
   425
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   426
    
deba@1565
   427
    /// \brief Increment operator.
deba@1565
   428
    ///
deba@1565
   429
    /// It increments the iterator and gives back the next edge.
deba@1565
   430
    ConEdgeIt& operator++() {
deba@1565
   431
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   432
				 graph.target(*this), *this));
deba@1565
   433
      return *this;
deba@1565
   434
    }
deba@1565
   435
  private:
deba@1565
   436
    const Graph& graph;
deba@1565
   437
  };
deba@1565
   438
deba@2020
   439
  namespace _graph_utils_bits {
deba@2020
   440
    
deba@2020
   441
    template <typename Graph, typename Enable = void>
deba@2020
   442
    struct FindUEdgeSelector {
deba@2020
   443
      typedef typename Graph::Node Node;
deba@2020
   444
      typedef typename Graph::UEdge UEdge;
deba@2020
   445
      static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
deba@2020
   446
        bool b;
deba@2020
   447
        if (u != v) {
deba@2020
   448
          if (e == INVALID) {
deba@2031
   449
            g.firstInc(e, b, u);
deba@2020
   450
          } else {
deba@2020
   451
            b = g.source(e) == u;
deba@2020
   452
            g.nextInc(e, b);
deba@2020
   453
          }
deba@2064
   454
          while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
deba@2020
   455
            g.nextInc(e, b);
deba@2020
   456
          }
deba@2020
   457
        } else {
deba@2020
   458
          if (e == INVALID) {
deba@2031
   459
            g.firstInc(e, b, u);
deba@2020
   460
          } else {
deba@2020
   461
            b = true;
deba@2020
   462
            g.nextInc(e, b);
deba@2020
   463
          }
deba@2020
   464
          while (e != INVALID && (!b || g.target(e) != v)) {
deba@2020
   465
            g.nextInc(e, b);
deba@2020
   466
          }
deba@2020
   467
        }
deba@2020
   468
        return e;
deba@2020
   469
      }
deba@2020
   470
    };
deba@1704
   471
deba@2020
   472
    template <typename Graph>
deba@2020
   473
    struct FindUEdgeSelector<
deba@2020
   474
      Graph, 
deba@2020
   475
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
deba@2020
   476
    {
deba@2020
   477
      typedef typename Graph::Node Node;
deba@2020
   478
      typedef typename Graph::UEdge UEdge;
deba@2020
   479
      static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
deba@2020
   480
        return g.findUEdge(u, v, prev);
deba@2020
   481
      }
deba@2020
   482
    };    
deba@1704
   483
  }
deba@1704
   484
klao@1909
   485
  /// \brief Finds an uedge between two nodes of a graph.
deba@1704
   486
  ///
klao@1909
   487
  /// Finds an uedge from node \c u to node \c v in graph \c g.
deba@2020
   488
  /// If the node \c u and node \c v is equal then each loop edge
deba@2020
   489
  /// will be enumerated.
deba@1704
   490
  ///
deba@1704
   491
  /// If \c prev is \ref INVALID (this is the default value), then
deba@1704
   492
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
deba@1704
   493
  /// the next edge from \c u to \c v after \c prev.
deba@1704
   494
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@1704
   495
  ///
deba@1704
   496
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   497
  ///\code
klao@1909
   498
  /// for(UEdge e = findUEdge(g,u,v); e != INVALID; 
klao@1909
   499
  ///     e = findUEdge(g,u,v,e)) {
deba@1704
   500
  ///   ...
deba@1704
   501
  /// }
alpar@1946
   502
  ///\endcode
alpar@2155
   503
  ///
alpar@2155
   504
  ///\sa ConEdgeIt
alpar@2155
   505
deba@1704
   506
  template <typename Graph>
deba@2286
   507
  inline typename Graph::UEdge 
deba@2286
   508
  findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@2286
   509
            typename Graph::UEdge p = INVALID) {
deba@2031
   510
    return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
deba@1704
   511
  }
deba@1704
   512
klao@1909
   513
  /// \brief Iterator for iterating on uedges connected the same nodes.
deba@1704
   514
  ///
klao@1909
   515
  /// Iterator for iterating on uedges connected the same nodes. It is 
klao@1909
   516
  /// higher level interface for the findUEdge() function. You can
deba@1704
   517
  /// use it the following way:
alpar@1946
   518
  ///\code
klao@1909
   519
  /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1704
   520
  ///   ...
deba@1704
   521
  /// }
alpar@1946
   522
  ///\endcode
deba@1704
   523
  ///
alpar@2155
   524
  ///\sa findUEdge()
alpar@2155
   525
  ///
deba@1704
   526
  /// \author Balazs Dezso 
deba@1704
   527
  template <typename _Graph>
klao@1909
   528
  class ConUEdgeIt : public _Graph::UEdge {
deba@1704
   529
  public:
deba@1704
   530
deba@1704
   531
    typedef _Graph Graph;
klao@1909
   532
    typedef typename Graph::UEdge Parent;
deba@1704
   533
klao@1909
   534
    typedef typename Graph::UEdge UEdge;
deba@1704
   535
    typedef typename Graph::Node Node;
deba@1704
   536
deba@1704
   537
    /// \brief Constructor.
deba@1704
   538
    ///
klao@1909
   539
    /// Construct a new ConUEdgeIt iterating on the edges which
deba@1704
   540
    /// connects the \c u and \c v node.
klao@1909
   541
    ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
klao@1909
   542
      Parent::operator=(findUEdge(graph, u, v));
deba@1704
   543
    }
deba@1704
   544
deba@1704
   545
    /// \brief Constructor.
deba@1704
   546
    ///
klao@1909
   547
    /// Construct a new ConUEdgeIt which continues the iterating from 
deba@1704
   548
    /// the \c e edge.
klao@1909
   549
    ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
deba@1704
   550
    
deba@1704
   551
    /// \brief Increment operator.
deba@1704
   552
    ///
deba@1704
   553
    /// It increments the iterator and gives back the next edge.
klao@1909
   554
    ConUEdgeIt& operator++() {
klao@1909
   555
      Parent::operator=(findUEdge(graph, graph.source(*this), 
deba@1829
   556
				      graph.target(*this), *this));
deba@1704
   557
      return *this;
deba@1704
   558
    }
deba@1704
   559
  private:
deba@1704
   560
    const Graph& graph;
deba@1704
   561
  };
deba@1704
   562
athos@1540
   563
  /// \brief Copy a map.
alpar@964
   564
  ///
alpar@1547
   565
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   566
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   567
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   568
  template <typename Target, typename Source, 
deba@1531
   569
	    typename ItemIt, typename Ref>	    
deba@1531
   570
  void copyMap(Target& target, const Source& source, 
deba@1531
   571
	       ItemIt it, const Ref& ref) {
deba@1531
   572
    for (; it != INVALID; ++it) {
deba@1531
   573
      target[ref[it]] = source[it];
klao@946
   574
    }
klao@946
   575
  }
klao@946
   576
deba@1531
   577
  /// \brief Copy the source map to the target map.
deba@1531
   578
  ///
deba@1531
   579
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   580
  /// to iterate on the data structure.
deba@1830
   581
  template <typename Target, typename Source, typename ItemIt>	    
deba@1531
   582
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   583
    for (; it != INVALID; ++it) {
deba@1531
   584
      target[it] = source[it];
klao@946
   585
    }
klao@946
   586
  }
klao@946
   587
deba@2286
   588
  namespace _graph_utils_bits {
deba@2286
   589
deba@2286
   590
    template <typename Graph, typename Item, typename RefMap>
deba@2286
   591
    class MapCopyBase {
deba@2286
   592
    public:
deba@2286
   593
      virtual void copy(const Graph& source, const RefMap& refMap) = 0;
deba@2286
   594
      
deba@2286
   595
      virtual ~MapCopyBase() {}
deba@2286
   596
    };
deba@2286
   597
deba@2286
   598
    template <typename Graph, typename Item, typename RefMap, 
deba@2286
   599
              typename TargetMap, typename SourceMap>
deba@2286
   600
    class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   601
    public:
deba@2286
   602
deba@2286
   603
      MapCopy(TargetMap& tmap, const SourceMap& map) 
deba@2286
   604
        : _tmap(tmap), _map(map) {}
deba@2286
   605
      
deba@2286
   606
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   607
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   608
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   609
          _tmap.set(refMap[it], _map[it]);
deba@2286
   610
        }
deba@2286
   611
      }
deba@2286
   612
deba@2286
   613
    private:
deba@2286
   614
      TargetMap& _tmap;
deba@2286
   615
      const SourceMap& _map;
deba@2286
   616
    };
deba@2286
   617
deba@2290
   618
    template <typename Graph, typename Item, typename RefMap, typename It>
deba@2290
   619
    class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2290
   620
    public:
deba@2290
   621
deba@2290
   622
      ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
deba@2290
   623
      
deba@2290
   624
      virtual void copy(const Graph&, const RefMap& refMap) {
deba@2290
   625
        _it = refMap[_item];
deba@2290
   626
      }
deba@2290
   627
deba@2290
   628
    private:
deba@2290
   629
      It& _it;
deba@2290
   630
      Item _item;
deba@2290
   631
    };
deba@2290
   632
deba@2286
   633
    template <typename Graph, typename Item, typename RefMap, typename Ref>
deba@2286
   634
    class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   635
    public:
deba@2286
   636
deba@2286
   637
      RefCopy(Ref& map) : _map(map) {}
deba@2286
   638
      
deba@2286
   639
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   640
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   641
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   642
          _map.set(it, refMap[it]);
deba@2286
   643
        }
deba@2286
   644
      }
deba@2286
   645
deba@2286
   646
    private:
deba@2286
   647
      Ref& _map;
deba@2286
   648
    };
deba@2286
   649
deba@2286
   650
    template <typename Graph, typename Item, typename RefMap, 
deba@2286
   651
              typename CrossRef>
deba@2286
   652
    class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   653
    public:
deba@2286
   654
deba@2286
   655
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
deba@2286
   656
      
deba@2286
   657
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   658
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   659
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   660
          _cmap.set(refMap[it], it);
deba@2286
   661
        }
deba@2286
   662
      }
deba@2286
   663
deba@2286
   664
    private:
deba@2286
   665
      CrossRef& _cmap;
deba@2286
   666
    };
deba@2286
   667
deba@2290
   668
    template <typename Graph, typename Enable = void>
deba@2290
   669
    struct GraphCopySelector {
deba@2290
   670
      template <typename Source, typename NodeRefMap, typename EdgeRefMap>
deba@2290
   671
      static void copy(Graph &target, const Source& source,
deba@2290
   672
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@2290
   673
        for (typename Source::NodeIt it(source); it != INVALID; ++it) {
deba@2290
   674
          nodeRefMap[it] = target.addNode();
deba@2290
   675
        }
deba@2290
   676
        for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
deba@2290
   677
          edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@2290
   678
                                          nodeRefMap[source.target(it)]);
deba@2290
   679
        }
deba@2290
   680
      }
deba@2290
   681
    };
deba@2290
   682
deba@2290
   683
    template <typename Graph>
deba@2290
   684
    struct GraphCopySelector<
deba@2290
   685
      Graph, 
deba@2329
   686
      typename enable_if<typename Graph::BuildTag, void>::type> 
deba@2290
   687
    {
deba@2290
   688
      template <typename Source, typename NodeRefMap, typename EdgeRefMap>
deba@2290
   689
      static void copy(Graph &target, const Source& source,
deba@2290
   690
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@2329
   691
        target.build(source, nodeRefMap, edgeRefMap);
deba@2290
   692
      }
deba@2290
   693
    };
deba@2290
   694
deba@2290
   695
    template <typename UGraph, typename Enable = void>
deba@2290
   696
    struct UGraphCopySelector {
deba@2290
   697
      template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
deba@2290
   698
      static void copy(UGraph &target, const Source& source,
deba@2290
   699
                       NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
deba@2290
   700
        for (typename Source::NodeIt it(source); it != INVALID; ++it) {
deba@2290
   701
          nodeRefMap[it] = target.addNode();
deba@2290
   702
        }
deba@2290
   703
        for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
deba@2290
   704
          uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@2290
   705
                                          nodeRefMap[source.target(it)]);
deba@2290
   706
        }
deba@2290
   707
      }
deba@2290
   708
    };
deba@2290
   709
deba@2290
   710
    template <typename UGraph>
deba@2290
   711
    struct UGraphCopySelector<
deba@2290
   712
      UGraph, 
deba@2329
   713
      typename enable_if<typename UGraph::BuildTag, void>::type> 
deba@2290
   714
    {
deba@2290
   715
      template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
deba@2290
   716
      static void copy(UGraph &target, const Source& source,
deba@2290
   717
                       NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
deba@2329
   718
        target.build(source, nodeRefMap, uEdgeRefMap);
deba@2290
   719
      }
deba@2290
   720
    };
deba@2290
   721
deba@2290
   722
    template <typename BpUGraph, typename Enable = void>
deba@2290
   723
    struct BpUGraphCopySelector {
deba@2290
   724
      template <typename Source, typename ANodeRefMap, 
deba@2290
   725
                typename BNodeRefMap, typename UEdgeRefMap>
deba@2290
   726
      static void copy(BpUGraph &target, const Source& source,
deba@2290
   727
                       ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
deba@2290
   728
                       UEdgeRefMap& uEdgeRefMap) {
deba@2290
   729
        for (typename Source::ANodeIt it(source); it != INVALID; ++it) {
deba@2290
   730
          aNodeRefMap[it] = target.addANode();
deba@2290
   731
        }
deba@2290
   732
        for (typename Source::BNodeIt it(source); it != INVALID; ++it) {
deba@2290
   733
          bNodeRefMap[it] = target.addBNode();
deba@2290
   734
        }
deba@2290
   735
        for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
deba@2290
   736
          uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)], 
deba@2290
   737
                                           bNodeRefMap[source.bNode(it)]);
deba@2290
   738
        }
deba@2290
   739
      }
deba@2290
   740
    };
deba@2290
   741
deba@2290
   742
    template <typename BpUGraph>
deba@2290
   743
    struct BpUGraphCopySelector<
deba@2290
   744
      BpUGraph, 
deba@2329
   745
      typename enable_if<typename BpUGraph::BuildTag, void>::type> 
deba@2290
   746
    {
deba@2290
   747
      template <typename Source, typename ANodeRefMap, 
deba@2290
   748
                typename BNodeRefMap, typename UEdgeRefMap>
deba@2290
   749
      static void copy(BpUGraph &target, const Source& source,
deba@2290
   750
                       ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
deba@2290
   751
                       UEdgeRefMap& uEdgeRefMap) {
deba@2329
   752
        target.build(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
deba@2290
   753
      }
deba@2290
   754
    };
deba@2290
   755
    
deba@2290
   756
deba@2286
   757
  }
deba@2286
   758
athos@1540
   759
  /// \brief Class to copy a graph.
deba@1531
   760
  ///
alpar@2006
   761
  /// Class to copy a graph to another graph (duplicate a graph). The
athos@1540
   762
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   763
  template <typename Target, typename Source>
deba@1267
   764
  class GraphCopy {
deba@2286
   765
  private:
deba@2286
   766
deba@1531
   767
    typedef typename Source::Node Node;
deba@1531
   768
    typedef typename Source::NodeIt NodeIt;
deba@1531
   769
    typedef typename Source::Edge Edge;
deba@1531
   770
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   771
deba@2286
   772
    typedef typename Target::Node TNode;
deba@2286
   773
    typedef typename Target::Edge TEdge;
deba@2286
   774
deba@2286
   775
    typedef typename Source::template NodeMap<TNode> NodeRefMap;
deba@2286
   776
    typedef typename Source::template EdgeMap<TEdge> EdgeRefMap;
deba@2286
   777
    
deba@2286
   778
    
deba@2286
   779
  public: 
deba@2286
   780
klao@946
   781
deba@1531
   782
    /// \brief Constructor for the GraphCopy.
deba@1531
   783
    ///
deba@1531
   784
    /// It copies the content of the \c _source graph into the
deba@2286
   785
    /// \c _target graph.
deba@1531
   786
    GraphCopy(Target& _target, const Source& _source) 
deba@2286
   787
      : source(_source), target(_target) {}
deba@2286
   788
deba@2286
   789
    /// \brief Destructor of the GraphCopy
deba@2286
   790
    ///
deba@2286
   791
    /// Destructor of the GraphCopy
deba@2286
   792
    ~GraphCopy() {
deba@2386
   793
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
   794
        delete nodeMapCopies[i];
deba@1531
   795
      }
deba@2386
   796
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
   797
        delete edgeMapCopies[i];
deba@1531
   798
      }
deba@2286
   799
deba@1267
   800
    }
klao@946
   801
deba@1531
   802
    /// \brief Copies the node references into the given map.
deba@1531
   803
    ///
deba@1531
   804
    /// Copies the node references into the given map.
deba@1531
   805
    template <typename NodeRef>
deba@2286
   806
    GraphCopy& nodeRef(NodeRef& map) {
deba@2286
   807
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
deba@2286
   808
                              NodeRefMap, NodeRef>(map));
deba@1531
   809
      return *this;
deba@1267
   810
    }
deba@1531
   811
deba@2290
   812
    /// \brief Copies the node cross references into the given map.
deba@1531
   813
    ///
deba@2290
   814
    ///  Copies the node cross references (reverse references) into
deba@2290
   815
    ///  the given map.
deba@2286
   816
    template <typename NodeCrossRef>
deba@2286
   817
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2286
   818
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
deba@2286
   819
                              NodeRefMap, NodeCrossRef>(map));
deba@1531
   820
      return *this;
deba@1531
   821
    }
deba@1531
   822
deba@1531
   823
    /// \brief Make copy of the given map.
deba@1531
   824
    ///
deba@1531
   825
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   826
    /// The new map's key type is the target graph's node type,
deba@1531
   827
    /// and the copied map's key type is the source graph's node
deba@1531
   828
    /// type.  
deba@1531
   829
    template <typename TargetMap, typename SourceMap>
deba@2286
   830
    GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
   831
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
deba@2286
   832
                              NodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2286
   833
      return *this;
deba@2286
   834
    }
deba@2286
   835
deba@2290
   836
    /// \brief Make a copy of the given node.
deba@2290
   837
    ///
deba@2290
   838
    /// Make a copy of the given node.
deba@2386
   839
    GraphCopy& node(TNode& tnode, const Node& snode) {
deba@2290
   840
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2386
   841
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
   842
      return *this;
deba@2290
   843
    }
deba@2290
   844
deba@2286
   845
    /// \brief Copies the edge references into the given map.
deba@2286
   846
    ///
deba@2286
   847
    /// Copies the edge references into the given map.
deba@2286
   848
    template <typename EdgeRef>
deba@2286
   849
    GraphCopy& edgeRef(EdgeRef& map) {
deba@2286
   850
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
deba@2286
   851
                              EdgeRefMap, EdgeRef>(map));
deba@2286
   852
      return *this;
deba@2286
   853
    }
deba@2286
   854
deba@2290
   855
    /// \brief Copies the edge cross references into the given map.
deba@2286
   856
    ///
deba@2290
   857
    ///  Copies the edge cross references (reverse references) into
deba@2290
   858
    ///  the given map.
deba@2286
   859
    template <typename EdgeCrossRef>
deba@2286
   860
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2286
   861
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
deba@2286
   862
                              EdgeRefMap, EdgeCrossRef>(map));
deba@1531
   863
      return *this;
deba@1531
   864
    }
deba@1531
   865
deba@1531
   866
    /// \brief Make copy of the given map.
deba@1531
   867
    ///
deba@1531
   868
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   869
    /// The new map's key type is the target graph's edge type,
deba@1531
   870
    /// and the copied map's key type is the source graph's edge
deba@1531
   871
    /// type.  
deba@1531
   872
    template <typename TargetMap, typename SourceMap>
deba@2286
   873
    GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
   874
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
deba@2286
   875
                              EdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@1531
   876
      return *this;
deba@1531
   877
    }
deba@1531
   878
deba@2290
   879
    /// \brief Make a copy of the given edge.
deba@2290
   880
    ///
deba@2290
   881
    /// Make a copy of the given edge.
deba@2386
   882
    GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2290
   883
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2386
   884
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
   885
      return *this;
deba@2290
   886
    }
deba@2290
   887
deba@2286
   888
    /// \brief Executes the copies.
deba@1531
   889
    ///
deba@2286
   890
    /// Executes the copies.
deba@2286
   891
    void run() {
deba@2286
   892
      NodeRefMap nodeRefMap(source);
deba@2290
   893
      EdgeRefMap edgeRefMap(source);
deba@2290
   894
      _graph_utils_bits::GraphCopySelector<Target>::
deba@2290
   895
        copy(target, source, nodeRefMap, edgeRefMap);
deba@2386
   896
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
   897
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2286
   898
      }
deba@2386
   899
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
   900
        edgeMapCopies[i]->copy(source, edgeRefMap);
deba@2290
   901
      }      
deba@1531
   902
    }
deba@1531
   903
deba@2290
   904
  protected:
deba@2290
   905
deba@2290
   906
deba@1531
   907
    const Source& source;
deba@1531
   908
    Target& target;
deba@1531
   909
deba@2286
   910
    std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
deba@2286
   911
    nodeMapCopies;
deba@2286
   912
deba@2286
   913
    std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
deba@2286
   914
    edgeMapCopies;
deba@2286
   915
deba@1267
   916
  };
klao@946
   917
alpar@2006
   918
  /// \brief Copy a graph to another graph.
deba@1531
   919
  ///
alpar@2006
   920
  /// Copy a graph to another graph.
deba@1531
   921
  /// The usage of the function:
deba@1531
   922
  /// 
alpar@1946
   923
  ///\code
deba@2286
   924
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
alpar@1946
   925
  ///\endcode
deba@1531
   926
  /// 
deba@1531
   927
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   928
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   929
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   930
  /// edges.
deba@2290
   931
  ///
deba@2290
   932
  /// \see GraphCopy 
deba@1531
   933
  template <typename Target, typename Source>
deba@1531
   934
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   935
    return GraphCopy<Target, Source>(target, source);
deba@1531
   936
  }
klao@946
   937
deba@1720
   938
  /// \brief Class to copy an undirected graph.
deba@1720
   939
  ///
alpar@2006
   940
  /// Class to copy an undirected graph to another graph (duplicate a graph).
klao@1909
   941
  /// The simplest way of using it is through the \c copyUGraph() function.
deba@1720
   942
  template <typename Target, typename Source>
klao@1909
   943
  class UGraphCopy {
deba@2286
   944
  private:
deba@2286
   945
deba@1720
   946
    typedef typename Source::Node Node;
deba@1720
   947
    typedef typename Source::NodeIt NodeIt;
deba@1720
   948
    typedef typename Source::Edge Edge;
deba@1720
   949
    typedef typename Source::EdgeIt EdgeIt;
klao@1909
   950
    typedef typename Source::UEdge UEdge;
klao@1909
   951
    typedef typename Source::UEdgeIt UEdgeIt;
deba@1720
   952
deba@2286
   953
    typedef typename Target::Node TNode;
deba@2286
   954
    typedef typename Target::Edge TEdge;
deba@2286
   955
    typedef typename Target::UEdge TUEdge;
deba@1720
   956
deba@2286
   957
    typedef typename Source::template NodeMap<TNode> NodeRefMap;
deba@2286
   958
    typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
deba@1720
   959
deba@1720
   960
    struct EdgeRefMap {
deba@2286
   961
      EdgeRefMap(const Target& _target, const Source& _source,
deba@2286
   962
                 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
deba@2286
   963
        : target(_target), source(_source), 
deba@2286
   964
          uedge_ref(_uedge_ref), node_ref(_node_ref) {}
deba@2286
   965
deba@1720
   966
      typedef typename Source::Edge Key;
deba@1720
   967
      typedef typename Target::Edge Value;
deba@1720
   968
deba@2286
   969
      Value operator[](const Key& key) const {
deba@2386
   970
        bool forward = 
deba@2386
   971
          (source.direction(key) == 
deba@2386
   972
           (node_ref[source.source(static_cast<const UEdge&>(key))] == 
deba@2386
   973
            target.source(uedge_ref[static_cast<const UEdge&>(key)])));
deba@2286
   974
	return target.direct(uedge_ref[key], forward); 
deba@1720
   975
      }
deba@1720
   976
      
deba@2286
   977
      const Target& target;
deba@2286
   978
      const Source& source;
deba@2286
   979
      const UEdgeRefMap& uedge_ref;
deba@2286
   980
      const NodeRefMap& node_ref;
deba@1720
   981
    };
deba@2286
   982
deba@1720
   983
    
deba@2286
   984
  public: 
deba@1720
   985
deba@2286
   986
deba@2286
   987
    /// \brief Constructor for the GraphCopy.
deba@1720
   988
    ///
deba@1720
   989
    /// It copies the content of the \c _source graph into the
deba@2286
   990
    /// \c _target graph.
klao@1909
   991
    UGraphCopy(Target& _target, const Source& _source) 
deba@2286
   992
      : source(_source), target(_target) {}
deba@2286
   993
deba@2286
   994
    /// \brief Destructor of the GraphCopy
deba@2286
   995
    ///
deba@2286
   996
    /// Destructor of the GraphCopy
deba@2286
   997
    ~UGraphCopy() {
deba@2386
   998
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
   999
        delete nodeMapCopies[i];
deba@1720
  1000
      }
deba@2386
  1001
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
  1002
        delete edgeMapCopies[i];
deba@1720
  1003
      }
deba@2386
  1004
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2286
  1005
        delete uEdgeMapCopies[i];
deba@2286
  1006
      }
deba@2286
  1007
deba@1720
  1008
    }
deba@1720
  1009
deba@1720
  1010
    /// \brief Copies the node references into the given map.
deba@1720
  1011
    ///
deba@1720
  1012
    /// Copies the node references into the given map.
deba@1720
  1013
    template <typename NodeRef>
deba@2286
  1014
    UGraphCopy& nodeRef(NodeRef& map) {
deba@2286
  1015
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
deba@2286
  1016
                              NodeRefMap, NodeRef>(map));
deba@1720
  1017
      return *this;
deba@1720
  1018
    }
deba@1720
  1019
deba@2290
  1020
    /// \brief Copies the node cross references into the given map.
deba@1720
  1021
    ///
deba@2290
  1022
    ///  Copies the node cross references (reverse references) into
deba@2290
  1023
    ///  the given map.
deba@2286
  1024
    template <typename NodeCrossRef>
deba@2286
  1025
    UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2286
  1026
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
deba@2286
  1027
                              NodeRefMap, NodeCrossRef>(map));
deba@1720
  1028
      return *this;
deba@1720
  1029
    }
deba@1720
  1030
deba@1720
  1031
    /// \brief Make copy of the given map.
deba@1720
  1032
    ///
deba@1720
  1033
    /// Makes copy of the given map for the newly created graph. 
deba@1720
  1034
    /// The new map's key type is the target graph's node type,
deba@1720
  1035
    /// and the copied map's key type is the source graph's node
deba@1720
  1036
    /// type.  
deba@1720
  1037
    template <typename TargetMap, typename SourceMap>
deba@2286
  1038
    UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1039
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
deba@2286
  1040
                              NodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2286
  1041
      return *this;
deba@2286
  1042
    }
deba@2286
  1043
deba@2290
  1044
    /// \brief Make a copy of the given node.
deba@2290
  1045
    ///
deba@2290
  1046
    /// Make a copy of the given node.
deba@2386
  1047
    UGraphCopy& node(TNode& tnode, const Node& snode) {
deba@2290
  1048
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2386
  1049
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
  1050
      return *this;
deba@2290
  1051
    }
deba@2290
  1052
deba@2286
  1053
    /// \brief Copies the edge references into the given map.
deba@2286
  1054
    ///
deba@2286
  1055
    /// Copies the edge references into the given map.
deba@2286
  1056
    template <typename EdgeRef>
deba@2286
  1057
    UGraphCopy& edgeRef(EdgeRef& map) {
deba@2286
  1058
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
deba@2286
  1059
                              EdgeRefMap, EdgeRef>(map));
deba@2286
  1060
      return *this;
deba@2286
  1061
    }
deba@2286
  1062
deba@2290
  1063
    /// \brief Copies the edge cross references into the given map.
deba@2286
  1064
    ///
deba@2290
  1065
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1066
    ///  the given map.
deba@2286
  1067
    template <typename EdgeCrossRef>
deba@2286
  1068
    UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2286
  1069
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
deba@2286
  1070
                              EdgeRefMap, EdgeCrossRef>(map));
deba@1720
  1071
      return *this;
deba@1720
  1072
    }
deba@1720
  1073
deba@1720
  1074
    /// \brief Make copy of the given map.
deba@1720
  1075
    ///
deba@1720
  1076
    /// Makes copy of the given map for the newly created graph. 
deba@1720
  1077
    /// The new map's key type is the target graph's edge type,
deba@1720
  1078
    /// and the copied map's key type is the source graph's edge
deba@1720
  1079
    /// type.  
deba@1720
  1080
    template <typename TargetMap, typename SourceMap>
deba@2286
  1081
    UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1082
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
deba@2286
  1083
                              EdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2286
  1084
      return *this;
deba@2286
  1085
    }
deba@2286
  1086
deba@2290
  1087
    /// \brief Make a copy of the given edge.
deba@2286
  1088
    ///
deba@2290
  1089
    /// Make a copy of the given edge.
deba@2386
  1090
    UGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2290
  1091
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2386
  1092
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
  1093
      return *this;
deba@2290
  1094
    }
deba@2290
  1095
deba@2290
  1096
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1097
    ///
deba@2290
  1098
    /// Copies the undirected edge references into the given map.
deba@2286
  1099
    template <typename UEdgeRef>
deba@2286
  1100
    UGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2286
  1101
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
deba@2286
  1102
                               UEdgeRefMap, UEdgeRef>(map));
deba@2286
  1103
      return *this;
deba@2286
  1104
    }
deba@2286
  1105
deba@2290
  1106
    /// \brief Copies the undirected edge cross references into the given map.
deba@2286
  1107
    ///
deba@2290
  1108
    /// Copies the undirected edge cross references (reverse
deba@2290
  1109
    /// references) into the given map.
deba@2286
  1110
    template <typename UEdgeCrossRef>
deba@2286
  1111
    UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2286
  1112
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2286
  1113
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@1720
  1114
      return *this;
deba@1720
  1115
    }
deba@1720
  1116
deba@1720
  1117
    /// \brief Make copy of the given map.
deba@1720
  1118
    ///
deba@1720
  1119
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1120
    /// The new map's key type is the target graph's undirected edge type,
deba@2290
  1121
    /// and the copied map's key type is the source graph's undirected edge
deba@1720
  1122
    /// type.  
deba@1720
  1123
    template <typename TargetMap, typename SourceMap>
deba@2286
  1124
    UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1125
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
deba@2286
  1126
                               UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@1720
  1127
      return *this;
deba@1720
  1128
    }
deba@1720
  1129
deba@2290
  1130
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1131
    ///
deba@2290
  1132
    /// Make a copy of the given undirected edge.
deba@2386
  1133
    UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
deba@2290
  1134
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
deba@2386
  1135
                               UEdgeRefMap, TUEdge>(tuedge, suedge));
deba@2290
  1136
      return *this;
deba@2290
  1137
    }
deba@2290
  1138
deba@2286
  1139
    /// \brief Executes the copies.
deba@1720
  1140
    ///
deba@2286
  1141
    /// Executes the copies.
deba@2286
  1142
    void run() {
deba@2286
  1143
      NodeRefMap nodeRefMap(source);
deba@2290
  1144
      UEdgeRefMap uEdgeRefMap(source);
deba@2290
  1145
      EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
deba@2290
  1146
      _graph_utils_bits::UGraphCopySelector<Target>::
deba@2290
  1147
        copy(target, source, nodeRefMap, uEdgeRefMap);
deba@2386
  1148
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
  1149
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2286
  1150
      }
deba@2386
  1151
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2286
  1152
        uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
deba@2286
  1153
      }
deba@2386
  1154
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
  1155
        edgeMapCopies[i]->copy(source, edgeRefMap);
deba@2286
  1156
      }
deba@1720
  1157
    }
deba@1720
  1158
deba@1720
  1159
  private:
deba@1192
  1160
    
deba@1720
  1161
    const Source& source;
deba@1720
  1162
    Target& target;
alpar@947
  1163
deba@2286
  1164
    std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
deba@2286
  1165
    nodeMapCopies;
deba@2286
  1166
deba@2286
  1167
    std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
deba@2286
  1168
    edgeMapCopies;
deba@2286
  1169
deba@2286
  1170
    std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
deba@2286
  1171
    uEdgeMapCopies;
deba@2286
  1172
deba@1192
  1173
  };
deba@1192
  1174
deba@2290
  1175
  /// \brief Copy an undirected graph to another graph.
deba@1720
  1176
  ///
deba@2290
  1177
  /// Copy an undirected graph to another graph.
deba@1720
  1178
  /// The usage of the function:
deba@1720
  1179
  /// 
alpar@1946
  1180
  ///\code
deba@2286
  1181
  /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
alpar@1946
  1182
  ///\endcode
deba@1720
  1183
  /// 
deba@1720
  1184
  /// After the copy the \c nr map will contain the mapping from the
deba@1720
  1185
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@1720
  1186
  /// contain the mapping from the target graph's edges to the source's
deba@1720
  1187
  /// edges.
deba@2290
  1188
  ///
deba@2290
  1189
  /// \see UGraphCopy 
deba@1720
  1190
  template <typename Target, typename Source>
klao@1909
  1191
  UGraphCopy<Target, Source> 
klao@1909
  1192
  copyUGraph(Target& target, const Source& source) {
klao@1909
  1193
    return UGraphCopy<Target, Source>(target, source);
deba@1720
  1194
  }
deba@1192
  1195
deba@2290
  1196
  /// \brief Class to copy a bipartite undirected graph.
deba@2290
  1197
  ///
deba@2290
  1198
  /// Class to copy a bipartite undirected graph to another graph
deba@2290
  1199
  /// (duplicate a graph).  The simplest way of using it is through
deba@2290
  1200
  /// the \c copyBpUGraph() function.
deba@2290
  1201
  template <typename Target, typename Source>
deba@2290
  1202
  class BpUGraphCopy {
deba@2290
  1203
  private:
deba@2290
  1204
deba@2290
  1205
    typedef typename Source::Node Node;
deba@2290
  1206
    typedef typename Source::ANode ANode;
deba@2290
  1207
    typedef typename Source::BNode BNode;
deba@2290
  1208
    typedef typename Source::NodeIt NodeIt;
deba@2290
  1209
    typedef typename Source::Edge Edge;
deba@2290
  1210
    typedef typename Source::EdgeIt EdgeIt;
deba@2290
  1211
    typedef typename Source::UEdge UEdge;
deba@2290
  1212
    typedef typename Source::UEdgeIt UEdgeIt;
deba@2290
  1213
deba@2290
  1214
    typedef typename Target::Node TNode;
deba@2290
  1215
    typedef typename Target::Edge TEdge;
deba@2290
  1216
    typedef typename Target::UEdge TUEdge;
deba@2290
  1217
deba@2290
  1218
    typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
deba@2290
  1219
    typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
deba@2290
  1220
    typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
deba@2290
  1221
deba@2290
  1222
    struct NodeRefMap {
deba@2290
  1223
      NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
deba@2290
  1224
                 const BNodeRefMap& _bnode_ref)
deba@2290
  1225
        : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
deba@2290
  1226
deba@2290
  1227
      typedef typename Source::Node Key;
deba@2290
  1228
      typedef typename Target::Node Value;
deba@2290
  1229
deba@2290
  1230
      Value operator[](const Key& key) const {
deba@2290
  1231
	return source.aNode(key) ? anode_ref[key] : bnode_ref[key]; 
deba@2290
  1232
      }
deba@2290
  1233
      
deba@2290
  1234
      const Source& source;
deba@2290
  1235
      const ANodeRefMap& anode_ref;
deba@2290
  1236
      const BNodeRefMap& bnode_ref;
deba@2290
  1237
    };
deba@2290
  1238
deba@2290
  1239
    struct EdgeRefMap {
deba@2290
  1240
      EdgeRefMap(const Target& _target, const Source& _source,
deba@2290
  1241
                 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
deba@2290
  1242
        : target(_target), source(_source), 
deba@2290
  1243
          uedge_ref(_uedge_ref), node_ref(_node_ref) {}
deba@2290
  1244
deba@2290
  1245
      typedef typename Source::Edge Key;
deba@2290
  1246
      typedef typename Target::Edge Value;
deba@2290
  1247
deba@2290
  1248
      Value operator[](const Key& key) const {
deba@2386
  1249
        bool forward = 
deba@2386
  1250
          (source.direction(key) == 
deba@2386
  1251
           (node_ref[source.source(static_cast<const UEdge&>(key))] == 
deba@2386
  1252
            target.source(uedge_ref[static_cast<const UEdge&>(key)])));
deba@2290
  1253
	return target.direct(uedge_ref[key], forward); 
deba@2290
  1254
      }
deba@2290
  1255
      
deba@2290
  1256
      const Target& target;
deba@2290
  1257
      const Source& source;
deba@2290
  1258
      const UEdgeRefMap& uedge_ref;
deba@2290
  1259
      const NodeRefMap& node_ref;
deba@2290
  1260
    };
deba@2290
  1261
    
deba@2290
  1262
  public: 
deba@2290
  1263
deba@2290
  1264
deba@2290
  1265
    /// \brief Constructor for the GraphCopy.
deba@2290
  1266
    ///
deba@2290
  1267
    /// It copies the content of the \c _source graph into the
deba@2290
  1268
    /// \c _target graph.
deba@2290
  1269
    BpUGraphCopy(Target& _target, const Source& _source) 
deba@2290
  1270
      : source(_source), target(_target) {}
deba@2290
  1271
deba@2290
  1272
    /// \brief Destructor of the GraphCopy
deba@2290
  1273
    ///
deba@2290
  1274
    /// Destructor of the GraphCopy
deba@2290
  1275
    ~BpUGraphCopy() {
deba@2386
  1276
      for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
deba@2290
  1277
        delete aNodeMapCopies[i];
deba@2290
  1278
      }
deba@2386
  1279
      for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
deba@2290
  1280
        delete bNodeMapCopies[i];
deba@2290
  1281
      }
deba@2386
  1282
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2290
  1283
        delete nodeMapCopies[i];
deba@2290
  1284
      }
deba@2386
  1285
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2290
  1286
        delete edgeMapCopies[i];
deba@2290
  1287
      }
deba@2386
  1288
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2290
  1289
        delete uEdgeMapCopies[i];
deba@2290
  1290
      }
deba@2290
  1291
deba@2290
  1292
    }
deba@2290
  1293
deba@2290
  1294
    /// \brief Copies the A-node references into the given map.
deba@2290
  1295
    ///
deba@2290
  1296
    /// Copies the A-node references into the given map.
deba@2290
  1297
    template <typename ANodeRef>
deba@2290
  1298
    BpUGraphCopy& aNodeRef(ANodeRef& map) {
deba@2290
  1299
      aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode, 
deba@2290
  1300
                               ANodeRefMap, ANodeRef>(map));
deba@2290
  1301
      return *this;
deba@2290
  1302
    }
deba@2290
  1303
deba@2290
  1304
    /// \brief Copies the A-node cross references into the given map.
deba@2290
  1305
    ///
deba@2290
  1306
    /// Copies the A-node cross references (reverse references) into
deba@2290
  1307
    /// the given map.
deba@2290
  1308
    template <typename ANodeCrossRef>
deba@2290
  1309
    BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
deba@2290
  1310
      aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1311
                               ANode, ANodeRefMap, ANodeCrossRef>(map));
deba@2290
  1312
      return *this;
deba@2290
  1313
    }
deba@2290
  1314
deba@2290
  1315
    /// \brief Make copy of the given A-node map.
deba@2290
  1316
    ///
deba@2290
  1317
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1318
    /// The new map's key type is the target graph's node type,
deba@2290
  1319
    /// and the copied map's key type is the source graph's node
deba@2290
  1320
    /// type.  
deba@2290
  1321
    template <typename TargetMap, typename SourceMap>
deba@2290
  1322
    BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1323
      aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode, 
deba@2290
  1324
                               ANodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1325
      return *this;
deba@2290
  1326
    }
deba@2290
  1327
deba@2290
  1328
    /// \brief Copies the B-node references into the given map.
deba@2290
  1329
    ///
deba@2290
  1330
    /// Copies the B-node references into the given map.
deba@2290
  1331
    template <typename BNodeRef>
deba@2290
  1332
    BpUGraphCopy& bNodeRef(BNodeRef& map) {
deba@2290
  1333
      bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode, 
deba@2290
  1334
                               BNodeRefMap, BNodeRef>(map));
deba@2290
  1335
      return *this;
deba@2290
  1336
    }
deba@2290
  1337
deba@2290
  1338
    /// \brief Copies the B-node cross references into the given map.
deba@2290
  1339
    ///
deba@2290
  1340
    ///  Copies the B-node cross references (reverse references) into
deba@2290
  1341
    ///  the given map.
deba@2290
  1342
    template <typename BNodeCrossRef>
deba@2290
  1343
    BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
deba@2290
  1344
      bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1345
                              BNode, BNodeRefMap, BNodeCrossRef>(map));
deba@2290
  1346
      return *this;
deba@2290
  1347
    }
deba@2290
  1348
deba@2290
  1349
    /// \brief Make copy of the given B-node map.
deba@2290
  1350
    ///
deba@2290
  1351
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1352
    /// The new map's key type is the target graph's node type,
deba@2290
  1353
    /// and the copied map's key type is the source graph's node
deba@2290
  1354
    /// type.  
deba@2290
  1355
    template <typename TargetMap, typename SourceMap>
deba@2290
  1356
    BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1357
      bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode, 
deba@2290
  1358
                               BNodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1359
      return *this;
deba@2290
  1360
    }
deba@2290
  1361
    /// \brief Copies the node references into the given map.
deba@2290
  1362
    ///
deba@2290
  1363
    /// Copies the node references into the given map.
deba@2290
  1364
    template <typename NodeRef>
deba@2290
  1365
    BpUGraphCopy& nodeRef(NodeRef& map) {
deba@2290
  1366
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
deba@2290
  1367
                              NodeRefMap, NodeRef>(map));
deba@2290
  1368
      return *this;
deba@2290
  1369
    }
deba@2290
  1370
deba@2290
  1371
    /// \brief Copies the node cross references into the given map.
deba@2290
  1372
    ///
deba@2290
  1373
    ///  Copies the node cross references (reverse references) into
deba@2290
  1374
    ///  the given map.
deba@2290
  1375
    template <typename NodeCrossRef>
deba@2290
  1376
    BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2290
  1377
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
deba@2290
  1378
                              NodeRefMap, NodeCrossRef>(map));
deba@2290
  1379
      return *this;
deba@2290
  1380
    }
deba@2290
  1381
deba@2290
  1382
    /// \brief Make copy of the given map.
deba@2290
  1383
    ///
deba@2290
  1384
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1385
    /// The new map's key type is the target graph's node type,
deba@2290
  1386
    /// and the copied map's key type is the source graph's node
deba@2290
  1387
    /// type.  
deba@2290
  1388
    template <typename TargetMap, typename SourceMap>
deba@2290
  1389
    BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1390
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
deba@2290
  1391
                              NodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1392
      return *this;
deba@2290
  1393
    }
deba@2290
  1394
deba@2290
  1395
    /// \brief Make a copy of the given node.
deba@2290
  1396
    ///
deba@2290
  1397
    /// Make a copy of the given node.
deba@2386
  1398
    BpUGraphCopy& node(TNode& tnode, const Node& snode) {
deba@2290
  1399
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2386
  1400
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
  1401
      return *this;
deba@2290
  1402
    }
deba@2290
  1403
deba@2290
  1404
    /// \brief Copies the edge references into the given map.
deba@2290
  1405
    ///
deba@2290
  1406
    /// Copies the edge references into the given map.
deba@2290
  1407
    template <typename EdgeRef>
deba@2290
  1408
    BpUGraphCopy& edgeRef(EdgeRef& map) {
deba@2290
  1409
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
deba@2290
  1410
                              EdgeRefMap, EdgeRef>(map));
deba@2290
  1411
      return *this;
deba@2290
  1412
    }
deba@2290
  1413
deba@2290
  1414
    /// \brief Copies the edge cross references into the given map.
deba@2290
  1415
    ///
deba@2290
  1416
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1417
    ///  the given map.
deba@2290
  1418
    template <typename EdgeCrossRef>
deba@2290
  1419
    BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2290
  1420
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
deba@2290
  1421
                              EdgeRefMap, EdgeCrossRef>(map));
deba@2290
  1422
      return *this;
deba@2290
  1423
    }
deba@2290
  1424
deba@2290
  1425
    /// \brief Make copy of the given map.
deba@2290
  1426
    ///
deba@2290
  1427
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1428
    /// The new map's key type is the target graph's edge type,
deba@2290
  1429
    /// and the copied map's key type is the source graph's edge
deba@2290
  1430
    /// type.  
deba@2290
  1431
    template <typename TargetMap, typename SourceMap>
deba@2290
  1432
    BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1433
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
deba@2290
  1434
                              EdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1435
      return *this;
deba@2290
  1436
    }
deba@2290
  1437
deba@2290
  1438
    /// \brief Make a copy of the given edge.
deba@2290
  1439
    ///
deba@2290
  1440
    /// Make a copy of the given edge.
deba@2386
  1441
    BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2290
  1442
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2386
  1443
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
  1444
      return *this;
deba@2290
  1445
    }
deba@2290
  1446
deba@2290
  1447
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1448
    ///
deba@2290
  1449
    /// Copies the undirected edge references into the given map.
deba@2290
  1450
    template <typename UEdgeRef>
deba@2290
  1451
    BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2290
  1452
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
deba@2290
  1453
                               UEdgeRefMap, UEdgeRef>(map));
deba@2290
  1454
      return *this;
deba@2290
  1455
    }
deba@2290
  1456
deba@2290
  1457
    /// \brief Copies the undirected edge cross references into the given map.
deba@2290
  1458
    ///
deba@2290
  1459
    /// Copies the undirected edge cross references (reverse
deba@2290
  1460
    /// references) into the given map.
deba@2290
  1461
    template <typename UEdgeCrossRef>
deba@2290
  1462
    BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2290
  1463
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1464
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@2290
  1465
      return *this;
deba@2290
  1466
    }
deba@2290
  1467
deba@2290
  1468
    /// \brief Make copy of the given map.
deba@2290
  1469
    ///
deba@2290
  1470
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1471
    /// The new map's key type is the target graph's undirected edge type,
deba@2290
  1472
    /// and the copied map's key type is the source graph's undirected edge
deba@2290
  1473
    /// type.  
deba@2290
  1474
    template <typename TargetMap, typename SourceMap>
deba@2290
  1475
    BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1476
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
deba@2290
  1477
                               UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1478
      return *this;
deba@2290
  1479
    }
deba@2290
  1480
deba@2290
  1481
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1482
    ///
deba@2290
  1483
    /// Make a copy of the given undirected edge.
deba@2386
  1484
    BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
deba@2290
  1485
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
deba@2386
  1486
                               UEdgeRefMap, TUEdge>(tuedge, suedge));
deba@2290
  1487
      return *this;
deba@2290
  1488
    }
deba@2290
  1489
deba@2290
  1490
    /// \brief Executes the copies.
deba@2290
  1491
    ///
deba@2290
  1492
    /// Executes the copies.
deba@2290
  1493
    void run() {
deba@2290
  1494
      ANodeRefMap aNodeRefMap(source);
deba@2290
  1495
      BNodeRefMap bNodeRefMap(source);
deba@2290
  1496
      NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
deba@2290
  1497
      UEdgeRefMap uEdgeRefMap(source);
deba@2290
  1498
      EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
deba@2290
  1499
      _graph_utils_bits::BpUGraphCopySelector<Target>::
deba@2290
  1500
        copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
deba@2386
  1501
      for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
deba@2290
  1502
        aNodeMapCopies[i]->copy(source, aNodeRefMap);
deba@2290
  1503
      }
deba@2386
  1504
      for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
deba@2290
  1505
        bNodeMapCopies[i]->copy(source, bNodeRefMap);
deba@2290
  1506
      }
deba@2386
  1507
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2290
  1508
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2290
  1509
      }
deba@2386
  1510
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2290
  1511
        uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
deba@2290
  1512
      }
deba@2386
  1513
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2290
  1514
        edgeMapCopies[i]->copy(source, edgeRefMap);
deba@2290
  1515
      }
deba@2290
  1516
    }
deba@2290
  1517
deba@2290
  1518
  private:
deba@2290
  1519
    
deba@2290
  1520
    const Source& source;
deba@2290
  1521
    Target& target;
deba@2290
  1522
deba@2290
  1523
    std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* > 
deba@2290
  1524
    aNodeMapCopies;
deba@2290
  1525
deba@2290
  1526
    std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* > 
deba@2290
  1527
    bNodeMapCopies;
deba@2290
  1528
deba@2290
  1529
    std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
deba@2290
  1530
    nodeMapCopies;
deba@2290
  1531
deba@2290
  1532
    std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
deba@2290
  1533
    edgeMapCopies;
deba@2290
  1534
deba@2290
  1535
    std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
deba@2290
  1536
    uEdgeMapCopies;
deba@2290
  1537
deba@2290
  1538
  };
deba@2290
  1539
deba@2290
  1540
  /// \brief Copy a bipartite undirected graph to another graph.
deba@2290
  1541
  ///
deba@2290
  1542
  /// Copy a bipartite undirected graph to another graph.
deba@2290
  1543
  /// The usage of the function:
deba@2290
  1544
  /// 
deba@2290
  1545
  ///\code
deba@2290
  1546
  /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
deba@2290
  1547
  ///\endcode
deba@2290
  1548
  /// 
deba@2290
  1549
  /// After the copy the \c nr map will contain the mapping from the
deba@2290
  1550
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@2290
  1551
  /// contain the mapping from the target graph's edges to the source's
deba@2290
  1552
  /// edges.
deba@2290
  1553
  ///
deba@2290
  1554
  /// \see BpUGraphCopy
deba@2290
  1555
  template <typename Target, typename Source>
deba@2290
  1556
  BpUGraphCopy<Target, Source> 
deba@2290
  1557
  copyBpUGraph(Target& target, const Source& source) {
deba@2290
  1558
    return BpUGraphCopy<Target, Source>(target, source);
deba@2290
  1559
  }
deba@2290
  1560
deba@1192
  1561
deba@1192
  1562
  /// @}
alpar@1402
  1563
alpar@1402
  1564
  /// \addtogroup graph_maps
alpar@1402
  1565
  /// @{
alpar@1402
  1566
deba@1413
  1567
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
  1568
athos@1540
  1569
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
  1570
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
  1571
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
  1572
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
  1573
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
  1574
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
  1575
  /// class \c InverseMap.
deba@1413
  1576
  ///
deba@1413
  1577
  template <typename _Graph, typename _Item>
deba@1413
  1578
  class IdMap {
deba@1413
  1579
  public:
deba@1413
  1580
    typedef _Graph Graph;
deba@1413
  1581
    typedef int Value;
deba@1413
  1582
    typedef _Item Item;
deba@1413
  1583
    typedef _Item Key;
deba@1413
  1584
deba@1413
  1585
    /// \brief Constructor.
deba@1413
  1586
    ///
deba@2331
  1587
    /// Constructor of the map.
deba@2286
  1588
    explicit IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1589
deba@1413
  1590
    /// \brief Gives back the \e id of the item.
deba@1413
  1591
    ///
deba@2331
  1592
    /// Gives back the immutable and unique \e id of the item.
deba@1413
  1593
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
  1594
deba@2331
  1595
    /// \brief Gives back the item by its id.
deba@2331
  1596
    ///
deba@2331
  1597
    /// Gives back the item by its id.
deba@2331
  1598
    Item operator()(int id) { return graph->fromId(id, Item()); }
deba@1413
  1599
deba@1413
  1600
  private:
deba@1413
  1601
    const Graph* graph;
deba@1413
  1602
deba@1413
  1603
  public:
deba@1413
  1604
athos@1540
  1605
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
  1606
    ///
athos@1540
  1607
    /// The class represents the inverse of its owner (IdMap).
deba@1413
  1608
    /// \see inverse()
deba@1413
  1609
    class InverseMap {
deba@1413
  1610
    public:
deba@1419
  1611
deba@1413
  1612
      /// \brief Constructor.
deba@1413
  1613
      ///
deba@1413
  1614
      /// Constructor for creating an id-to-item map.
deba@2286
  1615
      explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1616
deba@1413
  1617
      /// \brief Constructor.
deba@1413
  1618
      ///
deba@1413
  1619
      /// Constructor for creating an id-to-item map.
deba@2286
  1620
      explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
  1621
deba@1413
  1622
      /// \brief Gives back the given item from its id.
deba@1413
  1623
      ///
deba@1413
  1624
      /// Gives back the given item from its id.
deba@1413
  1625
      /// 
deba@1413
  1626
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@2331
  1627
deba@1413
  1628
    private:
deba@1413
  1629
      const Graph* graph;
deba@1413
  1630
    };
deba@1413
  1631
deba@1413
  1632
    /// \brief Gives back the inverse of the map.
deba@1413
  1633
    ///
athos@1540
  1634
    /// Gives back the inverse of the IdMap.
deba@1413
  1635
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
  1636
deba@1413
  1637
  };
deba@1413
  1638
deba@1413
  1639
  
athos@1526
  1640
  /// \brief General invertable graph-map type.
alpar@1402
  1641
athos@1540
  1642
  /// This type provides simple invertable graph-maps. 
athos@1526
  1643
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
  1644
  /// and if a key is set to a new value then store it
alpar@1402
  1645
  /// in the inverse map.
deba@1931
  1646
  ///
deba@1931
  1647
  /// The values of the map can be accessed
deba@1931
  1648
  /// with stl compatible forward iterator.
deba@1931
  1649
  ///
alpar@1402
  1650
  /// \param _Graph The graph type.
deba@1830
  1651
  /// \param _Item The item type of the graph.
deba@1830
  1652
  /// \param _Value The value type of the map.
deba@1931
  1653
  ///
deba@1931
  1654
  /// \see IterableValueMap
deba@1830
  1655
  template <typename _Graph, typename _Item, typename _Value>
deba@2287
  1656
  class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
deba@1931
  1657
  private:
deba@1931
  1658
    
deba@2287
  1659
    typedef DefaultMap<_Graph, _Item, _Value> Map;
deba@1931
  1660
    typedef _Graph Graph;
deba@1931
  1661
deba@2287
  1662
    typedef std::map<_Value, _Item> Container;
deba@1931
  1663
    Container invMap;    
deba@1931
  1664
deba@1931
  1665
  public:
deba@1931
  1666
 
deba@2287
  1667
    /// The key type of InvertableMap (Node, Edge, UEdge).
deba@2287
  1668
    typedef typename Map::Key Key;
deba@2287
  1669
    /// The value type of the InvertableMap.
deba@2287
  1670
    typedef typename Map::Value Value;
deba@2287
  1671
deba@1931
  1672
deba@1931
  1673
alpar@1402
  1674
    /// \brief Constructor.
alpar@1402
  1675
    ///
deba@1413
  1676
    /// Construct a new InvertableMap for the graph.
alpar@1402
  1677
    ///
deba@2286
  1678
    explicit InvertableMap(const Graph& graph) : Map(graph) {} 
deba@1931
  1679
deba@1931
  1680
    /// \brief Forward iterator for values.
deba@1931
  1681
    ///
deba@1931
  1682
    /// This iterator is an stl compatible forward
deba@1931
  1683
    /// iterator on the values of the map. The values can
deba@1931
  1684
    /// be accessed in the [beginValue, endValue) range.
deba@1931
  1685
    ///
deba@1931
  1686
    class ValueIterator 
deba@1931
  1687
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
  1688
      friend class InvertableMap;
deba@1931
  1689
    private:
deba@1931
  1690
      ValueIterator(typename Container::const_iterator _it) 
deba@1931
  1691
        : it(_it) {}
deba@1931
  1692
    public:
deba@1931
  1693
      
deba@1931
  1694
      ValueIterator() {}
deba@1931
  1695
deba@1931
  1696
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
  1697
      ValueIterator operator++(int) { 
deba@1931
  1698
        ValueIterator tmp(*this); 
deba@1931
  1699
        operator++();
deba@1931
  1700
        return tmp; 
deba@1931
  1701
      }
deba@1931
  1702
deba@1931
  1703
      const Value& operator*() const { return it->first; }
deba@1931
  1704
      const Value* operator->() const { return &(it->first); }
deba@1931
  1705
deba@1931
  1706
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
  1707
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
  1708
      
deba@1931
  1709
    private:
deba@1931
  1710
      typename Container::const_iterator it;
deba@1931
  1711
    };
deba@1931
  1712
deba@1931
  1713
    /// \brief Returns an iterator to the first value.
deba@1931
  1714
    ///
deba@1931
  1715
    /// Returns an stl compatible iterator to the 
deba@1931
  1716
    /// first value of the map. The values of the
deba@1931
  1717
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1718
    /// range.
deba@1931
  1719
    ValueIterator beginValue() const {
deba@1931
  1720
      return ValueIterator(invMap.begin());
deba@1931
  1721
    }
deba@1931
  1722
deba@1931
  1723
    /// \brief Returns an iterator after the last value.
deba@1931
  1724
    ///
deba@1931
  1725
    /// Returns an stl compatible iterator after the 
deba@1931
  1726
    /// last value of the map. The values of the
deba@1931
  1727
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1728
    /// range.
deba@1931
  1729
    ValueIterator endValue() const {
deba@1931
  1730
      return ValueIterator(invMap.end());
deba@1931
  1731
    }
alpar@1402
  1732
    
alpar@1402
  1733
    /// \brief The setter function of the map.
alpar@1402
  1734
    ///
deba@1413
  1735
    /// Sets the mapped value.
alpar@1402
  1736
    void set(const Key& key, const Value& val) {
alpar@1402
  1737
      Value oldval = Map::operator[](key);
deba@1413
  1738
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
  1739
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1740
	invMap.erase(it);
alpar@1402
  1741
      }      
alpar@1402
  1742
      invMap.insert(make_pair(val, key));
alpar@1402
  1743
      Map::set(key, val);
alpar@1402
  1744
    }
alpar@1402
  1745
alpar@1402
  1746
    /// \brief The getter function of the map.
alpar@1402
  1747
    ///
alpar@1402
  1748
    /// It gives back the value associated with the key.
deba@1931
  1749
    typename MapTraits<Map>::ConstReturnValue 
deba@1931
  1750
    operator[](const Key& key) const {
alpar@1402
  1751
      return Map::operator[](key);
alpar@1402
  1752
    }
alpar@1402
  1753
deba@2331
  1754
    /// \brief Gives back the item by its value.
deba@2331
  1755
    ///
deba@2331
  1756
    /// Gives back the item by its value.
deba@2331
  1757
    Key operator()(const Value& key) const {
deba@2331
  1758
      typename Container::const_iterator it = invMap.find(key);
deba@2331
  1759
      return it != invMap.end() ? it->second : INVALID;
deba@2331
  1760
    }
deba@2331
  1761
deba@1515
  1762
  protected:
deba@1515
  1763
alpar@1402
  1764
    /// \brief Erase the key from the map.
alpar@1402
  1765
    ///
alpar@1402
  1766
    /// Erase the key to the map. It is called by the
alpar@1402
  1767
    /// \c AlterationNotifier.
alpar@1402
  1768
    virtual void erase(const Key& key) {
alpar@1402
  1769
      Value val = Map::operator[](key);
deba@1413
  1770
      typename Container::iterator it = invMap.find(val);
alpar@1402
  1771
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1772
	invMap.erase(it);
alpar@1402
  1773
      }
alpar@1402
  1774
      Map::erase(key);
alpar@1402
  1775
    }
alpar@1402
  1776
deba@1829
  1777
    /// \brief Erase more keys from the map.
deba@1829
  1778
    ///
deba@1829
  1779
    /// Erase more keys from the map. It is called by the
deba@1829
  1780
    /// \c AlterationNotifier.
deba@1829
  1781
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
  1782
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  1783
	Value val = Map::operator[](keys[i]);
deba@1829
  1784
	typename Container::iterator it = invMap.find(val);
deba@1829
  1785
	if (it != invMap.end() && it->second == keys[i]) {
deba@1829
  1786
	  invMap.erase(it);
deba@1829
  1787
	}
deba@1829
  1788
      }
deba@1829
  1789
      Map::erase(keys);
deba@1829
  1790
    }
deba@1829
  1791
alpar@1402
  1792
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
  1793
    ///
alpar@1402
  1794
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
  1795
    /// \c AlterationNotifier.
alpar@1402
  1796
    virtual void clear() {
alpar@1402
  1797
      invMap.clear();
alpar@1402
  1798
      Map::clear();
alpar@1402
  1799
    }
alpar@1402
  1800
deba@1413
  1801
  public:
deba@1413
  1802
deba@1413
  1803
    /// \brief The inverse map type.
deba@1413
  1804
    ///
deba@1413
  1805
    /// The inverse of this map. The subscript operator of the map
deba@1413
  1806
    /// gives back always the item what was last assigned to the value. 
deba@1413
  1807
    class InverseMap {
deba@1413
  1808
    public:
deba@1413
  1809
      /// \brief Constructor of the InverseMap.
deba@1413
  1810
      ///
deba@1413
  1811
      /// Constructor of the InverseMap.
deba@2286
  1812
      explicit InverseMap(const InvertableMap& _inverted) 
deba@2286
  1813
        : inverted(_inverted) {}
deba@1413
  1814
deba@1413
  1815
      /// The value type of the InverseMap.
deba@1413
  1816
      typedef typename InvertableMap::Key Value;
deba@1413
  1817
      /// The key type of the InverseMap.
deba@1413
  1818
      typedef typename InvertableMap::Value Key; 
deba@1413
  1819
deba@1413
  1820
      /// \brief Subscript operator. 
deba@1413
  1821
      ///
deba@1413
  1822
      /// Subscript operator. It gives back always the item 
deba@1413
  1823
      /// what was last assigned to the value.
deba@1413
  1824
      Value operator[](const Key& key) const {
deba@2331
  1825
	return inverted(key);
deba@1413
  1826
      }
deba@1413
  1827
      
deba@1413
  1828
    private:
deba@1413
  1829
      const InvertableMap& inverted;
deba@1413
  1830
    };
deba@1413
  1831
alpar@2094
  1832
    /// \brief It gives back the just readable inverse map.
alpar@1402
  1833
    ///
alpar@2094
  1834
    /// It gives back the just readable inverse map.
deba@1413
  1835
    InverseMap inverse() const {
deba@1413
  1836
      return InverseMap(*this);
alpar@1402
  1837
    } 
alpar@1402
  1838
alpar@1402
  1839
deba@1413
  1840
    
alpar@1402
  1841
  };
alpar@1402
  1842
alpar@1402
  1843
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
  1844
  /// item in the graph.
alpar@1402
  1845
  ///
athos@1540
  1846
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
  1847
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
  1848
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
  1849
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
  1850
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
  1851
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
  1852
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
  1853
  /// with its member class \c InverseMap.
alpar@1402
  1854
  ///
alpar@1402
  1855
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
  1856
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
klao@1909
  1857
  /// UEdge.
deba@1830
  1858
  template <typename _Graph, typename _Item>
deba@2287
  1859
  class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
alpar@1402
  1860
alpar@1402
  1861
    typedef _Item Item;
deba@2287
  1862
    typedef DefaultMap<_Graph, _Item, int> Map;
alpar@1402
  1863
alpar@1402
  1864
  public:
alpar@1402
  1865
    /// The graph class of DescriptorMap.
alpar@1402
  1866
    typedef _Graph Graph;
alpar@1402
  1867
klao@1909
  1868
    /// The key type of DescriptorMap (Node, Edge, UEdge).
deba@2287
  1869
    typedef typename Map::Key Key;
alpar@1402
  1870
    /// The value type of DescriptorMap.
deba@2287
  1871
    typedef typename Map::Value Value;
alpar@1402
  1872
alpar@1402
  1873
    /// \brief Constructor.
alpar@1402
  1874
    ///
deba@1413
  1875
    /// Constructor for descriptor map.
deba@2286
  1876
    explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@2201
  1877
      Item it;
deba@2386
  1878
      const typename Map::Notifier* nf = Map::notifier(); 
deba@2386
  1879
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2201
  1880
	Map::set(it, invMap.size());
deba@2201
  1881
	invMap.push_back(it);	
deba@2201
  1882
      }      
alpar@1402
  1883
    }
alpar@1402
  1884
deba@1515
  1885
  protected:
deba@1515
  1886
alpar@1402
  1887
    /// \brief Add a new key to the map.
alpar@1402
  1888
    ///
alpar@1402
  1889
    /// Add a new key to the map. It is called by the
alpar@1402
  1890
    /// \c AlterationNotifier.
alpar@1402
  1891
    virtual void add(const Item& item) {
alpar@1402
  1892
      Map::add(item);
alpar@1402
  1893
      Map::set(item, invMap.size());
alpar@1402
  1894
      invMap.push_back(item);
alpar@1402
  1895
    }
alpar@1402
  1896
deba@1829
  1897
    /// \brief Add more new keys to the map.
deba@1829
  1898
    ///
deba@1829
  1899
    /// Add more new keys to the map. It is called by the
deba@1829
  1900
    /// \c AlterationNotifier.
deba@1829
  1901
    virtual void add(const std::vector<Item>& items) {
deba@1829
  1902
      Map::add(items);
deba@2386
  1903
      for (int i = 0; i < int(items.size()); ++i) {
deba@1829
  1904
	Map::set(items[i], invMap.size());
deba@1829
  1905
	invMap.push_back(items[i]);
deba@1829
  1906
      }
deba@1829
  1907
    }
deba@1829
  1908
alpar@1402
  1909
    /// \brief Erase the key from the map.
alpar@1402
  1910
    ///
deba@1829
  1911
    /// Erase the key from the map. It is called by the
alpar@1402
  1912
    /// \c AlterationNotifier.
alpar@1402
  1913
    virtual void erase(const Item& item) {
alpar@1402
  1914
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1915
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1916
      invMap.pop_back();
alpar@1402
  1917
      Map::erase(item);
alpar@1402
  1918
    }
alpar@1402
  1919
deba@1829
  1920
    /// \brief Erase more keys from the map.
deba@1829
  1921
    ///
deba@1829
  1922
    /// Erase more keys from the map. It is called by the
deba@1829
  1923
    /// \c AlterationNotifier.
deba@1829
  1924
    virtual void erase(const std::vector<Item>& items) {
deba@2386
  1925
      for (int i = 0; i < int(items.size()); ++i) {
deba@1829
  1926
	Map::set(invMap.back(), Map::operator[](items[i]));
deba@1829
  1927
	invMap[Map::operator[](items[i])] = invMap.back();
deba@1829
  1928
	invMap.pop_back();
deba@1829
  1929
      }
deba@1829
  1930
      Map::erase(items);
deba@1829
  1931
    }
deba@1829
  1932
alpar@1402
  1933
    /// \brief Build the unique map.
alpar@1402
  1934
    ///
alpar@1402
  1935
    /// Build the unique map. It is called by the
alpar@1402
  1936
    /// \c AlterationNotifier.
alpar@1402
  1937
    virtual void build() {
alpar@1402
  1938
      Map::build();
alpar@1402
  1939
      Item it;
deba@2386
  1940
      const typename Map::Notifier* nf = Map::notifier(); 
deba@2386
  1941
      for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@1402
  1942
	Map::set(it, invMap.size());
alpar@1402
  1943
	invMap.push_back(it);	
alpar@1402
  1944
      }      
alpar@1402
  1945
    }
alpar@1402
  1946
    
alpar@1402
  1947
    /// \brief Clear the keys from the map.
alpar@1402
  1948
    ///
alpar@1402
  1949
    /// Clear the keys from the map. It is called by the
alpar@1402
  1950
    /// \c AlterationNotifier.
alpar@1402
  1951
    virtual void clear() {
alpar@1402
  1952
      invMap.clear();
alpar@1402
  1953
      Map::clear();
alpar@1402
  1954
    }
alpar@1402
  1955
deba@1538
  1956
  public:
deba@1538
  1957
deba@1931
  1958
    /// \brief Returns the maximal value plus one.
deba@1931
  1959
    ///
deba@1931
  1960
    /// Returns the maximal value plus one in the map.
deba@1931
  1961
    unsigned int size() const {
deba@1931
  1962
      return invMap.size();
deba@1931
  1963
    }
deba@1931
  1964
deba@1552
  1965
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1966
    ///
deba@1552
  1967
    /// Swaps the position of the two items in the map.
deba@1552
  1968
    void swap(const Item& p, const Item& q) {
deba@1552
  1969
      int pi = Map::operator[](p);
deba@1552
  1970
      int qi = Map::operator[](q);
deba@1552
  1971
      Map::set(p, qi);
deba@1552
  1972
      invMap[qi] = p;
deba@1552
  1973
      Map::set(q, pi);
deba@1552
  1974
      invMap[pi] = q;
deba@1552
  1975
    }
deba@1552
  1976
alpar@1402
  1977
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1978
    ///
alpar@1402
  1979
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1980
    int operator[](const Item& item) const {
alpar@1402
  1981
      return Map::operator[](item);
alpar@1402
  1982
    }
deba@2331
  1983
deba@2331
  1984
    /// \brief Gives back the item by its descriptor.
deba@2331
  1985
    ///
deba@2331
  1986
    /// Gives back th item by its descriptor.
deba@2331
  1987
    Item operator()(int id) const {
deba@2331
  1988
      return invMap[id];
deba@2331
  1989
    }
alpar@1402
  1990
    
deba@1413
  1991
  private:
deba@1413
  1992
deba@1413
  1993
    typedef std::vector<Item> Container;
deba@1413
  1994
    Container invMap;
deba@1413
  1995
deba@1413
  1996
  public:
athos@1540
  1997
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  1998
    ///
athos@1540
  1999
    /// The inverse map type of DescriptorMap.
deba@1413
  2000
    class InverseMap {
deba@1413
  2001
    public:
deba@1413
  2002
      /// \brief Constructor of the InverseMap.
deba@1413
  2003
      ///
deba@1413
  2004
      /// Constructor of the InverseMap.
deba@2286
  2005
      explicit InverseMap(const DescriptorMap& _inverted) 
deba@1413
  2006
	: inverted(_inverted) {}
deba@1413
  2007
deba@1413
  2008
deba@1413
  2009
      /// The value type of the InverseMap.
deba@1413
  2010
      typedef typename DescriptorMap::Key Value;
deba@1413
  2011
      /// The key type of the InverseMap.
deba@1413
  2012
      typedef typename DescriptorMap::Value Key; 
deba@1413
  2013
deba@1413
  2014
      /// \brief Subscript operator. 
deba@1413
  2015
      ///
deba@1413
  2016
      /// Subscript operator. It gives back the item 
deba@1413
  2017
      /// that the descriptor belongs to currently.
deba@1413
  2018
      Value operator[](const Key& key) const {
deba@2331
  2019
	return inverted(key);
deba@1413
  2020
      }
deba@1470
  2021
deba@1470
  2022
      /// \brief Size of the map.
deba@1470
  2023
      ///
deba@1470
  2024
      /// Returns the size of the map.
deba@1931
  2025
      unsigned int size() const {
deba@2331
  2026
	return inverted.size();
deba@1470
  2027
      }
deba@1413
  2028
      
deba@1413
  2029
    private:
deba@1413
  2030
      const DescriptorMap& inverted;
deba@1413
  2031
    };
deba@1413
  2032
alpar@1402
  2033
    /// \brief Gives back the inverse of the map.
alpar@1402
  2034
    ///
alpar@1402
  2035
    /// Gives back the inverse of the map.
alpar@1402
  2036
    const InverseMap inverse() const {
deba@1413
  2037
      return InverseMap(*this);
alpar@1402
  2038
    }
alpar@1402
  2039
  };
alpar@1402
  2040
alpar@1402
  2041
  /// \brief Returns the source of the given edge.
alpar@1402
  2042
  ///
alpar@1402
  2043
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
  2044
  /// \author Balazs Dezso
alpar@1402
  2045
  template <typename Graph>
alpar@1402
  2046
  class SourceMap {
alpar@1402
  2047
  public:
deba@1419
  2048
alpar@1402
  2049
    typedef typename Graph::Node Value;
alpar@1402
  2050
    typedef typename Graph::Edge Key;
alpar@1402
  2051
alpar@1402
  2052
    /// \brief Constructor
alpar@1402
  2053
    ///
alpar@1402
  2054
    /// Constructor
alpar@1402
  2055
    /// \param _graph The graph that the map belongs to.
deba@2286
  2056
    explicit SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2057
alpar@1402
  2058
    /// \brief The subscript operator.
alpar@1402
  2059
    ///
alpar@1402
  2060
    /// The subscript operator.
alpar@1402
  2061
    /// \param edge The edge 
alpar@1402
  2062
    /// \return The source of the edge 
deba@1679
  2063
    Value operator[](const Key& edge) const {
alpar@1402
  2064
      return graph.source(edge);
alpar@1402
  2065
    }
alpar@1402
  2066
alpar@1402
  2067
  private:
alpar@1402
  2068
    const Graph& graph;
alpar@1402
  2069
  };
alpar@1402
  2070
alpar@1402
  2071
  /// \brief Returns a \ref SourceMap class
alpar@1402
  2072
  ///
alpar@1402
  2073
  /// This function just returns an \ref SourceMap class.
alpar@1402
  2074
  /// \relates SourceMap
alpar@1402
  2075
  template <typename Graph>
alpar@1402
  2076
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  2077
    return SourceMap<Graph>(graph);
alpar@1402
  2078
  } 
alpar@1402
  2079
alpar@1402
  2080
  /// \brief Returns the target of the given edge.
alpar@1402
  2081
  ///
alpar@1402
  2082
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
  2083
  /// \author Balazs Dezso
alpar@1402
  2084
  template <typename Graph>
alpar@1402
  2085
  class TargetMap {
alpar@1402
  2086
  public:
deba@1419
  2087
alpar@1402
  2088
    typedef typename Graph::Node Value;
alpar@1402
  2089
    typedef typename Graph::Edge Key;
alpar@1402
  2090
alpar@1402
  2091
    /// \brief Constructor
alpar@1402
  2092
    ///
alpar@1402
  2093
    /// Constructor
alpar@1402
  2094
    /// \param _graph The graph that the map belongs to.
deba@2286
  2095
    explicit TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2096
alpar@1402
  2097
    /// \brief The subscript operator.
alpar@1402
  2098
    ///
alpar@1402
  2099
    /// The subscript operator.
alpar@1536
  2100
    /// \param e The edge 
alpar@1402
  2101
    /// \return The target of the edge 
deba@1679
  2102
    Value operator[](const Key& e) const {
alpar@1536
  2103
      return graph.target(e);
alpar@1402
  2104
    }
alpar@1402
  2105
alpar@1402
  2106
  private:
alpar@1402
  2107
    const Graph& graph;
alpar@1402
  2108
  };
alpar@1402
  2109
alpar@1402
  2110
  /// \brief Returns a \ref TargetMap class
deba@1515
  2111
  ///
athos@1540
  2112
  /// This function just returns a \ref TargetMap class.
alpar@1402
  2113
  /// \relates TargetMap
alpar@1402
  2114
  template <typename Graph>
alpar@1402
  2115
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  2116
    return TargetMap<Graph>(graph);
alpar@1402
  2117
  }
alpar@1402
  2118
athos@1540
  2119
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  2120
  ///
athos@1540
  2121
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
  2122
  /// \author Balazs Dezso
deba@1419
  2123
  template <typename Graph>
deba@1419
  2124
  class ForwardMap {
deba@1419
  2125
  public:
deba@1419
  2126
deba@1419
  2127
    typedef typename Graph::Edge Value;
klao@1909
  2128
    typedef typename Graph::UEdge Key;
deba@1419
  2129
deba@1419
  2130
    /// \brief Constructor
deba@1419
  2131
    ///
deba@1419
  2132
    /// Constructor
deba@1419
  2133
    /// \param _graph The graph that the map belongs to.
deba@2286
  2134
    explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2135
deba@1419
  2136
    /// \brief The subscript operator.
deba@1419
  2137
    ///
deba@1419
  2138
    /// The subscript operator.
deba@1419
  2139
    /// \param key An undirected edge 
deba@1419
  2140
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  2141
    Value operator[](const Key& key) const {
deba@1627
  2142
      return graph.direct(key, true);
deba@1419
  2143
    }
deba@1419
  2144
deba@1419
  2145
  private:
deba@1419
  2146
    const Graph& graph;
deba@1419
  2147
  };
deba@1419
  2148
deba@1419
  2149
  /// \brief Returns a \ref ForwardMap class
deba@1515
  2150
  ///
deba@1419
  2151
  /// This function just returns an \ref ForwardMap class.
deba@1419
  2152
  /// \relates ForwardMap
deba@1419
  2153
  template <typename Graph>
deba@1419
  2154
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  2155
    return ForwardMap<Graph>(graph);
deba@1419
  2156
  }
deba@1419
  2157
athos@1540
  2158
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  2159
  ///
athos@1540
  2160
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  2161
  /// \author Balazs Dezso
deba@1419
  2162
  template <typename Graph>
deba@1419
  2163
  class BackwardMap {
deba@1419
  2164
  public:
deba@1419
  2165
deba@1419
  2166
    typedef typename Graph::Edge Value;
klao@1909
  2167
    typedef typename Graph::UEdge Key;
deba@1419
  2168
deba@1419
  2169
    /// \brief Constructor
deba@1419
  2170
    ///
deba@1419
  2171
    /// Constructor
deba@1419
  2172
    /// \param _graph The graph that the map belongs to.
deba@2286
  2173
    explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2174
deba@1419
  2175
    /// \brief The subscript operator.
deba@1419
  2176
    ///
deba@1419
  2177
    /// The subscript operator.
deba@1419
  2178
    /// \param key An undirected edge 
deba@1419
  2179
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  2180
    Value operator[](const Key& key) const {
deba@1627
  2181
      return graph.direct(key, false);
deba@1419
  2182
    }
deba@1419
  2183
deba@1419
  2184
  private:
deba@1419
  2185
    const Graph& graph;
deba@1419
  2186
  };
deba@1419
  2187
deba@1419
  2188
  /// \brief Returns a \ref BackwardMap class
deba@1419
  2189
athos@1540
  2190
  /// This function just returns a \ref BackwardMap class.
deba@1419
  2191
  /// \relates BackwardMap
deba@1419
  2192
  template <typename Graph>
deba@1419
  2193
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  2194
    return BackwardMap<Graph>(graph);
deba@1419
  2195
  }
deba@1419
  2196
deba@1695
  2197
  /// \brief Potential difference map
deba@1695
  2198
  ///
deba@1695
  2199
  /// If there is an potential map on the nodes then we
deba@1695
  2200
  /// can get an edge map as we get the substraction of the
deba@1695
  2201
  /// values of the target and source.
deba@1695
  2202
  template <typename Graph, typename NodeMap>
deba@1695
  2203
  class PotentialDifferenceMap {
deba@1515
  2204
  public:
deba@1695
  2205
    typedef typename Graph::Edge Key;
deba@1695
  2206
    typedef typename NodeMap::Value Value;
deba@1695
  2207
deba@1695
  2208
    /// \brief Constructor
deba@1695
  2209
    ///
deba@1695
  2210
    /// Contructor of the map
deba@2286
  2211
    explicit PotentialDifferenceMap(const Graph& _graph, 
deba@2286
  2212
                                    const NodeMap& _potential) 
deba@1695
  2213
      : graph(_graph), potential(_potential) {}
deba@1695
  2214
deba@1695
  2215
    /// \brief Const subscription operator
deba@1695
  2216
    ///
deba@1695
  2217
    /// Const subscription operator
deba@1695
  2218
    Value operator[](const Key& edge) const {
deba@1695
  2219
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  2220
    }
deba@1695
  2221
deba@1695
  2222
  private:
deba@1695
  2223
    const Graph& graph;
deba@1695
  2224
    const NodeMap& potential;
deba@1695
  2225
  };
deba@1695
  2226
deba@1695
  2227
  /// \brief Just returns a PotentialDifferenceMap
deba@1695
  2228
  ///
deba@1695
  2229
  /// Just returns a PotentialDifferenceMap
deba@1695
  2230
  /// \relates PotentialDifferenceMap
deba@1695
  2231
  template <typename Graph, typename NodeMap>
deba@1695
  2232
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  2233
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  2234
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  2235
  }
deba@1695
  2236
deba@1515
  2237
  /// \brief Map of the node in-degrees.
alpar@1453
  2238
  ///
athos@1540
  2239
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  2240
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2241
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2242
  /// whenever the graph changes.
deba@1515
  2243
  ///
deba@1729
  2244
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2245
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1829
  2246
  /// is not guarantied if these additional features are used. For example
deba@1829
  2247
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2248
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2249
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2250
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2251
  ///
deba@1515
  2252
  /// \sa OutDegMap
deba@1515
  2253
alpar@1453
  2254
  template <typename _Graph>
deba@1515
  2255
  class InDegMap  
deba@1999
  2256
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2257
      ::ItemNotifier::ObserverBase {
deba@1515
  2258
alpar@1453
  2259
  public:
deba@1515
  2260
    
deba@1515
  2261
    typedef _Graph Graph;
alpar@1453
  2262
    typedef int Value;
deba@1515
  2263
    typedef typename Graph::Node Key;
deba@1515
  2264
deba@1999
  2265
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2266
    ::ItemNotifier::ObserverBase Parent;
deba@1999
  2267
deba@1515
  2268
  private:
deba@1515
  2269
deba@1990
  2270
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2271
    public:
deba@1515
  2272
deba@1990
  2273
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2274
      typedef typename Parent::Graph Graph;
deba@1515
  2275
deba@1515
  2276
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2277
      
deba@1829
  2278
      virtual void add(const Key& key) {
deba@1515
  2279
	Parent::add(key);
deba@1515
  2280
	Parent::set(key, 0);
deba@1515
  2281
      }
deba@1931
  2282
deba@1829
  2283
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2284
	Parent::add(keys);
deba@2386
  2285
	for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  2286
	  Parent::set(keys[i], 0);
deba@1829
  2287
	}
deba@1829
  2288
      }
deba@1515
  2289
    };
deba@1515
  2290
deba@1515
  2291
  public:
alpar@1453
  2292
alpar@1453
  2293
    /// \brief Constructor.
alpar@1453
  2294
    ///
alpar@1453
  2295
    /// Constructor for creating in-degree map.
deba@2286
  2296
    explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@2384
  2297
      Parent::attach(graph.notifier(typename _Graph::Edge()));
deba@1515
  2298
      
deba@1515
  2299
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2300
	deg[it] = countInEdges(graph, it);
deba@1515
  2301
      }
alpar@1453
  2302
    }
alpar@1453
  2303
    
alpar@1459
  2304
    /// Gives back the in-degree of a Node.
deba@1515
  2305
    int operator[](const Key& key) const {
deba@1515
  2306
      return deg[key];
alpar@1459
  2307
    }
alpar@1453
  2308
alpar@1453
  2309
  protected:
deba@1515
  2310
    
deba@1515
  2311
    typedef typename Graph::Edge Edge;
deba@1515
  2312
deba@1515
  2313
    virtual void add(const Edge& edge) {
deba@1515
  2314
      ++deg[graph.target(edge)];
alpar@1453
  2315
    }
alpar@1453
  2316
deba@1931
  2317
    virtual void add(const std::vector<Edge>& edges) {
deba@2386
  2318
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2319
        ++deg[graph.target(edges[i])];
deba@1931
  2320
      }
deba@1931
  2321
    }
deba@1931
  2322
deba@1515
  2323
    virtual void erase(const Edge& edge) {
deba@1515
  2324
      --deg[graph.target(edge)];
deba@1515
  2325
    }
deba@1515
  2326
deba@1931
  2327
    virtual void erase(const std::vector<Edge>& edges) {
deba@2386
  2328
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2329
        --deg[graph.target(edges[i])];
deba@1931
  2330
      }
deba@1931
  2331
    }
deba@1931
  2332
deba@1515
  2333
    virtual void build() {
deba@1515
  2334
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2335
	deg[it] = countInEdges(graph, it);
deba@1515
  2336
      }      
deba@1515
  2337
    }
deba@1515
  2338
deba@1515
  2339
    virtual void clear() {
deba@1515
  2340
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2341
	deg[it] = 0;
deba@1515
  2342
      }
deba@1515
  2343
    }
deba@1515
  2344
  private:
alpar@1506
  2345
    
deba@1515
  2346
    const _Graph& graph;
deba@1515
  2347
    AutoNodeMap deg;
alpar@1459
  2348
  };
alpar@1459
  2349
deba@1515
  2350
  /// \brief Map of the node out-degrees.
deba@1515
  2351
  ///
athos@1540
  2352
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  2353
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2354
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2355
  /// whenever the graph changes.
deba@1515
  2356
  ///
deba@1729
  2357
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2358
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1829
  2359
  /// is not guarantied if these additional features are used. For example
deba@1829
  2360
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2361
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2362
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2363
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2364
  ///
alpar@1555
  2365
  /// \sa InDegMap
alpar@1459
  2366
alpar@1459
  2367
  template <typename _Graph>
deba@1515
  2368
  class OutDegMap  
deba@1999
  2369
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2370
      ::ItemNotifier::ObserverBase {
deba@1515
  2371
alpar@1459
  2372
  public:
deba@1999
  2373
deba@1999
  2374
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2375
    ::ItemNotifier::ObserverBase Parent;
deba@1515
  2376
    
deba@1515
  2377
    typedef _Graph Graph;
alpar@1459
  2378
    typedef int Value;
deba@1515
  2379
    typedef typename Graph::Node Key;
deba@1515
  2380
deba@1515
  2381
  private:
deba@1515
  2382
deba@1990
  2383
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2384
    public:
deba@1515
  2385
deba@1990
  2386
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2387
      typedef typename Parent::Graph Graph;
deba@1515
  2388
deba@1515
  2389
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2390
      
deba@1829
  2391
      virtual void add(const Key& key) {
deba@1515
  2392
	Parent::add(key);
deba@1515
  2393
	Parent::set(key, 0);
deba@1515
  2394
      }
deba@1829
  2395
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2396
	Parent::add(keys);
deba@2386
  2397
	for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  2398
	  Parent::set(keys[i], 0);
deba@1829
  2399
	}
deba@1829
  2400
      }
deba@1515
  2401
    };
deba@1515
  2402
deba@1515
  2403
  public:
alpar@1459
  2404
alpar@1459
  2405
    /// \brief Constructor.
alpar@1459
  2406
    ///
alpar@1459
  2407
    /// Constructor for creating out-degree map.
deba@2286
  2408
    explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@2384
  2409
      Parent::attach(graph.notifier(typename _Graph::Edge()));
deba@1515
  2410
      
deba@1515
  2411
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2412
	deg[it] = countOutEdges(graph, it);
deba@1515
  2413
      }
alpar@1459
  2414
    }
alpar@1459
  2415
deba@1990
  2416
    /// Gives back the out-degree of a Node.
deba@1515
  2417
    int operator[](const Key& key) const {
deba@1515
  2418
      return deg[key];
alpar@1459
  2419
    }
alpar@1459
  2420
alpar@1459
  2421
  protected:
deba@1515
  2422
    
deba@1515
  2423
    typedef typename Graph::Edge Edge;
deba@1515
  2424
deba@1515
  2425
    virtual void add(const Edge& edge) {
deba@1515
  2426
      ++deg[graph.source(edge)];
alpar@1459
  2427
    }
alpar@1459
  2428
deba@1931
  2429
    virtual void add(const std::vector<Edge>& edges) {
deba@2386
  2430
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2431
        ++deg[graph.source(edges[i])];
deba@1931
  2432
      }
deba@1931
  2433
    }
deba@1931
  2434
deba@1515
  2435
    virtual void erase(const Edge& edge) {
deba@1515
  2436
      --deg[graph.source(edge)];
deba@1515
  2437
    }
deba@1515
  2438
deba@1931
  2439
    virtual void erase(const std::vector<Edge>& edges) {
deba@2386
  2440
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2441
        --deg[graph.source(edges[i])];
deba@1931
  2442
      }
deba@1931
  2443
    }
deba@1931
  2444
deba@1515
  2445
    virtual void build() {
deba@1515
  2446
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2447
	deg[it] = countOutEdges(graph, it);
deba@1515
  2448
      }      
deba@1515
  2449
    }
deba@1515
  2450
deba@1515
  2451
    virtual void clear() {
deba@1515
  2452
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2453
	deg[it] = 0;
deba@1515
  2454
      }
deba@1515
  2455
    }
deba@1515
  2456
  private:
alpar@1506
  2457
    
deba@1515
  2458
    const _Graph& graph;
deba@1515
  2459
    AutoNodeMap deg;
alpar@1453
  2460
  };
alpar@1453
  2461
deba@1695
  2462
alpar@2235
  2463
  ///Fast edge look up between given endpoints.
alpar@2235
  2464
  
alpar@2235
  2465
  ///\ingroup gutils
alpar@2235
  2466
  ///Using this class, you can find an edge in a graph from a given
alpar@2235
  2467
  ///source to a given target in time <em>O(log d)</em>,
alpar@2235
  2468
  ///where <em>d</em> is the out-degree of the source node.
alpar@2235
  2469
  ///
alpar@2235
  2470
  ///It is not possible to find \e all parallel edges between two nodes.
alpar@2235
  2471
  ///Use \ref AllEdgeLookUp for this purpose.
alpar@2235
  2472
  ///
alpar@2235
  2473
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  2474
  ///refresh(Node)) this data structure
alpar@2235
  2475
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  2476
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  2477
  ///
alpar@2235
  2478
  ///\param G The type of the underlying graph.
alpar@2235
  2479
  ///
alpar@2235
  2480
  ///\sa AllEdgeLookUp  
alpar@2235
  2481
  template<class G>
alpar@2235
  2482
  class EdgeLookUp 
alpar@2235
  2483
  {
alpar@2235
  2484
  public:
alpar@2235
  2485
    GRAPH_TYPEDEFS(typename G)
alpar@2235
  2486
    typedef G Graph;
alpar@2235
  2487
alpar@2235
  2488
  protected:
alpar@2235
  2489
    const Graph &_g;
alpar@2235
  2490
    typename Graph::template NodeMap<Edge> _head;
alpar@2235
  2491
    typename Graph::template EdgeMap<Edge> _left;
alpar@2235
  2492
    typename Graph::template EdgeMap<Edge> _right;
alpar@2235
  2493
    
alpar@2235
  2494
    class EdgeLess {
alpar@2235
  2495
      const Graph &g;
alpar@2235
  2496
    public:
alpar@2235
  2497
      EdgeLess(const Graph &_g) : g(_g) {}
alpar@2235
  2498
      bool operator()(Edge a,Edge b) const 
alpar@2235
  2499
      {
alpar@2235
  2500
	return g.target(a)<g.target(b);
alpar@2235
  2501
      }
alpar@2235
  2502
    };
alpar@2235
  2503
    
alpar@2235
  2504
  public:
alpar@2235
  2505
    
alpar@2235
  2506
    ///Constructor
alpar@2235
  2507
alpar@2235
  2508
    ///Constructor.
alpar@2235
  2509
    ///
alpar@2235
  2510
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  2511
    ///changes.
alpar@2235
  2512
    EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
alpar@2235
  2513
    
alpar@2235
  2514
  private:
alpar@2235
  2515
    Edge refresh_rec(std::vector<Edge> &v,int a,int b) 
alpar@2235
  2516
    {
alpar@2235
  2517
      int m=(a+b)/2;
alpar@2235
  2518
      Edge me=v[m];
alpar@2235
  2519
      _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
alpar@2235
  2520
      _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
alpar@2235
  2521
      return me;
alpar@2235
  2522
    }
alpar@2235
  2523
  public:
alpar@2235
  2524
    ///Refresh the data structure at a node.
alpar@2235
  2525
alpar@2235
  2526
    ///Build up the search database of node \c n.
alpar@2235
  2527
    ///
alpar@2235
  2528
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  2529
    ///the number of the outgoing edges of \c n.
alpar@2235
  2530
    void refresh(Node n) 
alpar@2235
  2531
    {
alpar@2235
  2532
      std::vector<Edge> v;
alpar@2235
  2533
      for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
alpar@2235
  2534
      if(v.size()) {
alpar@2235
  2535
	std::sort(v.begin(),v.end(),EdgeLess(_g));
alpar@2235
  2536
	_head[n]=refresh_rec(v,0,v.size()-1);
alpar@2235
  2537
      }
alpar@2235
  2538
      else _head[n]=INVALID;
alpar@2235
  2539
    }
alpar@2235
  2540
    ///Refresh the full data structure.
alpar@2235
  2541
alpar@2235
  2542
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  2543
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  2544
    ///
alpar@2235
  2545
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  2546
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  2547
    ///out-degree of the graph.
alpar@2235
  2548
alpar@2235
  2549
    void refresh() 
alpar@2235
  2550
    {
alpar@2235
  2551
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
alpar@2235
  2552
    }
alpar@2235
  2553
    
alpar@2235
  2554
    ///Find an edge between two nodes.
alpar@2235
  2555
    
alpar@2235
  2556
    ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
alpar@2235
  2557
    /// <em>d</em> is the number of outgoing edges of \c s.
alpar@2235
  2558
    ///\param s The source node
alpar@2235
  2559
    ///\param t The target node
alpar@2235
  2560
    ///\return An edge from \c s to \c t if there exists,
alpar@2235
  2561
    ///\ref INVALID otherwise.
alpar@2235
  2562
    ///
alpar@2235
  2563
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  2564
    ///this operator. If you change the outgoing edges of
alpar@2235
  2565
    ///a single node \c n, then
alpar@2235
  2566
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  2567
    ///
alpar@2235
  2568
    Edge operator()(Node s, Node t) const
alpar@2235
  2569
    {
alpar@2235
  2570
      Edge e;
alpar@2235
  2571
      for(e=_head[s];
alpar@2235
  2572
	  e!=INVALID&&_g.target(e)!=t;
alpar@2235
  2573
	  e = t < _g.target(e)?_left[e]:_right[e]) ;
alpar@2235
  2574
      return e;
alpar@2235
  2575
    }
alpar@2235
  2576
alpar@2235
  2577
  };
alpar@2235
  2578
alpar@2235
  2579
  ///Fast look up of all edges between given endpoints.
alpar@2235
  2580
  
alpar@2235
  2581
  ///\ingroup gutils
alpar@2235
  2582
  ///This class is the same as \ref EdgeLookUp, with the addition
alpar@2235
  2583
  ///that it makes it possible to find all edges between given endpoints.
alpar@2235
  2584
  ///
alpar@2235
  2585
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  2586
  ///refresh(Node)) this data structure
alpar@2235
  2587
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  2588
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  2589
  ///
alpar@2235
  2590
  ///\param G The type of the underlying graph.
alpar@2235
  2591
  ///
alpar@2235
  2592
  ///\sa EdgeLookUp  
alpar@2235
  2593
  template<class G>
alpar@2235
  2594
  class AllEdgeLookUp : public EdgeLookUp<G>
alpar@2235
  2595
  {
alpar@2235
  2596
    using EdgeLookUp<G>::_g;
alpar@2235
  2597
    using EdgeLookUp<G>::_right;
alpar@2235
  2598
    using EdgeLookUp<G>::_left;
alpar@2235
  2599
    using EdgeLookUp<G>::_head;
alpar@2235
  2600
alpar@2235
  2601
    GRAPH_TYPEDEFS(typename G)
alpar@2235
  2602
    typedef G Graph;
alpar@2235
  2603
    
alpar@2235
  2604
    typename Graph::template EdgeMap<Edge> _next;
alpar@2235
  2605
    
alpar@2235
  2606
    Edge refreshNext(Edge head,Edge next=INVALID)
alpar@2235
  2607
    {
alpar@2235
  2608
      if(head==INVALID) return next;
alpar@2235
  2609
      else {
alpar@2235
  2610
	next=refreshNext(_right[head],next);
alpar@2235
  2611
// 	_next[head]=next;
alpar@2235
  2612
	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
alpar@2235
  2613
	  ? next : INVALID;
alpar@2235
  2614
	return refreshNext(_left[head],head);
alpar@2235
  2615
      }
alpar@2235
  2616
    }
alpar@2235
  2617
    
alpar@2235
  2618
    void refreshNext()
alpar@2235
  2619
    {
alpar@2235
  2620
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
alpar@2235
  2621
    }
alpar@2235
  2622
    
alpar@2235
  2623
  public:
alpar@2235
  2624
    ///Constructor
alpar@2235
  2625
alpar@2235
  2626
    ///Constructor.
alpar@2235
  2627
    ///
alpar@2235
  2628
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  2629
    ///changes.
alpar@2235
  2630
    AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
alpar@2235
  2631
alpar@2235
  2632
    ///Refresh the data structure at a node.
alpar@2235
  2633
alpar@2235
  2634
    ///Build up the search database of node \c n.
alpar@2235
  2635
    ///
alpar@2235
  2636
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  2637
    ///the number of the outgoing edges of \c n.
alpar@2235
  2638
    
alpar@2235
  2639
    void refresh(Node n) 
alpar@2235
  2640
    {
alpar@2235
  2641
      EdgeLookUp<G>::refresh(n);
alpar@2235
  2642
      refreshNext(_head[n]);
alpar@2235
  2643
    }
alpar@2235
  2644
    
alpar@2235
  2645
    ///Refresh the full data structure.
alpar@2235
  2646
alpar@2235
  2647
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  2648
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  2649
    ///
alpar@2235
  2650
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  2651
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  2652
    ///out-degree of the graph.
alpar@2235
  2653
alpar@2235
  2654
    void refresh() 
alpar@2235
  2655
    {
alpar@2235
  2656
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
alpar@2235
  2657
    }
alpar@2235
  2658
    
alpar@2235
  2659
    ///Find an edge between two nodes.
alpar@2235
  2660
    
alpar@2235
  2661
    ///Find an edge between two nodes.
alpar@2235
  2662
    ///\param s The source node
alpar@2235
  2663
    ///\param t The target node
alpar@2235
  2664
    ///\param prev The previous edge between \c s and \c t. It it is INVALID or
alpar@2235
  2665
    ///not given, the operator finds the first appropriate edge.
alpar@2350
  2666
    ///\return An edge from \c s to \c t after \c prev or
alpar@2235
  2667
    ///\ref INVALID if there is no more.
alpar@2235
  2668
    ///
alpar@2235
  2669
    ///For example, you can count the number of edges from \c u to \c v in the
alpar@2235
  2670
    ///following way.
alpar@2235
  2671
    ///\code
alpar@2235
  2672
    ///AllEdgeLookUp<ListGraph> ae(g);
alpar@2235
  2673
    ///...
alpar@2235
  2674
    ///int n=0;
alpar@2235
  2675
    ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
alpar@2235
  2676
    ///\endcode
alpar@2235
  2677
    ///
alpar@2235
  2678
    ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
alpar@2235
  2679
    /// <em>d</em> is the number of outgoing edges of \c s. Then, the
alpar@2235
  2680
    ///consecutive edges are found in constant time.
alpar@2235
  2681
    ///
alpar@2235
  2682
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  2683
    ///this operator. If you change the outgoing edges of
alpar@2235
  2684
    ///a single node \c n, then
alpar@2235
  2685
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  2686
    ///
alpar@2235
  2687
#ifdef DOXYGEN
alpar@2235
  2688
    Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
alpar@2235
  2689
#else
alpar@2235
  2690
    using EdgeLookUp<G>::operator() ;
alpar@2235
  2691
    Edge operator()(Node s, Node t, Edge prev) const
alpar@2235
  2692
    {
alpar@2235
  2693
      return prev==INVALID?(*this)(s,t):_next[prev];
alpar@2235
  2694
    }
alpar@2235
  2695
#endif
alpar@2235
  2696
      
alpar@2235
  2697
  };
alpar@2235
  2698
alpar@1402
  2699
  /// @}
alpar@1402
  2700
alpar@947
  2701
} //END OF NAMESPACE LEMON
klao@946
  2702
klao@946
  2703
#endif