lemon/graph_utils.h
author deba
Mon, 13 Nov 2006 12:30:59 +0000
changeset 2300 69330d717235
parent 2287 16954ac69517
child 2329 3f4a04a9b7bf
permissions -rw-r--r--
Conditional execution until the target is reached
/previous implementation: until the target is the next to process/

todo: query the target when we give nodemap as condition
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@1956
     5
 * Copyright (C) 2003-2006
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@2235
   400
  ///\se 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@2290
   686
      typename enable_if<typename Graph::CloneableTag, 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@2290
   691
        target.clone(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@2290
   713
      typename enable_if<typename UGraph::CloneableTag, 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@2290
   718
        target.clone(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@2290
   745
      typename enable_if<typename BpUGraph::CloneableTag, 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@2290
   752
        target.clone(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@2286
   793
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2286
   794
        delete nodeMapCopies[i];
deba@1531
   795
      }
deba@2286
   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@2290
   839
    GraphCopy& node(TNode& tnode, const Node& node) {
deba@2290
   840
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2290
   841
                              NodeRefMap, TNode>(tnode, node));
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@2290
   882
    GraphCopy& edge(TEdge& tedge, const Edge& edge) {
deba@2290
   883
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2290
   884
                              EdgeRefMap, TEdge>(tedge, edge));
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@2286
   896
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2286
   897
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2286
   898
      }
deba@2286
   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@2286
   970
        bool forward = (source.direction(key) == 
deba@2286
   971
                        (node_ref[source.source((UEdge)key)] == 
deba@2286
   972
                         target.source(uedge_ref[(UEdge)key])));
deba@2286
   973
	return target.direct(uedge_ref[key], forward); 
deba@1720
   974
      }
deba@1720
   975
      
deba@2286
   976
      const Target& target;
deba@2286
   977
      const Source& source;
deba@2286
   978
      const UEdgeRefMap& uedge_ref;
deba@2286
   979
      const NodeRefMap& node_ref;
deba@1720
   980
    };
deba@2286
   981
deba@1720
   982
    
deba@2286
   983
  public: 
deba@1720
   984
deba@2286
   985
deba@2286
   986
    /// \brief Constructor for the GraphCopy.
deba@1720
   987
    ///
deba@1720
   988
    /// It copies the content of the \c _source graph into the
deba@2286
   989
    /// \c _target graph.
klao@1909
   990
    UGraphCopy(Target& _target, const Source& _source) 
deba@2286
   991
      : source(_source), target(_target) {}
deba@2286
   992
deba@2286
   993
    /// \brief Destructor of the GraphCopy
deba@2286
   994
    ///
deba@2286
   995
    /// Destructor of the GraphCopy
deba@2286
   996
    ~UGraphCopy() {
deba@2286
   997
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2286
   998
        delete nodeMapCopies[i];
deba@1720
   999
      }
deba@2286
  1000
      for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
deba@2286
  1001
        delete edgeMapCopies[i];
deba@1720
  1002
      }
deba@2286
  1003
      for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
deba@2286
  1004
        delete uEdgeMapCopies[i];
deba@2286
  1005
      }
deba@2286
  1006
deba@1720
  1007
    }
deba@1720
  1008
deba@1720
  1009
    /// \brief Copies the node references into the given map.
deba@1720
  1010
    ///
deba@1720
  1011
    /// Copies the node references into the given map.
deba@1720
  1012
    template <typename NodeRef>
deba@2286
  1013
    UGraphCopy& nodeRef(NodeRef& map) {
deba@2286
  1014
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
deba@2286
  1015
                              NodeRefMap, NodeRef>(map));
deba@1720
  1016
      return *this;
deba@1720
  1017
    }
deba@1720
  1018
deba@2290
  1019
    /// \brief Copies the node cross references into the given map.
deba@1720
  1020
    ///
deba@2290
  1021
    ///  Copies the node cross references (reverse references) into
deba@2290
  1022
    ///  the given map.
deba@2286
  1023
    template <typename NodeCrossRef>
deba@2286
  1024
    UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2286
  1025
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
deba@2286
  1026
                              NodeRefMap, NodeCrossRef>(map));
deba@1720
  1027
      return *this;
deba@1720
  1028
    }
deba@1720
  1029
deba@1720
  1030
    /// \brief Make copy of the given map.
deba@1720
  1031
    ///
deba@1720
  1032
    /// Makes copy of the given map for the newly created graph. 
deba@1720
  1033
    /// The new map's key type is the target graph's node type,
deba@1720
  1034
    /// and the copied map's key type is the source graph's node
deba@1720
  1035
    /// type.  
deba@1720
  1036
    template <typename TargetMap, typename SourceMap>
deba@2286
  1037
    UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1038
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
deba@2286
  1039
                              NodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2286
  1040
      return *this;
deba@2286
  1041
    }
deba@2286
  1042
deba@2290
  1043
    /// \brief Make a copy of the given node.
deba@2290
  1044
    ///
deba@2290
  1045
    /// Make a copy of the given node.
deba@2290
  1046
    UGraphCopy& node(TNode& tnode, const Node& node) {
deba@2290
  1047
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2290
  1048
                              NodeRefMap, TNode>(tnode, node));
deba@2290
  1049
      return *this;
deba@2290
  1050
    }
deba@2290
  1051
deba@2286
  1052
    /// \brief Copies the edge references into the given map.
deba@2286
  1053
    ///
deba@2286
  1054
    /// Copies the edge references into the given map.
deba@2286
  1055
    template <typename EdgeRef>
deba@2286
  1056
    UGraphCopy& edgeRef(EdgeRef& map) {
deba@2286
  1057
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
deba@2286
  1058
                              EdgeRefMap, EdgeRef>(map));
deba@2286
  1059
      return *this;
deba@2286
  1060
    }
deba@2286
  1061
deba@2290
  1062
    /// \brief Copies the edge cross references into the given map.
deba@2286
  1063
    ///
deba@2290
  1064
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1065
    ///  the given map.
deba@2286
  1066
    template <typename EdgeCrossRef>
deba@2286
  1067
    UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2286
  1068
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
deba@2286
  1069
                              EdgeRefMap, EdgeCrossRef>(map));
deba@1720
  1070
      return *this;
deba@1720
  1071
    }
deba@1720
  1072
deba@1720
  1073
    /// \brief Make copy of the given map.
deba@1720
  1074
    ///
deba@1720
  1075
    /// Makes copy of the given map for the newly created graph. 
deba@1720
  1076
    /// The new map's key type is the target graph's edge type,
deba@1720
  1077
    /// and the copied map's key type is the source graph's edge
deba@1720
  1078
    /// type.  
deba@1720
  1079
    template <typename TargetMap, typename SourceMap>
deba@2286
  1080
    UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1081
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
deba@2286
  1082
                              EdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2286
  1083
      return *this;
deba@2286
  1084
    }
deba@2286
  1085
deba@2290
  1086
    /// \brief Make a copy of the given edge.
deba@2286
  1087
    ///
deba@2290
  1088
    /// Make a copy of the given edge.
deba@2290
  1089
    UGraphCopy& edge(TEdge& tedge, const Edge& edge) {
deba@2290
  1090
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2290
  1091
                              EdgeRefMap, TEdge>(tedge, edge));
deba@2290
  1092
      return *this;
deba@2290
  1093
    }
deba@2290
  1094
deba@2290
  1095
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1096
    ///
deba@2290
  1097
    /// Copies the undirected edge references into the given map.
deba@2286
  1098
    template <typename UEdgeRef>
deba@2286
  1099
    UGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2286
  1100
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
deba@2286
  1101
                               UEdgeRefMap, UEdgeRef>(map));
deba@2286
  1102
      return *this;
deba@2286
  1103
    }
deba@2286
  1104
deba@2290
  1105
    /// \brief Copies the undirected edge cross references into the given map.
deba@2286
  1106
    ///
deba@2290
  1107
    /// Copies the undirected edge cross references (reverse
deba@2290
  1108
    /// references) into the given map.
deba@2286
  1109
    template <typename UEdgeCrossRef>
deba@2286
  1110
    UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2286
  1111
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2286
  1112
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@1720
  1113
      return *this;
deba@1720
  1114
    }
deba@1720
  1115
deba@1720
  1116
    /// \brief Make copy of the given map.
deba@1720
  1117
    ///
deba@1720
  1118
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1119
    /// The new map's key type is the target graph's undirected edge type,
deba@2290
  1120
    /// and the copied map's key type is the source graph's undirected edge
deba@1720
  1121
    /// type.  
deba@1720
  1122
    template <typename TargetMap, typename SourceMap>
deba@2286
  1123
    UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2286
  1124
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
deba@2286
  1125
                               UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@1720
  1126
      return *this;
deba@1720
  1127
    }
deba@1720
  1128
deba@2290
  1129
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1130
    ///
deba@2290
  1131
    /// Make a copy of the given undirected edge.
deba@2290
  1132
    UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
deba@2290
  1133
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
deba@2290
  1134
                               UEdgeRefMap, TUEdge>(tuedge, uedge));
deba@2290
  1135
      return *this;
deba@2290
  1136
    }
deba@2290
  1137
deba@2286
  1138
    /// \brief Executes the copies.
deba@1720
  1139
    ///
deba@2286
  1140
    /// Executes the copies.
deba@2286
  1141
    void run() {
deba@2286
  1142
      NodeRefMap nodeRefMap(source);
deba@2290
  1143
      UEdgeRefMap uEdgeRefMap(source);
deba@2290
  1144
      EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
deba@2290
  1145
      _graph_utils_bits::UGraphCopySelector<Target>::
deba@2290
  1146
        copy(target, source, nodeRefMap, uEdgeRefMap);
deba@2286
  1147
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2286
  1148
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2286
  1149
      }
deba@2286
  1150
      for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
deba@2286
  1151
        uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
deba@2286
  1152
      }
deba@2286
  1153
      for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
deba@2286
  1154
        edgeMapCopies[i]->copy(source, edgeRefMap);
deba@2286
  1155
      }
deba@1720
  1156
    }
deba@1720
  1157
deba@1720
  1158
  private:
deba@1192
  1159
    
deba@1720
  1160
    const Source& source;
deba@1720
  1161
    Target& target;
alpar@947
  1162
deba@2286
  1163
    std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
deba@2286
  1164
    nodeMapCopies;
deba@2286
  1165
deba@2286
  1166
    std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
deba@2286
  1167
    edgeMapCopies;
deba@2286
  1168
deba@2286
  1169
    std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
deba@2286
  1170
    uEdgeMapCopies;
deba@2286
  1171
deba@1192
  1172
  };
deba@1192
  1173
deba@2290
  1174
  /// \brief Copy an undirected graph to another graph.
deba@1720
  1175
  ///
deba@2290
  1176
  /// Copy an undirected graph to another graph.
deba@1720
  1177
  /// The usage of the function:
deba@1720
  1178
  /// 
alpar@1946
  1179
  ///\code
deba@2286
  1180
  /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
alpar@1946
  1181
  ///\endcode
deba@1720
  1182
  /// 
deba@1720
  1183
  /// After the copy the \c nr map will contain the mapping from the
deba@1720
  1184
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@1720
  1185
  /// contain the mapping from the target graph's edges to the source's
deba@1720
  1186
  /// edges.
deba@2290
  1187
  ///
deba@2290
  1188
  /// \see UGraphCopy 
deba@1720
  1189
  template <typename Target, typename Source>
klao@1909
  1190
  UGraphCopy<Target, Source> 
klao@1909
  1191
  copyUGraph(Target& target, const Source& source) {
klao@1909
  1192
    return UGraphCopy<Target, Source>(target, source);
deba@1720
  1193
  }
deba@1192
  1194
deba@2290
  1195
  /// \brief Class to copy a bipartite undirected graph.
deba@2290
  1196
  ///
deba@2290
  1197
  /// Class to copy a bipartite undirected graph to another graph
deba@2290
  1198
  /// (duplicate a graph).  The simplest way of using it is through
deba@2290
  1199
  /// the \c copyBpUGraph() function.
deba@2290
  1200
  template <typename Target, typename Source>
deba@2290
  1201
  class BpUGraphCopy {
deba@2290
  1202
  private:
deba@2290
  1203
deba@2290
  1204
    typedef typename Source::Node Node;
deba@2290
  1205
    typedef typename Source::ANode ANode;
deba@2290
  1206
    typedef typename Source::BNode BNode;
deba@2290
  1207
    typedef typename Source::NodeIt NodeIt;
deba@2290
  1208
    typedef typename Source::Edge Edge;
deba@2290
  1209
    typedef typename Source::EdgeIt EdgeIt;
deba@2290
  1210
    typedef typename Source::UEdge UEdge;
deba@2290
  1211
    typedef typename Source::UEdgeIt UEdgeIt;
deba@2290
  1212
deba@2290
  1213
    typedef typename Target::Node TNode;
deba@2290
  1214
    typedef typename Target::Edge TEdge;
deba@2290
  1215
    typedef typename Target::UEdge TUEdge;
deba@2290
  1216
deba@2290
  1217
    typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
deba@2290
  1218
    typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
deba@2290
  1219
    typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
deba@2290
  1220
deba@2290
  1221
    struct NodeRefMap {
deba@2290
  1222
      NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
deba@2290
  1223
                 const BNodeRefMap& _bnode_ref)
deba@2290
  1224
        : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
deba@2290
  1225
deba@2290
  1226
      typedef typename Source::Node Key;
deba@2290
  1227
      typedef typename Target::Node Value;
deba@2290
  1228
deba@2290
  1229
      Value operator[](const Key& key) const {
deba@2290
  1230
	return source.aNode(key) ? anode_ref[key] : bnode_ref[key]; 
deba@2290
  1231
      }
deba@2290
  1232
      
deba@2290
  1233
      const Source& source;
deba@2290
  1234
      const ANodeRefMap& anode_ref;
deba@2290
  1235
      const BNodeRefMap& bnode_ref;
deba@2290
  1236
    };
deba@2290
  1237
deba@2290
  1238
    struct EdgeRefMap {
deba@2290
  1239
      EdgeRefMap(const Target& _target, const Source& _source,
deba@2290
  1240
                 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
deba@2290
  1241
        : target(_target), source(_source), 
deba@2290
  1242
          uedge_ref(_uedge_ref), node_ref(_node_ref) {}
deba@2290
  1243
deba@2290
  1244
      typedef typename Source::Edge Key;
deba@2290
  1245
      typedef typename Target::Edge Value;
deba@2290
  1246
deba@2290
  1247
      Value operator[](const Key& key) const {
deba@2290
  1248
        bool forward = (source.direction(key) == 
deba@2290
  1249
                        (node_ref[source.source((UEdge)key)] == 
deba@2290
  1250
                         target.source(uedge_ref[(UEdge)key])));
deba@2290
  1251
	return target.direct(uedge_ref[key], forward); 
deba@2290
  1252
      }
deba@2290
  1253
      
deba@2290
  1254
      const Target& target;
deba@2290
  1255
      const Source& source;
deba@2290
  1256
      const UEdgeRefMap& uedge_ref;
deba@2290
  1257
      const NodeRefMap& node_ref;
deba@2290
  1258
    };
deba@2290
  1259
    
deba@2290
  1260
  public: 
deba@2290
  1261
deba@2290
  1262
deba@2290
  1263
    /// \brief Constructor for the GraphCopy.
deba@2290
  1264
    ///
deba@2290
  1265
    /// It copies the content of the \c _source graph into the
deba@2290
  1266
    /// \c _target graph.
deba@2290
  1267
    BpUGraphCopy(Target& _target, const Source& _source) 
deba@2290
  1268
      : source(_source), target(_target) {}
deba@2290
  1269
deba@2290
  1270
    /// \brief Destructor of the GraphCopy
deba@2290
  1271
    ///
deba@2290
  1272
    /// Destructor of the GraphCopy
deba@2290
  1273
    ~BpUGraphCopy() {
deba@2290
  1274
      for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
deba@2290
  1275
        delete aNodeMapCopies[i];
deba@2290
  1276
      }
deba@2290
  1277
      for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
deba@2290
  1278
        delete bNodeMapCopies[i];
deba@2290
  1279
      }
deba@2290
  1280
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2290
  1281
        delete nodeMapCopies[i];
deba@2290
  1282
      }
deba@2290
  1283
      for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
deba@2290
  1284
        delete edgeMapCopies[i];
deba@2290
  1285
      }
deba@2290
  1286
      for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
deba@2290
  1287
        delete uEdgeMapCopies[i];
deba@2290
  1288
      }
deba@2290
  1289
deba@2290
  1290
    }
deba@2290
  1291
deba@2290
  1292
    /// \brief Copies the A-node references into the given map.
deba@2290
  1293
    ///
deba@2290
  1294
    /// Copies the A-node references into the given map.
deba@2290
  1295
    template <typename ANodeRef>
deba@2290
  1296
    BpUGraphCopy& aNodeRef(ANodeRef& map) {
deba@2290
  1297
      aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode, 
deba@2290
  1298
                               ANodeRefMap, ANodeRef>(map));
deba@2290
  1299
      return *this;
deba@2290
  1300
    }
deba@2290
  1301
deba@2290
  1302
    /// \brief Copies the A-node cross references into the given map.
deba@2290
  1303
    ///
deba@2290
  1304
    /// Copies the A-node cross references (reverse references) into
deba@2290
  1305
    /// the given map.
deba@2290
  1306
    template <typename ANodeCrossRef>
deba@2290
  1307
    BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
deba@2290
  1308
      aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1309
                               ANode, ANodeRefMap, ANodeCrossRef>(map));
deba@2290
  1310
      return *this;
deba@2290
  1311
    }
deba@2290
  1312
deba@2290
  1313
    /// \brief Make copy of the given A-node map.
deba@2290
  1314
    ///
deba@2290
  1315
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1316
    /// The new map's key type is the target graph's node type,
deba@2290
  1317
    /// and the copied map's key type is the source graph's node
deba@2290
  1318
    /// type.  
deba@2290
  1319
    template <typename TargetMap, typename SourceMap>
deba@2290
  1320
    BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1321
      aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode, 
deba@2290
  1322
                               ANodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1323
      return *this;
deba@2290
  1324
    }
deba@2290
  1325
deba@2290
  1326
    /// \brief Copies the B-node references into the given map.
deba@2290
  1327
    ///
deba@2290
  1328
    /// Copies the B-node references into the given map.
deba@2290
  1329
    template <typename BNodeRef>
deba@2290
  1330
    BpUGraphCopy& bNodeRef(BNodeRef& map) {
deba@2290
  1331
      bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode, 
deba@2290
  1332
                               BNodeRefMap, BNodeRef>(map));
deba@2290
  1333
      return *this;
deba@2290
  1334
    }
deba@2290
  1335
deba@2290
  1336
    /// \brief Copies the B-node cross references into the given map.
deba@2290
  1337
    ///
deba@2290
  1338
    ///  Copies the B-node cross references (reverse references) into
deba@2290
  1339
    ///  the given map.
deba@2290
  1340
    template <typename BNodeCrossRef>
deba@2290
  1341
    BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
deba@2290
  1342
      bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1343
                              BNode, BNodeRefMap, BNodeCrossRef>(map));
deba@2290
  1344
      return *this;
deba@2290
  1345
    }
deba@2290
  1346
deba@2290
  1347
    /// \brief Make copy of the given B-node map.
deba@2290
  1348
    ///
deba@2290
  1349
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1350
    /// The new map's key type is the target graph's node type,
deba@2290
  1351
    /// and the copied map's key type is the source graph's node
deba@2290
  1352
    /// type.  
deba@2290
  1353
    template <typename TargetMap, typename SourceMap>
deba@2290
  1354
    BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1355
      bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode, 
deba@2290
  1356
                               BNodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1357
      return *this;
deba@2290
  1358
    }
deba@2290
  1359
    /// \brief Copies the node references into the given map.
deba@2290
  1360
    ///
deba@2290
  1361
    /// Copies the node references into the given map.
deba@2290
  1362
    template <typename NodeRef>
deba@2290
  1363
    BpUGraphCopy& nodeRef(NodeRef& map) {
deba@2290
  1364
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node, 
deba@2290
  1365
                              NodeRefMap, NodeRef>(map));
deba@2290
  1366
      return *this;
deba@2290
  1367
    }
deba@2290
  1368
deba@2290
  1369
    /// \brief Copies the node cross references into the given map.
deba@2290
  1370
    ///
deba@2290
  1371
    ///  Copies the node cross references (reverse references) into
deba@2290
  1372
    ///  the given map.
deba@2290
  1373
    template <typename NodeCrossRef>
deba@2290
  1374
    BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2290
  1375
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
deba@2290
  1376
                              NodeRefMap, NodeCrossRef>(map));
deba@2290
  1377
      return *this;
deba@2290
  1378
    }
deba@2290
  1379
deba@2290
  1380
    /// \brief Make copy of the given map.
deba@2290
  1381
    ///
deba@2290
  1382
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1383
    /// The new map's key type is the target graph's node type,
deba@2290
  1384
    /// and the copied map's key type is the source graph's node
deba@2290
  1385
    /// type.  
deba@2290
  1386
    template <typename TargetMap, typename SourceMap>
deba@2290
  1387
    BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1388
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node, 
deba@2290
  1389
                              NodeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1390
      return *this;
deba@2290
  1391
    }
deba@2290
  1392
deba@2290
  1393
    /// \brief Make a copy of the given node.
deba@2290
  1394
    ///
deba@2290
  1395
    /// Make a copy of the given node.
deba@2290
  1396
    BpUGraphCopy& node(TNode& tnode, const Node& node) {
deba@2290
  1397
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node, 
deba@2290
  1398
                              NodeRefMap, TNode>(tnode, node));
deba@2290
  1399
      return *this;
deba@2290
  1400
    }
deba@2290
  1401
deba@2290
  1402
    /// \brief Copies the edge references into the given map.
deba@2290
  1403
    ///
deba@2290
  1404
    /// Copies the edge references into the given map.
deba@2290
  1405
    template <typename EdgeRef>
deba@2290
  1406
    BpUGraphCopy& edgeRef(EdgeRef& map) {
deba@2290
  1407
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge, 
deba@2290
  1408
                              EdgeRefMap, EdgeRef>(map));
deba@2290
  1409
      return *this;
deba@2290
  1410
    }
deba@2290
  1411
deba@2290
  1412
    /// \brief Copies the edge cross references into the given map.
deba@2290
  1413
    ///
deba@2290
  1414
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1415
    ///  the given map.
deba@2290
  1416
    template <typename EdgeCrossRef>
deba@2290
  1417
    BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2290
  1418
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
deba@2290
  1419
                              EdgeRefMap, EdgeCrossRef>(map));
deba@2290
  1420
      return *this;
deba@2290
  1421
    }
deba@2290
  1422
deba@2290
  1423
    /// \brief Make copy of the given map.
deba@2290
  1424
    ///
deba@2290
  1425
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1426
    /// The new map's key type is the target graph's edge type,
deba@2290
  1427
    /// and the copied map's key type is the source graph's edge
deba@2290
  1428
    /// type.  
deba@2290
  1429
    template <typename TargetMap, typename SourceMap>
deba@2290
  1430
    BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1431
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge, 
deba@2290
  1432
                              EdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1433
      return *this;
deba@2290
  1434
    }
deba@2290
  1435
deba@2290
  1436
    /// \brief Make a copy of the given edge.
deba@2290
  1437
    ///
deba@2290
  1438
    /// Make a copy of the given edge.
deba@2290
  1439
    BpUGraphCopy& edge(TEdge& tedge, const Edge& edge) {
deba@2290
  1440
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge, 
deba@2290
  1441
                              EdgeRefMap, TEdge>(tedge, edge));
deba@2290
  1442
      return *this;
deba@2290
  1443
    }
deba@2290
  1444
deba@2290
  1445
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1446
    ///
deba@2290
  1447
    /// Copies the undirected edge references into the given map.
deba@2290
  1448
    template <typename UEdgeRef>
deba@2290
  1449
    BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2290
  1450
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge, 
deba@2290
  1451
                               UEdgeRefMap, UEdgeRef>(map));
deba@2290
  1452
      return *this;
deba@2290
  1453
    }
deba@2290
  1454
deba@2290
  1455
    /// \brief Copies the undirected edge cross references into the given map.
deba@2290
  1456
    ///
deba@2290
  1457
    /// Copies the undirected edge cross references (reverse
deba@2290
  1458
    /// references) into the given map.
deba@2290
  1459
    template <typename UEdgeCrossRef>
deba@2290
  1460
    BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2290
  1461
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, 
deba@2290
  1462
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@2290
  1463
      return *this;
deba@2290
  1464
    }
deba@2290
  1465
deba@2290
  1466
    /// \brief Make copy of the given map.
deba@2290
  1467
    ///
deba@2290
  1468
    /// Makes copy of the given map for the newly created graph. 
deba@2290
  1469
    /// The new map's key type is the target graph's undirected edge type,
deba@2290
  1470
    /// and the copied map's key type is the source graph's undirected edge
deba@2290
  1471
    /// type.  
deba@2290
  1472
    template <typename TargetMap, typename SourceMap>
deba@2290
  1473
    BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
deba@2290
  1474
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge, 
deba@2290
  1475
                               UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
deba@2290
  1476
      return *this;
deba@2290
  1477
    }
deba@2290
  1478
deba@2290
  1479
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1480
    ///
deba@2290
  1481
    /// Make a copy of the given undirected edge.
deba@2290
  1482
    BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
deba@2290
  1483
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge, 
deba@2290
  1484
                               UEdgeRefMap, TUEdge>(tuedge, uedge));
deba@2290
  1485
      return *this;
deba@2290
  1486
    }
deba@2290
  1487
deba@2290
  1488
    /// \brief Executes the copies.
deba@2290
  1489
    ///
deba@2290
  1490
    /// Executes the copies.
deba@2290
  1491
    void run() {
deba@2290
  1492
      ANodeRefMap aNodeRefMap(source);
deba@2290
  1493
      BNodeRefMap bNodeRefMap(source);
deba@2290
  1494
      NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
deba@2290
  1495
      UEdgeRefMap uEdgeRefMap(source);
deba@2290
  1496
      EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
deba@2290
  1497
      _graph_utils_bits::BpUGraphCopySelector<Target>::
deba@2290
  1498
        copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
deba@2290
  1499
      for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
deba@2290
  1500
        aNodeMapCopies[i]->copy(source, aNodeRefMap);
deba@2290
  1501
      }
deba@2290
  1502
      for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
deba@2290
  1503
        bNodeMapCopies[i]->copy(source, bNodeRefMap);
deba@2290
  1504
      }
deba@2290
  1505
      for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
deba@2290
  1506
        nodeMapCopies[i]->copy(source, nodeRefMap);
deba@2290
  1507
      }
deba@2290
  1508
      for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
deba@2290
  1509
        uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
deba@2290
  1510
      }
deba@2290
  1511
      for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
deba@2290
  1512
        edgeMapCopies[i]->copy(source, edgeRefMap);
deba@2290
  1513
      }
deba@2290
  1514
    }
deba@2290
  1515
deba@2290
  1516
  private:
deba@2290
  1517
    
deba@2290
  1518
    const Source& source;
deba@2290
  1519
    Target& target;
deba@2290
  1520
deba@2290
  1521
    std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* > 
deba@2290
  1522
    aNodeMapCopies;
deba@2290
  1523
deba@2290
  1524
    std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* > 
deba@2290
  1525
    bNodeMapCopies;
deba@2290
  1526
deba@2290
  1527
    std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* > 
deba@2290
  1528
    nodeMapCopies;
deba@2290
  1529
deba@2290
  1530
    std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* > 
deba@2290
  1531
    edgeMapCopies;
deba@2290
  1532
deba@2290
  1533
    std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* > 
deba@2290
  1534
    uEdgeMapCopies;
deba@2290
  1535
deba@2290
  1536
  };
deba@2290
  1537
deba@2290
  1538
  /// \brief Copy a bipartite undirected graph to another graph.
deba@2290
  1539
  ///
deba@2290
  1540
  /// Copy a bipartite undirected graph to another graph.
deba@2290
  1541
  /// The usage of the function:
deba@2290
  1542
  /// 
deba@2290
  1543
  ///\code
deba@2290
  1544
  /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
deba@2290
  1545
  ///\endcode
deba@2290
  1546
  /// 
deba@2290
  1547
  /// After the copy the \c nr map will contain the mapping from the
deba@2290
  1548
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@2290
  1549
  /// contain the mapping from the target graph's edges to the source's
deba@2290
  1550
  /// edges.
deba@2290
  1551
  ///
deba@2290
  1552
  /// \see BpUGraphCopy
deba@2290
  1553
  template <typename Target, typename Source>
deba@2290
  1554
  BpUGraphCopy<Target, Source> 
deba@2290
  1555
  copyBpUGraph(Target& target, const Source& source) {
deba@2290
  1556
    return BpUGraphCopy<Target, Source>(target, source);
deba@2290
  1557
  }
deba@2290
  1558
deba@1192
  1559
deba@1192
  1560
  /// @}
alpar@1402
  1561
alpar@1402
  1562
  /// \addtogroup graph_maps
alpar@1402
  1563
  /// @{
alpar@1402
  1564
deba@1413
  1565
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
  1566
athos@1540
  1567
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
  1568
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
  1569
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
  1570
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
  1571
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
  1572
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
  1573
  /// class \c InverseMap.
deba@1413
  1574
  ///
deba@1413
  1575
  template <typename _Graph, typename _Item>
deba@1413
  1576
  class IdMap {
deba@1413
  1577
  public:
deba@1413
  1578
    typedef _Graph Graph;
deba@1413
  1579
    typedef int Value;
deba@1413
  1580
    typedef _Item Item;
deba@1413
  1581
    typedef _Item Key;
deba@1413
  1582
deba@1413
  1583
    /// \brief Constructor.
deba@1413
  1584
    ///
deba@1413
  1585
    /// Constructor for creating id map.
deba@2286
  1586
    explicit IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1587
deba@1413
  1588
    /// \brief Gives back the \e id of the item.
deba@1413
  1589
    ///
deba@1413
  1590
    /// Gives back the immutable and unique \e id of the map.
deba@1413
  1591
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
  1592
deba@1413
  1593
deba@1413
  1594
  private:
deba@1413
  1595
    const Graph* graph;
deba@1413
  1596
deba@1413
  1597
  public:
deba@1413
  1598
athos@1540
  1599
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
  1600
    ///
athos@1540
  1601
    /// The class represents the inverse of its owner (IdMap).
deba@1413
  1602
    /// \see inverse()
deba@1413
  1603
    class InverseMap {
deba@1413
  1604
    public:
deba@1419
  1605
deba@1413
  1606
      /// \brief Constructor.
deba@1413
  1607
      ///
deba@1413
  1608
      /// Constructor for creating an id-to-item map.
deba@2286
  1609
      explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1610
deba@1413
  1611
      /// \brief Constructor.
deba@1413
  1612
      ///
deba@1413
  1613
      /// Constructor for creating an id-to-item map.
deba@2286
  1614
      explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
  1615
deba@1413
  1616
      /// \brief Gives back the given item from its id.
deba@1413
  1617
      ///
deba@1413
  1618
      /// Gives back the given item from its id.
deba@1413
  1619
      /// 
deba@1413
  1620
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
  1621
    private:
deba@1413
  1622
      const Graph* graph;
deba@1413
  1623
    };
deba@1413
  1624
deba@1413
  1625
    /// \brief Gives back the inverse of the map.
deba@1413
  1626
    ///
athos@1540
  1627
    /// Gives back the inverse of the IdMap.
deba@1413
  1628
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
  1629
deba@1413
  1630
  };
deba@1413
  1631
deba@1413
  1632
  
athos@1526
  1633
  /// \brief General invertable graph-map type.
alpar@1402
  1634
athos@1540
  1635
  /// This type provides simple invertable graph-maps. 
athos@1526
  1636
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
  1637
  /// and if a key is set to a new value then store it
alpar@1402
  1638
  /// in the inverse map.
deba@1931
  1639
  ///
deba@1931
  1640
  /// The values of the map can be accessed
deba@1931
  1641
  /// with stl compatible forward iterator.
deba@1931
  1642
  ///
alpar@1402
  1643
  /// \param _Graph The graph type.
deba@1830
  1644
  /// \param _Item The item type of the graph.
deba@1830
  1645
  /// \param _Value The value type of the map.
deba@1931
  1646
  ///
deba@1931
  1647
  /// \see IterableValueMap
deba@1830
  1648
  template <typename _Graph, typename _Item, typename _Value>
deba@2287
  1649
  class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
deba@1931
  1650
  private:
deba@1931
  1651
    
deba@2287
  1652
    typedef DefaultMap<_Graph, _Item, _Value> Map;
deba@1931
  1653
    typedef _Graph Graph;
deba@1931
  1654
deba@2287
  1655
    typedef std::map<_Value, _Item> Container;
deba@1931
  1656
    Container invMap;    
deba@1931
  1657
deba@1931
  1658
  public:
deba@1931
  1659
 
deba@2287
  1660
    /// The key type of InvertableMap (Node, Edge, UEdge).
deba@2287
  1661
    typedef typename Map::Key Key;
deba@2287
  1662
    /// The value type of the InvertableMap.
deba@2287
  1663
    typedef typename Map::Value Value;
deba@2287
  1664
deba@1931
  1665
deba@1931
  1666
alpar@1402
  1667
    /// \brief Constructor.
alpar@1402
  1668
    ///
deba@1413
  1669
    /// Construct a new InvertableMap for the graph.
alpar@1402
  1670
    ///
deba@2286
  1671
    explicit InvertableMap(const Graph& graph) : Map(graph) {} 
deba@1931
  1672
deba@1931
  1673
    /// \brief Forward iterator for values.
deba@1931
  1674
    ///
deba@1931
  1675
    /// This iterator is an stl compatible forward
deba@1931
  1676
    /// iterator on the values of the map. The values can
deba@1931
  1677
    /// be accessed in the [beginValue, endValue) range.
deba@1931
  1678
    ///
deba@1931
  1679
    class ValueIterator 
deba@1931
  1680
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
  1681
      friend class InvertableMap;
deba@1931
  1682
    private:
deba@1931
  1683
      ValueIterator(typename Container::const_iterator _it) 
deba@1931
  1684
        : it(_it) {}
deba@1931
  1685
    public:
deba@1931
  1686
      
deba@1931
  1687
      ValueIterator() {}
deba@1931
  1688
deba@1931
  1689
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
  1690
      ValueIterator operator++(int) { 
deba@1931
  1691
        ValueIterator tmp(*this); 
deba@1931
  1692
        operator++();
deba@1931
  1693
        return tmp; 
deba@1931
  1694
      }
deba@1931
  1695
deba@1931
  1696
      const Value& operator*() const { return it->first; }
deba@1931
  1697
      const Value* operator->() const { return &(it->first); }
deba@1931
  1698
deba@1931
  1699
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
  1700
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
  1701
      
deba@1931
  1702
    private:
deba@1931
  1703
      typename Container::const_iterator it;
deba@1931
  1704
    };
deba@1931
  1705
deba@1931
  1706
    /// \brief Returns an iterator to the first value.
deba@1931
  1707
    ///
deba@1931
  1708
    /// Returns an stl compatible iterator to the 
deba@1931
  1709
    /// first value of the map. The values of the
deba@1931
  1710
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1711
    /// range.
deba@1931
  1712
    ValueIterator beginValue() const {
deba@1931
  1713
      return ValueIterator(invMap.begin());
deba@1931
  1714
    }
deba@1931
  1715
deba@1931
  1716
    /// \brief Returns an iterator after the last value.
deba@1931
  1717
    ///
deba@1931
  1718
    /// Returns an stl compatible iterator after the 
deba@1931
  1719
    /// last value of the map. The values of the
deba@1931
  1720
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1721
    /// range.
deba@1931
  1722
    ValueIterator endValue() const {
deba@1931
  1723
      return ValueIterator(invMap.end());
deba@1931
  1724
    }
alpar@1402
  1725
    
alpar@1402
  1726
    /// \brief The setter function of the map.
alpar@1402
  1727
    ///
deba@1413
  1728
    /// Sets the mapped value.
alpar@1402
  1729
    void set(const Key& key, const Value& val) {
alpar@1402
  1730
      Value oldval = Map::operator[](key);
deba@1413
  1731
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
  1732
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1733
	invMap.erase(it);
alpar@1402
  1734
      }      
alpar@1402
  1735
      invMap.insert(make_pair(val, key));
alpar@1402
  1736
      Map::set(key, val);
alpar@1402
  1737
    }
alpar@1402
  1738
alpar@1402
  1739
    /// \brief The getter function of the map.
alpar@1402
  1740
    ///
alpar@1402
  1741
    /// It gives back the value associated with the key.
deba@1931
  1742
    typename MapTraits<Map>::ConstReturnValue 
deba@1931
  1743
    operator[](const Key& key) const {
alpar@1402
  1744
      return Map::operator[](key);
alpar@1402
  1745
    }
alpar@1402
  1746
deba@1515
  1747
  protected:
deba@1515
  1748
alpar@1402
  1749
    /// \brief Erase the key from the map.
alpar@1402
  1750
    ///
alpar@1402
  1751
    /// Erase the key to the map. It is called by the
alpar@1402
  1752
    /// \c AlterationNotifier.
alpar@1402
  1753
    virtual void erase(const Key& key) {
alpar@1402
  1754
      Value val = Map::operator[](key);
deba@1413
  1755
      typename Container::iterator it = invMap.find(val);
alpar@1402
  1756
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1757
	invMap.erase(it);
alpar@1402
  1758
      }
alpar@1402
  1759
      Map::erase(key);
alpar@1402
  1760
    }
alpar@1402
  1761
deba@1829
  1762
    /// \brief Erase more keys from the map.
deba@1829
  1763
    ///
deba@1829
  1764
    /// Erase more keys from the map. It is called by the
deba@1829
  1765
    /// \c AlterationNotifier.
deba@1829
  1766
    virtual void erase(const std::vector<Key>& keys) {
deba@1829
  1767
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1768
	Value val = Map::operator[](keys[i]);
deba@1829
  1769
	typename Container::iterator it = invMap.find(val);
deba@1829
  1770
	if (it != invMap.end() && it->second == keys[i]) {
deba@1829
  1771
	  invMap.erase(it);
deba@1829
  1772
	}
deba@1829
  1773
      }
deba@1829
  1774
      Map::erase(keys);
deba@1829
  1775
    }
deba@1829
  1776
alpar@1402
  1777
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
  1778
    ///
alpar@1402
  1779
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
  1780
    /// \c AlterationNotifier.
alpar@1402
  1781
    virtual void clear() {
alpar@1402
  1782
      invMap.clear();
alpar@1402
  1783
      Map::clear();
alpar@1402
  1784
    }
alpar@1402
  1785
deba@1413
  1786
  public:
deba@1413
  1787
deba@1413
  1788
    /// \brief The inverse map type.
deba@1413
  1789
    ///
deba@1413
  1790
    /// The inverse of this map. The subscript operator of the map
deba@1413
  1791
    /// gives back always the item what was last assigned to the value. 
deba@1413
  1792
    class InverseMap {
deba@1413
  1793
    public:
deba@1413
  1794
      /// \brief Constructor of the InverseMap.
deba@1413
  1795
      ///
deba@1413
  1796
      /// Constructor of the InverseMap.
deba@2286
  1797
      explicit InverseMap(const InvertableMap& _inverted) 
deba@2286
  1798
        : inverted(_inverted) {}
deba@1413
  1799
deba@1413
  1800
      /// The value type of the InverseMap.
deba@1413
  1801
      typedef typename InvertableMap::Key Value;
deba@1413
  1802
      /// The key type of the InverseMap.
deba@1413
  1803
      typedef typename InvertableMap::Value Key; 
deba@1413
  1804
deba@1413
  1805
      /// \brief Subscript operator. 
deba@1413
  1806
      ///
deba@1413
  1807
      /// Subscript operator. It gives back always the item 
deba@1413
  1808
      /// what was last assigned to the value.
deba@1413
  1809
      Value operator[](const Key& key) const {
deba@1413
  1810
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
  1811
	return it->second;
deba@1413
  1812
      }
deba@1413
  1813
      
deba@1413
  1814
    private:
deba@1413
  1815
      const InvertableMap& inverted;
deba@1413
  1816
    };
deba@1413
  1817
alpar@2094
  1818
    /// \brief It gives back the just readable inverse map.
alpar@1402
  1819
    ///
alpar@2094
  1820
    /// It gives back the just readable inverse map.
deba@1413
  1821
    InverseMap inverse() const {
deba@1413
  1822
      return InverseMap(*this);
alpar@1402
  1823
    } 
alpar@1402
  1824
alpar@1402
  1825
deba@1413
  1826
    
alpar@1402
  1827
  };
alpar@1402
  1828
alpar@1402
  1829
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
  1830
  /// item in the graph.
alpar@1402
  1831
  ///
athos@1540
  1832
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
  1833
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
  1834
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
  1835
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
  1836
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
  1837
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
  1838
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
  1839
  /// with its member class \c InverseMap.
alpar@1402
  1840
  ///
alpar@1402
  1841
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
  1842
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
klao@1909
  1843
  /// UEdge.
deba@1830
  1844
  template <typename _Graph, typename _Item>
deba@2287
  1845
  class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
alpar@1402
  1846
alpar@1402
  1847
    typedef _Item Item;
deba@2287
  1848
    typedef DefaultMap<_Graph, _Item, int> Map;
alpar@1402
  1849
alpar@1402
  1850
  public:
alpar@1402
  1851
    /// The graph class of DescriptorMap.
alpar@1402
  1852
    typedef _Graph Graph;
alpar@1402
  1853
klao@1909
  1854
    /// The key type of DescriptorMap (Node, Edge, UEdge).
deba@2287
  1855
    typedef typename Map::Key Key;
alpar@1402
  1856
    /// The value type of DescriptorMap.
deba@2287
  1857
    typedef typename Map::Value Value;
alpar@1402
  1858
alpar@1402
  1859
    /// \brief Constructor.
alpar@1402
  1860
    ///
deba@1413
  1861
    /// Constructor for descriptor map.
deba@2286
  1862
    explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@2201
  1863
      Item it;
deba@2201
  1864
      const typename Map::Notifier* notifier = Map::getNotifier(); 
deba@2201
  1865
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2201
  1866
	Map::set(it, invMap.size());
deba@2201
  1867
	invMap.push_back(it);	
deba@2201
  1868
      }      
alpar@1402
  1869
    }
alpar@1402
  1870
deba@1515
  1871
  protected:
deba@1515
  1872
alpar@1402
  1873
    /// \brief Add a new key to the map.
alpar@1402
  1874
    ///
alpar@1402
  1875
    /// Add a new key to the map. It is called by the
alpar@1402
  1876
    /// \c AlterationNotifier.
alpar@1402
  1877
    virtual void add(const Item& item) {
alpar@1402
  1878
      Map::add(item);
alpar@1402
  1879
      Map::set(item, invMap.size());
alpar@1402
  1880
      invMap.push_back(item);
alpar@1402
  1881
    }
alpar@1402
  1882
deba@1829
  1883
    /// \brief Add more new keys to the map.
deba@1829
  1884
    ///
deba@1829
  1885
    /// Add more new keys to the map. It is called by the
deba@1829
  1886
    /// \c AlterationNotifier.
deba@1829
  1887
    virtual void add(const std::vector<Item>& items) {
deba@1829
  1888
      Map::add(items);
deba@1829
  1889
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1890
	Map::set(items[i], invMap.size());
deba@1829
  1891
	invMap.push_back(items[i]);
deba@1829
  1892
      }
deba@1829
  1893
    }
deba@1829
  1894
alpar@1402
  1895
    /// \brief Erase the key from the map.
alpar@1402
  1896
    ///
deba@1829
  1897
    /// Erase the key from the map. It is called by the
alpar@1402
  1898
    /// \c AlterationNotifier.
alpar@1402
  1899
    virtual void erase(const Item& item) {
alpar@1402
  1900
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1901
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1902
      invMap.pop_back();
alpar@1402
  1903
      Map::erase(item);
alpar@1402
  1904
    }
alpar@1402
  1905
deba@1829
  1906
    /// \brief Erase more keys from the map.
deba@1829
  1907
    ///
deba@1829
  1908
    /// Erase more keys from the map. It is called by the
deba@1829
  1909
    /// \c AlterationNotifier.
deba@1829
  1910
    virtual void erase(const std::vector<Item>& items) {
deba@1829
  1911
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1912
	Map::set(invMap.back(), Map::operator[](items[i]));
deba@1829
  1913
	invMap[Map::operator[](items[i])] = invMap.back();
deba@1829
  1914
	invMap.pop_back();
deba@1829
  1915
      }
deba@1829
  1916
      Map::erase(items);
deba@1829
  1917
    }
deba@1829
  1918
alpar@1402
  1919
    /// \brief Build the unique map.
alpar@1402
  1920
    ///
alpar@1402
  1921
    /// Build the unique map. It is called by the
alpar@1402
  1922
    /// \c AlterationNotifier.
alpar@1402
  1923
    virtual void build() {
alpar@1402
  1924
      Map::build();
alpar@1402
  1925
      Item it;
deba@1999
  1926
      const typename Map::Notifier* notifier = Map::getNotifier(); 
deba@1999
  1927
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
alpar@1402
  1928
	Map::set(it, invMap.size());
alpar@1402
  1929
	invMap.push_back(it);	
alpar@1402
  1930
      }      
alpar@1402
  1931
    }
alpar@1402
  1932
    
alpar@1402
  1933
    /// \brief Clear the keys from the map.
alpar@1402
  1934
    ///
alpar@1402
  1935
    /// Clear the keys from the map. It is called by the
alpar@1402
  1936
    /// \c AlterationNotifier.
alpar@1402
  1937
    virtual void clear() {
alpar@1402
  1938
      invMap.clear();
alpar@1402
  1939
      Map::clear();
alpar@1402
  1940
    }
alpar@1402
  1941
deba@1538
  1942
  public:
deba@1538
  1943
deba@1931
  1944
    /// \brief Returns the maximal value plus one.
deba@1931
  1945
    ///
deba@1931
  1946
    /// Returns the maximal value plus one in the map.
deba@1931
  1947
    unsigned int size() const {
deba@1931
  1948
      return invMap.size();
deba@1931
  1949
    }
deba@1931
  1950
deba@1552
  1951
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1952
    ///
deba@1552
  1953
    /// Swaps the position of the two items in the map.
deba@1552
  1954
    void swap(const Item& p, const Item& q) {
deba@1552
  1955
      int pi = Map::operator[](p);
deba@1552
  1956
      int qi = Map::operator[](q);
deba@1552
  1957
      Map::set(p, qi);
deba@1552
  1958
      invMap[qi] = p;
deba@1552
  1959
      Map::set(q, pi);
deba@1552
  1960
      invMap[pi] = q;
deba@1552
  1961
    }
deba@1552
  1962
alpar@1402
  1963
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1964
    ///
alpar@1402
  1965
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1966
    int operator[](const Item& item) const {
alpar@1402
  1967
      return Map::operator[](item);
alpar@1402
  1968
    }
alpar@1402
  1969
    
deba@1413
  1970
  private:
deba@1413
  1971
deba@1413
  1972
    typedef std::vector<Item> Container;
deba@1413
  1973
    Container invMap;
deba@1413
  1974
deba@1413
  1975
  public:
athos@1540
  1976
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  1977
    ///
athos@1540
  1978
    /// The inverse map type of DescriptorMap.
deba@1413
  1979
    class InverseMap {
deba@1413
  1980
    public:
deba@1413
  1981
      /// \brief Constructor of the InverseMap.
deba@1413
  1982
      ///
deba@1413
  1983
      /// Constructor of the InverseMap.
deba@2286
  1984
      explicit InverseMap(const DescriptorMap& _inverted) 
deba@1413
  1985
	: inverted(_inverted) {}
deba@1413
  1986
deba@1413
  1987
deba@1413
  1988
      /// The value type of the InverseMap.
deba@1413
  1989
      typedef typename DescriptorMap::Key Value;
deba@1413
  1990
      /// The key type of the InverseMap.
deba@1413
  1991
      typedef typename DescriptorMap::Value Key; 
deba@1413
  1992
deba@1413
  1993
      /// \brief Subscript operator. 
deba@1413
  1994
      ///
deba@1413
  1995
      /// Subscript operator. It gives back the item 
deba@1413
  1996
      /// that the descriptor belongs to currently.
deba@1413
  1997
      Value operator[](const Key& key) const {
deba@1413
  1998
	return inverted.invMap[key];
deba@1413
  1999
      }
deba@1470
  2000
deba@1470
  2001
      /// \brief Size of the map.
deba@1470
  2002
      ///
deba@1470
  2003
      /// Returns the size of the map.
deba@1931
  2004
      unsigned int size() const {
deba@1470
  2005
	return inverted.invMap.size();
deba@1470
  2006
      }
deba@1413
  2007
      
deba@1413
  2008
    private:
deba@1413
  2009
      const DescriptorMap& inverted;
deba@1413
  2010
    };
deba@1413
  2011
alpar@1402
  2012
    /// \brief Gives back the inverse of the map.
alpar@1402
  2013
    ///
alpar@1402
  2014
    /// Gives back the inverse of the map.
alpar@1402
  2015
    const InverseMap inverse() const {
deba@1413
  2016
      return InverseMap(*this);
alpar@1402
  2017
    }
alpar@1402
  2018
  };
alpar@1402
  2019
alpar@1402
  2020
  /// \brief Returns the source of the given edge.
alpar@1402
  2021
  ///
alpar@1402
  2022
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
  2023
  /// \author Balazs Dezso
alpar@1402
  2024
  template <typename Graph>
alpar@1402
  2025
  class SourceMap {
alpar@1402
  2026
  public:
deba@1419
  2027
alpar@1402
  2028
    typedef typename Graph::Node Value;
alpar@1402
  2029
    typedef typename Graph::Edge Key;
alpar@1402
  2030
alpar@1402
  2031
    /// \brief Constructor
alpar@1402
  2032
    ///
alpar@1402
  2033
    /// Constructor
alpar@1402
  2034
    /// \param _graph The graph that the map belongs to.
deba@2286
  2035
    explicit SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2036
alpar@1402
  2037
    /// \brief The subscript operator.
alpar@1402
  2038
    ///
alpar@1402
  2039
    /// The subscript operator.
alpar@1402
  2040
    /// \param edge The edge 
alpar@1402
  2041
    /// \return The source of the edge 
deba@1679
  2042
    Value operator[](const Key& edge) const {
alpar@1402
  2043
      return graph.source(edge);
alpar@1402
  2044
    }
alpar@1402
  2045
alpar@1402
  2046
  private:
alpar@1402
  2047
    const Graph& graph;
alpar@1402
  2048
  };
alpar@1402
  2049
alpar@1402
  2050
  /// \brief Returns a \ref SourceMap class
alpar@1402
  2051
  ///
alpar@1402
  2052
  /// This function just returns an \ref SourceMap class.
alpar@1402
  2053
  /// \relates SourceMap
alpar@1402
  2054
  template <typename Graph>
alpar@1402
  2055
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  2056
    return SourceMap<Graph>(graph);
alpar@1402
  2057
  } 
alpar@1402
  2058
alpar@1402
  2059
  /// \brief Returns the target of the given edge.
alpar@1402
  2060
  ///
alpar@1402
  2061
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
  2062
  /// \author Balazs Dezso
alpar@1402
  2063
  template <typename Graph>
alpar@1402
  2064
  class TargetMap {
alpar@1402
  2065
  public:
deba@1419
  2066
alpar@1402
  2067
    typedef typename Graph::Node Value;
alpar@1402
  2068
    typedef typename Graph::Edge Key;
alpar@1402
  2069
alpar@1402
  2070
    /// \brief Constructor
alpar@1402
  2071
    ///
alpar@1402
  2072
    /// Constructor
alpar@1402
  2073
    /// \param _graph The graph that the map belongs to.
deba@2286
  2074
    explicit TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2075
alpar@1402
  2076
    /// \brief The subscript operator.
alpar@1402
  2077
    ///
alpar@1402
  2078
    /// The subscript operator.
alpar@1536
  2079
    /// \param e The edge 
alpar@1402
  2080
    /// \return The target of the edge 
deba@1679
  2081
    Value operator[](const Key& e) const {
alpar@1536
  2082
      return graph.target(e);
alpar@1402
  2083
    }
alpar@1402
  2084
alpar@1402
  2085
  private:
alpar@1402
  2086
    const Graph& graph;
alpar@1402
  2087
  };
alpar@1402
  2088
alpar@1402
  2089
  /// \brief Returns a \ref TargetMap class
deba@1515
  2090
  ///
athos@1540
  2091
  /// This function just returns a \ref TargetMap class.
alpar@1402
  2092
  /// \relates TargetMap
alpar@1402
  2093
  template <typename Graph>
alpar@1402
  2094
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  2095
    return TargetMap<Graph>(graph);
alpar@1402
  2096
  }
alpar@1402
  2097
athos@1540
  2098
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  2099
  ///
athos@1540
  2100
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
  2101
  /// \author Balazs Dezso
deba@1419
  2102
  template <typename Graph>
deba@1419
  2103
  class ForwardMap {
deba@1419
  2104
  public:
deba@1419
  2105
deba@1419
  2106
    typedef typename Graph::Edge Value;
klao@1909
  2107
    typedef typename Graph::UEdge Key;
deba@1419
  2108
deba@1419
  2109
    /// \brief Constructor
deba@1419
  2110
    ///
deba@1419
  2111
    /// Constructor
deba@1419
  2112
    /// \param _graph The graph that the map belongs to.
deba@2286
  2113
    explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2114
deba@1419
  2115
    /// \brief The subscript operator.
deba@1419
  2116
    ///
deba@1419
  2117
    /// The subscript operator.
deba@1419
  2118
    /// \param key An undirected edge 
deba@1419
  2119
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  2120
    Value operator[](const Key& key) const {
deba@1627
  2121
      return graph.direct(key, true);
deba@1419
  2122
    }
deba@1419
  2123
deba@1419
  2124
  private:
deba@1419
  2125
    const Graph& graph;
deba@1419
  2126
  };
deba@1419
  2127
deba@1419
  2128
  /// \brief Returns a \ref ForwardMap class
deba@1515
  2129
  ///
deba@1419
  2130
  /// This function just returns an \ref ForwardMap class.
deba@1419
  2131
  /// \relates ForwardMap
deba@1419
  2132
  template <typename Graph>
deba@1419
  2133
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  2134
    return ForwardMap<Graph>(graph);
deba@1419
  2135
  }
deba@1419
  2136
athos@1540
  2137
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  2138
  ///
athos@1540
  2139
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  2140
  /// \author Balazs Dezso
deba@1419
  2141
  template <typename Graph>
deba@1419
  2142
  class BackwardMap {
deba@1419
  2143
  public:
deba@1419
  2144
deba@1419
  2145
    typedef typename Graph::Edge Value;
klao@1909
  2146
    typedef typename Graph::UEdge Key;
deba@1419
  2147
deba@1419
  2148
    /// \brief Constructor
deba@1419
  2149
    ///
deba@1419
  2150
    /// Constructor
deba@1419
  2151
    /// \param _graph The graph that the map belongs to.
deba@2286
  2152
    explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2153
deba@1419
  2154
    /// \brief The subscript operator.
deba@1419
  2155
    ///
deba@1419
  2156
    /// The subscript operator.
deba@1419
  2157
    /// \param key An undirected edge 
deba@1419
  2158
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  2159
    Value operator[](const Key& key) const {
deba@1627
  2160
      return graph.direct(key, false);
deba@1419
  2161
    }
deba@1419
  2162
deba@1419
  2163
  private:
deba@1419
  2164
    const Graph& graph;
deba@1419
  2165
  };
deba@1419
  2166
deba@1419
  2167
  /// \brief Returns a \ref BackwardMap class
deba@1419
  2168
athos@1540
  2169
  /// This function just returns a \ref BackwardMap class.
deba@1419
  2170
  /// \relates BackwardMap
deba@1419
  2171
  template <typename Graph>
deba@1419
  2172
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  2173
    return BackwardMap<Graph>(graph);
deba@1419
  2174
  }
deba@1419
  2175
deba@1695
  2176
  /// \brief Potential difference map
deba@1695
  2177
  ///
deba@1695
  2178
  /// If there is an potential map on the nodes then we
deba@1695
  2179
  /// can get an edge map as we get the substraction of the
deba@1695
  2180
  /// values of the target and source.
deba@1695
  2181
  template <typename Graph, typename NodeMap>
deba@1695
  2182
  class PotentialDifferenceMap {
deba@1515
  2183
  public:
deba@1695
  2184
    typedef typename Graph::Edge Key;
deba@1695
  2185
    typedef typename NodeMap::Value Value;
deba@1695
  2186
deba@1695
  2187
    /// \brief Constructor
deba@1695
  2188
    ///
deba@1695
  2189
    /// Contructor of the map
deba@2286
  2190
    explicit PotentialDifferenceMap(const Graph& _graph, 
deba@2286
  2191
                                    const NodeMap& _potential) 
deba@1695
  2192
      : graph(_graph), potential(_potential) {}
deba@1695
  2193
deba@1695
  2194
    /// \brief Const subscription operator
deba@1695
  2195
    ///
deba@1695
  2196
    /// Const subscription operator
deba@1695
  2197
    Value operator[](const Key& edge) const {
deba@1695
  2198
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  2199
    }
deba@1695
  2200
deba@1695
  2201
  private:
deba@1695
  2202
    const Graph& graph;
deba@1695
  2203
    const NodeMap& potential;
deba@1695
  2204
  };
deba@1695
  2205
deba@1695
  2206
  /// \brief Just returns a PotentialDifferenceMap
deba@1695
  2207
  ///
deba@1695
  2208
  /// Just returns a PotentialDifferenceMap
deba@1695
  2209
  /// \relates PotentialDifferenceMap
deba@1695
  2210
  template <typename Graph, typename NodeMap>
deba@1695
  2211
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  2212
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  2213
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  2214
  }
deba@1695
  2215
deba@1515
  2216
  /// \brief Map of the node in-degrees.
alpar@1453
  2217
  ///
athos@1540
  2218
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  2219
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2220
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2221
  /// whenever the graph changes.
deba@1515
  2222
  ///
deba@1729
  2223
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2224
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1829
  2225
  /// is not guarantied if these additional features are used. For example
deba@1829
  2226
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2227
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2228
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2229
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2230
  ///
deba@1515
  2231
  /// \sa OutDegMap
deba@1515
  2232
alpar@1453
  2233
  template <typename _Graph>
deba@1515
  2234
  class InDegMap  
deba@1999
  2235
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2236
      ::ItemNotifier::ObserverBase {
deba@1515
  2237
alpar@1453
  2238
  public:
deba@1515
  2239
    
deba@1515
  2240
    typedef _Graph Graph;
alpar@1453
  2241
    typedef int Value;
deba@1515
  2242
    typedef typename Graph::Node Key;
deba@1515
  2243
deba@1999
  2244
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2245
    ::ItemNotifier::ObserverBase Parent;
deba@1999
  2246
deba@1515
  2247
  private:
deba@1515
  2248
deba@1990
  2249
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2250
    public:
deba@1515
  2251
deba@1990
  2252
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2253
      typedef typename Parent::Graph Graph;
deba@1515
  2254
deba@1515
  2255
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2256
      
deba@1829
  2257
      virtual void add(const Key& key) {
deba@1515
  2258
	Parent::add(key);
deba@1515
  2259
	Parent::set(key, 0);
deba@1515
  2260
      }
deba@1931
  2261
deba@1829
  2262
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2263
	Parent::add(keys);
deba@1829
  2264
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  2265
	  Parent::set(keys[i], 0);
deba@1829
  2266
	}
deba@1829
  2267
      }
deba@1515
  2268
    };
deba@1515
  2269
deba@1515
  2270
  public:
alpar@1453
  2271
alpar@1453
  2272
    /// \brief Constructor.
alpar@1453
  2273
    ///
alpar@1453
  2274
    /// Constructor for creating in-degree map.
deba@2286
  2275
    explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@1999
  2276
      Parent::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  2277
      
deba@1515
  2278
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2279
	deg[it] = countInEdges(graph, it);
deba@1515
  2280
      }
alpar@1453
  2281
    }
alpar@1453
  2282
    
alpar@1459
  2283
    /// Gives back the in-degree of a Node.
deba@1515
  2284
    int operator[](const Key& key) const {
deba@1515
  2285
      return deg[key];
alpar@1459
  2286
    }
alpar@1453
  2287
alpar@1453
  2288
  protected:
deba@1515
  2289
    
deba@1515
  2290
    typedef typename Graph::Edge Edge;
deba@1515
  2291
deba@1515
  2292
    virtual void add(const Edge& edge) {
deba@1515
  2293
      ++deg[graph.target(edge)];
alpar@1453
  2294
    }
alpar@1453
  2295
deba@1931
  2296
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  2297
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  2298
        ++deg[graph.target(edges[i])];
deba@1931
  2299
      }
deba@1931
  2300
    }
deba@1931
  2301
deba@1515
  2302
    virtual void erase(const Edge& edge) {
deba@1515
  2303
      --deg[graph.target(edge)];
deba@1515
  2304
    }
deba@1515
  2305
deba@1931
  2306
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  2307
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  2308
        --deg[graph.target(edges[i])];
deba@1931
  2309
      }
deba@1931
  2310
    }
deba@1931
  2311
deba@1515
  2312
    virtual void build() {
deba@1515
  2313
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2314
	deg[it] = countInEdges(graph, it);
deba@1515
  2315
      }      
deba@1515
  2316
    }
deba@1515
  2317
deba@1515
  2318
    virtual void clear() {
deba@1515
  2319
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2320
	deg[it] = 0;
deba@1515
  2321
      }
deba@1515
  2322
    }
deba@1515
  2323
  private:
alpar@1506
  2324
    
deba@1515
  2325
    const _Graph& graph;
deba@1515
  2326
    AutoNodeMap deg;
alpar@1459
  2327
  };
alpar@1459
  2328
deba@1515
  2329
  /// \brief Map of the node out-degrees.
deba@1515
  2330
  ///
athos@1540
  2331
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  2332
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2333
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2334
  /// whenever the graph changes.
deba@1515
  2335
  ///
deba@1729
  2336
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2337
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1829
  2338
  /// is not guarantied if these additional features are used. For example
deba@1829
  2339
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2340
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2341
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2342
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2343
  ///
alpar@1555
  2344
  /// \sa InDegMap
alpar@1459
  2345
alpar@1459
  2346
  template <typename _Graph>
deba@1515
  2347
  class OutDegMap  
deba@1999
  2348
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2349
      ::ItemNotifier::ObserverBase {
deba@1515
  2350
alpar@1459
  2351
  public:
deba@1999
  2352
deba@1999
  2353
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2354
    ::ItemNotifier::ObserverBase Parent;
deba@1515
  2355
    
deba@1515
  2356
    typedef _Graph Graph;
alpar@1459
  2357
    typedef int Value;
deba@1515
  2358
    typedef typename Graph::Node Key;
deba@1515
  2359
deba@1515
  2360
  private:
deba@1515
  2361
deba@1990
  2362
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2363
    public:
deba@1515
  2364
deba@1990
  2365
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2366
      typedef typename Parent::Graph Graph;
deba@1515
  2367
deba@1515
  2368
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2369
      
deba@1829
  2370
      virtual void add(const Key& key) {
deba@1515
  2371
	Parent::add(key);
deba@1515
  2372
	Parent::set(key, 0);
deba@1515
  2373
      }
deba@1829
  2374
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2375
	Parent::add(keys);
deba@1829
  2376
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  2377
	  Parent::set(keys[i], 0);
deba@1829
  2378
	}
deba@1829
  2379
      }
deba@1515
  2380
    };
deba@1515
  2381
deba@1515
  2382
  public:
alpar@1459
  2383
alpar@1459
  2384
    /// \brief Constructor.
alpar@1459
  2385
    ///
alpar@1459
  2386
    /// Constructor for creating out-degree map.
deba@2286
  2387
    explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@1999
  2388
      Parent::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  2389
      
deba@1515
  2390
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2391
	deg[it] = countOutEdges(graph, it);
deba@1515
  2392
      }
alpar@1459
  2393
    }
alpar@1459
  2394
deba@1990
  2395
    /// Gives back the out-degree of a Node.
deba@1515
  2396
    int operator[](const Key& key) const {
deba@1515
  2397
      return deg[key];
alpar@1459
  2398
    }
alpar@1459
  2399
alpar@1459
  2400
  protected:
deba@1515
  2401
    
deba@1515
  2402
    typedef typename Graph::Edge Edge;
deba@1515
  2403
deba@1515
  2404
    virtual void add(const Edge& edge) {
deba@1515
  2405
      ++deg[graph.source(edge)];
alpar@1459
  2406
    }
alpar@1459
  2407
deba@1931
  2408
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  2409
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  2410
        ++deg[graph.source(edges[i])];
deba@1931
  2411
      }
deba@1931
  2412
    }
deba@1931
  2413
deba@1515
  2414
    virtual void erase(const Edge& edge) {
deba@1515
  2415
      --deg[graph.source(edge)];
deba@1515
  2416
    }
deba@1515
  2417
deba@1931
  2418
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  2419
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  2420
        --deg[graph.source(edges[i])];
deba@1931
  2421
      }
deba@1931
  2422
    }
deba@1931
  2423
deba@1515
  2424
    virtual void build() {
deba@1515
  2425
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2426
	deg[it] = countOutEdges(graph, it);
deba@1515
  2427
      }      
deba@1515
  2428
    }
deba@1515
  2429
deba@1515
  2430
    virtual void clear() {
deba@1515
  2431
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2432
	deg[it] = 0;
deba@1515
  2433
      }
deba@1515
  2434
    }
deba@1515
  2435
  private:
alpar@1506
  2436
    
deba@1515
  2437
    const _Graph& graph;
deba@1515
  2438
    AutoNodeMap deg;
alpar@1453
  2439
  };
alpar@1453
  2440
deba@1695
  2441
alpar@2235
  2442
  ///Fast edge look up between given endpoints.
alpar@2235
  2443
  
alpar@2235
  2444
  ///\ingroup gutils
alpar@2235
  2445
  ///Using this class, you can find an edge in a graph from a given
alpar@2235
  2446
  ///source to a given target in time <em>O(log d)</em>,
alpar@2235
  2447
  ///where <em>d</em> is the out-degree of the source node.
alpar@2235
  2448
  ///
alpar@2235
  2449
  ///It is not possible to find \e all parallel edges between two nodes.
alpar@2235
  2450
  ///Use \ref AllEdgeLookUp for this purpose.
alpar@2235
  2451
  ///
alpar@2235
  2452
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  2453
  ///refresh(Node)) this data structure
alpar@2235
  2454
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  2455
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  2456
  ///
alpar@2235
  2457
  ///\param G The type of the underlying graph.
alpar@2235
  2458
  ///
alpar@2235
  2459
  ///\sa AllEdgeLookUp  
alpar@2235
  2460
  template<class G>
alpar@2235
  2461
  class EdgeLookUp 
alpar@2235
  2462
  {
alpar@2235
  2463
  public:
alpar@2235
  2464
    GRAPH_TYPEDEFS(typename G)
alpar@2235
  2465
    typedef G Graph;
alpar@2235
  2466
alpar@2235
  2467
  protected:
alpar@2235
  2468
    const Graph &_g;
alpar@2235
  2469
    typename Graph::template NodeMap<Edge> _head;
alpar@2235
  2470
    typename Graph::template EdgeMap<Edge> _left;
alpar@2235
  2471
    typename Graph::template EdgeMap<Edge> _right;
alpar@2235
  2472
    
alpar@2235
  2473
    class EdgeLess {
alpar@2235
  2474
      const Graph &g;
alpar@2235
  2475
    public:
alpar@2235
  2476
      EdgeLess(const Graph &_g) : g(_g) {}
alpar@2235
  2477
      bool operator()(Edge a,Edge b) const 
alpar@2235
  2478
      {
alpar@2235
  2479
	return g.target(a)<g.target(b);
alpar@2235
  2480
      }
alpar@2235
  2481
    };
alpar@2235
  2482
    
alpar@2235
  2483
  public:
alpar@2235
  2484
    
alpar@2235
  2485
    ///Constructor
alpar@2235
  2486
alpar@2235
  2487
    ///Constructor.
alpar@2235
  2488
    ///
alpar@2235
  2489
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  2490
    ///changes.
alpar@2235
  2491
    EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
alpar@2235
  2492
    
alpar@2235
  2493
  private:
alpar@2235
  2494
    Edge refresh_rec(std::vector<Edge> &v,int a,int b) 
alpar@2235
  2495
    {
alpar@2235
  2496
      int m=(a+b)/2;
alpar@2235
  2497
      Edge me=v[m];
alpar@2235
  2498
      _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
alpar@2235
  2499
      _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
alpar@2235
  2500
      return me;
alpar@2235
  2501
    }
alpar@2235
  2502
  public:
alpar@2235
  2503
    ///Refresh the data structure at a node.
alpar@2235
  2504
alpar@2235
  2505
    ///Build up the search database of node \c n.
alpar@2235
  2506
    ///
alpar@2235
  2507
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  2508
    ///the number of the outgoing edges of \c n.
alpar@2235
  2509
    void refresh(Node n) 
alpar@2235
  2510
    {
alpar@2235
  2511
      std::vector<Edge> v;
alpar@2235
  2512
      for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
alpar@2235
  2513
      if(v.size()) {
alpar@2235
  2514
	std::sort(v.begin(),v.end(),EdgeLess(_g));
alpar@2235
  2515
	_head[n]=refresh_rec(v,0,v.size()-1);
alpar@2235
  2516
      }
alpar@2235
  2517
      else _head[n]=INVALID;
alpar@2235
  2518
    }
alpar@2235
  2519
    ///Refresh the full data structure.
alpar@2235
  2520
alpar@2235
  2521
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  2522
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  2523
    ///
alpar@2235
  2524
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  2525
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  2526
    ///out-degree of the graph.
alpar@2235
  2527
alpar@2235
  2528
    void refresh() 
alpar@2235
  2529
    {
alpar@2235
  2530
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
alpar@2235
  2531
    }
alpar@2235
  2532
    
alpar@2235
  2533
    ///Find an edge between two nodes.
alpar@2235
  2534
    
alpar@2235
  2535
    ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
alpar@2235
  2536
    /// <em>d</em> is the number of outgoing edges of \c s.
alpar@2235
  2537
    ///\param s The source node
alpar@2235
  2538
    ///\param t The target node
alpar@2235
  2539
    ///\return An edge from \c s to \c t if there exists,
alpar@2235
  2540
    ///\ref INVALID otherwise.
alpar@2235
  2541
    ///
alpar@2235
  2542
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  2543
    ///this operator. If you change the outgoing edges of
alpar@2235
  2544
    ///a single node \c n, then
alpar@2235
  2545
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  2546
    ///
alpar@2235
  2547
    Edge operator()(Node s, Node t) const
alpar@2235
  2548
    {
alpar@2235
  2549
      Edge e;
alpar@2235
  2550
      for(e=_head[s];
alpar@2235
  2551
	  e!=INVALID&&_g.target(e)!=t;
alpar@2235
  2552
	  e = t < _g.target(e)?_left[e]:_right[e]) ;
alpar@2235
  2553
      return e;
alpar@2235
  2554
    }
alpar@2235
  2555
alpar@2235
  2556
  };
alpar@2235
  2557
alpar@2235
  2558
  ///Fast look up of all edges between given endpoints.
alpar@2235
  2559
  
alpar@2235
  2560
  ///\ingroup gutils
alpar@2235
  2561
  ///This class is the same as \ref EdgeLookUp, with the addition
alpar@2235
  2562
  ///that it makes it possible to find all edges between given endpoints.
alpar@2235
  2563
  ///
alpar@2235
  2564
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  2565
  ///refresh(Node)) this data structure
alpar@2235
  2566
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  2567
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  2568
  ///
alpar@2235
  2569
  ///\param G The type of the underlying graph.
alpar@2235
  2570
  ///
alpar@2235
  2571
  ///\sa EdgeLookUp  
alpar@2235
  2572
  template<class G>
alpar@2235
  2573
  class AllEdgeLookUp : public EdgeLookUp<G>
alpar@2235
  2574
  {
alpar@2235
  2575
    using EdgeLookUp<G>::_g;
alpar@2235
  2576
    using EdgeLookUp<G>::_right;
alpar@2235
  2577
    using EdgeLookUp<G>::_left;
alpar@2235
  2578
    using EdgeLookUp<G>::_head;
alpar@2235
  2579
alpar@2235
  2580
    GRAPH_TYPEDEFS(typename G)
alpar@2235
  2581
    typedef G Graph;
alpar@2235
  2582
    
alpar@2235
  2583
    typename Graph::template EdgeMap<Edge> _next;
alpar@2235
  2584
    
alpar@2235
  2585
    Edge refreshNext(Edge head,Edge next=INVALID)
alpar@2235
  2586
    {
alpar@2235
  2587
      if(head==INVALID) return next;
alpar@2235
  2588
      else {
alpar@2235
  2589
	next=refreshNext(_right[head],next);
alpar@2235
  2590
// 	_next[head]=next;
alpar@2235
  2591
	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
alpar@2235
  2592
	  ? next : INVALID;
alpar@2235
  2593
	return refreshNext(_left[head],head);
alpar@2235
  2594
      }
alpar@2235
  2595
    }
alpar@2235
  2596
    
alpar@2235
  2597
    void refreshNext()
alpar@2235
  2598
    {
alpar@2235
  2599
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
alpar@2235
  2600
    }
alpar@2235
  2601
    
alpar@2235
  2602
  public:
alpar@2235
  2603
    ///Constructor
alpar@2235
  2604
alpar@2235
  2605
    ///Constructor.
alpar@2235
  2606
    ///
alpar@2235
  2607
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  2608
    ///changes.
alpar@2235
  2609
    AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
alpar@2235
  2610
alpar@2235
  2611
    ///Refresh the data structure at a node.
alpar@2235
  2612
alpar@2235
  2613
    ///Build up the search database of node \c n.
alpar@2235
  2614
    ///
alpar@2235
  2615
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  2616
    ///the number of the outgoing edges of \c n.
alpar@2235
  2617
    
alpar@2235
  2618
    void refresh(Node n) 
alpar@2235
  2619
    {
alpar@2235
  2620
      EdgeLookUp<G>::refresh(n);
alpar@2235
  2621
      refreshNext(_head[n]);
alpar@2235
  2622
    }
alpar@2235
  2623
    
alpar@2235
  2624
    ///Refresh the full data structure.
alpar@2235
  2625
alpar@2235
  2626
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  2627
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  2628
    ///
alpar@2235
  2629
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  2630
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  2631
    ///out-degree of the graph.
alpar@2235
  2632
alpar@2235
  2633
    void refresh() 
alpar@2235
  2634
    {
alpar@2235
  2635
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
alpar@2235
  2636
    }
alpar@2235
  2637
    
alpar@2235
  2638
    ///Find an edge between two nodes.
alpar@2235
  2639
    
alpar@2235
  2640
    ///Find an edge between two nodes.
alpar@2235
  2641
    ///\param s The source node
alpar@2235
  2642
    ///\param t The target node
alpar@2235
  2643
    ///\param prev The previous edge between \c s and \c t. It it is INVALID or
alpar@2235
  2644
    ///not given, the operator finds the first appropriate edge.
alpar@2235
  2645
    ///\return An edge from \c s to \c t after \prev or
alpar@2235
  2646
    ///\ref INVALID if there is no more.
alpar@2235
  2647
    ///
alpar@2235
  2648
    ///For example, you can count the number of edges from \c u to \c v in the
alpar@2235
  2649
    ///following way.
alpar@2235
  2650
    ///\code
alpar@2235
  2651
    ///AllEdgeLookUp<ListGraph> ae(g);
alpar@2235
  2652
    ///...
alpar@2235
  2653
    ///int n=0;
alpar@2235
  2654
    ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
alpar@2235
  2655
    ///\endcode
alpar@2235
  2656
    ///
alpar@2235
  2657
    ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
alpar@2235
  2658
    /// <em>d</em> is the number of outgoing edges of \c s. Then, the
alpar@2235
  2659
    ///consecutive edges are found in constant time.
alpar@2235
  2660
    ///
alpar@2235
  2661
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  2662
    ///this operator. If you change the outgoing edges of
alpar@2235
  2663
    ///a single node \c n, then
alpar@2235
  2664
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  2665
    ///
alpar@2235
  2666
#ifdef DOXYGEN
alpar@2235
  2667
    Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
alpar@2235
  2668
#else
alpar@2235
  2669
    using EdgeLookUp<G>::operator() ;
alpar@2235
  2670
    Edge operator()(Node s, Node t, Edge prev) const
alpar@2235
  2671
    {
alpar@2235
  2672
      return prev==INVALID?(*this)(s,t):_next[prev];
alpar@2235
  2673
    }
alpar@2235
  2674
#endif
alpar@2235
  2675
      
alpar@2235
  2676
  };
alpar@2235
  2677
alpar@1402
  2678
  /// @}
alpar@1402
  2679
alpar@947
  2680
} //END OF NAMESPACE LEMON
klao@946
  2681
klao@946
  2682
#endif