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