lemon/graph_utils.h
author ladanyi
Wed, 02 Jul 2008 12:37:47 +0000
changeset 2615 2bf1f6e3d5ae
parent 2553 bfced05fa852
child 2616 02971275e7bf
permissions -rw-r--r--
Fix bug caused by m4 consuming pairs of square brackets (#108).
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@2553
     5
 * Copyright (C) 2003-2008
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>
alpar@2569
    25
#include <lemon/math.h>
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
deba@2539
   385
  ///\sa DynEdgeLookUp
alpar@2155
   386
  ///\sa ConEdgeIt
alpar@967
   387
  template <typename Graph>
deba@2286
   388
  inline typename Graph::Edge 
deba@2286
   389
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@2286
   390
           typename Graph::Edge prev = INVALID) {
deba@2020
   391
    return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
alpar@967
   392
  }
deba@1531
   393
deba@1565
   394
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   395
  ///
deba@1565
   396
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   397
  /// higher level interface for the findEdge() function. You can
alpar@1591
   398
  /// use it the following way:
alpar@1946
   399
  ///\code
deba@1565
   400
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   401
  ///   ...
deba@1565
   402
  /// }
alpar@1946
   403
  ///\endcode
alpar@2155
   404
  /// 
alpar@2155
   405
  ///\sa findEdge()
alpar@2235
   406
  ///\sa EdgeLookUp
kpeter@2474
   407
  ///\sa AllEdgeLookUp
deba@2539
   408
  ///\sa DynEdgeLookUp
deba@1565
   409
  ///
deba@1565
   410
  /// \author Balazs Dezso 
deba@1565
   411
  template <typename _Graph>
deba@1565
   412
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   413
  public:
deba@1565
   414
deba@1565
   415
    typedef _Graph Graph;
deba@1565
   416
    typedef typename Graph::Edge Parent;
deba@1565
   417
deba@1565
   418
    typedef typename Graph::Edge Edge;
deba@1565
   419
    typedef typename Graph::Node Node;
deba@1565
   420
deba@1565
   421
    /// \brief Constructor.
deba@1565
   422
    ///
deba@1565
   423
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   424
    /// connects the \c u and \c v node.
deba@1565
   425
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   426
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   427
    }
deba@1565
   428
deba@1565
   429
    /// \brief Constructor.
deba@1565
   430
    ///
deba@1565
   431
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   432
    /// the \c e edge.
deba@1565
   433
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   434
    
deba@1565
   435
    /// \brief Increment operator.
deba@1565
   436
    ///
deba@1565
   437
    /// It increments the iterator and gives back the next edge.
deba@1565
   438
    ConEdgeIt& operator++() {
deba@1565
   439
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   440
				 graph.target(*this), *this));
deba@1565
   441
      return *this;
deba@1565
   442
    }
deba@1565
   443
  private:
deba@1565
   444
    const Graph& graph;
deba@1565
   445
  };
deba@1565
   446
deba@2020
   447
  namespace _graph_utils_bits {
deba@2020
   448
    
deba@2020
   449
    template <typename Graph, typename Enable = void>
deba@2020
   450
    struct FindUEdgeSelector {
deba@2020
   451
      typedef typename Graph::Node Node;
deba@2020
   452
      typedef typename Graph::UEdge UEdge;
deba@2020
   453
      static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
deba@2020
   454
        bool b;
deba@2020
   455
        if (u != v) {
deba@2020
   456
          if (e == INVALID) {
deba@2031
   457
            g.firstInc(e, b, u);
deba@2020
   458
          } else {
deba@2020
   459
            b = g.source(e) == u;
deba@2020
   460
            g.nextInc(e, b);
deba@2020
   461
          }
deba@2064
   462
          while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
deba@2020
   463
            g.nextInc(e, b);
deba@2020
   464
          }
deba@2020
   465
        } else {
deba@2020
   466
          if (e == INVALID) {
deba@2031
   467
            g.firstInc(e, b, u);
deba@2020
   468
          } else {
deba@2020
   469
            b = true;
deba@2020
   470
            g.nextInc(e, b);
deba@2020
   471
          }
deba@2020
   472
          while (e != INVALID && (!b || g.target(e) != v)) {
deba@2020
   473
            g.nextInc(e, b);
deba@2020
   474
          }
deba@2020
   475
        }
deba@2020
   476
        return e;
deba@2020
   477
      }
deba@2020
   478
    };
deba@1704
   479
deba@2020
   480
    template <typename Graph>
deba@2020
   481
    struct FindUEdgeSelector<
deba@2020
   482
      Graph, 
deba@2020
   483
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
deba@2020
   484
    {
deba@2020
   485
      typedef typename Graph::Node Node;
deba@2020
   486
      typedef typename Graph::UEdge UEdge;
deba@2020
   487
      static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
deba@2020
   488
        return g.findUEdge(u, v, prev);
deba@2020
   489
      }
deba@2020
   490
    };    
deba@1704
   491
  }
deba@1704
   492
klao@1909
   493
  /// \brief Finds an uedge between two nodes of a graph.
deba@1704
   494
  ///
klao@1909
   495
  /// Finds an uedge from node \c u to node \c v in graph \c g.
deba@2020
   496
  /// If the node \c u and node \c v is equal then each loop edge
deba@2020
   497
  /// will be enumerated.
deba@1704
   498
  ///
deba@1704
   499
  /// If \c prev is \ref INVALID (this is the default value), then
deba@1704
   500
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
deba@1704
   501
  /// the next edge from \c u to \c v after \c prev.
deba@1704
   502
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@1704
   503
  ///
deba@1704
   504
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   505
  ///\code
klao@1909
   506
  /// for(UEdge e = findUEdge(g,u,v); e != INVALID; 
klao@1909
   507
  ///     e = findUEdge(g,u,v,e)) {
deba@1704
   508
  ///   ...
deba@1704
   509
  /// }
alpar@1946
   510
  ///\endcode
alpar@2155
   511
  ///
alpar@2155
   512
  ///\sa ConEdgeIt
alpar@2155
   513
deba@1704
   514
  template <typename Graph>
deba@2286
   515
  inline typename Graph::UEdge 
deba@2286
   516
  findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
deba@2286
   517
            typename Graph::UEdge p = INVALID) {
deba@2031
   518
    return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
deba@1704
   519
  }
deba@1704
   520
klao@1909
   521
  /// \brief Iterator for iterating on uedges connected the same nodes.
deba@1704
   522
  ///
klao@1909
   523
  /// Iterator for iterating on uedges connected the same nodes. It is 
klao@1909
   524
  /// higher level interface for the findUEdge() function. You can
deba@1704
   525
  /// use it the following way:
alpar@1946
   526
  ///\code
klao@1909
   527
  /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1704
   528
  ///   ...
deba@1704
   529
  /// }
alpar@1946
   530
  ///\endcode
deba@1704
   531
  ///
alpar@2155
   532
  ///\sa findUEdge()
alpar@2155
   533
  ///
deba@1704
   534
  /// \author Balazs Dezso 
deba@1704
   535
  template <typename _Graph>
klao@1909
   536
  class ConUEdgeIt : public _Graph::UEdge {
deba@1704
   537
  public:
deba@1704
   538
deba@1704
   539
    typedef _Graph Graph;
klao@1909
   540
    typedef typename Graph::UEdge Parent;
deba@1704
   541
klao@1909
   542
    typedef typename Graph::UEdge UEdge;
deba@1704
   543
    typedef typename Graph::Node Node;
deba@1704
   544
deba@1704
   545
    /// \brief Constructor.
deba@1704
   546
    ///
klao@1909
   547
    /// Construct a new ConUEdgeIt iterating on the edges which
deba@1704
   548
    /// connects the \c u and \c v node.
klao@1909
   549
    ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
klao@1909
   550
      Parent::operator=(findUEdge(graph, u, v));
deba@1704
   551
    }
deba@1704
   552
deba@1704
   553
    /// \brief Constructor.
deba@1704
   554
    ///
klao@1909
   555
    /// Construct a new ConUEdgeIt which continues the iterating from 
deba@1704
   556
    /// the \c e edge.
klao@1909
   557
    ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
deba@1704
   558
    
deba@1704
   559
    /// \brief Increment operator.
deba@1704
   560
    ///
deba@1704
   561
    /// It increments the iterator and gives back the next edge.
klao@1909
   562
    ConUEdgeIt& operator++() {
klao@1909
   563
      Parent::operator=(findUEdge(graph, graph.source(*this), 
deba@1829
   564
				      graph.target(*this), *this));
deba@1704
   565
      return *this;
deba@1704
   566
    }
deba@1704
   567
  private:
deba@1704
   568
    const Graph& graph;
deba@1704
   569
  };
deba@1704
   570
athos@1540
   571
  /// \brief Copy a map.
alpar@964
   572
  ///
deba@2485
   573
  /// This function copies the \c from map to the \c to map. It uses the
athos@1540
   574
  /// given iterator to iterate on the data structure and it uses the \c ref
deba@2485
   575
  /// mapping to convert the from's keys to the to's keys.
deba@2485
   576
  template <typename To, typename From, 
deba@1531
   577
	    typename ItemIt, typename Ref>	    
deba@2485
   578
  void copyMap(To& to, const From& from, 
deba@1531
   579
	       ItemIt it, const Ref& ref) {
deba@1531
   580
    for (; it != INVALID; ++it) {
deba@2485
   581
      to[ref[it]] = from[it];
klao@946
   582
    }
klao@946
   583
  }
klao@946
   584
deba@2485
   585
  /// \brief Copy the from map to the to map.
deba@1531
   586
  ///
deba@2485
   587
  /// Copy the \c from map to the \c to map. It uses the given iterator
deba@1531
   588
  /// to iterate on the data structure.
deba@2485
   589
  template <typename To, typename From, typename ItemIt>	    
deba@2485
   590
  void copyMap(To& to, const From& from, ItemIt it) {
deba@1531
   591
    for (; it != INVALID; ++it) {
deba@2485
   592
      to[it] = from[it];
klao@946
   593
    }
klao@946
   594
  }
klao@946
   595
deba@2286
   596
  namespace _graph_utils_bits {
deba@2286
   597
deba@2286
   598
    template <typename Graph, typename Item, typename RefMap>
deba@2286
   599
    class MapCopyBase {
deba@2286
   600
    public:
deba@2485
   601
      virtual void copy(const Graph& from, const RefMap& refMap) = 0;
deba@2286
   602
      
deba@2286
   603
      virtual ~MapCopyBase() {}
deba@2286
   604
    };
deba@2286
   605
deba@2286
   606
    template <typename Graph, typename Item, typename RefMap, 
deba@2485
   607
              typename ToMap, typename FromMap>
deba@2286
   608
    class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   609
    public:
deba@2286
   610
deba@2485
   611
      MapCopy(ToMap& tmap, const FromMap& map) 
deba@2286
   612
        : _tmap(tmap), _map(map) {}
deba@2286
   613
      
deba@2286
   614
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   615
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   616
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   617
          _tmap.set(refMap[it], _map[it]);
deba@2286
   618
        }
deba@2286
   619
      }
deba@2286
   620
deba@2286
   621
    private:
deba@2485
   622
      ToMap& _tmap;
deba@2485
   623
      const FromMap& _map;
deba@2286
   624
    };
deba@2286
   625
deba@2290
   626
    template <typename Graph, typename Item, typename RefMap, typename It>
deba@2290
   627
    class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2290
   628
    public:
deba@2290
   629
deba@2290
   630
      ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
deba@2290
   631
      
deba@2290
   632
      virtual void copy(const Graph&, const RefMap& refMap) {
deba@2290
   633
        _it = refMap[_item];
deba@2290
   634
      }
deba@2290
   635
deba@2290
   636
    private:
deba@2290
   637
      It& _it;
deba@2290
   638
      Item _item;
deba@2290
   639
    };
deba@2290
   640
deba@2286
   641
    template <typename Graph, typename Item, typename RefMap, typename Ref>
deba@2286
   642
    class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   643
    public:
deba@2286
   644
deba@2286
   645
      RefCopy(Ref& map) : _map(map) {}
deba@2286
   646
      
deba@2286
   647
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   648
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   649
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   650
          _map.set(it, refMap[it]);
deba@2286
   651
        }
deba@2286
   652
      }
deba@2286
   653
deba@2286
   654
    private:
deba@2286
   655
      Ref& _map;
deba@2286
   656
    };
deba@2286
   657
deba@2286
   658
    template <typename Graph, typename Item, typename RefMap, 
deba@2286
   659
              typename CrossRef>
deba@2286
   660
    class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
deba@2286
   661
    public:
deba@2286
   662
deba@2286
   663
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
deba@2286
   664
      
deba@2286
   665
      virtual void copy(const Graph& graph, const RefMap& refMap) {
deba@2286
   666
        typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@2286
   667
        for (ItemIt it(graph); it != INVALID; ++it) {
deba@2286
   668
          _cmap.set(refMap[it], it);
deba@2286
   669
        }
deba@2286
   670
      }
deba@2286
   671
deba@2286
   672
    private:
deba@2286
   673
      CrossRef& _cmap;
deba@2286
   674
    };
deba@2286
   675
deba@2290
   676
    template <typename Graph, typename Enable = void>
deba@2290
   677
    struct GraphCopySelector {
deba@2485
   678
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
deba@2485
   679
      static void copy(Graph &to, const From& from,
deba@2290
   680
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@2485
   681
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@2485
   682
          nodeRefMap[it] = to.addNode();
deba@2290
   683
        }
deba@2485
   684
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
deba@2485
   685
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)], 
deba@2485
   686
                                          nodeRefMap[from.target(it)]);
deba@2290
   687
        }
deba@2290
   688
      }
deba@2290
   689
    };
deba@2290
   690
deba@2290
   691
    template <typename Graph>
deba@2290
   692
    struct GraphCopySelector<
deba@2290
   693
      Graph, 
deba@2329
   694
      typename enable_if<typename Graph::BuildTag, void>::type> 
deba@2290
   695
    {
deba@2485
   696
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
deba@2485
   697
      static void copy(Graph &to, const From& from,
deba@2290
   698
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
deba@2485
   699
        to.build(from, nodeRefMap, edgeRefMap);
deba@2290
   700
      }
deba@2290
   701
    };
deba@2290
   702
deba@2290
   703
    template <typename UGraph, typename Enable = void>
deba@2290
   704
    struct UGraphCopySelector {
deba@2485
   705
      template <typename From, typename NodeRefMap, typename UEdgeRefMap>
deba@2485
   706
      static void copy(UGraph &to, const From& from,
deba@2290
   707
                       NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
deba@2485
   708
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
deba@2485
   709
          nodeRefMap[it] = to.addNode();
deba@2290
   710
        }
deba@2485
   711
        for (typename From::UEdgeIt it(from); it != INVALID; ++it) {
deba@2485
   712
          uEdgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)], 
deba@2485
   713
				       nodeRefMap[from.target(it)]);
deba@2290
   714
        }
deba@2290
   715
      }
deba@2290
   716
    };
deba@2290
   717
deba@2290
   718
    template <typename UGraph>
deba@2290
   719
    struct UGraphCopySelector<
deba@2290
   720
      UGraph, 
deba@2329
   721
      typename enable_if<typename UGraph::BuildTag, void>::type> 
deba@2290
   722
    {
deba@2485
   723
      template <typename From, typename NodeRefMap, typename UEdgeRefMap>
deba@2485
   724
      static void copy(UGraph &to, const From& from,
deba@2290
   725
                       NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
deba@2485
   726
        to.build(from, nodeRefMap, uEdgeRefMap);
deba@2290
   727
      }
deba@2290
   728
    };
deba@2290
   729
deba@2290
   730
    template <typename BpUGraph, typename Enable = void>
deba@2290
   731
    struct BpUGraphCopySelector {
deba@2485
   732
      template <typename From, typename ANodeRefMap, 
deba@2290
   733
                typename BNodeRefMap, typename UEdgeRefMap>
deba@2485
   734
      static void copy(BpUGraph &to, const From& from,
deba@2290
   735
                       ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
deba@2290
   736
                       UEdgeRefMap& uEdgeRefMap) {
deba@2485
   737
        for (typename From::ANodeIt it(from); it != INVALID; ++it) {
deba@2485
   738
          aNodeRefMap[it] = to.addANode();
deba@2290
   739
        }
deba@2485
   740
        for (typename From::BNodeIt it(from); it != INVALID; ++it) {
deba@2485
   741
          bNodeRefMap[it] = to.addBNode();
deba@2290
   742
        }
deba@2485
   743
        for (typename From::UEdgeIt it(from); it != INVALID; ++it) {
deba@2485
   744
          uEdgeRefMap[it] = to.addEdge(aNodeRefMap[from.aNode(it)], 
deba@2485
   745
                                           bNodeRefMap[from.bNode(it)]);
deba@2290
   746
        }
deba@2290
   747
      }
deba@2290
   748
    };
deba@2290
   749
deba@2290
   750
    template <typename BpUGraph>
deba@2290
   751
    struct BpUGraphCopySelector<
deba@2290
   752
      BpUGraph, 
deba@2329
   753
      typename enable_if<typename BpUGraph::BuildTag, void>::type> 
deba@2290
   754
    {
deba@2485
   755
      template <typename From, typename ANodeRefMap, 
deba@2290
   756
                typename BNodeRefMap, typename UEdgeRefMap>
deba@2485
   757
      static void copy(BpUGraph &to, const From& from,
deba@2290
   758
                       ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
deba@2290
   759
                       UEdgeRefMap& uEdgeRefMap) {
deba@2485
   760
        to.build(from, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
deba@2290
   761
      }
deba@2290
   762
    };
deba@2290
   763
    
deba@2290
   764
deba@2286
   765
  }
deba@2286
   766
athos@1540
   767
  /// \brief Class to copy a graph.
deba@1531
   768
  ///
alpar@2006
   769
  /// Class to copy a graph to another graph (duplicate a graph). The
athos@1540
   770
  /// simplest way of using it is through the \c copyGraph() function.
deba@2485
   771
  template <typename To, typename From>
deba@1267
   772
  class GraphCopy {
deba@2286
   773
  private:
deba@2286
   774
deba@2485
   775
    typedef typename From::Node Node;
deba@2485
   776
    typedef typename From::NodeIt NodeIt;
deba@2485
   777
    typedef typename From::Edge Edge;
deba@2485
   778
    typedef typename From::EdgeIt EdgeIt;
klao@946
   779
deba@2485
   780
    typedef typename To::Node TNode;
deba@2485
   781
    typedef typename To::Edge TEdge;
deba@2286
   782
deba@2485
   783
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@2485
   784
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
deba@2286
   785
    
deba@2286
   786
    
deba@2286
   787
  public: 
deba@2286
   788
klao@946
   789
deba@1531
   790
    /// \brief Constructor for the GraphCopy.
deba@1531
   791
    ///
deba@2485
   792
    /// It copies the content of the \c _from graph into the
deba@2485
   793
    /// \c _to graph.
deba@2485
   794
    GraphCopy(To& _to, const From& _from) 
deba@2485
   795
      : from(_from), to(_to) {}
deba@2286
   796
deba@2286
   797
    /// \brief Destructor of the GraphCopy
deba@2286
   798
    ///
deba@2286
   799
    /// Destructor of the GraphCopy
deba@2286
   800
    ~GraphCopy() {
deba@2386
   801
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
   802
        delete nodeMapCopies[i];
deba@1531
   803
      }
deba@2386
   804
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
   805
        delete edgeMapCopies[i];
deba@1531
   806
      }
deba@2286
   807
deba@1267
   808
    }
klao@946
   809
deba@1531
   810
    /// \brief Copies the node references into the given map.
deba@1531
   811
    ///
deba@1531
   812
    /// Copies the node references into the given map.
deba@1531
   813
    template <typename NodeRef>
deba@2286
   814
    GraphCopy& nodeRef(NodeRef& map) {
deba@2485
   815
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, 
deba@2286
   816
                              NodeRefMap, NodeRef>(map));
deba@1531
   817
      return *this;
deba@1267
   818
    }
deba@1531
   819
deba@2290
   820
    /// \brief Copies the node cross references into the given map.
deba@1531
   821
    ///
deba@2290
   822
    ///  Copies the node cross references (reverse references) into
deba@2290
   823
    ///  the given map.
deba@2286
   824
    template <typename NodeCrossRef>
deba@2286
   825
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2485
   826
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
deba@2286
   827
                              NodeRefMap, NodeCrossRef>(map));
deba@1531
   828
      return *this;
deba@1531
   829
    }
deba@1531
   830
deba@1531
   831
    /// \brief Make copy of the given map.
deba@1531
   832
    ///
deba@1531
   833
    /// Makes copy of the given map for the newly created graph. 
deba@2485
   834
    /// The new map's key type is the to graph's node type,
deba@2485
   835
    /// and the copied map's key type is the from graph's node
deba@1531
   836
    /// type.  
deba@2485
   837
    template <typename ToMap, typename FromMap>
deba@2485
   838
    GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
deba@2485
   839
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, 
deba@2485
   840
                              NodeRefMap, ToMap, FromMap>(tmap, map));
deba@2286
   841
      return *this;
deba@2286
   842
    }
deba@2286
   843
deba@2290
   844
    /// \brief Make a copy of the given node.
deba@2290
   845
    ///
deba@2290
   846
    /// Make a copy of the given node.
deba@2386
   847
    GraphCopy& node(TNode& tnode, const Node& snode) {
deba@2485
   848
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, 
deba@2386
   849
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
   850
      return *this;
deba@2290
   851
    }
deba@2290
   852
deba@2286
   853
    /// \brief Copies the edge references into the given map.
deba@2286
   854
    ///
deba@2286
   855
    /// Copies the edge references into the given map.
deba@2286
   856
    template <typename EdgeRef>
deba@2286
   857
    GraphCopy& edgeRef(EdgeRef& map) {
deba@2485
   858
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, 
deba@2286
   859
                              EdgeRefMap, EdgeRef>(map));
deba@2286
   860
      return *this;
deba@2286
   861
    }
deba@2286
   862
deba@2290
   863
    /// \brief Copies the edge cross references into the given map.
deba@2286
   864
    ///
deba@2290
   865
    ///  Copies the edge cross references (reverse references) into
deba@2290
   866
    ///  the given map.
deba@2286
   867
    template <typename EdgeCrossRef>
deba@2286
   868
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2485
   869
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
deba@2286
   870
                              EdgeRefMap, EdgeCrossRef>(map));
deba@1531
   871
      return *this;
deba@1531
   872
    }
deba@1531
   873
deba@1531
   874
    /// \brief Make copy of the given map.
deba@1531
   875
    ///
deba@1531
   876
    /// Makes copy of the given map for the newly created graph. 
deba@2485
   877
    /// The new map's key type is the to graph's edge type,
deba@2485
   878
    /// and the copied map's key type is the from graph's edge
deba@1531
   879
    /// type.  
deba@2485
   880
    template <typename ToMap, typename FromMap>
deba@2485
   881
    GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
deba@2485
   882
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, 
deba@2485
   883
                              EdgeRefMap, ToMap, FromMap>(tmap, map));
deba@1531
   884
      return *this;
deba@1531
   885
    }
deba@1531
   886
deba@2290
   887
    /// \brief Make a copy of the given edge.
deba@2290
   888
    ///
deba@2290
   889
    /// Make a copy of the given edge.
deba@2386
   890
    GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2485
   891
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, 
deba@2386
   892
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
   893
      return *this;
deba@2290
   894
    }
deba@2290
   895
deba@2286
   896
    /// \brief Executes the copies.
deba@1531
   897
    ///
deba@2286
   898
    /// Executes the copies.
deba@2286
   899
    void run() {
deba@2485
   900
      NodeRefMap nodeRefMap(from);
deba@2485
   901
      EdgeRefMap edgeRefMap(from);
deba@2485
   902
      _graph_utils_bits::GraphCopySelector<To>::
deba@2485
   903
        copy(to, from, nodeRefMap, edgeRefMap);
deba@2386
   904
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2485
   905
        nodeMapCopies[i]->copy(from, nodeRefMap);
deba@2286
   906
      }
deba@2386
   907
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2485
   908
        edgeMapCopies[i]->copy(from, edgeRefMap);
deba@2290
   909
      }      
deba@1531
   910
    }
deba@1531
   911
deba@2290
   912
  protected:
deba@2290
   913
deba@2290
   914
deba@2485
   915
    const From& from;
deba@2485
   916
    To& to;
deba@1531
   917
deba@2485
   918
    std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
deba@2286
   919
    nodeMapCopies;
deba@2286
   920
deba@2485
   921
    std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > 
deba@2286
   922
    edgeMapCopies;
deba@2286
   923
deba@1267
   924
  };
klao@946
   925
alpar@2006
   926
  /// \brief Copy a graph to another graph.
deba@1531
   927
  ///
alpar@2006
   928
  /// Copy a graph to another graph.
deba@1531
   929
  /// The usage of the function:
deba@1531
   930
  /// 
alpar@1946
   931
  ///\code
deba@2286
   932
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
alpar@1946
   933
  ///\endcode
deba@1531
   934
  /// 
deba@1531
   935
  /// After the copy the \c nr map will contain the mapping from the
kpeter@2534
   936
  /// nodes of the \c from graph to the nodes of the \c to graph and
kpeter@2534
   937
  /// \c ecr will contain the mapping from the edges of the \c to graph
kpeter@2534
   938
  /// to the edges of the \c from graph.
deba@2290
   939
  ///
deba@2290
   940
  /// \see GraphCopy 
deba@2485
   941
  template <typename To, typename From>
deba@2485
   942
  GraphCopy<To, From> copyGraph(To& to, const From& from) {
deba@2485
   943
    return GraphCopy<To, From>(to, from);
deba@1531
   944
  }
klao@946
   945
deba@1720
   946
  /// \brief Class to copy an undirected graph.
deba@1720
   947
  ///
alpar@2006
   948
  /// Class to copy an undirected graph to another graph (duplicate a graph).
klao@1909
   949
  /// The simplest way of using it is through the \c copyUGraph() function.
deba@2485
   950
  template <typename To, typename From>
klao@1909
   951
  class UGraphCopy {
deba@2286
   952
  private:
deba@2286
   953
deba@2485
   954
    typedef typename From::Node Node;
deba@2485
   955
    typedef typename From::NodeIt NodeIt;
deba@2485
   956
    typedef typename From::Edge Edge;
deba@2485
   957
    typedef typename From::EdgeIt EdgeIt;
deba@2485
   958
    typedef typename From::UEdge UEdge;
deba@2485
   959
    typedef typename From::UEdgeIt UEdgeIt;
deba@1720
   960
deba@2485
   961
    typedef typename To::Node TNode;
deba@2485
   962
    typedef typename To::Edge TEdge;
deba@2485
   963
    typedef typename To::UEdge TUEdge;
deba@1720
   964
deba@2485
   965
    typedef typename From::template NodeMap<TNode> NodeRefMap;
deba@2485
   966
    typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap;
deba@1720
   967
deba@1720
   968
    struct EdgeRefMap {
deba@2485
   969
      EdgeRefMap(const To& _to, const From& _from,
deba@2286
   970
                 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
deba@2485
   971
        : to(_to), from(_from), 
deba@2286
   972
          uedge_ref(_uedge_ref), node_ref(_node_ref) {}
deba@2286
   973
deba@2485
   974
      typedef typename From::Edge Key;
deba@2485
   975
      typedef typename To::Edge Value;
deba@1720
   976
deba@2286
   977
      Value operator[](const Key& key) const {
deba@2386
   978
        bool forward = 
deba@2485
   979
          (from.direction(key) == 
deba@2485
   980
           (node_ref[from.source(static_cast<const UEdge&>(key))] == 
deba@2485
   981
            to.source(uedge_ref[static_cast<const UEdge&>(key)])));
deba@2485
   982
	return to.direct(uedge_ref[key], forward); 
deba@1720
   983
      }
deba@1720
   984
      
deba@2485
   985
      const To& to;
deba@2485
   986
      const From& from;
deba@2286
   987
      const UEdgeRefMap& uedge_ref;
deba@2286
   988
      const NodeRefMap& node_ref;
deba@1720
   989
    };
deba@2286
   990
deba@1720
   991
    
deba@2286
   992
  public: 
deba@1720
   993
deba@2286
   994
deba@2286
   995
    /// \brief Constructor for the GraphCopy.
deba@1720
   996
    ///
deba@2485
   997
    /// It copies the content of the \c _from graph into the
deba@2485
   998
    /// \c _to graph.
deba@2485
   999
    UGraphCopy(To& _to, const From& _from) 
deba@2485
  1000
      : from(_from), to(_to) {}
deba@2286
  1001
deba@2286
  1002
    /// \brief Destructor of the GraphCopy
deba@2286
  1003
    ///
deba@2286
  1004
    /// Destructor of the GraphCopy
deba@2286
  1005
    ~UGraphCopy() {
deba@2386
  1006
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2286
  1007
        delete nodeMapCopies[i];
deba@1720
  1008
      }
deba@2386
  1009
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2286
  1010
        delete edgeMapCopies[i];
deba@1720
  1011
      }
deba@2386
  1012
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2286
  1013
        delete uEdgeMapCopies[i];
deba@2286
  1014
      }
deba@2286
  1015
deba@1720
  1016
    }
deba@1720
  1017
deba@1720
  1018
    /// \brief Copies the node references into the given map.
deba@1720
  1019
    ///
deba@1720
  1020
    /// Copies the node references into the given map.
deba@1720
  1021
    template <typename NodeRef>
deba@2286
  1022
    UGraphCopy& nodeRef(NodeRef& map) {
deba@2485
  1023
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, 
deba@2286
  1024
                              NodeRefMap, NodeRef>(map));
deba@1720
  1025
      return *this;
deba@1720
  1026
    }
deba@1720
  1027
deba@2290
  1028
    /// \brief Copies the node cross references into the given map.
deba@1720
  1029
    ///
deba@2290
  1030
    ///  Copies the node cross references (reverse references) into
deba@2290
  1031
    ///  the given map.
deba@2286
  1032
    template <typename NodeCrossRef>
deba@2286
  1033
    UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2485
  1034
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
deba@2286
  1035
                              NodeRefMap, NodeCrossRef>(map));
deba@1720
  1036
      return *this;
deba@1720
  1037
    }
deba@1720
  1038
deba@1720
  1039
    /// \brief Make copy of the given map.
deba@1720
  1040
    ///
deba@1720
  1041
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1042
    /// The new map's key type is the to graph's node type,
deba@2485
  1043
    /// and the copied map's key type is the from graph's node
deba@1720
  1044
    /// type.  
deba@2485
  1045
    template <typename ToMap, typename FromMap>
deba@2485
  1046
    UGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1047
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, 
deba@2485
  1048
                              NodeRefMap, ToMap, FromMap>(tmap, map));
deba@2286
  1049
      return *this;
deba@2286
  1050
    }
deba@2286
  1051
deba@2290
  1052
    /// \brief Make a copy of the given node.
deba@2290
  1053
    ///
deba@2290
  1054
    /// Make a copy of the given node.
deba@2386
  1055
    UGraphCopy& node(TNode& tnode, const Node& snode) {
deba@2485
  1056
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, 
deba@2386
  1057
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
  1058
      return *this;
deba@2290
  1059
    }
deba@2290
  1060
deba@2286
  1061
    /// \brief Copies the edge references into the given map.
deba@2286
  1062
    ///
deba@2286
  1063
    /// Copies the edge references into the given map.
deba@2286
  1064
    template <typename EdgeRef>
deba@2286
  1065
    UGraphCopy& edgeRef(EdgeRef& map) {
deba@2485
  1066
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, 
deba@2286
  1067
                              EdgeRefMap, EdgeRef>(map));
deba@2286
  1068
      return *this;
deba@2286
  1069
    }
deba@2286
  1070
deba@2290
  1071
    /// \brief Copies the edge cross references into the given map.
deba@2286
  1072
    ///
deba@2290
  1073
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1074
    ///  the given map.
deba@2286
  1075
    template <typename EdgeCrossRef>
deba@2286
  1076
    UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2485
  1077
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
deba@2286
  1078
                              EdgeRefMap, EdgeCrossRef>(map));
deba@1720
  1079
      return *this;
deba@1720
  1080
    }
deba@1720
  1081
deba@1720
  1082
    /// \brief Make copy of the given map.
deba@1720
  1083
    ///
deba@1720
  1084
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1085
    /// The new map's key type is the to graph's edge type,
deba@2485
  1086
    /// and the copied map's key type is the from graph's edge
deba@1720
  1087
    /// type.  
deba@2485
  1088
    template <typename ToMap, typename FromMap>
deba@2485
  1089
    UGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1090
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, 
deba@2485
  1091
                              EdgeRefMap, ToMap, FromMap>(tmap, map));
deba@2286
  1092
      return *this;
deba@2286
  1093
    }
deba@2286
  1094
deba@2290
  1095
    /// \brief Make a copy of the given edge.
deba@2286
  1096
    ///
deba@2290
  1097
    /// Make a copy of the given edge.
deba@2386
  1098
    UGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2485
  1099
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, 
deba@2386
  1100
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
  1101
      return *this;
deba@2290
  1102
    }
deba@2290
  1103
deba@2290
  1104
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1105
    ///
deba@2290
  1106
    /// Copies the undirected edge references into the given map.
deba@2286
  1107
    template <typename UEdgeRef>
deba@2286
  1108
    UGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2485
  1109
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge, 
deba@2286
  1110
                               UEdgeRefMap, UEdgeRef>(map));
deba@2286
  1111
      return *this;
deba@2286
  1112
    }
deba@2286
  1113
deba@2290
  1114
    /// \brief Copies the undirected edge cross references into the given map.
deba@2286
  1115
    ///
deba@2290
  1116
    /// Copies the undirected edge cross references (reverse
deba@2290
  1117
    /// references) into the given map.
deba@2286
  1118
    template <typename UEdgeCrossRef>
deba@2286
  1119
    UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2485
  1120
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, 
deba@2286
  1121
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@1720
  1122
      return *this;
deba@1720
  1123
    }
deba@1720
  1124
deba@1720
  1125
    /// \brief Make copy of the given map.
deba@1720
  1126
    ///
deba@1720
  1127
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1128
    /// The new map's key type is the to graph's undirected edge type,
deba@2485
  1129
    /// and the copied map's key type is the from graph's undirected edge
deba@1720
  1130
    /// type.  
deba@2485
  1131
    template <typename ToMap, typename FromMap>
deba@2485
  1132
    UGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1133
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge, 
deba@2485
  1134
                               UEdgeRefMap, ToMap, FromMap>(tmap, map));
deba@1720
  1135
      return *this;
deba@1720
  1136
    }
deba@1720
  1137
deba@2290
  1138
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1139
    ///
deba@2290
  1140
    /// Make a copy of the given undirected edge.
deba@2386
  1141
    UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
deba@2485
  1142
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge, 
deba@2386
  1143
                               UEdgeRefMap, TUEdge>(tuedge, suedge));
deba@2290
  1144
      return *this;
deba@2290
  1145
    }
deba@2290
  1146
deba@2286
  1147
    /// \brief Executes the copies.
deba@1720
  1148
    ///
deba@2286
  1149
    /// Executes the copies.
deba@2286
  1150
    void run() {
deba@2485
  1151
      NodeRefMap nodeRefMap(from);
deba@2485
  1152
      UEdgeRefMap uEdgeRefMap(from);
deba@2485
  1153
      EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap);
deba@2485
  1154
      _graph_utils_bits::UGraphCopySelector<To>::
deba@2485
  1155
        copy(to, from, nodeRefMap, uEdgeRefMap);
deba@2386
  1156
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2485
  1157
        nodeMapCopies[i]->copy(from, nodeRefMap);
deba@2286
  1158
      }
deba@2386
  1159
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2485
  1160
        uEdgeMapCopies[i]->copy(from, uEdgeRefMap);
deba@2286
  1161
      }
deba@2386
  1162
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2485
  1163
        edgeMapCopies[i]->copy(from, edgeRefMap);
deba@2286
  1164
      }
deba@1720
  1165
    }
deba@1720
  1166
deba@1720
  1167
  private:
deba@1192
  1168
    
deba@2485
  1169
    const From& from;
deba@2485
  1170
    To& to;
alpar@947
  1171
deba@2485
  1172
    std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
deba@2286
  1173
    nodeMapCopies;
deba@2286
  1174
deba@2485
  1175
    std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > 
deba@2286
  1176
    edgeMapCopies;
deba@2286
  1177
deba@2485
  1178
    std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* > 
deba@2286
  1179
    uEdgeMapCopies;
deba@2286
  1180
deba@1192
  1181
  };
deba@1192
  1182
deba@2290
  1183
  /// \brief Copy an undirected graph to another graph.
deba@1720
  1184
  ///
deba@2290
  1185
  /// Copy an undirected graph to another graph.
deba@1720
  1186
  /// The usage of the function:
deba@1720
  1187
  /// 
alpar@1946
  1188
  ///\code
deba@2286
  1189
  /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
alpar@1946
  1190
  ///\endcode
deba@1720
  1191
  /// 
deba@1720
  1192
  /// After the copy the \c nr map will contain the mapping from the
kpeter@2534
  1193
  /// nodes of the \c from graph to the nodes of the \c to graph and
kpeter@2534
  1194
  /// \c ecr will contain the mapping from the edges of the \c to graph
kpeter@2534
  1195
  /// to the edges of the \c from graph.
deba@2290
  1196
  ///
deba@2290
  1197
  /// \see UGraphCopy 
deba@2485
  1198
  template <typename To, typename From>
deba@2485
  1199
  UGraphCopy<To, From> 
deba@2485
  1200
  copyUGraph(To& to, const From& from) {
deba@2485
  1201
    return UGraphCopy<To, From>(to, from);
deba@1720
  1202
  }
deba@1192
  1203
deba@2290
  1204
  /// \brief Class to copy a bipartite undirected graph.
deba@2290
  1205
  ///
deba@2290
  1206
  /// Class to copy a bipartite undirected graph to another graph
deba@2290
  1207
  /// (duplicate a graph).  The simplest way of using it is through
deba@2290
  1208
  /// the \c copyBpUGraph() function.
deba@2485
  1209
  template <typename To, typename From>
deba@2290
  1210
  class BpUGraphCopy {
deba@2290
  1211
  private:
deba@2290
  1212
deba@2485
  1213
    typedef typename From::Node Node;
deba@2485
  1214
    typedef typename From::ANode ANode;
deba@2485
  1215
    typedef typename From::BNode BNode;
deba@2485
  1216
    typedef typename From::NodeIt NodeIt;
deba@2485
  1217
    typedef typename From::Edge Edge;
deba@2485
  1218
    typedef typename From::EdgeIt EdgeIt;
deba@2485
  1219
    typedef typename From::UEdge UEdge;
deba@2485
  1220
    typedef typename From::UEdgeIt UEdgeIt;
deba@2290
  1221
deba@2485
  1222
    typedef typename To::Node TNode;
deba@2485
  1223
    typedef typename To::Edge TEdge;
deba@2485
  1224
    typedef typename To::UEdge TUEdge;
deba@2290
  1225
deba@2485
  1226
    typedef typename From::template ANodeMap<TNode> ANodeRefMap;
deba@2485
  1227
    typedef typename From::template BNodeMap<TNode> BNodeRefMap;
deba@2485
  1228
    typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap;
deba@2290
  1229
deba@2290
  1230
    struct NodeRefMap {
deba@2485
  1231
      NodeRefMap(const From& _from, const ANodeRefMap& _anode_ref,
deba@2290
  1232
                 const BNodeRefMap& _bnode_ref)
deba@2485
  1233
        : from(_from), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
deba@2290
  1234
deba@2485
  1235
      typedef typename From::Node Key;
deba@2485
  1236
      typedef typename To::Node Value;
deba@2290
  1237
deba@2290
  1238
      Value operator[](const Key& key) const {
deba@2485
  1239
	return from.aNode(key) ? anode_ref[key] : bnode_ref[key]; 
deba@2290
  1240
      }
deba@2290
  1241
      
deba@2485
  1242
      const From& from;
deba@2290
  1243
      const ANodeRefMap& anode_ref;
deba@2290
  1244
      const BNodeRefMap& bnode_ref;
deba@2290
  1245
    };
deba@2290
  1246
deba@2290
  1247
    struct EdgeRefMap {
deba@2485
  1248
      EdgeRefMap(const To& _to, const From& _from,
deba@2290
  1249
                 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref) 
deba@2485
  1250
        : to(_to), from(_from), 
deba@2290
  1251
          uedge_ref(_uedge_ref), node_ref(_node_ref) {}
deba@2290
  1252
deba@2485
  1253
      typedef typename From::Edge Key;
deba@2485
  1254
      typedef typename To::Edge Value;
deba@2290
  1255
deba@2290
  1256
      Value operator[](const Key& key) const {
deba@2386
  1257
        bool forward = 
deba@2485
  1258
          (from.direction(key) == 
deba@2485
  1259
           (node_ref[from.source(static_cast<const UEdge&>(key))] == 
deba@2485
  1260
            to.source(uedge_ref[static_cast<const UEdge&>(key)])));
deba@2485
  1261
	return to.direct(uedge_ref[key], forward); 
deba@2290
  1262
      }
deba@2290
  1263
      
deba@2485
  1264
      const To& to;
deba@2485
  1265
      const From& from;
deba@2290
  1266
      const UEdgeRefMap& uedge_ref;
deba@2290
  1267
      const NodeRefMap& node_ref;
deba@2290
  1268
    };
deba@2290
  1269
    
deba@2290
  1270
  public: 
deba@2290
  1271
deba@2290
  1272
deba@2290
  1273
    /// \brief Constructor for the GraphCopy.
deba@2290
  1274
    ///
deba@2485
  1275
    /// It copies the content of the \c _from graph into the
deba@2485
  1276
    /// \c _to graph.
deba@2485
  1277
    BpUGraphCopy(To& _to, const From& _from) 
deba@2485
  1278
      : from(_from), to(_to) {}
deba@2290
  1279
deba@2290
  1280
    /// \brief Destructor of the GraphCopy
deba@2290
  1281
    ///
deba@2290
  1282
    /// Destructor of the GraphCopy
deba@2290
  1283
    ~BpUGraphCopy() {
deba@2386
  1284
      for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
deba@2290
  1285
        delete aNodeMapCopies[i];
deba@2290
  1286
      }
deba@2386
  1287
      for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
deba@2290
  1288
        delete bNodeMapCopies[i];
deba@2290
  1289
      }
deba@2386
  1290
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2290
  1291
        delete nodeMapCopies[i];
deba@2290
  1292
      }
deba@2386
  1293
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2290
  1294
        delete edgeMapCopies[i];
deba@2290
  1295
      }
deba@2386
  1296
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2290
  1297
        delete uEdgeMapCopies[i];
deba@2290
  1298
      }
deba@2290
  1299
deba@2290
  1300
    }
deba@2290
  1301
deba@2290
  1302
    /// \brief Copies the A-node references into the given map.
deba@2290
  1303
    ///
deba@2290
  1304
    /// Copies the A-node references into the given map.
deba@2290
  1305
    template <typename ANodeRef>
deba@2290
  1306
    BpUGraphCopy& aNodeRef(ANodeRef& map) {
deba@2485
  1307
      aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, ANode, 
deba@2290
  1308
                               ANodeRefMap, ANodeRef>(map));
deba@2290
  1309
      return *this;
deba@2290
  1310
    }
deba@2290
  1311
deba@2290
  1312
    /// \brief Copies the A-node cross references into the given map.
deba@2290
  1313
    ///
deba@2290
  1314
    /// Copies the A-node cross references (reverse references) into
deba@2290
  1315
    /// the given map.
deba@2290
  1316
    template <typename ANodeCrossRef>
deba@2290
  1317
    BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
deba@2485
  1318
      aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, 
deba@2290
  1319
                               ANode, ANodeRefMap, ANodeCrossRef>(map));
deba@2290
  1320
      return *this;
deba@2290
  1321
    }
deba@2290
  1322
deba@2290
  1323
    /// \brief Make copy of the given A-node map.
deba@2290
  1324
    ///
deba@2290
  1325
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1326
    /// The new map's key type is the to graph's node type,
deba@2485
  1327
    /// and the copied map's key type is the from graph's node
deba@2290
  1328
    /// type.  
deba@2485
  1329
    template <typename ToMap, typename FromMap>
deba@2485
  1330
    BpUGraphCopy& aNodeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1331
      aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, ANode, 
deba@2485
  1332
                               ANodeRefMap, ToMap, FromMap>(tmap, map));
deba@2290
  1333
      return *this;
deba@2290
  1334
    }
deba@2290
  1335
deba@2290
  1336
    /// \brief Copies the B-node references into the given map.
deba@2290
  1337
    ///
deba@2290
  1338
    /// Copies the B-node references into the given map.
deba@2290
  1339
    template <typename BNodeRef>
deba@2290
  1340
    BpUGraphCopy& bNodeRef(BNodeRef& map) {
deba@2485
  1341
      bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, BNode, 
deba@2290
  1342
                               BNodeRefMap, BNodeRef>(map));
deba@2290
  1343
      return *this;
deba@2290
  1344
    }
deba@2290
  1345
deba@2290
  1346
    /// \brief Copies the B-node cross references into the given map.
deba@2290
  1347
    ///
deba@2290
  1348
    ///  Copies the B-node cross references (reverse references) into
deba@2290
  1349
    ///  the given map.
deba@2290
  1350
    template <typename BNodeCrossRef>
deba@2290
  1351
    BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
deba@2485
  1352
      bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, 
deba@2290
  1353
                              BNode, BNodeRefMap, BNodeCrossRef>(map));
deba@2290
  1354
      return *this;
deba@2290
  1355
    }
deba@2290
  1356
deba@2290
  1357
    /// \brief Make copy of the given B-node map.
deba@2290
  1358
    ///
deba@2290
  1359
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1360
    /// The new map's key type is the to graph's node type,
deba@2485
  1361
    /// and the copied map's key type is the from graph's node
deba@2290
  1362
    /// type.  
deba@2485
  1363
    template <typename ToMap, typename FromMap>
deba@2485
  1364
    BpUGraphCopy& bNodeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1365
      bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, BNode, 
deba@2485
  1366
                               BNodeRefMap, ToMap, FromMap>(tmap, map));
deba@2290
  1367
      return *this;
deba@2290
  1368
    }
deba@2290
  1369
    /// \brief Copies the node references into the given map.
deba@2290
  1370
    ///
deba@2290
  1371
    /// Copies the node references into the given map.
deba@2290
  1372
    template <typename NodeRef>
deba@2290
  1373
    BpUGraphCopy& nodeRef(NodeRef& map) {
deba@2485
  1374
      nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node, 
deba@2290
  1375
                              NodeRefMap, NodeRef>(map));
deba@2290
  1376
      return *this;
deba@2290
  1377
    }
deba@2290
  1378
deba@2290
  1379
    /// \brief Copies the node cross references into the given map.
deba@2290
  1380
    ///
deba@2290
  1381
    ///  Copies the node cross references (reverse references) into
deba@2290
  1382
    ///  the given map.
deba@2290
  1383
    template <typename NodeCrossRef>
deba@2290
  1384
    BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
deba@2485
  1385
      nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
deba@2290
  1386
                              NodeRefMap, NodeCrossRef>(map));
deba@2290
  1387
      return *this;
deba@2290
  1388
    }
deba@2290
  1389
deba@2290
  1390
    /// \brief Make copy of the given map.
deba@2290
  1391
    ///
deba@2290
  1392
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1393
    /// The new map's key type is the to graph's node type,
deba@2485
  1394
    /// and the copied map's key type is the from graph's node
deba@2290
  1395
    /// type.  
deba@2485
  1396
    template <typename ToMap, typename FromMap>
deba@2485
  1397
    BpUGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1398
      nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node, 
deba@2485
  1399
                              NodeRefMap, ToMap, FromMap>(tmap, map));
deba@2290
  1400
      return *this;
deba@2290
  1401
    }
deba@2290
  1402
deba@2290
  1403
    /// \brief Make a copy of the given node.
deba@2290
  1404
    ///
deba@2290
  1405
    /// Make a copy of the given node.
deba@2386
  1406
    BpUGraphCopy& node(TNode& tnode, const Node& snode) {
deba@2485
  1407
      nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node, 
deba@2386
  1408
                              NodeRefMap, TNode>(tnode, snode));
deba@2290
  1409
      return *this;
deba@2290
  1410
    }
deba@2290
  1411
deba@2290
  1412
    /// \brief Copies the edge references into the given map.
deba@2290
  1413
    ///
deba@2290
  1414
    /// Copies the edge references into the given map.
deba@2290
  1415
    template <typename EdgeRef>
deba@2290
  1416
    BpUGraphCopy& edgeRef(EdgeRef& map) {
deba@2485
  1417
      edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge, 
deba@2290
  1418
                              EdgeRefMap, EdgeRef>(map));
deba@2290
  1419
      return *this;
deba@2290
  1420
    }
deba@2290
  1421
deba@2290
  1422
    /// \brief Copies the edge cross references into the given map.
deba@2290
  1423
    ///
deba@2290
  1424
    ///  Copies the edge cross references (reverse references) into
deba@2290
  1425
    ///  the given map.
deba@2290
  1426
    template <typename EdgeCrossRef>
deba@2290
  1427
    BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
deba@2485
  1428
      edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
deba@2290
  1429
                              EdgeRefMap, EdgeCrossRef>(map));
deba@2290
  1430
      return *this;
deba@2290
  1431
    }
deba@2290
  1432
deba@2290
  1433
    /// \brief Make copy of the given map.
deba@2290
  1434
    ///
deba@2290
  1435
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1436
    /// The new map's key type is the to graph's edge type,
deba@2485
  1437
    /// and the copied map's key type is the from graph's edge
deba@2290
  1438
    /// type.  
deba@2485
  1439
    template <typename ToMap, typename FromMap>
deba@2485
  1440
    BpUGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1441
      edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge, 
deba@2485
  1442
                              EdgeRefMap, ToMap, FromMap>(tmap, map));
deba@2290
  1443
      return *this;
deba@2290
  1444
    }
deba@2290
  1445
deba@2290
  1446
    /// \brief Make a copy of the given edge.
deba@2290
  1447
    ///
deba@2290
  1448
    /// Make a copy of the given edge.
deba@2386
  1449
    BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
deba@2485
  1450
      edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge, 
deba@2386
  1451
                              EdgeRefMap, TEdge>(tedge, sedge));
deba@2290
  1452
      return *this;
deba@2290
  1453
    }
deba@2290
  1454
deba@2290
  1455
    /// \brief Copies the undirected edge references into the given map.
deba@2290
  1456
    ///
deba@2290
  1457
    /// Copies the undirected edge references into the given map.
deba@2290
  1458
    template <typename UEdgeRef>
deba@2290
  1459
    BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
deba@2485
  1460
      uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge, 
deba@2290
  1461
                               UEdgeRefMap, UEdgeRef>(map));
deba@2290
  1462
      return *this;
deba@2290
  1463
    }
deba@2290
  1464
deba@2290
  1465
    /// \brief Copies the undirected edge cross references into the given map.
deba@2290
  1466
    ///
deba@2290
  1467
    /// Copies the undirected edge cross references (reverse
deba@2290
  1468
    /// references) into the given map.
deba@2290
  1469
    template <typename UEdgeCrossRef>
deba@2290
  1470
    BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
deba@2485
  1471
      uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, 
deba@2290
  1472
                               UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
deba@2290
  1473
      return *this;
deba@2290
  1474
    }
deba@2290
  1475
deba@2290
  1476
    /// \brief Make copy of the given map.
deba@2290
  1477
    ///
deba@2290
  1478
    /// Makes copy of the given map for the newly created graph. 
deba@2485
  1479
    /// The new map's key type is the to graph's undirected edge type,
deba@2485
  1480
    /// and the copied map's key type is the from graph's undirected edge
deba@2290
  1481
    /// type.  
deba@2485
  1482
    template <typename ToMap, typename FromMap>
deba@2485
  1483
    BpUGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) {
deba@2485
  1484
      uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge, 
deba@2485
  1485
                               UEdgeRefMap, ToMap, FromMap>(tmap, map));
deba@2290
  1486
      return *this;
deba@2290
  1487
    }
deba@2290
  1488
deba@2290
  1489
    /// \brief Make a copy of the given undirected edge.
deba@2290
  1490
    ///
deba@2290
  1491
    /// Make a copy of the given undirected edge.
deba@2386
  1492
    BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
deba@2485
  1493
      uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge, 
deba@2386
  1494
                               UEdgeRefMap, TUEdge>(tuedge, suedge));
deba@2290
  1495
      return *this;
deba@2290
  1496
    }
deba@2290
  1497
deba@2290
  1498
    /// \brief Executes the copies.
deba@2290
  1499
    ///
deba@2290
  1500
    /// Executes the copies.
deba@2290
  1501
    void run() {
deba@2485
  1502
      ANodeRefMap aNodeRefMap(from);
deba@2485
  1503
      BNodeRefMap bNodeRefMap(from);
deba@2485
  1504
      NodeRefMap nodeRefMap(from, aNodeRefMap, bNodeRefMap);
deba@2485
  1505
      UEdgeRefMap uEdgeRefMap(from);
deba@2485
  1506
      EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap);
deba@2485
  1507
      _graph_utils_bits::BpUGraphCopySelector<To>::
deba@2485
  1508
        copy(to, from, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
deba@2386
  1509
      for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
deba@2485
  1510
        aNodeMapCopies[i]->copy(from, aNodeRefMap);
deba@2290
  1511
      }
deba@2386
  1512
      for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
deba@2485
  1513
        bNodeMapCopies[i]->copy(from, bNodeRefMap);
deba@2290
  1514
      }
deba@2386
  1515
      for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
deba@2485
  1516
        nodeMapCopies[i]->copy(from, nodeRefMap);
deba@2290
  1517
      }
deba@2386
  1518
      for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
deba@2485
  1519
        uEdgeMapCopies[i]->copy(from, uEdgeRefMap);
deba@2290
  1520
      }
deba@2386
  1521
      for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
deba@2485
  1522
        edgeMapCopies[i]->copy(from, edgeRefMap);
deba@2290
  1523
      }
deba@2290
  1524
    }
deba@2290
  1525
deba@2290
  1526
  private:
deba@2290
  1527
    
deba@2485
  1528
    const From& from;
deba@2485
  1529
    To& to;
deba@2290
  1530
deba@2485
  1531
    std::vector<_graph_utils_bits::MapCopyBase<From, ANode, ANodeRefMap>* > 
deba@2290
  1532
    aNodeMapCopies;
deba@2290
  1533
deba@2485
  1534
    std::vector<_graph_utils_bits::MapCopyBase<From, BNode, BNodeRefMap>* > 
deba@2290
  1535
    bNodeMapCopies;
deba@2290
  1536
deba@2485
  1537
    std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > 
deba@2290
  1538
    nodeMapCopies;
deba@2290
  1539
deba@2485
  1540
    std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > 
deba@2290
  1541
    edgeMapCopies;
deba@2290
  1542
deba@2485
  1543
    std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* > 
deba@2290
  1544
    uEdgeMapCopies;
deba@2290
  1545
deba@2290
  1546
  };
deba@2290
  1547
deba@2290
  1548
  /// \brief Copy a bipartite undirected graph to another graph.
deba@2290
  1549
  ///
deba@2290
  1550
  /// Copy a bipartite undirected graph to another graph.
deba@2290
  1551
  /// The usage of the function:
deba@2290
  1552
  /// 
deba@2290
  1553
  ///\code
deba@2290
  1554
  /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
deba@2290
  1555
  ///\endcode
deba@2290
  1556
  /// 
deba@2290
  1557
  /// After the copy the \c nr map will contain the mapping from the
kpeter@2534
  1558
  /// nodes of the \c from graph to the nodes of the \c to graph and
kpeter@2534
  1559
  /// \c ecr will contain the mapping from the edges of the \c to graph
kpeter@2534
  1560
  /// to the edges of the \c from graph.
deba@2290
  1561
  ///
deba@2290
  1562
  /// \see BpUGraphCopy
deba@2485
  1563
  template <typename To, typename From>
deba@2485
  1564
  BpUGraphCopy<To, From> 
deba@2485
  1565
  copyBpUGraph(To& to, const From& from) {
deba@2485
  1566
    return BpUGraphCopy<To, From>(to, from);
deba@2290
  1567
  }
deba@2290
  1568
deba@1192
  1569
deba@1192
  1570
  /// @}
alpar@1402
  1571
alpar@1402
  1572
  /// \addtogroup graph_maps
alpar@1402
  1573
  /// @{
alpar@1402
  1574
deba@1413
  1575
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
  1576
athos@1540
  1577
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
  1578
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
  1579
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
  1580
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
  1581
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
  1582
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
  1583
  /// class \c InverseMap.
deba@1413
  1584
  ///
deba@1413
  1585
  template <typename _Graph, typename _Item>
deba@1413
  1586
  class IdMap {
deba@1413
  1587
  public:
deba@1413
  1588
    typedef _Graph Graph;
deba@1413
  1589
    typedef int Value;
deba@1413
  1590
    typedef _Item Item;
deba@1413
  1591
    typedef _Item Key;
deba@1413
  1592
deba@1413
  1593
    /// \brief Constructor.
deba@1413
  1594
    ///
deba@2331
  1595
    /// Constructor of the map.
deba@2286
  1596
    explicit IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1597
deba@1413
  1598
    /// \brief Gives back the \e id of the item.
deba@1413
  1599
    ///
deba@2331
  1600
    /// Gives back the immutable and unique \e id of the item.
deba@1413
  1601
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
  1602
deba@2331
  1603
    /// \brief Gives back the item by its id.
deba@2331
  1604
    ///
deba@2331
  1605
    /// Gives back the item by its id.
deba@2331
  1606
    Item operator()(int id) { return graph->fromId(id, Item()); }
deba@1413
  1607
deba@1413
  1608
  private:
deba@1413
  1609
    const Graph* graph;
deba@1413
  1610
deba@1413
  1611
  public:
deba@1413
  1612
athos@1540
  1613
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
  1614
    ///
athos@1540
  1615
    /// The class represents the inverse of its owner (IdMap).
deba@1413
  1616
    /// \see inverse()
deba@1413
  1617
    class InverseMap {
deba@1413
  1618
    public:
deba@1419
  1619
deba@1413
  1620
      /// \brief Constructor.
deba@1413
  1621
      ///
deba@1413
  1622
      /// Constructor for creating an id-to-item map.
deba@2286
  1623
      explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
  1624
deba@1413
  1625
      /// \brief Constructor.
deba@1413
  1626
      ///
deba@1413
  1627
      /// Constructor for creating an id-to-item map.
deba@2286
  1628
      explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
  1629
deba@1413
  1630
      /// \brief Gives back the given item from its id.
deba@1413
  1631
      ///
deba@1413
  1632
      /// Gives back the given item from its id.
deba@1413
  1633
      /// 
deba@1413
  1634
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@2331
  1635
deba@1413
  1636
    private:
deba@1413
  1637
      const Graph* graph;
deba@1413
  1638
    };
deba@1413
  1639
deba@1413
  1640
    /// \brief Gives back the inverse of the map.
deba@1413
  1641
    ///
athos@1540
  1642
    /// Gives back the inverse of the IdMap.
deba@1413
  1643
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
  1644
deba@1413
  1645
  };
deba@1413
  1646
deba@1413
  1647
  
athos@1526
  1648
  /// \brief General invertable graph-map type.
alpar@1402
  1649
athos@1540
  1650
  /// This type provides simple invertable graph-maps. 
athos@1526
  1651
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
  1652
  /// and if a key is set to a new value then store it
alpar@1402
  1653
  /// in the inverse map.
deba@1931
  1654
  ///
deba@1931
  1655
  /// The values of the map can be accessed
deba@1931
  1656
  /// with stl compatible forward iterator.
deba@1931
  1657
  ///
alpar@1402
  1658
  /// \param _Graph The graph type.
deba@1830
  1659
  /// \param _Item The item type of the graph.
deba@1830
  1660
  /// \param _Value The value type of the map.
deba@1931
  1661
  ///
deba@1931
  1662
  /// \see IterableValueMap
deba@1830
  1663
  template <typename _Graph, typename _Item, typename _Value>
deba@2287
  1664
  class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
deba@1931
  1665
  private:
deba@1931
  1666
    
deba@2287
  1667
    typedef DefaultMap<_Graph, _Item, _Value> Map;
deba@1931
  1668
    typedef _Graph Graph;
deba@1931
  1669
deba@2287
  1670
    typedef std::map<_Value, _Item> Container;
deba@1931
  1671
    Container invMap;    
deba@1931
  1672
deba@1931
  1673
  public:
deba@1931
  1674
 
deba@2287
  1675
    /// The key type of InvertableMap (Node, Edge, UEdge).
deba@2287
  1676
    typedef typename Map::Key Key;
deba@2287
  1677
    /// The value type of the InvertableMap.
deba@2287
  1678
    typedef typename Map::Value Value;
deba@2287
  1679
deba@1931
  1680
deba@1931
  1681
alpar@1402
  1682
    /// \brief Constructor.
alpar@1402
  1683
    ///
deba@1413
  1684
    /// Construct a new InvertableMap for the graph.
alpar@1402
  1685
    ///
deba@2286
  1686
    explicit InvertableMap(const Graph& graph) : Map(graph) {} 
deba@1931
  1687
deba@1931
  1688
    /// \brief Forward iterator for values.
deba@1931
  1689
    ///
deba@1931
  1690
    /// This iterator is an stl compatible forward
deba@1931
  1691
    /// iterator on the values of the map. The values can
deba@1931
  1692
    /// be accessed in the [beginValue, endValue) range.
deba@1931
  1693
    ///
deba@1931
  1694
    class ValueIterator 
deba@1931
  1695
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
  1696
      friend class InvertableMap;
deba@1931
  1697
    private:
deba@1931
  1698
      ValueIterator(typename Container::const_iterator _it) 
deba@1931
  1699
        : it(_it) {}
deba@1931
  1700
    public:
deba@1931
  1701
      
deba@1931
  1702
      ValueIterator() {}
deba@1931
  1703
deba@1931
  1704
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
  1705
      ValueIterator operator++(int) { 
deba@1931
  1706
        ValueIterator tmp(*this); 
deba@1931
  1707
        operator++();
deba@1931
  1708
        return tmp; 
deba@1931
  1709
      }
deba@1931
  1710
deba@1931
  1711
      const Value& operator*() const { return it->first; }
deba@1931
  1712
      const Value* operator->() const { return &(it->first); }
deba@1931
  1713
deba@1931
  1714
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
  1715
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
  1716
      
deba@1931
  1717
    private:
deba@1931
  1718
      typename Container::const_iterator it;
deba@1931
  1719
    };
deba@1931
  1720
deba@1931
  1721
    /// \brief Returns an iterator to the first value.
deba@1931
  1722
    ///
deba@1931
  1723
    /// Returns an stl compatible iterator to the 
deba@1931
  1724
    /// first value of the map. The values of the
deba@1931
  1725
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1726
    /// range.
deba@1931
  1727
    ValueIterator beginValue() const {
deba@1931
  1728
      return ValueIterator(invMap.begin());
deba@1931
  1729
    }
deba@1931
  1730
deba@1931
  1731
    /// \brief Returns an iterator after the last value.
deba@1931
  1732
    ///
deba@1931
  1733
    /// Returns an stl compatible iterator after the 
deba@1931
  1734
    /// last value of the map. The values of the
deba@1931
  1735
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1736
    /// range.
deba@1931
  1737
    ValueIterator endValue() const {
deba@1931
  1738
      return ValueIterator(invMap.end());
deba@1931
  1739
    }
alpar@1402
  1740
    
alpar@1402
  1741
    /// \brief The setter function of the map.
alpar@1402
  1742
    ///
deba@1413
  1743
    /// Sets the mapped value.
alpar@1402
  1744
    void set(const Key& key, const Value& val) {
alpar@1402
  1745
      Value oldval = Map::operator[](key);
deba@1413
  1746
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
  1747
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1748
	invMap.erase(it);
alpar@1402
  1749
      }      
alpar@1402
  1750
      invMap.insert(make_pair(val, key));
alpar@1402
  1751
      Map::set(key, val);
alpar@1402
  1752
    }
alpar@1402
  1753
alpar@1402
  1754
    /// \brief The getter function of the map.
alpar@1402
  1755
    ///
alpar@1402
  1756
    /// It gives back the value associated with the key.
deba@1931
  1757
    typename MapTraits<Map>::ConstReturnValue 
deba@1931
  1758
    operator[](const Key& key) const {
alpar@1402
  1759
      return Map::operator[](key);
alpar@1402
  1760
    }
alpar@1402
  1761
deba@2331
  1762
    /// \brief Gives back the item by its value.
deba@2331
  1763
    ///
deba@2331
  1764
    /// Gives back the item by its value.
deba@2331
  1765
    Key operator()(const Value& key) const {
deba@2331
  1766
      typename Container::const_iterator it = invMap.find(key);
deba@2331
  1767
      return it != invMap.end() ? it->second : INVALID;
deba@2331
  1768
    }
deba@2331
  1769
deba@1515
  1770
  protected:
deba@1515
  1771
alpar@1402
  1772
    /// \brief Erase the key from the map.
alpar@1402
  1773
    ///
alpar@1402
  1774
    /// Erase the key to the map. It is called by the
alpar@1402
  1775
    /// \c AlterationNotifier.
alpar@1402
  1776
    virtual void erase(const Key& key) {
alpar@1402
  1777
      Value val = Map::operator[](key);
deba@1413
  1778
      typename Container::iterator it = invMap.find(val);
alpar@1402
  1779
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1780
	invMap.erase(it);
alpar@1402
  1781
      }
alpar@1402
  1782
      Map::erase(key);
alpar@1402
  1783
    }
alpar@1402
  1784
deba@1829
  1785
    /// \brief Erase more keys from the map.
deba@1829
  1786
    ///
deba@1829
  1787
    /// Erase more keys from the map. It is called by the
deba@1829
  1788
    /// \c AlterationNotifier.
deba@1829
  1789
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
  1790
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  1791
	Value val = Map::operator[](keys[i]);
deba@1829
  1792
	typename Container::iterator it = invMap.find(val);
deba@1829
  1793
	if (it != invMap.end() && it->second == keys[i]) {
deba@1829
  1794
	  invMap.erase(it);
deba@1829
  1795
	}
deba@1829
  1796
      }
deba@1829
  1797
      Map::erase(keys);
deba@1829
  1798
    }
deba@1829
  1799
alpar@1402
  1800
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
  1801
    ///
alpar@1402
  1802
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
  1803
    /// \c AlterationNotifier.
alpar@1402
  1804
    virtual void clear() {
alpar@1402
  1805
      invMap.clear();
alpar@1402
  1806
      Map::clear();
alpar@1402
  1807
    }
alpar@1402
  1808
deba@1413
  1809
  public:
deba@1413
  1810
deba@1413
  1811
    /// \brief The inverse map type.
deba@1413
  1812
    ///
deba@1413
  1813
    /// The inverse of this map. The subscript operator of the map
deba@1413
  1814
    /// gives back always the item what was last assigned to the value. 
deba@1413
  1815
    class InverseMap {
deba@1413
  1816
    public:
deba@1413
  1817
      /// \brief Constructor of the InverseMap.
deba@1413
  1818
      ///
deba@1413
  1819
      /// Constructor of the InverseMap.
deba@2286
  1820
      explicit InverseMap(const InvertableMap& _inverted) 
deba@2286
  1821
        : inverted(_inverted) {}
deba@1413
  1822
deba@1413
  1823
      /// The value type of the InverseMap.
deba@1413
  1824
      typedef typename InvertableMap::Key Value;
deba@1413
  1825
      /// The key type of the InverseMap.
deba@1413
  1826
      typedef typename InvertableMap::Value Key; 
deba@1413
  1827
deba@1413
  1828
      /// \brief Subscript operator. 
deba@1413
  1829
      ///
deba@1413
  1830
      /// Subscript operator. It gives back always the item 
deba@1413
  1831
      /// what was last assigned to the value.
deba@1413
  1832
      Value operator[](const Key& key) const {
deba@2331
  1833
	return inverted(key);
deba@1413
  1834
      }
deba@1413
  1835
      
deba@1413
  1836
    private:
deba@1413
  1837
      const InvertableMap& inverted;
deba@1413
  1838
    };
deba@1413
  1839
alpar@2094
  1840
    /// \brief It gives back the just readable inverse map.
alpar@1402
  1841
    ///
alpar@2094
  1842
    /// It gives back the just readable inverse map.
deba@1413
  1843
    InverseMap inverse() const {
deba@1413
  1844
      return InverseMap(*this);
alpar@1402
  1845
    } 
alpar@1402
  1846
alpar@1402
  1847
deba@1413
  1848
    
alpar@1402
  1849
  };
alpar@1402
  1850
alpar@1402
  1851
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
  1852
  /// item in the graph.
alpar@1402
  1853
  ///
athos@1540
  1854
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
  1855
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
  1856
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
  1857
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
  1858
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
  1859
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
  1860
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
  1861
  /// with its member class \c InverseMap.
alpar@1402
  1862
  ///
alpar@1402
  1863
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
  1864
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
klao@1909
  1865
  /// UEdge.
deba@1830
  1866
  template <typename _Graph, typename _Item>
deba@2287
  1867
  class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
alpar@1402
  1868
alpar@1402
  1869
    typedef _Item Item;
deba@2287
  1870
    typedef DefaultMap<_Graph, _Item, int> Map;
alpar@1402
  1871
alpar@1402
  1872
  public:
alpar@1402
  1873
    /// The graph class of DescriptorMap.
alpar@1402
  1874
    typedef _Graph Graph;
alpar@1402
  1875
klao@1909
  1876
    /// The key type of DescriptorMap (Node, Edge, UEdge).
deba@2287
  1877
    typedef typename Map::Key Key;
alpar@1402
  1878
    /// The value type of DescriptorMap.
deba@2287
  1879
    typedef typename Map::Value Value;
alpar@1402
  1880
alpar@1402
  1881
    /// \brief Constructor.
alpar@1402
  1882
    ///
deba@1413
  1883
    /// Constructor for descriptor map.
deba@2286
  1884
    explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@2201
  1885
      Item it;
deba@2386
  1886
      const typename Map::Notifier* nf = Map::notifier(); 
deba@2386
  1887
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2201
  1888
	Map::set(it, invMap.size());
deba@2201
  1889
	invMap.push_back(it);	
deba@2201
  1890
      }      
alpar@1402
  1891
    }
alpar@1402
  1892
deba@1515
  1893
  protected:
deba@1515
  1894
alpar@1402
  1895
    /// \brief Add a new key to the map.
alpar@1402
  1896
    ///
alpar@1402
  1897
    /// Add a new key to the map. It is called by the
alpar@1402
  1898
    /// \c AlterationNotifier.
alpar@1402
  1899
    virtual void add(const Item& item) {
alpar@1402
  1900
      Map::add(item);
alpar@1402
  1901
      Map::set(item, invMap.size());
alpar@1402
  1902
      invMap.push_back(item);
alpar@1402
  1903
    }
alpar@1402
  1904
deba@1829
  1905
    /// \brief Add more new keys to the map.
deba@1829
  1906
    ///
deba@1829
  1907
    /// Add more new keys to the map. It is called by the
deba@1829
  1908
    /// \c AlterationNotifier.
deba@1829
  1909
    virtual void add(const std::vector<Item>& items) {
deba@1829
  1910
      Map::add(items);
deba@2386
  1911
      for (int i = 0; i < int(items.size()); ++i) {
deba@1829
  1912
	Map::set(items[i], invMap.size());
deba@1829
  1913
	invMap.push_back(items[i]);
deba@1829
  1914
      }
deba@1829
  1915
    }
deba@1829
  1916
alpar@1402
  1917
    /// \brief Erase the key from the map.
alpar@1402
  1918
    ///
deba@1829
  1919
    /// Erase the key from the map. It is called by the
alpar@1402
  1920
    /// \c AlterationNotifier.
alpar@1402
  1921
    virtual void erase(const Item& item) {
alpar@1402
  1922
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1923
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1924
      invMap.pop_back();
alpar@1402
  1925
      Map::erase(item);
alpar@1402
  1926
    }
alpar@1402
  1927
deba@1829
  1928
    /// \brief Erase more keys from the map.
deba@1829
  1929
    ///
deba@1829
  1930
    /// Erase more keys from the map. It is called by the
deba@1829
  1931
    /// \c AlterationNotifier.
deba@1829
  1932
    virtual void erase(const std::vector<Item>& items) {
deba@2386
  1933
      for (int i = 0; i < int(items.size()); ++i) {
deba@1829
  1934
	Map::set(invMap.back(), Map::operator[](items[i]));
deba@1829
  1935
	invMap[Map::operator[](items[i])] = invMap.back();
deba@1829
  1936
	invMap.pop_back();
deba@1829
  1937
      }
deba@1829
  1938
      Map::erase(items);
deba@1829
  1939
    }
deba@1829
  1940
alpar@1402
  1941
    /// \brief Build the unique map.
alpar@1402
  1942
    ///
alpar@1402
  1943
    /// Build the unique map. It is called by the
alpar@1402
  1944
    /// \c AlterationNotifier.
alpar@1402
  1945
    virtual void build() {
alpar@1402
  1946
      Map::build();
alpar@1402
  1947
      Item it;
deba@2386
  1948
      const typename Map::Notifier* nf = Map::notifier(); 
deba@2386
  1949
      for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@1402
  1950
	Map::set(it, invMap.size());
alpar@1402
  1951
	invMap.push_back(it);	
alpar@1402
  1952
      }      
alpar@1402
  1953
    }
alpar@1402
  1954
    
alpar@1402
  1955
    /// \brief Clear the keys from the map.
alpar@1402
  1956
    ///
alpar@1402
  1957
    /// Clear the keys from the map. It is called by the
alpar@1402
  1958
    /// \c AlterationNotifier.
alpar@1402
  1959
    virtual void clear() {
alpar@1402
  1960
      invMap.clear();
alpar@1402
  1961
      Map::clear();
alpar@1402
  1962
    }
alpar@1402
  1963
deba@1538
  1964
  public:
deba@1538
  1965
deba@1931
  1966
    /// \brief Returns the maximal value plus one.
deba@1931
  1967
    ///
deba@1931
  1968
    /// Returns the maximal value plus one in the map.
deba@1931
  1969
    unsigned int size() const {
deba@1931
  1970
      return invMap.size();
deba@1931
  1971
    }
deba@1931
  1972
deba@1552
  1973
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1974
    ///
deba@1552
  1975
    /// Swaps the position of the two items in the map.
deba@1552
  1976
    void swap(const Item& p, const Item& q) {
deba@1552
  1977
      int pi = Map::operator[](p);
deba@1552
  1978
      int qi = Map::operator[](q);
deba@1552
  1979
      Map::set(p, qi);
deba@1552
  1980
      invMap[qi] = p;
deba@1552
  1981
      Map::set(q, pi);
deba@1552
  1982
      invMap[pi] = q;
deba@1552
  1983
    }
deba@1552
  1984
alpar@1402
  1985
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1986
    ///
alpar@1402
  1987
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1988
    int operator[](const Item& item) const {
alpar@1402
  1989
      return Map::operator[](item);
alpar@1402
  1990
    }
deba@2331
  1991
deba@2331
  1992
    /// \brief Gives back the item by its descriptor.
deba@2331
  1993
    ///
deba@2331
  1994
    /// Gives back th item by its descriptor.
deba@2331
  1995
    Item operator()(int id) const {
deba@2331
  1996
      return invMap[id];
deba@2331
  1997
    }
alpar@1402
  1998
    
deba@1413
  1999
  private:
deba@1413
  2000
deba@1413
  2001
    typedef std::vector<Item> Container;
deba@1413
  2002
    Container invMap;
deba@1413
  2003
deba@1413
  2004
  public:
athos@1540
  2005
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  2006
    ///
athos@1540
  2007
    /// The inverse map type of DescriptorMap.
deba@1413
  2008
    class InverseMap {
deba@1413
  2009
    public:
deba@1413
  2010
      /// \brief Constructor of the InverseMap.
deba@1413
  2011
      ///
deba@1413
  2012
      /// Constructor of the InverseMap.
deba@2286
  2013
      explicit InverseMap(const DescriptorMap& _inverted) 
deba@1413
  2014
	: inverted(_inverted) {}
deba@1413
  2015
deba@1413
  2016
deba@1413
  2017
      /// The value type of the InverseMap.
deba@1413
  2018
      typedef typename DescriptorMap::Key Value;
deba@1413
  2019
      /// The key type of the InverseMap.
deba@1413
  2020
      typedef typename DescriptorMap::Value Key; 
deba@1413
  2021
deba@1413
  2022
      /// \brief Subscript operator. 
deba@1413
  2023
      ///
deba@1413
  2024
      /// Subscript operator. It gives back the item 
deba@1413
  2025
      /// that the descriptor belongs to currently.
deba@1413
  2026
      Value operator[](const Key& key) const {
deba@2331
  2027
	return inverted(key);
deba@1413
  2028
      }
deba@1470
  2029
deba@1470
  2030
      /// \brief Size of the map.
deba@1470
  2031
      ///
deba@1470
  2032
      /// Returns the size of the map.
deba@1931
  2033
      unsigned int size() const {
deba@2331
  2034
	return inverted.size();
deba@1470
  2035
      }
deba@1413
  2036
      
deba@1413
  2037
    private:
deba@1413
  2038
      const DescriptorMap& inverted;
deba@1413
  2039
    };
deba@1413
  2040
alpar@1402
  2041
    /// \brief Gives back the inverse of the map.
alpar@1402
  2042
    ///
alpar@1402
  2043
    /// Gives back the inverse of the map.
alpar@1402
  2044
    const InverseMap inverse() const {
deba@1413
  2045
      return InverseMap(*this);
alpar@1402
  2046
    }
alpar@1402
  2047
  };
alpar@1402
  2048
alpar@1402
  2049
  /// \brief Returns the source of the given edge.
alpar@1402
  2050
  ///
alpar@1402
  2051
  /// The SourceMap gives back the source Node of the given edge. 
kpeter@2474
  2052
  /// \see TargetMap
alpar@1402
  2053
  /// \author Balazs Dezso
alpar@1402
  2054
  template <typename Graph>
alpar@1402
  2055
  class SourceMap {
alpar@1402
  2056
  public:
deba@1419
  2057
alpar@1402
  2058
    typedef typename Graph::Node Value;
alpar@1402
  2059
    typedef typename Graph::Edge Key;
alpar@1402
  2060
alpar@1402
  2061
    /// \brief Constructor
alpar@1402
  2062
    ///
alpar@1402
  2063
    /// Constructor
alpar@1402
  2064
    /// \param _graph The graph that the map belongs to.
deba@2286
  2065
    explicit SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2066
alpar@1402
  2067
    /// \brief The subscript operator.
alpar@1402
  2068
    ///
alpar@1402
  2069
    /// The subscript operator.
alpar@1402
  2070
    /// \param edge The edge 
alpar@1402
  2071
    /// \return The source of the edge 
deba@1679
  2072
    Value operator[](const Key& edge) const {
alpar@1402
  2073
      return graph.source(edge);
alpar@1402
  2074
    }
alpar@1402
  2075
alpar@1402
  2076
  private:
alpar@1402
  2077
    const Graph& graph;
alpar@1402
  2078
  };
alpar@1402
  2079
kpeter@2474
  2080
  /// \brief Returns a \ref SourceMap class.
alpar@1402
  2081
  ///
alpar@1402
  2082
  /// This function just returns an \ref SourceMap class.
alpar@1402
  2083
  /// \relates SourceMap
alpar@1402
  2084
  template <typename Graph>
alpar@1402
  2085
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  2086
    return SourceMap<Graph>(graph);
alpar@1402
  2087
  } 
alpar@1402
  2088
alpar@1402
  2089
  /// \brief Returns the target of the given edge.
alpar@1402
  2090
  ///
alpar@1402
  2091
  /// The TargetMap gives back the target Node of the given edge. 
kpeter@2474
  2092
  /// \see SourceMap
alpar@1402
  2093
  /// \author Balazs Dezso
alpar@1402
  2094
  template <typename Graph>
alpar@1402
  2095
  class TargetMap {
alpar@1402
  2096
  public:
deba@1419
  2097
alpar@1402
  2098
    typedef typename Graph::Node Value;
alpar@1402
  2099
    typedef typename Graph::Edge Key;
alpar@1402
  2100
alpar@1402
  2101
    /// \brief Constructor
alpar@1402
  2102
    ///
alpar@1402
  2103
    /// Constructor
alpar@1402
  2104
    /// \param _graph The graph that the map belongs to.
deba@2286
  2105
    explicit TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  2106
alpar@1402
  2107
    /// \brief The subscript operator.
alpar@1402
  2108
    ///
alpar@1402
  2109
    /// The subscript operator.
alpar@1536
  2110
    /// \param e The edge 
alpar@1402
  2111
    /// \return The target of the edge 
deba@1679
  2112
    Value operator[](const Key& e) const {
alpar@1536
  2113
      return graph.target(e);
alpar@1402
  2114
    }
alpar@1402
  2115
alpar@1402
  2116
  private:
alpar@1402
  2117
    const Graph& graph;
alpar@1402
  2118
  };
alpar@1402
  2119
kpeter@2474
  2120
  /// \brief Returns a \ref TargetMap class.
deba@1515
  2121
  ///
athos@1540
  2122
  /// This function just returns a \ref TargetMap class.
alpar@1402
  2123
  /// \relates TargetMap
alpar@1402
  2124
  template <typename Graph>
alpar@1402
  2125
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  2126
    return TargetMap<Graph>(graph);
alpar@1402
  2127
  }
alpar@1402
  2128
athos@1540
  2129
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  2130
  ///
athos@1540
  2131
  /// Returns the "forward" directed edge view of an undirected edge.
kpeter@2474
  2132
  /// \see BackwardMap
deba@1419
  2133
  /// \author Balazs Dezso
deba@1419
  2134
  template <typename Graph>
deba@1419
  2135
  class ForwardMap {
deba@1419
  2136
  public:
deba@1419
  2137
deba@1419
  2138
    typedef typename Graph::Edge Value;
klao@1909
  2139
    typedef typename Graph::UEdge Key;
deba@1419
  2140
deba@1419
  2141
    /// \brief Constructor
deba@1419
  2142
    ///
deba@1419
  2143
    /// Constructor
deba@1419
  2144
    /// \param _graph The graph that the map belongs to.
deba@2286
  2145
    explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2146
deba@1419
  2147
    /// \brief The subscript operator.
deba@1419
  2148
    ///
deba@1419
  2149
    /// The subscript operator.
deba@1419
  2150
    /// \param key An undirected edge 
deba@1419
  2151
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  2152
    Value operator[](const Key& key) const {
deba@1627
  2153
      return graph.direct(key, true);
deba@1419
  2154
    }
deba@1419
  2155
deba@1419
  2156
  private:
deba@1419
  2157
    const Graph& graph;
deba@1419
  2158
  };
deba@1419
  2159
kpeter@2474
  2160
  /// \brief Returns a \ref ForwardMap class.
deba@1515
  2161
  ///
deba@1419
  2162
  /// This function just returns an \ref ForwardMap class.
deba@1419
  2163
  /// \relates ForwardMap
deba@1419
  2164
  template <typename Graph>
deba@1419
  2165
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  2166
    return ForwardMap<Graph>(graph);
deba@1419
  2167
  }
deba@1419
  2168
athos@1540
  2169
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  2170
  ///
athos@1540
  2171
  /// Returns the "backward" directed edge view of an undirected edge.
kpeter@2474
  2172
  /// \see ForwardMap
deba@1419
  2173
  /// \author Balazs Dezso
deba@1419
  2174
  template <typename Graph>
deba@1419
  2175
  class BackwardMap {
deba@1419
  2176
  public:
deba@1419
  2177
deba@1419
  2178
    typedef typename Graph::Edge Value;
klao@1909
  2179
    typedef typename Graph::UEdge Key;
deba@1419
  2180
deba@1419
  2181
    /// \brief Constructor
deba@1419
  2182
    ///
deba@1419
  2183
    /// Constructor
deba@1419
  2184
    /// \param _graph The graph that the map belongs to.
deba@2286
  2185
    explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  2186
deba@1419
  2187
    /// \brief The subscript operator.
deba@1419
  2188
    ///
deba@1419
  2189
    /// The subscript operator.
deba@1419
  2190
    /// \param key An undirected edge 
deba@1419
  2191
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  2192
    Value operator[](const Key& key) const {
deba@1627
  2193
      return graph.direct(key, false);
deba@1419
  2194
    }
deba@1419
  2195
deba@1419
  2196
  private:
deba@1419
  2197
    const Graph& graph;
deba@1419
  2198
  };
deba@1419
  2199
deba@1419
  2200
  /// \brief Returns a \ref BackwardMap class
deba@1419
  2201
athos@1540
  2202
  /// This function just returns a \ref BackwardMap class.
deba@1419
  2203
  /// \relates BackwardMap
deba@1419
  2204
  template <typename Graph>
deba@1419
  2205
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  2206
    return BackwardMap<Graph>(graph);
deba@1419
  2207
  }
deba@1419
  2208
deba@1695
  2209
  /// \brief Potential difference map
deba@1695
  2210
  ///
deba@1695
  2211
  /// If there is an potential map on the nodes then we
deba@1695
  2212
  /// can get an edge map as we get the substraction of the
deba@1695
  2213
  /// values of the target and source.
deba@1695
  2214
  template <typename Graph, typename NodeMap>
deba@1695
  2215
  class PotentialDifferenceMap {
deba@1515
  2216
  public:
deba@1695
  2217
    typedef typename Graph::Edge Key;
deba@1695
  2218
    typedef typename NodeMap::Value Value;
deba@1695
  2219
deba@1695
  2220
    /// \brief Constructor
deba@1695
  2221
    ///
deba@1695
  2222
    /// Contructor of the map
deba@2286
  2223
    explicit PotentialDifferenceMap(const Graph& _graph, 
deba@2286
  2224
                                    const NodeMap& _potential) 
deba@1695
  2225
      : graph(_graph), potential(_potential) {}
deba@1695
  2226
deba@1695
  2227
    /// \brief Const subscription operator
deba@1695
  2228
    ///
deba@1695
  2229
    /// Const subscription operator
deba@1695
  2230
    Value operator[](const Key& edge) const {
deba@1695
  2231
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  2232
    }
deba@1695
  2233
deba@1695
  2234
  private:
deba@1695
  2235
    const Graph& graph;
deba@1695
  2236
    const NodeMap& potential;
deba@1695
  2237
  };
deba@1695
  2238
kpeter@2474
  2239
  /// \brief Returns a PotentialDifferenceMap.
deba@1695
  2240
  ///
kpeter@2474
  2241
  /// This function just returns a PotentialDifferenceMap.
deba@1695
  2242
  /// \relates PotentialDifferenceMap
deba@1695
  2243
  template <typename Graph, typename NodeMap>
deba@1695
  2244
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  2245
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  2246
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  2247
  }
deba@1695
  2248
deba@1515
  2249
  /// \brief Map of the node in-degrees.
alpar@1453
  2250
  ///
athos@1540
  2251
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  2252
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2253
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2254
  /// whenever the graph changes.
deba@1515
  2255
  ///
deba@1729
  2256
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2257
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1829
  2258
  /// is not guarantied if these additional features are used. For example
deba@1829
  2259
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2260
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2261
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2262
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2263
  ///
deba@1515
  2264
  /// \sa OutDegMap
deba@1515
  2265
alpar@1453
  2266
  template <typename _Graph>
deba@1515
  2267
  class InDegMap  
deba@1999
  2268
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2269
      ::ItemNotifier::ObserverBase {
deba@1515
  2270
alpar@1453
  2271
  public:
deba@1515
  2272
    
deba@1515
  2273
    typedef _Graph Graph;
alpar@1453
  2274
    typedef int Value;
deba@1515
  2275
    typedef typename Graph::Node Key;
deba@1515
  2276
deba@1999
  2277
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2278
    ::ItemNotifier::ObserverBase Parent;
deba@1999
  2279
deba@1515
  2280
  private:
deba@1515
  2281
deba@1990
  2282
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2283
    public:
deba@1515
  2284
deba@1990
  2285
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2286
      typedef typename Parent::Graph Graph;
deba@1515
  2287
deba@1515
  2288
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2289
      
deba@1829
  2290
      virtual void add(const Key& key) {
deba@1515
  2291
	Parent::add(key);
deba@1515
  2292
	Parent::set(key, 0);
deba@1515
  2293
      }
deba@1931
  2294
deba@1829
  2295
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2296
	Parent::add(keys);
deba@2386
  2297
	for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  2298
	  Parent::set(keys[i], 0);
deba@1829
  2299
	}
deba@1829
  2300
      }
deba@2539
  2301
deba@2539
  2302
      virtual void build() {
deba@2539
  2303
	Parent::build();
deba@2539
  2304
	Key it;
deba@2539
  2305
	typename Parent::Notifier* nf = Parent::notifier();
deba@2539
  2306
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2539
  2307
	  Parent::set(it, 0);
deba@2539
  2308
	}
deba@2539
  2309
      }
deba@1515
  2310
    };
deba@1515
  2311
deba@1515
  2312
  public:
alpar@1453
  2313
alpar@1453
  2314
    /// \brief Constructor.
alpar@1453
  2315
    ///
alpar@1453
  2316
    /// Constructor for creating in-degree map.
deba@2286
  2317
    explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@2384
  2318
      Parent::attach(graph.notifier(typename _Graph::Edge()));
deba@1515
  2319
      
deba@1515
  2320
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2321
	deg[it] = countInEdges(graph, it);
deba@1515
  2322
      }
alpar@1453
  2323
    }
alpar@1453
  2324
    
alpar@1459
  2325
    /// Gives back the in-degree of a Node.
deba@1515
  2326
    int operator[](const Key& key) const {
deba@1515
  2327
      return deg[key];
alpar@1459
  2328
    }
alpar@1453
  2329
alpar@1453
  2330
  protected:
deba@1515
  2331
    
deba@1515
  2332
    typedef typename Graph::Edge Edge;
deba@1515
  2333
deba@1515
  2334
    virtual void add(const Edge& edge) {
deba@1515
  2335
      ++deg[graph.target(edge)];
alpar@1453
  2336
    }
alpar@1453
  2337
deba@1931
  2338
    virtual void add(const std::vector<Edge>& edges) {
deba@2386
  2339
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2340
        ++deg[graph.target(edges[i])];
deba@1931
  2341
      }
deba@1931
  2342
    }
deba@1931
  2343
deba@1515
  2344
    virtual void erase(const Edge& edge) {
deba@1515
  2345
      --deg[graph.target(edge)];
deba@1515
  2346
    }
deba@1515
  2347
deba@1931
  2348
    virtual void erase(const std::vector<Edge>& edges) {
deba@2386
  2349
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2350
        --deg[graph.target(edges[i])];
deba@1931
  2351
      }
deba@1931
  2352
    }
deba@1931
  2353
deba@1515
  2354
    virtual void build() {
deba@1515
  2355
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2356
	deg[it] = countInEdges(graph, it);
deba@1515
  2357
      }      
deba@1515
  2358
    }
deba@1515
  2359
deba@1515
  2360
    virtual void clear() {
deba@1515
  2361
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2362
	deg[it] = 0;
deba@1515
  2363
      }
deba@1515
  2364
    }
deba@1515
  2365
  private:
alpar@1506
  2366
    
deba@1515
  2367
    const _Graph& graph;
deba@1515
  2368
    AutoNodeMap deg;
alpar@1459
  2369
  };
alpar@1459
  2370
deba@1515
  2371
  /// \brief Map of the node out-degrees.
deba@1515
  2372
  ///
athos@1540
  2373
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  2374
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  2375
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  2376
  /// whenever the graph changes.
deba@1515
  2377
  ///
deba@1729
  2378
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  2379
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1829
  2380
  /// is not guarantied if these additional features are used. For example
deba@1829
  2381
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  2382
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  2383
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  2384
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  2385
  ///
alpar@1555
  2386
  /// \sa InDegMap
alpar@1459
  2387
alpar@1459
  2388
  template <typename _Graph>
deba@1515
  2389
  class OutDegMap  
deba@1999
  2390
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2391
      ::ItemNotifier::ObserverBase {
deba@1515
  2392
alpar@1459
  2393
  public:
deba@1999
  2394
deba@1999
  2395
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  2396
    ::ItemNotifier::ObserverBase Parent;
deba@1515
  2397
    
deba@1515
  2398
    typedef _Graph Graph;
alpar@1459
  2399
    typedef int Value;
deba@1515
  2400
    typedef typename Graph::Node Key;
deba@1515
  2401
deba@1515
  2402
  private:
deba@1515
  2403
deba@1990
  2404
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  2405
    public:
deba@1515
  2406
deba@1990
  2407
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  2408
      typedef typename Parent::Graph Graph;
deba@1515
  2409
deba@1515
  2410
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  2411
      
deba@1829
  2412
      virtual void add(const Key& key) {
deba@1515
  2413
	Parent::add(key);
deba@1515
  2414
	Parent::set(key, 0);
deba@1515
  2415
      }
deba@1829
  2416
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  2417
	Parent::add(keys);
deba@2386
  2418
	for (int i = 0; i < int(keys.size()); ++i) {
deba@1829
  2419
	  Parent::set(keys[i], 0);
deba@1829
  2420
	}
deba@1829
  2421
      }
deba@2539
  2422
      virtual void build() {
deba@2539
  2423
	Parent::build();
deba@2539
  2424
	Key it;
deba@2539
  2425
	typename Parent::Notifier* nf = Parent::notifier();
deba@2539
  2426
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2539
  2427
	  Parent::set(it, 0);
deba@2539
  2428
	}
deba@2539
  2429
      }
deba@1515
  2430
    };
deba@1515
  2431
deba@1515
  2432
  public:
alpar@1459
  2433
alpar@1459
  2434
    /// \brief Constructor.
alpar@1459
  2435
    ///
alpar@1459
  2436
    /// Constructor for creating out-degree map.
deba@2286
  2437
    explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@2384
  2438
      Parent::attach(graph.notifier(typename _Graph::Edge()));
deba@1515
  2439
      
deba@1515
  2440
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2441
	deg[it] = countOutEdges(graph, it);
deba@1515
  2442
      }
alpar@1459
  2443
    }
alpar@1459
  2444
deba@1990
  2445
    /// Gives back the out-degree of a Node.
deba@1515
  2446
    int operator[](const Key& key) const {
deba@1515
  2447
      return deg[key];
alpar@1459
  2448
    }
alpar@1459
  2449
alpar@1459
  2450
  protected:
deba@1515
  2451
    
deba@1515
  2452
    typedef typename Graph::Edge Edge;
deba@1515
  2453
deba@1515
  2454
    virtual void add(const Edge& edge) {
deba@1515
  2455
      ++deg[graph.source(edge)];
alpar@1459
  2456
    }
alpar@1459
  2457
deba@1931
  2458
    virtual void add(const std::vector<Edge>& edges) {
deba@2386
  2459
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2460
        ++deg[graph.source(edges[i])];
deba@1931
  2461
      }
deba@1931
  2462
    }
deba@1931
  2463
deba@1515
  2464
    virtual void erase(const Edge& edge) {
deba@1515
  2465
      --deg[graph.source(edge)];
deba@1515
  2466
    }
deba@1515
  2467
deba@1931
  2468
    virtual void erase(const std::vector<Edge>& edges) {
deba@2386
  2469
      for (int i = 0; i < int(edges.size()); ++i) {
deba@1931
  2470
        --deg[graph.source(edges[i])];
deba@1931
  2471
      }
deba@1931
  2472
    }
deba@1931
  2473
deba@1515
  2474
    virtual void build() {
deba@1515
  2475
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2476
	deg[it] = countOutEdges(graph, it);
deba@1515
  2477
      }      
deba@1515
  2478
    }
deba@1515
  2479
deba@1515
  2480
    virtual void clear() {
deba@1515
  2481
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  2482
	deg[it] = 0;
deba@1515
  2483
      }
deba@1515
  2484
    }
deba@1515
  2485
  private:
alpar@1506
  2486
    
deba@1515
  2487
    const _Graph& graph;
deba@1515
  2488
    AutoNodeMap deg;
alpar@1453
  2489
  };
alpar@1453
  2490
deba@1695
  2491
deba@2539
  2492
  ///Dynamic edge look up between given endpoints.
deba@2539
  2493
  
deba@2539
  2494
  ///\ingroup gutils
deba@2539
  2495
  ///Using this class, you can find an edge in a graph from a given
deba@2539
  2496
  ///source to a given target in amortized time <em>O(log d)</em>,
deba@2539
  2497
  ///where <em>d</em> is the out-degree of the source node.
deba@2539
  2498
  ///
deba@2539
  2499
  ///It is possible to find \e all parallel edges between two nodes with
deba@2539
  2500
  ///the \c findFirst() and \c findNext() members.
deba@2539
  2501
  ///
deba@2539
  2502
  ///See the \ref EdgeLookUp and \ref AllEdgeLookUp classes if your
deba@2539
  2503
  ///graph do not changed so frequently.
deba@2539
  2504
  ///
deba@2539
  2505
  ///This class uses a self-adjusting binary search tree, Sleator's
deba@2539
  2506
  ///and Tarjan's Splay tree for guarantee the logarithmic amortized
deba@2539
  2507
  ///time bound for edge lookups. This class also guarantees the
deba@2539
  2508
  ///optimal time bound in a constant factor for any distribution of
deba@2539
  2509
  ///queries.
deba@2539
  2510
  ///
deba@2539
  2511
  ///\param G The type of the underlying graph.  
deba@2539
  2512
  ///
deba@2539
  2513
  ///\sa EdgeLookUp  
deba@2539
  2514
  ///\sa AllEdgeLookUp  
deba@2539
  2515
  template<class G>
deba@2539
  2516
  class DynEdgeLookUp 
deba@2539
  2517
    : protected ItemSetTraits<G, typename G::Edge>::ItemNotifier::ObserverBase
deba@2539
  2518
  {
deba@2539
  2519
  public:
deba@2539
  2520
    typedef typename ItemSetTraits<G, typename G::Edge>
deba@2539
  2521
    ::ItemNotifier::ObserverBase Parent;
deba@2539
  2522
deba@2539
  2523
    GRAPH_TYPEDEFS(typename G);
deba@2539
  2524
    typedef G Graph;
deba@2539
  2525
deba@2539
  2526
  protected:
deba@2539
  2527
deba@2540
  2528
    class AutoNodeMap : public DefaultMap<G, Node, Edge> {
deba@2539
  2529
    public:
deba@2539
  2530
deba@2540
  2531
      typedef DefaultMap<G, Node, Edge> Parent;
deba@2540
  2532
deba@2540
  2533
      AutoNodeMap(const G& graph) : Parent(graph, INVALID) {}
deba@2539
  2534
      
deba@2539
  2535
      virtual void add(const Node& node) {
deba@2539
  2536
	Parent::add(node);
deba@2539
  2537
	Parent::set(node, INVALID);
deba@2539
  2538
      }
deba@2539
  2539
deba@2539
  2540
      virtual void add(const std::vector<Node>& nodes) {
deba@2539
  2541
	Parent::add(nodes);
deba@2539
  2542
	for (int i = 0; i < int(nodes.size()); ++i) {
deba@2539
  2543
	  Parent::set(nodes[i], INVALID);
deba@2539
  2544
	}
deba@2539
  2545
      }
deba@2539
  2546
deba@2539
  2547
      virtual void build() {
deba@2539
  2548
	Parent::build();
deba@2539
  2549
	Node it;
deba@2539
  2550
	typename Parent::Notifier* nf = Parent::notifier();
deba@2539
  2551
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2539
  2552
	  Parent::set(it, INVALID);
deba@2539
  2553
	}
deba@2539
  2554
      }
deba@2539
  2555
    };
deba@2539
  2556
deba@2539
  2557
    const Graph &_g;
deba@2539
  2558
    AutoNodeMap _head;
deba@2539
  2559
    typename Graph::template EdgeMap<Edge> _parent;
deba@2539
  2560
    typename Graph::template EdgeMap<Edge> _left;
deba@2539
  2561
    typename Graph::template EdgeMap<Edge> _right;
deba@2539
  2562
    
deba@2539
  2563
    class EdgeLess {
deba@2539
  2564
      const Graph &g;
deba@2539
  2565
    public:
deba@2539
  2566
      EdgeLess(const Graph &_g) : g(_g) {}
deba@2539
  2567
      bool operator()(Edge a,Edge b) const 
deba@2539
  2568
      {
deba@2539
  2569
	return g.target(a)<g.target(b);
deba@2539
  2570
      }
deba@2539
  2571
    };
deba@2539
  2572
    
deba@2539
  2573
  public:
deba@2539
  2574
    
deba@2539
  2575
    ///Constructor
deba@2539
  2576
deba@2539
  2577
    ///Constructor.
deba@2539
  2578
    ///
deba@2539
  2579
    ///It builds up the search database.
deba@2539
  2580
    DynEdgeLookUp(const Graph &g) 
deba@2539
  2581
      : _g(g),_head(g),_parent(g),_left(g),_right(g) 
deba@2539
  2582
    { 
deba@2539
  2583
      Parent::attach(_g.notifier(typename Graph::Edge()));
deba@2539
  2584
      refresh(); 
deba@2539
  2585
    }
deba@2539
  2586
    
deba@2539
  2587
  protected:
deba@2539
  2588
deba@2539
  2589
    virtual void add(const Edge& edge) {
deba@2539
  2590
      insert(edge);
deba@2539
  2591
    }
deba@2539
  2592
deba@2539
  2593
    virtual void add(const std::vector<Edge>& edges) {
deba@2539
  2594
      for (int i = 0; i < int(edges.size()); ++i) {
deba@2539
  2595
	insert(edges[i]);
deba@2539
  2596
      }
deba@2539
  2597
    }
deba@2539
  2598
deba@2539
  2599
    virtual void erase(const Edge& edge) {
deba@2539
  2600
      remove(edge);
deba@2539
  2601
    }
deba@2539
  2602
deba@2539
  2603
    virtual void erase(const std::vector<Edge>& edges) {
deba@2539
  2604
      for (int i = 0; i < int(edges.size()); ++i) {
deba@2539
  2605
	remove(edges[i]);
deba@2539
  2606
      }     
deba@2539
  2607
    }
deba@2539
  2608
deba@2539
  2609
    virtual void build() {
deba@2539
  2610
      refresh();
deba@2539
  2611
    }
deba@2539
  2612
deba@2539
  2613
    virtual void clear() {
deba@2539
  2614
      for(NodeIt n(_g);n!=INVALID;++n) {
deba@2539
  2615
	_head.set(n, INVALID);
deba@2539
  2616
      }
deba@2539
  2617
    }
deba@2539
  2618
deba@2539
  2619
    void insert(Edge edge) {
deba@2539
  2620
      Node s = _g.source(edge);
deba@2539
  2621
      Node t = _g.target(edge);
deba@2539
  2622
      _left.set(edge, INVALID);
deba@2539
  2623
      _right.set(edge, INVALID);
deba@2539
  2624
      
deba@2539
  2625
      Edge e = _head[s];
deba@2539
  2626
      if (e == INVALID) {
deba@2539
  2627
	_head.set(s, edge);
deba@2539
  2628
	_parent.set(edge, INVALID);
deba@2539
  2629
	return;
deba@2539
  2630
      }
deba@2539
  2631
      while (true) {
deba@2539
  2632
	if (t < _g.target(e)) {
deba@2539
  2633
	  if (_left[e] == INVALID) {
deba@2539
  2634
	    _left.set(e, edge);
deba@2539
  2635
	    _parent.set(edge, e);
deba@2539
  2636
	    splay(edge);
deba@2539
  2637
	    return;
deba@2539
  2638
	  } else {
deba@2539
  2639
	    e = _left[e];
deba@2539
  2640
	  }
deba@2539
  2641
	} else {
deba@2539
  2642
	  if (_right[e] == INVALID) {
deba@2539
  2643
	    _right.set(e, edge);
deba@2539
  2644
	    _parent.set(edge, e);
deba@2539
  2645
	    splay(edge);
deba@2539
  2646
	    return;
deba@2539
  2647
	  } else {
deba@2539
  2648
	    e = _right[e];
deba@2539
  2649
	  }
deba@2539
  2650
	}
deba@2539
  2651
      }
deba@2539
  2652
    }
deba@2539
  2653
deba@2539
  2654
    void remove(Edge edge) {
deba@2539
  2655
      if (_left[edge] == INVALID) {
deba@2539
  2656
	if (_right[edge] != INVALID) {
deba@2539
  2657
	  _parent.set(_right[edge], _parent[edge]);
deba@2539
  2658
	}
deba@2539
  2659
	if (_parent[edge] != INVALID) {
deba@2539
  2660
	  if (_left[_parent[edge]] == edge) {
deba@2539
  2661
	    _left.set(_parent[edge], _right[edge]);
deba@2539
  2662
	  } else {
deba@2539
  2663
	    _right.set(_parent[edge], _right[edge]);
deba@2539
  2664
	  }
deba@2539
  2665
	} else {
deba@2539
  2666
	  _head.set(_g.source(edge), _right[edge]);
deba@2539
  2667
	}
deba@2539
  2668
      } else if (_right[edge] == INVALID) {
deba@2539
  2669
	_parent.set(_left[edge], _parent[edge]);
deba@2539
  2670
	if (_parent[edge] != INVALID) {
deba@2539
  2671
	  if (_left[_parent[edge]] == edge) {
deba@2539
  2672
	    _left.set(_parent[edge], _left[edge]);
deba@2539
  2673
	  } else {
deba@2539
  2674
	    _right.set(_parent[edge], _left[edge]);
deba@2539
  2675
	  }
deba@2539
  2676
	} else {
deba@2539
  2677
	  _head.set(_g.source(edge), _left[edge]);
deba@2539
  2678
	}
deba@2539
  2679
      } else {
deba@2539
  2680
	Edge e = _left[edge];
deba@2539
  2681
	if (_right[e] != INVALID) {
deba@2539
  2682
	  e = _right[e];	  
deba@2539
  2683
	  while (_right[e] != INVALID) {
deba@2539
  2684
	    e = _right[e];
deba@2539
  2685
	  }
deba@2539
  2686
	  Edge s = _parent[e];
deba@2539
  2687
	  _right.set(_parent[e], _left[e]);
deba@2539
  2688
	  if (_left[e] != INVALID) {
deba@2539
  2689
	    _parent.set(_left[e], _parent[e]);
deba@2539
  2690
	  }
deba@2539
  2691
	  
deba@2539
  2692
	  _left.set(e, _left[edge]);
deba@2539
  2693
	  _parent.set(_left[edge], e);
deba@2539
  2694
	  _right.set(e, _right[edge]);
deba@2539
  2695
	  _parent.set(_right[edge], e);
deba@2539
  2696
deba@2539
  2697
	  _parent.set(e, _parent[edge]);
deba@2539
  2698
	  if (_parent[edge] != INVALID) {
deba@2539
  2699
	    if (_left[_parent[edge]] == edge) {
deba@2539
  2700
	      _left.set(_parent[edge], e);
deba@2539
  2701
	    } else {
deba@2539
  2702
	      _right.set(_parent[edge], e);
deba@2539
  2703
	    }
deba@2539
  2704
	  }
deba@2539
  2705
	  splay(s);
deba@2539
  2706
	} else {
deba@2539
  2707
	  _right.set(e, _right[edge]);
deba@2539
  2708
	  _parent.set(_right[edge], e);
deba@2539
  2709
deba@2539
  2710
	  if (_parent[edge] != INVALID) {
deba@2539
  2711
	    if (_left[_parent[edge]] == edge) {
deba@2539
  2712
	      _left.set(_parent[edge], e);
deba@2539
  2713
	    } else {
deba@2539
  2714
	      _right.set(_parent[edge], e);
deba@2539
  2715
	    }
deba@2539
  2716
	  } else {
deba@2539
  2717
	    _head.set(_g.source(edge), e);
deba@2539
  2718
	  }
deba@2539
  2719
	}
deba@2539
  2720
      }
deba@2539
  2721
    }
deba@2539
  2722
deba@2539
  2723
    Edge refreshRec(std::vector<Edge> &v,int a,int b) 
deba@2539
  2724
    {
deba@2539
  2725
      int m=(a+b)/2;
deba@2539
  2726
      Edge me=v[m];
deba@2539
  2727
      if (a < m) {
deba@2539
  2728
	Edge left = refreshRec(v,a,m-1);
deba@2539
  2729
	_left.set(me, left);
deba@2539
  2730
	_parent.set(left, me);
deba@2539
  2731
      } else {
deba@2539
  2732
	_left.set(me, INVALID);
deba@2539
  2733
      }
deba@2539
  2734
      if (m < b) {
deba@2539
  2735
	Edge right = refreshRec(v,m+1,b);
deba@2539
  2736
	_right.set(me, right);
deba@2539
  2737
	_parent.set(right, me);
deba@2539
  2738
      } else {
deba@2539
  2739
	_right.set(me, INVALID);
deba@2539
  2740
      }
deba@2539
  2741
      return me;
deba@2539
  2742
    }
deba@2539
  2743
deba@2539
  2744
    void refresh() {
deba@2539
  2745
      for(NodeIt n(_g);n!=INVALID;++n) {
deba@2539
  2746
	std::vector<Edge> v;
deba@2539
  2747
	for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
deba@2539
  2748
	if(v.size()) {
deba@2539
  2749
	  std::sort(v.begin(),v.end(),EdgeLess(_g));
deba@2539
  2750
	  Edge head = refreshRec(v,0,v.size()-1);
deba@2539
  2751
	  _head.set(n, head);
deba@2539
  2752
	  _parent.set(head, INVALID);
deba@2539
  2753
	}
deba@2539
  2754
	else _head.set(n, INVALID);
deba@2539
  2755
      }
deba@2539
  2756
    }
deba@2539
  2757
deba@2539
  2758
    void zig(Edge v) {        
deba@2539
  2759
      Edge w = _parent[v];
deba@2539
  2760
      _parent.set(v, _parent[w]);
deba@2539
  2761
      _parent.set(w, v);
deba@2539
  2762
      _left.set(w, _right[v]);
deba@2539
  2763
      _right.set(v, w);
deba@2539
  2764
      if (_parent[v] != INVALID) {
deba@2539
  2765
	if (_right[_parent[v]] == w) {
deba@2539
  2766
	  _right.set(_parent[v], v);
deba@2539
  2767
	} else {
deba@2539
  2768
	  _left.set(_parent[v], v);
deba@2539
  2769
	}
deba@2539
  2770
      }
deba@2539
  2771
      if (_left[w] != INVALID){
deba@2539
  2772
	_parent.set(_left[w], w);
deba@2539
  2773
      }
deba@2539
  2774
    }
deba@2539
  2775
deba@2539
  2776
    void zag(Edge v) {        
deba@2539
  2777
      Edge w = _parent[v];
deba@2539
  2778
      _parent.set(v, _parent[w]);
deba@2539
  2779
      _parent.set(w, v);
deba@2539
  2780
      _right.set(w, _left[v]);
deba@2539
  2781
      _left.set(v, w);
deba@2539
  2782
      if (_parent[v] != INVALID){
deba@2539
  2783
	if (_left[_parent[v]] == w) {
deba@2539
  2784
	  _left.set(_parent[v], v);
deba@2539
  2785
	} else {
deba@2539
  2786
	  _right.set(_parent[v], v);
deba@2539
  2787
	}
deba@2539
  2788
      }
deba@2539
  2789
      if (_right[w] != INVALID){
deba@2539
  2790
	_parent.set(_right[w], w);
deba@2539
  2791
      }
deba@2539
  2792
    }
deba@2539
  2793
deba@2539
  2794
    void splay(Edge v) {
deba@2539
  2795
      while (_parent[v] != INVALID) {
deba@2539
  2796
	if (v == _left[_parent[v]]) {
deba@2539
  2797
	  if (_parent[_parent[v]] == INVALID) {
deba@2539
  2798
	    zig(v);
deba@2539
  2799
	  } else {
deba@2539
  2800
	    if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@2539
  2801
	      zig(_parent[v]);
deba@2539
  2802
	      zig(v);
deba@2539
  2803
	    } else {
deba@2539
  2804
	      zig(v);
deba@2539
  2805
	      zag(v);
deba@2539
  2806
	    }
deba@2539
  2807
	  }
deba@2539
  2808
	} else {
deba@2539
  2809
	  if (_parent[_parent[v]] == INVALID) {
deba@2539
  2810
	    zag(v);
deba@2539
  2811
	  } else {
deba@2539
  2812
	    if (_parent[v] == _left[_parent[_parent[v]]]) {
deba@2539
  2813
	      zag(v);
deba@2539
  2814
	      zig(v);
deba@2539
  2815
	    } else {
deba@2539
  2816
	      zag(_parent[v]);
deba@2539
  2817
	      zag(v);
deba@2539
  2818
	    }
deba@2539
  2819
	  }
deba@2539
  2820
	}
deba@2539
  2821
      }
deba@2539
  2822
      _head[_g.source(v)] = v;
deba@2539
  2823
    }
deba@2539
  2824
deba@2539
  2825
deba@2539
  2826
  public:
deba@2539
  2827
    
deba@2539
  2828
    ///Find an edge between two nodes.
deba@2539
  2829
    
deba@2539
  2830
    ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
deba@2539
  2831
    /// <em>d</em> is the number of outgoing edges of \c s.
deba@2539
  2832
    ///\param s The source node
deba@2539
  2833
    ///\param t The target node
deba@2539
  2834
    ///\return An edge from \c s to \c t if there exists,
deba@2539
  2835
    ///\ref INVALID otherwise.
deba@2539
  2836
    Edge operator()(Node s, Node t) const
deba@2539
  2837
    {
deba@2539
  2838
      Edge e = _head[s];
deba@2539
  2839
      while (true) {
deba@2539
  2840
	if (_g.target(e) == t) {
deba@2539
  2841
	  const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2842
	  return e;
deba@2539
  2843
	} else if (t < _g.target(e)) {
deba@2539
  2844
	  if (_left[e] == INVALID) {
deba@2539
  2845
	    const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2846
	    return INVALID;
deba@2539
  2847
	  } else {
deba@2539
  2848
	    e = _left[e];
deba@2539
  2849
	  }
deba@2539
  2850
	} else  {
deba@2539
  2851
	  if (_right[e] == INVALID) {
deba@2539
  2852
	    const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2853
	    return INVALID;
deba@2539
  2854
	  } else {
deba@2539
  2855
	    e = _right[e];
deba@2539
  2856
	  }
deba@2539
  2857
	}
deba@2539
  2858
      }
deba@2539
  2859
    }
deba@2539
  2860
deba@2539
  2861
    ///Find the first edge between two nodes.
deba@2539
  2862
    
deba@2539
  2863
    ///Find the first edge between two nodes in time
deba@2539
  2864
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
deba@2539
  2865
    /// outgoing edges of \c s.  
deba@2539
  2866
    ///\param s The source node 
deba@2539
  2867
    ///\param t The target node
deba@2539
  2868
    ///\return An edge from \c s to \c t if there exists, \ref INVALID
deba@2539
  2869
    /// otherwise.
deba@2539
  2870
    Edge findFirst(Node s, Node t) const
deba@2539
  2871
    {
deba@2539
  2872
      Edge e = _head[s];
deba@2539
  2873
      Edge r = INVALID;
deba@2539
  2874
      while (true) {
deba@2539
  2875
	if (_g.target(e) < t) {
deba@2539
  2876
	  if (_right[e] == INVALID) {
deba@2539
  2877
	    const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2878
	    return r;
deba@2539
  2879
	  } else {
deba@2539
  2880
	    e = _right[e];
deba@2539
  2881
	  }
deba@2539
  2882
	} else {
deba@2539
  2883
	  if (_g.target(e) == t) {
deba@2539
  2884
	    r = e;
deba@2539
  2885
	  }
deba@2539
  2886
	  if (_left[e] == INVALID) {
deba@2539
  2887
	    const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2888
	    return r;
deba@2539
  2889
	  } else {
deba@2539
  2890
	    e = _left[e];
deba@2539
  2891
	  }
deba@2539
  2892
	}
deba@2539
  2893
      }
deba@2539
  2894
    }
deba@2539
  2895
deba@2539
  2896
    ///Find the next edge between two nodes.
deba@2539
  2897
    
deba@2539
  2898
    ///Find the next edge between two nodes in time
deba@2539
  2899
    /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
deba@2539
  2900
    /// outgoing edges of \c s.  
deba@2539
  2901
    ///\param s The source node 
deba@2539
  2902
    ///\param t The target node
deba@2539
  2903
    ///\return An edge from \c s to \c t if there exists, \ref INVALID
deba@2539
  2904
    /// otherwise.
deba@2539
  2905
deba@2539
  2906
    ///\note If \c e is not the result of the previous \c findFirst()
deba@2539
  2907
    ///operation then the amorized time bound can not be guaranteed.
deba@2539
  2908
#ifdef DOXYGEN
deba@2539
  2909
    Edge findNext(Node s, Node t, Edge e) const
deba@2539
  2910
#else
deba@2539
  2911
    Edge findNext(Node, Node t, Edge e) const
deba@2539
  2912
#endif
deba@2539
  2913
    {
deba@2539
  2914
      if (_right[e] != INVALID) {
deba@2539
  2915
	e = _right[e];
deba@2539
  2916
	while (_left[e] != INVALID) {
deba@2539
  2917
	  e = _left[e];
deba@2539
  2918
	}
deba@2539
  2919
	const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2920
      } else {
deba@2539
  2921
	while (_parent[e] != INVALID && _right[_parent[e]] ==  e) {
deba@2539
  2922
	  e = _parent[e];
deba@2539
  2923
	}
deba@2539
  2924
	if (_parent[e] == INVALID) {
deba@2539
  2925
	  return INVALID;
deba@2539
  2926
	} else {
deba@2539
  2927
	  e = _parent[e];
deba@2539
  2928
	  const_cast<DynEdgeLookUp&>(*this).splay(e);
deba@2539
  2929
	}
deba@2539
  2930
      }
deba@2539
  2931
      if (_g.target(e) == t) return e;
deba@2539
  2932
      else return INVALID;    
deba@2539
  2933
    }
deba@2539
  2934
deba@2539
  2935
  };
deba@2539
  2936
alpar@2235
  2937
  ///Fast edge look up between given endpoints.
alpar@2235
  2938
  
alpar@2235
  2939
  ///\ingroup gutils
alpar@2235
  2940
  ///Using this class, you can find an edge in a graph from a given
alpar@2235
  2941
  ///source to a given target in time <em>O(log d)</em>,
alpar@2235
  2942
  ///where <em>d</em> is the out-degree of the source node.
alpar@2235
  2943
  ///
alpar@2235
  2944
  ///It is not possible to find \e all parallel edges between two nodes.
alpar@2235
  2945
  ///Use \ref AllEdgeLookUp for this purpose.
alpar@2235
  2946
  ///
alpar@2235
  2947
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  2948
  ///refresh(Node)) this data structure
alpar@2235
  2949
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  2950
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  2951
  ///
alpar@2235
  2952
  ///\param G The type of the underlying graph.
alpar@2235
  2953
  ///
deba@2539
  2954
  ///\sa DynEdgeLookUp
alpar@2235
  2955
  ///\sa AllEdgeLookUp  
alpar@2235
  2956
  template<class G>
alpar@2235
  2957
  class EdgeLookUp 
alpar@2235
  2958
  {
alpar@2235
  2959
  public:
deba@2510
  2960
    GRAPH_TYPEDEFS(typename G);
alpar@2235
  2961
    typedef G Graph;
alpar@2235
  2962
alpar@2235
  2963
  protected:
alpar@2235
  2964
    const Graph &_g;
alpar@2235
  2965
    typename Graph::template NodeMap<Edge> _head;
alpar@2235
  2966
    typename Graph::template EdgeMap<Edge> _left;
alpar@2235
  2967
    typename Graph::template EdgeMap<Edge> _right;
alpar@2235
  2968
    
alpar@2235
  2969
    class EdgeLess {
alpar@2235
  2970
      const Graph &g;
alpar@2235
  2971
    public:
alpar@2235
  2972
      EdgeLess(const Graph &_g) : g(_g) {}
alpar@2235
  2973
      bool operator()(Edge a,Edge b) const 
alpar@2235
  2974
      {
alpar@2235
  2975
	return g.target(a)<g.target(b);
alpar@2235
  2976
      }
alpar@2235
  2977
    };
alpar@2235
  2978
    
alpar@2235
  2979
  public:
alpar@2235
  2980
    
alpar@2235
  2981
    ///Constructor
alpar@2235
  2982
alpar@2235
  2983
    ///Constructor.
alpar@2235
  2984
    ///
alpar@2235
  2985
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  2986
    ///changes.
alpar@2235
  2987
    EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
alpar@2235
  2988
    
alpar@2235
  2989
  private:
deba@2539
  2990
    Edge refreshRec(std::vector<Edge> &v,int a,int b) 
alpar@2235
  2991
    {
alpar@2235
  2992
      int m=(a+b)/2;
alpar@2235
  2993
      Edge me=v[m];
deba@2539
  2994
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
deba@2539
  2995
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
alpar@2235
  2996
      return me;
alpar@2235
  2997
    }
alpar@2235
  2998
  public:
alpar@2235
  2999
    ///Refresh the data structure at a node.
alpar@2235
  3000
alpar@2235
  3001
    ///Build up the search database of node \c n.
alpar@2235
  3002
    ///
alpar@2235
  3003
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  3004
    ///the number of the outgoing edges of \c n.
alpar@2235
  3005
    void refresh(Node n) 
alpar@2235
  3006
    {
alpar@2235
  3007
      std::vector<Edge> v;
alpar@2235
  3008
      for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
alpar@2235
  3009
      if(v.size()) {
alpar@2235
  3010
	std::sort(v.begin(),v.end(),EdgeLess(_g));
deba@2539
  3011
	_head[n]=refreshRec(v,0,v.size()-1);
alpar@2235
  3012
      }
alpar@2235
  3013
      else _head[n]=INVALID;
alpar@2235
  3014
    }
alpar@2235
  3015
    ///Refresh the full data structure.
alpar@2235
  3016
alpar@2235
  3017
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  3018
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  3019
    ///
alpar@2235
  3020
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  3021
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  3022
    ///out-degree of the graph.
alpar@2235
  3023
alpar@2235
  3024
    void refresh() 
alpar@2235
  3025
    {
alpar@2235
  3026
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
alpar@2235
  3027
    }
alpar@2235
  3028
    
alpar@2235
  3029
    ///Find an edge between two nodes.
alpar@2235
  3030
    
alpar@2235
  3031
    ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
alpar@2235
  3032
    /// <em>d</em> is the number of outgoing edges of \c s.
alpar@2235
  3033
    ///\param s The source node
alpar@2235
  3034
    ///\param t The target node
alpar@2235
  3035
    ///\return An edge from \c s to \c t if there exists,
alpar@2235
  3036
    ///\ref INVALID otherwise.
alpar@2235
  3037
    ///
alpar@2235
  3038
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  3039
    ///this operator. If you change the outgoing edges of
alpar@2235
  3040
    ///a single node \c n, then
alpar@2235
  3041
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  3042
    ///
alpar@2235
  3043
    Edge operator()(Node s, Node t) const
alpar@2235
  3044
    {
alpar@2235
  3045
      Edge e;
alpar@2235
  3046
      for(e=_head[s];
alpar@2235
  3047
	  e!=INVALID&&_g.target(e)!=t;
alpar@2235
  3048
	  e = t < _g.target(e)?_left[e]:_right[e]) ;
alpar@2235
  3049
      return e;
alpar@2235
  3050
    }
alpar@2235
  3051
alpar@2235
  3052
  };
alpar@2235
  3053
alpar@2235
  3054
  ///Fast look up of all edges between given endpoints.
alpar@2235
  3055
  
alpar@2235
  3056
  ///\ingroup gutils
alpar@2235
  3057
  ///This class is the same as \ref EdgeLookUp, with the addition
alpar@2235
  3058
  ///that it makes it possible to find all edges between given endpoints.
alpar@2235
  3059
  ///
alpar@2235
  3060
  ///\warning This class is static, so you should refresh() (or at least
alpar@2235
  3061
  ///refresh(Node)) this data structure
alpar@2235
  3062
  ///whenever the graph changes. This is a time consuming (superlinearly
alpar@2235
  3063
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
alpar@2235
  3064
  ///
alpar@2235
  3065
  ///\param G The type of the underlying graph.
alpar@2235
  3066
  ///
deba@2539
  3067
  ///\sa DynEdgeLookUp
alpar@2235
  3068
  ///\sa EdgeLookUp  
alpar@2235
  3069
  template<class G>
alpar@2235
  3070
  class AllEdgeLookUp : public EdgeLookUp<G>
alpar@2235
  3071
  {
alpar@2235
  3072
    using EdgeLookUp<G>::_g;
alpar@2235
  3073
    using EdgeLookUp<G>::_right;
alpar@2235
  3074
    using EdgeLookUp<G>::_left;
alpar@2235
  3075
    using EdgeLookUp<G>::_head;
alpar@2235
  3076
deba@2510
  3077
    GRAPH_TYPEDEFS(typename G);
alpar@2235
  3078
    typedef G Graph;
alpar@2235
  3079
    
alpar@2235
  3080
    typename Graph::template EdgeMap<Edge> _next;
alpar@2235
  3081
    
alpar@2235
  3082
    Edge refreshNext(Edge head,Edge next=INVALID)
alpar@2235
  3083
    {
alpar@2235
  3084
      if(head==INVALID) return next;
alpar@2235
  3085
      else {
alpar@2235
  3086
	next=refreshNext(_right[head],next);
alpar@2235
  3087
// 	_next[head]=next;
alpar@2235
  3088
	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
alpar@2235
  3089
	  ? next : INVALID;
alpar@2235
  3090
	return refreshNext(_left[head],head);
alpar@2235
  3091
      }
alpar@2235
  3092
    }
alpar@2235
  3093
    
alpar@2235
  3094
    void refreshNext()
alpar@2235
  3095
    {
alpar@2235
  3096
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
alpar@2235
  3097
    }
alpar@2235
  3098
    
alpar@2235
  3099
  public:
alpar@2235
  3100
    ///Constructor
alpar@2235
  3101
alpar@2235
  3102
    ///Constructor.
alpar@2235
  3103
    ///
alpar@2235
  3104
    ///It builds up the search database, which remains valid until the graph
alpar@2235
  3105
    ///changes.
alpar@2235
  3106
    AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
alpar@2235
  3107
alpar@2235
  3108
    ///Refresh the data structure at a node.
alpar@2235
  3109
alpar@2235
  3110
    ///Build up the search database of node \c n.
alpar@2235
  3111
    ///
alpar@2235
  3112
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
alpar@2235
  3113
    ///the number of the outgoing edges of \c n.
alpar@2235
  3114
    
alpar@2235
  3115
    void refresh(Node n) 
alpar@2235
  3116
    {
alpar@2235
  3117
      EdgeLookUp<G>::refresh(n);
alpar@2235
  3118
      refreshNext(_head[n]);
alpar@2235
  3119
    }
alpar@2235
  3120
    
alpar@2235
  3121
    ///Refresh the full data structure.
alpar@2235
  3122
alpar@2235
  3123
    ///Build up the full search database. In fact, it simply calls
alpar@2235
  3124
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
alpar@2235
  3125
    ///
alpar@2235
  3126
    ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
alpar@2235
  3127
    ///the number of the edges of \c n and <em>D</em> is the maximum
alpar@2235
  3128
    ///out-degree of the graph.
alpar@2235
  3129
alpar@2235
  3130
    void refresh() 
alpar@2235
  3131
    {
alpar@2235
  3132
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
alpar@2235
  3133
    }
alpar@2235
  3134
    
alpar@2235
  3135
    ///Find an edge between two nodes.
alpar@2235
  3136
    
alpar@2235
  3137
    ///Find an edge between two nodes.
alpar@2235
  3138
    ///\param s The source node
alpar@2235
  3139
    ///\param t The target node
alpar@2235
  3140
    ///\param prev The previous edge between \c s and \c t. It it is INVALID or
alpar@2235
  3141
    ///not given, the operator finds the first appropriate edge.
alpar@2350
  3142
    ///\return An edge from \c s to \c t after \c prev or
alpar@2235
  3143
    ///\ref INVALID if there is no more.
alpar@2235
  3144
    ///
alpar@2235
  3145
    ///For example, you can count the number of edges from \c u to \c v in the
alpar@2235
  3146
    ///following way.
alpar@2235
  3147
    ///\code
alpar@2235
  3148
    ///AllEdgeLookUp<ListGraph> ae(g);
alpar@2235
  3149
    ///...
alpar@2235
  3150
    ///int n=0;
alpar@2235
  3151
    ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
alpar@2235
  3152
    ///\endcode
alpar@2235
  3153
    ///
alpar@2235
  3154
    ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
alpar@2235
  3155
    /// <em>d</em> is the number of outgoing edges of \c s. Then, the
alpar@2235
  3156
    ///consecutive edges are found in constant time.
alpar@2235
  3157
    ///
alpar@2235
  3158
    ///\warning If you change the graph, refresh() must be called before using
alpar@2235
  3159
    ///this operator. If you change the outgoing edges of
alpar@2235
  3160
    ///a single node \c n, then
alpar@2235
  3161
    ///\ref refresh(Node) "refresh(n)" is enough.
alpar@2235
  3162
    ///
alpar@2235
  3163
#ifdef DOXYGEN
alpar@2235
  3164
    Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
alpar@2235
  3165
#else
alpar@2235
  3166
    using EdgeLookUp<G>::operator() ;
alpar@2235
  3167
    Edge operator()(Node s, Node t, Edge prev) const
alpar@2235
  3168
    {
alpar@2235
  3169
      return prev==INVALID?(*this)(s,t):_next[prev];
alpar@2235
  3170
    }
alpar@2235
  3171
#endif
alpar@2235
  3172
      
alpar@2235
  3173
  };
alpar@2235
  3174
alpar@1402
  3175
  /// @}
alpar@1402
  3176
alpar@947
  3177
} //END OF NAMESPACE LEMON
klao@946
  3178
klao@946
  3179
#endif