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