lemon/graph_utils.h
author alpar
Mon, 24 Oct 2005 15:58:38 +0000
changeset 1738 470aa67893f5
parent 1729 06f939455cb1
child 1756 b1f441f24d08
permissions -rw-r--r--
Euler tour iterator.
klao@946
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
klao@946
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     6
 *
klao@946
     7
 * Permission to use, modify and distribute this software is granted
klao@946
     8
 * provided that this copyright notice appears in all copies. For
klao@946
     9
 * precise terms see the accompanying LICENSE file.
klao@946
    10
 *
klao@946
    11
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    12
 * express or implied, and with no claim as to its suitability for any
klao@946
    13
 * purpose.
klao@946
    14
 *
klao@946
    15
 */
klao@946
    16
klao@946
    17
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    18
#define LEMON_GRAPH_UTILS_H
klao@946
    19
klao@946
    20
#include <iterator>
deba@1419
    21
#include <vector>
alpar@1402
    22
#include <map>
deba@1695
    23
#include <cmath>
klao@946
    24
klao@946
    25
#include <lemon/invalid.h>
klao@977
    26
#include <lemon/utility.h>
deba@1413
    27
#include <lemon/maps.h>
deba@1720
    28
#include <lemon/traits.h>
alpar@1459
    29
#include <lemon/bits/alteration_notifier.h>
klao@946
    30
alpar@947
    31
///\ingroup gutils
klao@946
    32
///\file
alpar@947
    33
///\brief Graph utilities.
klao@946
    34
///
alpar@964
    35
///
klao@946
    36
klao@946
    37
klao@946
    38
namespace lemon {
klao@946
    39
deba@1267
    40
  /// \addtogroup gutils
deba@1267
    41
  /// @{
alpar@947
    42
klao@946
    43
  /// \brief Function to count the items in the graph.
klao@946
    44
  ///
athos@1540
    45
  /// This function counts the items (nodes, edges etc) in the graph.
klao@946
    46
  /// The complexity of the function is O(n) because
klao@946
    47
  /// it iterates on all of the items.
klao@946
    48
klao@946
    49
  template <typename Graph, typename ItemIt>
klao@977
    50
  inline int countItems(const Graph& g) {
klao@946
    51
    int num = 0;
klao@977
    52
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
    53
      ++num;
klao@946
    54
    }
klao@946
    55
    return num;
klao@946
    56
  }
klao@946
    57
klao@977
    58
  // Node counting:
klao@977
    59
klao@977
    60
  template <typename Graph>
klao@977
    61
  inline
klao@977
    62
  typename enable_if<typename Graph::NodeNumTag, int>::type
klao@977
    63
  _countNodes(const Graph &g) {
klao@977
    64
    return g.nodeNum();
klao@977
    65
  }
klao@977
    66
klao@977
    67
  template <typename Graph>
klao@977
    68
  inline int _countNodes(Wrap<Graph> w) {
klao@977
    69
    return countItems<Graph, typename Graph::NodeIt>(w.value);
klao@977
    70
  }
klao@977
    71
klao@946
    72
  /// \brief Function to count the nodes in the graph.
klao@946
    73
  ///
klao@946
    74
  /// This function counts the nodes in the graph.
klao@946
    75
  /// The complexity of the function is O(n) but for some
athos@1526
    76
  /// graph structures it is specialized to run in O(1).
klao@977
    77
  ///
klao@977
    78
  /// \todo refer how to specialize it
klao@946
    79
klao@946
    80
  template <typename Graph>
klao@977
    81
  inline int countNodes(const Graph& g) {
klao@977
    82
    return _countNodes<Graph>(g);
klao@977
    83
  }
klao@977
    84
klao@977
    85
  // Edge counting:
klao@977
    86
klao@977
    87
  template <typename Graph>
klao@977
    88
  inline
klao@977
    89
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@977
    90
  _countEdges(const Graph &g) {
klao@977
    91
    return g.edgeNum();
klao@977
    92
  }
klao@977
    93
klao@977
    94
  template <typename Graph>
klao@977
    95
  inline int _countEdges(Wrap<Graph> w) {
klao@977
    96
    return countItems<Graph, typename Graph::EdgeIt>(w.value);
klao@946
    97
  }
klao@946
    98
klao@946
    99
  /// \brief Function to count the edges in the graph.
klao@946
   100
  ///
klao@946
   101
  /// This function counts the edges in the graph.
klao@946
   102
  /// The complexity of the function is O(e) but for some
athos@1526
   103
  /// graph structures it is specialized to run in O(1).
klao@977
   104
klao@946
   105
  template <typename Graph>
klao@977
   106
  inline int countEdges(const Graph& g) {
klao@977
   107
    return _countEdges<Graph>(g);
klao@946
   108
  }
klao@946
   109
klao@1053
   110
  // Undirected edge counting:
klao@1053
   111
klao@1053
   112
  template <typename Graph>
klao@1053
   113
  inline
klao@1053
   114
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@1053
   115
  _countUndirEdges(const Graph &g) {
klao@1053
   116
    return g.undirEdgeNum();
klao@1053
   117
  }
klao@1053
   118
klao@1053
   119
  template <typename Graph>
klao@1053
   120
  inline int _countUndirEdges(Wrap<Graph> w) {
klao@1053
   121
    return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
klao@1053
   122
  }
klao@1053
   123
athos@1526
   124
  /// \brief Function to count the undirected edges in the graph.
klao@946
   125
  ///
athos@1526
   126
  /// This function counts the undirected edges in the graph.
klao@946
   127
  /// The complexity of the function is O(e) but for some
athos@1540
   128
  /// graph structures it is specialized to run in O(1).
klao@1053
   129
klao@946
   130
  template <typename Graph>
klao@1053
   131
  inline int countUndirEdges(const Graph& g) {
klao@1053
   132
    return _countUndirEdges<Graph>(g);
klao@946
   133
  }
klao@946
   134
klao@977
   135
klao@1053
   136
klao@946
   137
  template <typename Graph, typename DegIt>
klao@946
   138
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   139
    int num = 0;
klao@946
   140
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   141
      ++num;
klao@946
   142
    }
klao@946
   143
    return num;
klao@946
   144
  }
alpar@967
   145
deba@1531
   146
  /// \brief Function to count the number of the out-edges from node \c n.
deba@1531
   147
  ///
deba@1531
   148
  /// This function counts the number of the out-edges from node \c n
deba@1531
   149
  /// in the graph.  
deba@1531
   150
  template <typename Graph>
deba@1531
   151
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   152
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
deba@1531
   153
  }
deba@1531
   154
deba@1531
   155
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1531
   156
  ///
deba@1531
   157
  /// This function counts the number of the in-edges to node \c n
deba@1531
   158
  /// in the graph.  
deba@1531
   159
  template <typename Graph>
deba@1531
   160
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   161
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
deba@1531
   162
  }
deba@1531
   163
deba@1704
   164
  /// \brief Function to count the number of the inc-edges to node \c n.
deba@1679
   165
  ///
deba@1704
   166
  /// This function counts the number of the inc-edges to node \c n
deba@1679
   167
  /// in the graph.  
deba@1679
   168
  template <typename Graph>
deba@1679
   169
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1679
   170
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
deba@1679
   171
  }
deba@1679
   172
deba@1531
   173
deba@1565
   174
  template <typename Graph>
deba@1565
   175
  inline
deba@1565
   176
  typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type 
deba@1565
   177
  _findEdge(const Graph &g,
deba@1565
   178
	    typename Graph::Node u, typename Graph::Node v,
deba@1565
   179
	    typename Graph::Edge prev = INVALID) {
deba@1565
   180
    return g.findEdge(u, v, prev);
deba@1565
   181
  }
alpar@967
   182
deba@1565
   183
  template <typename Graph>
deba@1565
   184
  inline typename Graph::Edge 
deba@1565
   185
  _findEdge(Wrap<Graph> w,
deba@1565
   186
	    typename Graph::Node u, 
deba@1565
   187
	    typename Graph::Node v,
deba@1565
   188
	    typename Graph::Edge prev = INVALID) {
deba@1565
   189
    const Graph& g = w.value;
deba@1565
   190
    if (prev == INVALID) {
deba@1565
   191
      typename Graph::OutEdgeIt e(g, u);
deba@1565
   192
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   193
      return e;
deba@1565
   194
    } else {
deba@1565
   195
      typename Graph::OutEdgeIt e(g, prev); ++e;
deba@1565
   196
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   197
      return e;
deba@1565
   198
    }
deba@1565
   199
  }
deba@1565
   200
deba@1565
   201
  /// \brief Finds an edge between two nodes of a graph.
deba@1565
   202
  ///
alpar@967
   203
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   204
  ///
alpar@967
   205
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   206
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   207
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   208
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   209
  ///
alpar@967
   210
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@967
   211
  /// \code
alpar@967
   212
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   213
  ///   ...
alpar@967
   214
  /// }
alpar@967
   215
  /// \endcode
deba@1565
   216
  // /// \todo We may want to use the "GraphBase" 
deba@1565
   217
  // /// interface here...
alpar@967
   218
  template <typename Graph>
deba@1565
   219
  inline typename Graph::Edge findEdge(const Graph &g,
deba@1565
   220
				       typename Graph::Node u, 
deba@1565
   221
				       typename Graph::Node v,
deba@1565
   222
				       typename Graph::Edge prev = INVALID) {
deba@1565
   223
    return _findEdge<Graph>(g, u, v, prev);
alpar@967
   224
  }
deba@1531
   225
deba@1565
   226
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   227
  ///
deba@1565
   228
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   229
  /// higher level interface for the findEdge() function. You can
alpar@1591
   230
  /// use it the following way:
deba@1565
   231
  /// \code
deba@1565
   232
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   233
  ///   ...
deba@1565
   234
  /// }
deba@1565
   235
  /// \endcode
deba@1565
   236
  ///
deba@1565
   237
  /// \author Balazs Dezso 
deba@1565
   238
  template <typename _Graph>
deba@1565
   239
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   240
  public:
deba@1565
   241
deba@1565
   242
    typedef _Graph Graph;
deba@1565
   243
    typedef typename Graph::Edge Parent;
deba@1565
   244
deba@1565
   245
    typedef typename Graph::Edge Edge;
deba@1565
   246
    typedef typename Graph::Node Node;
deba@1565
   247
deba@1565
   248
    /// \brief Constructor.
deba@1565
   249
    ///
deba@1565
   250
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   251
    /// connects the \c u and \c v node.
deba@1565
   252
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   253
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   254
    }
deba@1565
   255
deba@1565
   256
    /// \brief Constructor.
deba@1565
   257
    ///
deba@1565
   258
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   259
    /// the \c e edge.
deba@1565
   260
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   261
    
deba@1565
   262
    /// \brief Increment operator.
deba@1565
   263
    ///
deba@1565
   264
    /// It increments the iterator and gives back the next edge.
deba@1565
   265
    ConEdgeIt& operator++() {
deba@1565
   266
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   267
				 graph.target(*this), *this));
deba@1565
   268
      return *this;
deba@1565
   269
    }
deba@1565
   270
  private:
deba@1565
   271
    const Graph& graph;
deba@1565
   272
  };
deba@1565
   273
deba@1704
   274
  template <typename Graph>
deba@1704
   275
  inline
deba@1704
   276
  typename enable_if<
deba@1704
   277
    typename Graph::FindEdgeTag, 
deba@1704
   278
    typename Graph::UndirEdge>::type 
deba@1704
   279
  _findUndirEdge(const Graph &g,
deba@1704
   280
	    typename Graph::Node u, typename Graph::Node v,
deba@1704
   281
	    typename Graph::UndirEdge prev = INVALID) {
deba@1704
   282
    return g.findUndirEdge(u, v, prev);
deba@1704
   283
  }
deba@1704
   284
deba@1704
   285
  template <typename Graph>
deba@1704
   286
  inline typename Graph::UndirEdge 
deba@1704
   287
  _findUndirEdge(Wrap<Graph> w,
deba@1704
   288
	    typename Graph::Node u, 
deba@1704
   289
	    typename Graph::Node v,
deba@1704
   290
	    typename Graph::UndirEdge prev = INVALID) {
deba@1704
   291
    const Graph& g = w.value;
deba@1704
   292
    if (prev == INVALID) {
deba@1704
   293
      typename Graph::OutEdgeIt e(g, u);
deba@1704
   294
      while (e != INVALID && g.target(e) != v) ++e;
deba@1704
   295
      return e;
deba@1704
   296
    } else {
deba@1704
   297
      typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
deba@1704
   298
      while (e != INVALID && g.target(e) != v) ++e;
deba@1704
   299
      return e;
deba@1704
   300
    }
deba@1704
   301
  }
deba@1704
   302
deba@1704
   303
  /// \brief Finds an undir edge between two nodes of a graph.
deba@1704
   304
  ///
deba@1704
   305
  /// Finds an undir edge from node \c u to node \c v in graph \c g.
deba@1704
   306
  ///
deba@1704
   307
  /// If \c prev is \ref INVALID (this is the default value), then
deba@1704
   308
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
deba@1704
   309
  /// the next edge from \c u to \c v after \c prev.
deba@1704
   310
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@1704
   311
  ///
deba@1704
   312
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
deba@1704
   313
  /// \code
deba@1704
   314
  /// for(UndirEdge e = findUndirEdge(g,u,v); e != INVALID; 
deba@1704
   315
  ///     e = findUndirEdge(g,u,v,e)) {
deba@1704
   316
  ///   ...
deba@1704
   317
  /// }
deba@1704
   318
  /// \endcode
deba@1704
   319
  // /// \todo We may want to use the "GraphBase" 
deba@1704
   320
  // /// interface here...
deba@1704
   321
  template <typename Graph>
deba@1704
   322
  inline typename Graph::UndirEdge 
deba@1704
   323
  findUndirEdge(const Graph &g,
deba@1704
   324
		typename Graph::Node u, 
deba@1704
   325
		typename Graph::Node v,
deba@1704
   326
		typename Graph::UndirEdge prev = INVALID) {
deba@1704
   327
    return _findUndirEdge<Graph>(g, u, v, prev);
deba@1704
   328
  }
deba@1704
   329
deba@1704
   330
  /// \brief Iterator for iterating on undir edges connected the same nodes.
deba@1704
   331
  ///
deba@1704
   332
  /// Iterator for iterating on undir edges connected the same nodes. It is 
deba@1704
   333
  /// higher level interface for the findUndirEdge() function. You can
deba@1704
   334
  /// use it the following way:
deba@1704
   335
  /// \code
deba@1704
   336
  /// for (ConUndirEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1704
   337
  ///   ...
deba@1704
   338
  /// }
deba@1704
   339
  /// \endcode
deba@1704
   340
  ///
deba@1704
   341
  /// \author Balazs Dezso 
deba@1704
   342
  template <typename _Graph>
deba@1704
   343
  class ConUndirEdgeIt : public _Graph::UndirEdge {
deba@1704
   344
  public:
deba@1704
   345
deba@1704
   346
    typedef _Graph Graph;
deba@1704
   347
    typedef typename Graph::UndirEdge Parent;
deba@1704
   348
deba@1704
   349
    typedef typename Graph::UndirEdge UndirEdge;
deba@1704
   350
    typedef typename Graph::Node Node;
deba@1704
   351
deba@1704
   352
    /// \brief Constructor.
deba@1704
   353
    ///
deba@1704
   354
    /// Construct a new ConUndirEdgeIt iterating on the edges which
deba@1704
   355
    /// connects the \c u and \c v node.
deba@1704
   356
    ConUndirEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1704
   357
      Parent::operator=(findUndirEdge(graph, u, v));
deba@1704
   358
    }
deba@1704
   359
deba@1704
   360
    /// \brief Constructor.
deba@1704
   361
    ///
deba@1704
   362
    /// Construct a new ConUndirEdgeIt which continues the iterating from 
deba@1704
   363
    /// the \c e edge.
deba@1704
   364
    ConUndirEdgeIt(const Graph& g, UndirEdge e) : Parent(e), graph(g) {}
deba@1704
   365
    
deba@1704
   366
    /// \brief Increment operator.
deba@1704
   367
    ///
deba@1704
   368
    /// It increments the iterator and gives back the next edge.
deba@1704
   369
    ConUndirEdgeIt& operator++() {
deba@1704
   370
      Parent::operator=(findUndirEdge(graph, graph.source(*this), 
deba@1704
   371
				 graph.target(*this), *this));
deba@1704
   372
      return *this;
deba@1704
   373
    }
deba@1704
   374
  private:
deba@1704
   375
    const Graph& graph;
deba@1704
   376
  };
deba@1704
   377
athos@1540
   378
  /// \brief Copy a map.
alpar@964
   379
  ///
alpar@1547
   380
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   381
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   382
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   383
  template <typename Target, typename Source, 
deba@1531
   384
	    typename ItemIt, typename Ref>	    
deba@1531
   385
  void copyMap(Target& target, const Source& source, 
deba@1531
   386
	       ItemIt it, const Ref& ref) {
deba@1531
   387
    for (; it != INVALID; ++it) {
deba@1531
   388
      target[ref[it]] = source[it];
klao@946
   389
    }
klao@946
   390
  }
klao@946
   391
deba@1531
   392
  /// \brief Copy the source map to the target map.
deba@1531
   393
  ///
deba@1531
   394
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   395
  /// to iterate on the data structure.
deba@1531
   396
  template <typename Target, typename Source, 
deba@1531
   397
	    typename ItemIt>	    
deba@1531
   398
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   399
    for (; it != INVALID; ++it) {
deba@1531
   400
      target[it] = source[it];
klao@946
   401
    }
klao@946
   402
  }
klao@946
   403
athos@1540
   404
  /// \brief Class to copy a graph.
deba@1531
   405
  ///
athos@1540
   406
  /// Class to copy a graph to an other graph (duplicate a graph). The
athos@1540
   407
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   408
  template <typename Target, typename Source>
deba@1267
   409
  class GraphCopy {
deba@1531
   410
  public: 
deba@1531
   411
    typedef typename Source::Node Node;
deba@1531
   412
    typedef typename Source::NodeIt NodeIt;
deba@1531
   413
    typedef typename Source::Edge Edge;
deba@1531
   414
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   415
deba@1531
   416
    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
deba@1531
   417
    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
klao@946
   418
deba@1531
   419
    /// \brief Constructor for the GraphCopy.
deba@1531
   420
    ///
deba@1531
   421
    /// It copies the content of the \c _source graph into the
deba@1531
   422
    /// \c _target graph. It creates also two references, one beetween
deba@1531
   423
    /// the two nodeset and one beetween the two edgesets.
deba@1531
   424
    GraphCopy(Target& _target, const Source& _source) 
deba@1531
   425
      : source(_source), target(_target), 
deba@1531
   426
	nodeRefMap(_source), edgeRefMap(_source) {
deba@1531
   427
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   428
	nodeRefMap[it] = target.addNode();
deba@1531
   429
      }
deba@1531
   430
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   431
	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1531
   432
					nodeRefMap[source.target(it)]);
deba@1531
   433
      }
deba@1267
   434
    }
klao@946
   435
deba@1531
   436
    /// \brief Copies the node references into the given map.
deba@1531
   437
    ///
deba@1531
   438
    /// Copies the node references into the given map.
deba@1531
   439
    template <typename NodeRef>
deba@1531
   440
    const GraphCopy& nodeRef(NodeRef& map) const {
deba@1531
   441
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   442
	map.set(it, nodeRefMap[it]);
deba@1531
   443
      }
deba@1531
   444
      return *this;
deba@1267
   445
    }
deba@1531
   446
deba@1531
   447
    /// \brief Reverse and copies the node references into the given map.
deba@1531
   448
    ///
deba@1531
   449
    /// Reverse and copies the node references into the given map.
deba@1531
   450
    template <typename NodeRef>
deba@1531
   451
    const GraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1531
   452
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   453
	map.set(nodeRefMap[it], it);
deba@1531
   454
      }
deba@1531
   455
      return *this;
deba@1531
   456
    }
deba@1531
   457
deba@1531
   458
    /// \brief Copies the edge references into the given map.
deba@1531
   459
    ///
deba@1531
   460
    /// Copies the edge references into the given map.
deba@1531
   461
    template <typename EdgeRef>
deba@1531
   462
    const GraphCopy& edgeRef(EdgeRef& map) const {
deba@1531
   463
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   464
	map.set(it, edgeRefMap[it]);
deba@1531
   465
      }
deba@1531
   466
      return *this;
deba@1531
   467
    }
deba@1531
   468
deba@1531
   469
    /// \brief Reverse and copies the edge references into the given map.
deba@1531
   470
    ///
deba@1531
   471
    /// Reverse and copies the edge references into the given map.
deba@1531
   472
    template <typename EdgeRef>
deba@1531
   473
    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1531
   474
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   475
	map.set(edgeRefMap[it], it);
deba@1531
   476
      }
deba@1531
   477
      return *this;
deba@1531
   478
    }
deba@1531
   479
deba@1531
   480
    /// \brief Make copy of the given map.
deba@1531
   481
    ///
deba@1531
   482
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   483
    /// The new map's key type is the target graph's node type,
deba@1531
   484
    /// and the copied map's key type is the source graph's node
deba@1531
   485
    /// type.  
deba@1531
   486
    template <typename TargetMap, typename SourceMap>
deba@1531
   487
    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   488
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1531
   489
      return *this;
deba@1531
   490
    }
deba@1531
   491
deba@1531
   492
    /// \brief Make copy of the given map.
deba@1531
   493
    ///
deba@1531
   494
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   495
    /// The new map's key type is the target graph's edge type,
deba@1531
   496
    /// and the copied map's key type is the source graph's edge
deba@1531
   497
    /// type.  
deba@1531
   498
    template <typename TargetMap, typename SourceMap>
deba@1531
   499
    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   500
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1531
   501
      return *this;
deba@1531
   502
    }
deba@1531
   503
deba@1531
   504
    /// \brief Gives back the stored node references.
deba@1531
   505
    ///
deba@1531
   506
    /// Gives back the stored node references.
deba@1531
   507
    const NodeRefMap& nodeRef() const {
deba@1531
   508
      return nodeRefMap;
deba@1531
   509
    }
deba@1531
   510
deba@1531
   511
    /// \brief Gives back the stored edge references.
deba@1531
   512
    ///
deba@1531
   513
    /// Gives back the stored edge references.
deba@1531
   514
    const EdgeRefMap& edgeRef() const {
deba@1531
   515
      return edgeRefMap;
deba@1531
   516
    }
deba@1531
   517
deba@1720
   518
    void run() {}
deba@1720
   519
deba@1531
   520
  private:
deba@1531
   521
    
deba@1531
   522
    const Source& source;
deba@1531
   523
    Target& target;
deba@1531
   524
deba@1531
   525
    NodeRefMap nodeRefMap;
deba@1531
   526
    EdgeRefMap edgeRefMap;
deba@1267
   527
  };
klao@946
   528
deba@1531
   529
  /// \brief Copy a graph to an other graph.
deba@1531
   530
  ///
deba@1531
   531
  /// Copy a graph to an other graph.
deba@1531
   532
  /// The usage of the function:
deba@1531
   533
  /// 
deba@1531
   534
  /// \code
deba@1531
   535
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
deba@1531
   536
  /// \endcode
deba@1531
   537
  /// 
deba@1531
   538
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   539
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   540
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   541
  /// edges.
deba@1531
   542
  template <typename Target, typename Source>
deba@1531
   543
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   544
    return GraphCopy<Target, Source>(target, source);
deba@1531
   545
  }
klao@946
   546
deba@1720
   547
  /// \brief Class to copy an undirected graph.
deba@1720
   548
  ///
deba@1720
   549
  /// Class to copy an undirected graph to an other graph (duplicate a graph).
deba@1720
   550
  /// The simplest way of using it is through the \c copyUndirGraph() function.
deba@1720
   551
  template <typename Target, typename Source>
deba@1720
   552
  class UndirGraphCopy {
deba@1720
   553
  public: 
deba@1720
   554
    typedef typename Source::Node Node;
deba@1720
   555
    typedef typename Source::NodeIt NodeIt;
deba@1720
   556
    typedef typename Source::Edge Edge;
deba@1720
   557
    typedef typename Source::EdgeIt EdgeIt;
deba@1720
   558
    typedef typename Source::UndirEdge UndirEdge;
deba@1720
   559
    typedef typename Source::UndirEdgeIt UndirEdgeIt;
deba@1720
   560
deba@1720
   561
    typedef typename Source::
deba@1720
   562
    template NodeMap<typename Target::Node> NodeRefMap;
deba@1720
   563
    
deba@1720
   564
    typedef typename Source::
deba@1720
   565
    template UndirEdgeMap<typename Target::UndirEdge> UndirEdgeRefMap;
deba@1720
   566
deba@1720
   567
  private:
deba@1720
   568
deba@1720
   569
    struct EdgeRefMap {
deba@1720
   570
      EdgeRefMap(UndirGraphCopy& _gc) : gc(_gc) {}
deba@1720
   571
      typedef typename Source::Edge Key;
deba@1720
   572
      typedef typename Target::Edge Value;
deba@1720
   573
deba@1720
   574
      Value operator[](const Key& key) {
deba@1720
   575
	return gc.target.direct(gc.undirEdgeRef[key], 
deba@1720
   576
				gc.target.direction(key));
deba@1720
   577
      }
deba@1720
   578
      
deba@1720
   579
      UndirGraphCopy& gc;
deba@1720
   580
    };
deba@1720
   581
    
deba@1192
   582
  public:
deba@1720
   583
deba@1720
   584
    /// \brief Constructor for the UndirGraphCopy.
deba@1720
   585
    ///
deba@1720
   586
    /// It copies the content of the \c _source graph into the
deba@1720
   587
    /// \c _target graph. It creates also two references, one beetween
deba@1720
   588
    /// the two nodeset and one beetween the two edgesets.
deba@1720
   589
    UndirGraphCopy(Target& _target, const Source& _source) 
deba@1720
   590
      : source(_source), target(_target), 
deba@1720
   591
	nodeRefMap(_source), edgeRefMap(*this), undirEdgeRefMap(_source) {
deba@1720
   592
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   593
	nodeRefMap[it] = target.addNode();
deba@1720
   594
      }
deba@1720
   595
      for (UndirEdgeIt it(source); it != INVALID; ++it) {
deba@1720
   596
	undirEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1720
   597
					nodeRefMap[source.target(it)]);
deba@1720
   598
      }
deba@1720
   599
    }
deba@1720
   600
deba@1720
   601
    /// \brief Copies the node references into the given map.
deba@1720
   602
    ///
deba@1720
   603
    /// Copies the node references into the given map.
deba@1720
   604
    template <typename NodeRef>
deba@1720
   605
    const UndirGraphCopy& nodeRef(NodeRef& map) const {
deba@1720
   606
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   607
	map.set(it, nodeRefMap[it]);
deba@1720
   608
      }
deba@1720
   609
      return *this;
deba@1720
   610
    }
deba@1720
   611
deba@1720
   612
    /// \brief Reverse and copies the node references into the given map.
deba@1720
   613
    ///
deba@1720
   614
    /// Reverse and copies the node references into the given map.
deba@1720
   615
    template <typename NodeRef>
deba@1720
   616
    const UndirGraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1720
   617
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   618
	map.set(nodeRefMap[it], it);
deba@1720
   619
      }
deba@1720
   620
      return *this;
deba@1720
   621
    }
deba@1720
   622
deba@1720
   623
    /// \brief Copies the edge references into the given map.
deba@1720
   624
    ///
deba@1720
   625
    /// Copies the edge references into the given map.
deba@1720
   626
    template <typename EdgeRef>
deba@1720
   627
    const UndirGraphCopy& edgeRef(EdgeRef& map) const {
deba@1720
   628
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   629
	map.set(edgeRefMap[it], it);
deba@1720
   630
      }
deba@1720
   631
      return *this;
deba@1720
   632
    }
deba@1720
   633
deba@1720
   634
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   635
    /// given map.
deba@1720
   636
    ///
deba@1720
   637
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   638
    template <typename EdgeRef>
deba@1720
   639
    const UndirGraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1720
   640
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   641
	map.set(it, edgeRefMap[it]);
deba@1720
   642
      }
deba@1720
   643
      return *this;
deba@1720
   644
    }
deba@1720
   645
deba@1720
   646
    /// \brief Copies the undirected edge references into the given map.
deba@1720
   647
    ///
deba@1720
   648
    /// Copies the undirected edge references into the given map.
deba@1720
   649
    template <typename EdgeRef>
deba@1720
   650
    const UndirGraphCopy& undirEdgeRef(EdgeRef& map) const {
deba@1720
   651
      for (UndirEdgeIt it(source); it != INVALID; ++it) {
deba@1720
   652
	map.set(it, undirEdgeRefMap[it]);
deba@1720
   653
      }
deba@1720
   654
      return *this;
deba@1720
   655
    }
deba@1720
   656
deba@1720
   657
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   658
    /// given map.
deba@1720
   659
    ///
deba@1720
   660
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   661
    template <typename EdgeRef>
deba@1720
   662
    const UndirGraphCopy& undirEdgeCrossRef(EdgeRef& map) const {
deba@1720
   663
      for (UndirEdgeIt it(source); it != INVALID; ++it) {
deba@1720
   664
	map.set(undirEdgeRefMap[it], it);
deba@1720
   665
      }
deba@1720
   666
      return *this;
deba@1720
   667
    }
deba@1720
   668
deba@1720
   669
    /// \brief Make copy of the given map.
deba@1720
   670
    ///
deba@1720
   671
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   672
    /// The new map's key type is the target graph's node type,
deba@1720
   673
    /// and the copied map's key type is the source graph's node
deba@1720
   674
    /// type.  
deba@1720
   675
    template <typename TargetMap, typename SourceMap>
deba@1720
   676
    const UndirGraphCopy& nodeMap(TargetMap& tMap, 
deba@1720
   677
				  const SourceMap& sMap) const {
deba@1720
   678
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1720
   679
      return *this;
deba@1720
   680
    }
deba@1720
   681
deba@1720
   682
    /// \brief Make copy of the given map.
deba@1720
   683
    ///
deba@1720
   684
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   685
    /// The new map's key type is the target graph's edge type,
deba@1720
   686
    /// and the copied map's key type is the source graph's edge
deba@1720
   687
    /// type.  
deba@1720
   688
    template <typename TargetMap, typename SourceMap>
deba@1720
   689
    const UndirGraphCopy& edgeMap(TargetMap& tMap, 
deba@1720
   690
				  const SourceMap& sMap) const {
deba@1720
   691
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1720
   692
      return *this;
deba@1720
   693
    }
deba@1720
   694
deba@1720
   695
    /// \brief Make copy of the given map.
deba@1720
   696
    ///
deba@1720
   697
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   698
    /// The new map's key type is the target graph's edge type,
deba@1720
   699
    /// and the copied map's key type is the source graph's edge
deba@1720
   700
    /// type.  
deba@1720
   701
    template <typename TargetMap, typename SourceMap>
deba@1720
   702
    const UndirGraphCopy& undirEdgeMap(TargetMap& tMap, 
deba@1720
   703
				  const SourceMap& sMap) const {
deba@1720
   704
      copyMap(tMap, sMap, UndirEdgeIt(source), undirEdgeRefMap);
deba@1720
   705
      return *this;
deba@1720
   706
    }
deba@1720
   707
deba@1720
   708
    /// \brief Gives back the stored node references.
deba@1720
   709
    ///
deba@1720
   710
    /// Gives back the stored node references.
deba@1720
   711
    const NodeRefMap& nodeRef() const {
deba@1720
   712
      return nodeRefMap;
deba@1720
   713
    }
deba@1720
   714
deba@1720
   715
    /// \brief Gives back the stored edge references.
deba@1720
   716
    ///
deba@1720
   717
    /// Gives back the stored edge references.
deba@1720
   718
    const EdgeRefMap& edgeRef() const {
deba@1720
   719
      return edgeRefMap;
deba@1720
   720
    }
deba@1720
   721
deba@1720
   722
    /// \brief Gives back the stored undir edge references.
deba@1720
   723
    ///
deba@1720
   724
    /// Gives back the stored undir edge references.
deba@1720
   725
    const UndirEdgeRefMap& undirEdgeRef() const {
deba@1720
   726
      return undirEdgeRefMap;
deba@1720
   727
    }
deba@1720
   728
deba@1720
   729
    void run() {}
deba@1720
   730
deba@1720
   731
  private:
deba@1192
   732
    
deba@1720
   733
    const Source& source;
deba@1720
   734
    Target& target;
alpar@947
   735
deba@1720
   736
    NodeRefMap nodeRefMap;
deba@1720
   737
    EdgeRefMap edgeRefMap;
deba@1720
   738
    UndirEdgeRefMap undirEdgeRefMap;
deba@1192
   739
  };
deba@1192
   740
deba@1720
   741
  /// \brief Copy a graph to an other graph.
deba@1720
   742
  ///
deba@1720
   743
  /// Copy a graph to an other graph.
deba@1720
   744
  /// The usage of the function:
deba@1720
   745
  /// 
deba@1720
   746
  /// \code
deba@1720
   747
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
deba@1720
   748
  /// \endcode
deba@1720
   749
  /// 
deba@1720
   750
  /// After the copy the \c nr map will contain the mapping from the
deba@1720
   751
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@1720
   752
  /// contain the mapping from the target graph's edges to the source's
deba@1720
   753
  /// edges.
deba@1720
   754
  template <typename Target, typename Source>
deba@1720
   755
  UndirGraphCopy<Target, Source> 
deba@1720
   756
  copyUndirGraph(Target& target, const Source& source) {
deba@1720
   757
    return UndirGraphCopy<Target, Source>(target, source);
deba@1720
   758
  }
deba@1192
   759
deba@1192
   760
deba@1192
   761
  /// @}
alpar@1402
   762
alpar@1402
   763
  /// \addtogroup graph_maps
alpar@1402
   764
  /// @{
alpar@1402
   765
deba@1413
   766
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   767
athos@1540
   768
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
   769
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
   770
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
   771
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
   772
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
   773
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
   774
  /// class \c InverseMap.
deba@1413
   775
  ///
deba@1413
   776
  template <typename _Graph, typename _Item>
deba@1413
   777
  class IdMap {
deba@1413
   778
  public:
deba@1413
   779
    typedef _Graph Graph;
deba@1413
   780
    typedef int Value;
deba@1413
   781
    typedef _Item Item;
deba@1413
   782
    typedef _Item Key;
deba@1413
   783
deba@1413
   784
    /// \brief Constructor.
deba@1413
   785
    ///
deba@1413
   786
    /// Constructor for creating id map.
deba@1413
   787
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   788
deba@1413
   789
    /// \brief Gives back the \e id of the item.
deba@1413
   790
    ///
deba@1413
   791
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   792
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   793
deba@1413
   794
deba@1413
   795
  private:
deba@1413
   796
    const Graph* graph;
deba@1413
   797
deba@1413
   798
  public:
deba@1413
   799
athos@1540
   800
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
   801
    ///
athos@1540
   802
    /// The class represents the inverse of its owner (IdMap).
deba@1413
   803
    /// \see inverse()
deba@1413
   804
    class InverseMap {
deba@1413
   805
    public:
deba@1419
   806
deba@1413
   807
      /// \brief Constructor.
deba@1413
   808
      ///
deba@1413
   809
      /// Constructor for creating an id-to-item map.
deba@1413
   810
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   811
deba@1413
   812
      /// \brief Constructor.
deba@1413
   813
      ///
deba@1413
   814
      /// Constructor for creating an id-to-item map.
deba@1413
   815
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   816
deba@1413
   817
      /// \brief Gives back the given item from its id.
deba@1413
   818
      ///
deba@1413
   819
      /// Gives back the given item from its id.
deba@1413
   820
      /// 
deba@1413
   821
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   822
    private:
deba@1413
   823
      const Graph* graph;
deba@1413
   824
    };
deba@1413
   825
deba@1413
   826
    /// \brief Gives back the inverse of the map.
deba@1413
   827
    ///
athos@1540
   828
    /// Gives back the inverse of the IdMap.
deba@1413
   829
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   830
deba@1413
   831
  };
deba@1413
   832
deba@1413
   833
  
athos@1526
   834
  /// \brief General invertable graph-map type.
alpar@1402
   835
athos@1540
   836
  /// This type provides simple invertable graph-maps. 
athos@1526
   837
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
   838
  /// and if a key is set to a new value then store it
alpar@1402
   839
  /// in the inverse map.
alpar@1402
   840
  /// \param _Graph The graph type.
athos@1526
   841
  /// \param _Map The map to extend with invertable functionality. 
alpar@1402
   842
  template <
alpar@1402
   843
    typename _Graph,
alpar@1402
   844
    typename _Item, 
alpar@1402
   845
    typename _Value,
alpar@1402
   846
    typename _Map 
deba@1413
   847
    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
alpar@1402
   848
  >
deba@1413
   849
  class InvertableMap : protected _Map {
alpar@1402
   850
alpar@1402
   851
  public:
alpar@1402
   852
 
alpar@1402
   853
    typedef _Map Map;
alpar@1402
   854
    typedef _Graph Graph;
deba@1413
   855
deba@1413
   856
    /// The key type of InvertableMap (Node, Edge, UndirEdge).
alpar@1402
   857
    typedef typename _Map::Key Key;
deba@1413
   858
    /// The value type of the InvertableMap.
alpar@1402
   859
    typedef typename _Map::Value Value;
alpar@1402
   860
alpar@1402
   861
    /// \brief Constructor.
alpar@1402
   862
    ///
deba@1413
   863
    /// Construct a new InvertableMap for the graph.
alpar@1402
   864
    ///
deba@1413
   865
    InvertableMap(const Graph& graph) : Map(graph) {} 
alpar@1402
   866
    
alpar@1402
   867
    /// \brief The setter function of the map.
alpar@1402
   868
    ///
deba@1413
   869
    /// Sets the mapped value.
alpar@1402
   870
    void set(const Key& key, const Value& val) {
alpar@1402
   871
      Value oldval = Map::operator[](key);
deba@1413
   872
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
   873
      if (it != invMap.end() && it->second == key) {
alpar@1402
   874
	invMap.erase(it);
alpar@1402
   875
      }      
alpar@1402
   876
      invMap.insert(make_pair(val, key));
alpar@1402
   877
      Map::set(key, val);
alpar@1402
   878
    }
alpar@1402
   879
alpar@1402
   880
    /// \brief The getter function of the map.
alpar@1402
   881
    ///
alpar@1402
   882
    /// It gives back the value associated with the key.
deba@1720
   883
    Value operator[](const Key& key) const {
alpar@1402
   884
      return Map::operator[](key);
alpar@1402
   885
    }
alpar@1402
   886
deba@1515
   887
  protected:
deba@1515
   888
alpar@1402
   889
    /// \brief Add a new key to the map.
alpar@1402
   890
    ///
alpar@1402
   891
    /// Add a new key to the map. It is called by the
alpar@1402
   892
    /// \c AlterationNotifier.
alpar@1402
   893
    virtual void add(const Key& key) {
alpar@1402
   894
      Map::add(key);
alpar@1402
   895
    }
alpar@1402
   896
alpar@1402
   897
    /// \brief Erase the key from the map.
alpar@1402
   898
    ///
alpar@1402
   899
    /// Erase the key to the map. It is called by the
alpar@1402
   900
    /// \c AlterationNotifier.
alpar@1402
   901
    virtual void erase(const Key& key) {
alpar@1402
   902
      Value val = Map::operator[](key);
deba@1413
   903
      typename Container::iterator it = invMap.find(val);
alpar@1402
   904
      if (it != invMap.end() && it->second == key) {
alpar@1402
   905
	invMap.erase(it);
alpar@1402
   906
      }
alpar@1402
   907
      Map::erase(key);
alpar@1402
   908
    }
alpar@1402
   909
alpar@1402
   910
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
   911
    ///
alpar@1402
   912
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
   913
    /// \c AlterationNotifier.
alpar@1402
   914
    virtual void clear() {
alpar@1402
   915
      invMap.clear();
alpar@1402
   916
      Map::clear();
alpar@1402
   917
    }
alpar@1402
   918
deba@1413
   919
  private:
deba@1413
   920
    
deba@1413
   921
    typedef std::map<Value, Key> Container;
deba@1413
   922
    Container invMap;    
deba@1413
   923
deba@1413
   924
  public:
deba@1413
   925
deba@1413
   926
    /// \brief The inverse map type.
deba@1413
   927
    ///
deba@1413
   928
    /// The inverse of this map. The subscript operator of the map
deba@1413
   929
    /// gives back always the item what was last assigned to the value. 
deba@1413
   930
    class InverseMap {
deba@1413
   931
    public:
deba@1413
   932
      /// \brief Constructor of the InverseMap.
deba@1413
   933
      ///
deba@1413
   934
      /// Constructor of the InverseMap.
deba@1413
   935
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
   936
deba@1413
   937
      /// The value type of the InverseMap.
deba@1413
   938
      typedef typename InvertableMap::Key Value;
deba@1413
   939
      /// The key type of the InverseMap.
deba@1413
   940
      typedef typename InvertableMap::Value Key; 
deba@1413
   941
deba@1413
   942
      /// \brief Subscript operator. 
deba@1413
   943
      ///
deba@1413
   944
      /// Subscript operator. It gives back always the item 
deba@1413
   945
      /// what was last assigned to the value.
deba@1413
   946
      Value operator[](const Key& key) const {
deba@1413
   947
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
   948
	return it->second;
deba@1413
   949
      }
deba@1413
   950
      
deba@1413
   951
    private:
deba@1413
   952
      const InvertableMap& inverted;
deba@1413
   953
    };
deba@1413
   954
alpar@1402
   955
    /// \brief It gives back the just readeable inverse map.
alpar@1402
   956
    ///
alpar@1402
   957
    /// It gives back the just readeable inverse map.
deba@1413
   958
    InverseMap inverse() const {
deba@1413
   959
      return InverseMap(*this);
alpar@1402
   960
    } 
alpar@1402
   961
alpar@1402
   962
deba@1413
   963
    
alpar@1402
   964
  };
alpar@1402
   965
alpar@1402
   966
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
   967
  /// item in the graph.
alpar@1402
   968
  ///
athos@1540
   969
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
   970
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
   971
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
   972
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
   973
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
   974
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
   975
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
   976
  /// with its member class \c InverseMap.
alpar@1402
   977
  ///
alpar@1402
   978
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
   979
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
alpar@1402
   980
  /// UndirEdge.
alpar@1402
   981
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
   982
  template <
alpar@1402
   983
    typename _Graph,   
alpar@1402
   984
    typename _Item,
deba@1413
   985
    typename _Map 
deba@1413
   986
    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
alpar@1402
   987
  >
alpar@1402
   988
  class DescriptorMap : protected _Map {
alpar@1402
   989
alpar@1402
   990
    typedef _Item Item;
alpar@1402
   991
    typedef _Map Map;
alpar@1402
   992
alpar@1402
   993
  public:
alpar@1402
   994
    /// The graph class of DescriptorMap.
alpar@1402
   995
    typedef _Graph Graph;
alpar@1402
   996
alpar@1402
   997
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
alpar@1402
   998
    typedef typename _Map::Key Key;
alpar@1402
   999
    /// The value type of DescriptorMap.
alpar@1402
  1000
    typedef typename _Map::Value Value;
alpar@1402
  1001
alpar@1402
  1002
    /// \brief Constructor.
alpar@1402
  1003
    ///
deba@1413
  1004
    /// Constructor for descriptor map.
alpar@1402
  1005
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
  1006
      build();
alpar@1402
  1007
    }
alpar@1402
  1008
deba@1515
  1009
  protected:
deba@1515
  1010
alpar@1402
  1011
    /// \brief Add a new key to the map.
alpar@1402
  1012
    ///
alpar@1402
  1013
    /// Add a new key to the map. It is called by the
alpar@1402
  1014
    /// \c AlterationNotifier.
alpar@1402
  1015
    virtual void add(const Item& item) {
alpar@1402
  1016
      Map::add(item);
alpar@1402
  1017
      Map::set(item, invMap.size());
alpar@1402
  1018
      invMap.push_back(item);
alpar@1402
  1019
    }
alpar@1402
  1020
alpar@1402
  1021
    /// \brief Erase the key from the map.
alpar@1402
  1022
    ///
alpar@1402
  1023
    /// Erase the key to the map. It is called by the
alpar@1402
  1024
    /// \c AlterationNotifier.
alpar@1402
  1025
    virtual void erase(const Item& item) {
alpar@1402
  1026
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1027
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1028
      invMap.pop_back();
alpar@1402
  1029
      Map::erase(item);
alpar@1402
  1030
    }
alpar@1402
  1031
alpar@1402
  1032
    /// \brief Build the unique map.
alpar@1402
  1033
    ///
alpar@1402
  1034
    /// Build the unique map. It is called by the
alpar@1402
  1035
    /// \c AlterationNotifier.
alpar@1402
  1036
    virtual void build() {
alpar@1402
  1037
      Map::build();
alpar@1402
  1038
      Item it;
alpar@1402
  1039
      const typename Map::Graph* graph = Map::getGraph(); 
alpar@1402
  1040
      for (graph->first(it); it != INVALID; graph->next(it)) {
alpar@1402
  1041
	Map::set(it, invMap.size());
alpar@1402
  1042
	invMap.push_back(it);	
alpar@1402
  1043
      }      
alpar@1402
  1044
    }
alpar@1402
  1045
    
alpar@1402
  1046
    /// \brief Clear the keys from the map.
alpar@1402
  1047
    ///
alpar@1402
  1048
    /// Clear the keys from the map. It is called by the
alpar@1402
  1049
    /// \c AlterationNotifier.
alpar@1402
  1050
    virtual void clear() {
alpar@1402
  1051
      invMap.clear();
alpar@1402
  1052
      Map::clear();
alpar@1402
  1053
    }
alpar@1402
  1054
deba@1538
  1055
  public:
deba@1538
  1056
deba@1552
  1057
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1058
    ///
deba@1552
  1059
    /// Swaps the position of the two items in the map.
deba@1552
  1060
    void swap(const Item& p, const Item& q) {
deba@1552
  1061
      int pi = Map::operator[](p);
deba@1552
  1062
      int qi = Map::operator[](q);
deba@1552
  1063
      Map::set(p, qi);
deba@1552
  1064
      invMap[qi] = p;
deba@1552
  1065
      Map::set(q, pi);
deba@1552
  1066
      invMap[pi] = q;
deba@1552
  1067
    }
deba@1552
  1068
alpar@1402
  1069
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1070
    ///
alpar@1402
  1071
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1072
    int operator[](const Item& item) const {
alpar@1402
  1073
      return Map::operator[](item);
alpar@1402
  1074
    }
alpar@1402
  1075
    
deba@1413
  1076
  private:
deba@1413
  1077
deba@1413
  1078
    typedef std::vector<Item> Container;
deba@1413
  1079
    Container invMap;
deba@1413
  1080
deba@1413
  1081
  public:
athos@1540
  1082
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  1083
    ///
athos@1540
  1084
    /// The inverse map type of DescriptorMap.
deba@1413
  1085
    class InverseMap {
deba@1413
  1086
    public:
deba@1413
  1087
      /// \brief Constructor of the InverseMap.
deba@1413
  1088
      ///
deba@1413
  1089
      /// Constructor of the InverseMap.
deba@1413
  1090
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
  1091
	: inverted(_inverted) {}
deba@1413
  1092
deba@1413
  1093
deba@1413
  1094
      /// The value type of the InverseMap.
deba@1413
  1095
      typedef typename DescriptorMap::Key Value;
deba@1413
  1096
      /// The key type of the InverseMap.
deba@1413
  1097
      typedef typename DescriptorMap::Value Key; 
deba@1413
  1098
deba@1413
  1099
      /// \brief Subscript operator. 
deba@1413
  1100
      ///
deba@1413
  1101
      /// Subscript operator. It gives back the item 
deba@1413
  1102
      /// that the descriptor belongs to currently.
deba@1413
  1103
      Value operator[](const Key& key) const {
deba@1413
  1104
	return inverted.invMap[key];
deba@1413
  1105
      }
deba@1470
  1106
deba@1470
  1107
      /// \brief Size of the map.
deba@1470
  1108
      ///
deba@1470
  1109
      /// Returns the size of the map.
deba@1552
  1110
      int size() const {
deba@1470
  1111
	return inverted.invMap.size();
deba@1470
  1112
      }
deba@1413
  1113
      
deba@1413
  1114
    private:
deba@1413
  1115
      const DescriptorMap& inverted;
deba@1413
  1116
    };
deba@1413
  1117
alpar@1402
  1118
    /// \brief Gives back the inverse of the map.
alpar@1402
  1119
    ///
alpar@1402
  1120
    /// Gives back the inverse of the map.
alpar@1402
  1121
    const InverseMap inverse() const {
deba@1413
  1122
      return InverseMap(*this);
alpar@1402
  1123
    }
alpar@1402
  1124
  };
alpar@1402
  1125
alpar@1402
  1126
  /// \brief Returns the source of the given edge.
alpar@1402
  1127
  ///
alpar@1402
  1128
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
  1129
  /// \author Balazs Dezso
alpar@1402
  1130
  template <typename Graph>
alpar@1402
  1131
  class SourceMap {
alpar@1402
  1132
  public:
deba@1419
  1133
alpar@1402
  1134
    typedef typename Graph::Node Value;
alpar@1402
  1135
    typedef typename Graph::Edge Key;
alpar@1402
  1136
alpar@1402
  1137
    /// \brief Constructor
alpar@1402
  1138
    ///
alpar@1402
  1139
    /// Constructor
alpar@1402
  1140
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1141
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1142
alpar@1402
  1143
    /// \brief The subscript operator.
alpar@1402
  1144
    ///
alpar@1402
  1145
    /// The subscript operator.
alpar@1402
  1146
    /// \param edge The edge 
alpar@1402
  1147
    /// \return The source of the edge 
deba@1679
  1148
    Value operator[](const Key& edge) const {
alpar@1402
  1149
      return graph.source(edge);
alpar@1402
  1150
    }
alpar@1402
  1151
alpar@1402
  1152
  private:
alpar@1402
  1153
    const Graph& graph;
alpar@1402
  1154
  };
alpar@1402
  1155
alpar@1402
  1156
  /// \brief Returns a \ref SourceMap class
alpar@1402
  1157
  ///
alpar@1402
  1158
  /// This function just returns an \ref SourceMap class.
alpar@1402
  1159
  /// \relates SourceMap
alpar@1402
  1160
  template <typename Graph>
alpar@1402
  1161
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  1162
    return SourceMap<Graph>(graph);
alpar@1402
  1163
  } 
alpar@1402
  1164
alpar@1402
  1165
  /// \brief Returns the target of the given edge.
alpar@1402
  1166
  ///
alpar@1402
  1167
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
  1168
  /// \author Balazs Dezso
alpar@1402
  1169
  template <typename Graph>
alpar@1402
  1170
  class TargetMap {
alpar@1402
  1171
  public:
deba@1419
  1172
alpar@1402
  1173
    typedef typename Graph::Node Value;
alpar@1402
  1174
    typedef typename Graph::Edge Key;
alpar@1402
  1175
alpar@1402
  1176
    /// \brief Constructor
alpar@1402
  1177
    ///
alpar@1402
  1178
    /// Constructor
alpar@1402
  1179
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1180
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1181
alpar@1402
  1182
    /// \brief The subscript operator.
alpar@1402
  1183
    ///
alpar@1402
  1184
    /// The subscript operator.
alpar@1536
  1185
    /// \param e The edge 
alpar@1402
  1186
    /// \return The target of the edge 
deba@1679
  1187
    Value operator[](const Key& e) const {
alpar@1536
  1188
      return graph.target(e);
alpar@1402
  1189
    }
alpar@1402
  1190
alpar@1402
  1191
  private:
alpar@1402
  1192
    const Graph& graph;
alpar@1402
  1193
  };
alpar@1402
  1194
alpar@1402
  1195
  /// \brief Returns a \ref TargetMap class
deba@1515
  1196
  ///
athos@1540
  1197
  /// This function just returns a \ref TargetMap class.
alpar@1402
  1198
  /// \relates TargetMap
alpar@1402
  1199
  template <typename Graph>
alpar@1402
  1200
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  1201
    return TargetMap<Graph>(graph);
alpar@1402
  1202
  }
alpar@1402
  1203
athos@1540
  1204
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1205
  ///
athos@1540
  1206
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1207
  /// \author Balazs Dezso
deba@1419
  1208
  template <typename Graph>
deba@1419
  1209
  class ForwardMap {
deba@1419
  1210
  public:
deba@1419
  1211
deba@1419
  1212
    typedef typename Graph::Edge Value;
deba@1419
  1213
    typedef typename Graph::UndirEdge Key;
deba@1419
  1214
deba@1419
  1215
    /// \brief Constructor
deba@1419
  1216
    ///
deba@1419
  1217
    /// Constructor
deba@1419
  1218
    /// \param _graph The graph that the map belongs to.
deba@1419
  1219
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1220
deba@1419
  1221
    /// \brief The subscript operator.
deba@1419
  1222
    ///
deba@1419
  1223
    /// The subscript operator.
deba@1419
  1224
    /// \param key An undirected edge 
deba@1419
  1225
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  1226
    Value operator[](const Key& key) const {
deba@1627
  1227
      return graph.direct(key, true);
deba@1419
  1228
    }
deba@1419
  1229
deba@1419
  1230
  private:
deba@1419
  1231
    const Graph& graph;
deba@1419
  1232
  };
deba@1419
  1233
deba@1419
  1234
  /// \brief Returns a \ref ForwardMap class
deba@1515
  1235
  ///
deba@1419
  1236
  /// This function just returns an \ref ForwardMap class.
deba@1419
  1237
  /// \relates ForwardMap
deba@1419
  1238
  template <typename Graph>
deba@1419
  1239
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  1240
    return ForwardMap<Graph>(graph);
deba@1419
  1241
  }
deba@1419
  1242
athos@1540
  1243
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1244
  ///
athos@1540
  1245
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1246
  /// \author Balazs Dezso
deba@1419
  1247
  template <typename Graph>
deba@1419
  1248
  class BackwardMap {
deba@1419
  1249
  public:
deba@1419
  1250
deba@1419
  1251
    typedef typename Graph::Edge Value;
deba@1419
  1252
    typedef typename Graph::UndirEdge Key;
deba@1419
  1253
deba@1419
  1254
    /// \brief Constructor
deba@1419
  1255
    ///
deba@1419
  1256
    /// Constructor
deba@1419
  1257
    /// \param _graph The graph that the map belongs to.
deba@1419
  1258
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1259
deba@1419
  1260
    /// \brief The subscript operator.
deba@1419
  1261
    ///
deba@1419
  1262
    /// The subscript operator.
deba@1419
  1263
    /// \param key An undirected edge 
deba@1419
  1264
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  1265
    Value operator[](const Key& key) const {
deba@1627
  1266
      return graph.direct(key, false);
deba@1419
  1267
    }
deba@1419
  1268
deba@1419
  1269
  private:
deba@1419
  1270
    const Graph& graph;
deba@1419
  1271
  };
deba@1419
  1272
deba@1419
  1273
  /// \brief Returns a \ref BackwardMap class
deba@1419
  1274
athos@1540
  1275
  /// This function just returns a \ref BackwardMap class.
deba@1419
  1276
  /// \relates BackwardMap
deba@1419
  1277
  template <typename Graph>
deba@1419
  1278
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  1279
    return BackwardMap<Graph>(graph);
deba@1419
  1280
  }
deba@1419
  1281
deba@1695
  1282
  /// \brief Potential difference map
deba@1695
  1283
  ///
deba@1695
  1284
  /// If there is an potential map on the nodes then we
deba@1695
  1285
  /// can get an edge map as we get the substraction of the
deba@1695
  1286
  /// values of the target and source.
deba@1695
  1287
  template <typename Graph, typename NodeMap>
deba@1695
  1288
  class PotentialDifferenceMap {
deba@1515
  1289
  public:
deba@1695
  1290
    typedef typename Graph::Edge Key;
deba@1695
  1291
    typedef typename NodeMap::Value Value;
deba@1695
  1292
deba@1695
  1293
    /// \brief Constructor
deba@1695
  1294
    ///
deba@1695
  1295
    /// Contructor of the map
deba@1695
  1296
    PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) 
deba@1695
  1297
      : graph(_graph), potential(_potential) {}
deba@1695
  1298
deba@1695
  1299
    /// \brief Const subscription operator
deba@1695
  1300
    ///
deba@1695
  1301
    /// Const subscription operator
deba@1695
  1302
    Value operator[](const Key& edge) const {
deba@1695
  1303
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  1304
    }
deba@1695
  1305
deba@1695
  1306
  private:
deba@1695
  1307
    const Graph& graph;
deba@1695
  1308
    const NodeMap& potential;
deba@1695
  1309
  };
deba@1695
  1310
deba@1695
  1311
  /// \brief Just returns a PotentialDifferenceMap
deba@1695
  1312
  ///
deba@1695
  1313
  /// Just returns a PotentialDifferenceMap
deba@1695
  1314
  /// \relates PotentialDifferenceMap
deba@1695
  1315
  template <typename Graph, typename NodeMap>
deba@1695
  1316
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  1317
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  1318
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  1319
  }
deba@1695
  1320
deba@1515
  1321
  /// \brief Map of the node in-degrees.
alpar@1453
  1322
  ///
athos@1540
  1323
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  1324
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1325
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1326
  /// whenever the graph changes.
deba@1515
  1327
  ///
deba@1729
  1328
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1329
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1729
  1330
  /// is not guarantied if these additional featureas are used. For example
deba@1729
  1331
  /// the funstions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1332
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1333
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1334
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1335
  ///
deba@1515
  1336
  /// \sa OutDegMap
deba@1515
  1337
alpar@1453
  1338
  template <typename _Graph>
deba@1515
  1339
  class InDegMap  
deba@1515
  1340
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1341
alpar@1453
  1342
  public:
deba@1515
  1343
    
deba@1515
  1344
    typedef _Graph Graph;
alpar@1453
  1345
    typedef int Value;
deba@1515
  1346
    typedef typename Graph::Node Key;
deba@1515
  1347
deba@1515
  1348
  private:
deba@1515
  1349
deba@1515
  1350
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1351
    public:
deba@1515
  1352
deba@1515
  1353
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1354
deba@1515
  1355
      typedef typename Parent::Key Key;
deba@1515
  1356
      typedef typename Parent::Value Value;
deba@1515
  1357
      
deba@1515
  1358
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1359
      
deba@1515
  1360
      void add(const Key& key) {
deba@1515
  1361
	Parent::add(key);
deba@1515
  1362
	Parent::set(key, 0);
deba@1515
  1363
      }
deba@1515
  1364
    };
deba@1515
  1365
deba@1515
  1366
  public:
alpar@1453
  1367
alpar@1453
  1368
    /// \brief Constructor.
alpar@1453
  1369
    ///
alpar@1453
  1370
    /// Constructor for creating in-degree map.
deba@1515
  1371
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1372
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1373
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1374
      
deba@1515
  1375
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1376
	deg[it] = countInEdges(graph, it);
deba@1515
  1377
      }
alpar@1453
  1378
    }
alpar@1453
  1379
deba@1515
  1380
    virtual ~InDegMap() {
alpar@1459
  1381
      AlterationNotifier<typename _Graph::Edge>::
alpar@1453
  1382
	ObserverBase::detach();
alpar@1453
  1383
    }
alpar@1453
  1384
    
alpar@1459
  1385
    /// Gives back the in-degree of a Node.
deba@1515
  1386
    int operator[](const Key& key) const {
deba@1515
  1387
      return deg[key];
alpar@1459
  1388
    }
alpar@1453
  1389
alpar@1453
  1390
  protected:
deba@1515
  1391
    
deba@1515
  1392
    typedef typename Graph::Edge Edge;
deba@1515
  1393
deba@1515
  1394
    virtual void add(const Edge& edge) {
deba@1515
  1395
      ++deg[graph.target(edge)];
alpar@1453
  1396
    }
alpar@1453
  1397
deba@1515
  1398
    virtual void erase(const Edge& edge) {
deba@1515
  1399
      --deg[graph.target(edge)];
deba@1515
  1400
    }
deba@1515
  1401
deba@1720
  1402
    virtual void signalChange(const Edge& edge) {
deba@1720
  1403
      erase(edge);
deba@1720
  1404
    }
deba@1720
  1405
deba@1720
  1406
    virtual void commitChange(const Edge& edge) {
deba@1720
  1407
      add(edge);
deba@1720
  1408
    }
deba@1515
  1409
deba@1515
  1410
    virtual void build() {
deba@1515
  1411
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1412
	deg[it] = countInEdges(graph, it);
deba@1515
  1413
      }      
deba@1515
  1414
    }
deba@1515
  1415
deba@1515
  1416
    virtual void clear() {
deba@1515
  1417
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1418
	deg[it] = 0;
deba@1515
  1419
      }
deba@1515
  1420
    }
deba@1515
  1421
  private:
alpar@1506
  1422
    
deba@1515
  1423
    const _Graph& graph;
deba@1515
  1424
    AutoNodeMap deg;
alpar@1459
  1425
  };
alpar@1459
  1426
deba@1515
  1427
  /// \brief Map of the node out-degrees.
deba@1515
  1428
  ///
athos@1540
  1429
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1430
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1431
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1432
  /// whenever the graph changes.
deba@1515
  1433
  ///
deba@1729
  1434
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1435
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1729
  1436
  /// is not guarantied if these additional featureas are used. For example
deba@1729
  1437
  /// the funstions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1438
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1439
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1440
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1441
  ///
alpar@1555
  1442
  /// \sa InDegMap
alpar@1459
  1443
alpar@1459
  1444
  template <typename _Graph>
deba@1515
  1445
  class OutDegMap  
deba@1515
  1446
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1447
alpar@1459
  1448
  public:
deba@1515
  1449
    
deba@1515
  1450
    typedef _Graph Graph;
alpar@1459
  1451
    typedef int Value;
deba@1515
  1452
    typedef typename Graph::Node Key;
deba@1515
  1453
deba@1515
  1454
  private:
deba@1515
  1455
deba@1515
  1456
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1457
    public:
deba@1515
  1458
deba@1515
  1459
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1460
deba@1515
  1461
      typedef typename Parent::Key Key;
deba@1515
  1462
      typedef typename Parent::Value Value;
deba@1515
  1463
      
deba@1515
  1464
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1465
      
deba@1515
  1466
      void add(const Key& key) {
deba@1515
  1467
	Parent::add(key);
deba@1515
  1468
	Parent::set(key, 0);
deba@1515
  1469
      }
deba@1515
  1470
    };
deba@1515
  1471
deba@1515
  1472
  public:
alpar@1459
  1473
alpar@1459
  1474
    /// \brief Constructor.
alpar@1459
  1475
    ///
alpar@1459
  1476
    /// Constructor for creating out-degree map.
deba@1515
  1477
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1478
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1479
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1480
      
deba@1515
  1481
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1482
	deg[it] = countOutEdges(graph, it);
deba@1515
  1483
      }
alpar@1459
  1484
    }
alpar@1459
  1485
deba@1515
  1486
    virtual ~OutDegMap() {
alpar@1459
  1487
      AlterationNotifier<typename _Graph::Edge>::
alpar@1459
  1488
	ObserverBase::detach();
alpar@1459
  1489
    }
alpar@1459
  1490
    
alpar@1459
  1491
    /// Gives back the in-degree of a Node.
deba@1515
  1492
    int operator[](const Key& key) const {
deba@1515
  1493
      return deg[key];
alpar@1459
  1494
    }
alpar@1459
  1495
alpar@1459
  1496
  protected:
deba@1515
  1497
    
deba@1515
  1498
    typedef typename Graph::Edge Edge;
deba@1515
  1499
deba@1515
  1500
    virtual void add(const Edge& edge) {
deba@1515
  1501
      ++deg[graph.source(edge)];
alpar@1459
  1502
    }
alpar@1459
  1503
deba@1515
  1504
    virtual void erase(const Edge& edge) {
deba@1515
  1505
      --deg[graph.source(edge)];
deba@1515
  1506
    }
deba@1515
  1507
deba@1720
  1508
    virtual void signalChange(const Edge& edge) {
deba@1720
  1509
      erase(edge);
deba@1720
  1510
    }
deba@1720
  1511
deba@1720
  1512
    virtual void commitChange(const Edge& edge) {
deba@1720
  1513
      add(edge);
deba@1720
  1514
    }
deba@1720
  1515
deba@1515
  1516
deba@1515
  1517
    virtual void build() {
deba@1515
  1518
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1519
	deg[it] = countOutEdges(graph, it);
deba@1515
  1520
      }      
deba@1515
  1521
    }
deba@1515
  1522
deba@1515
  1523
    virtual void clear() {
deba@1515
  1524
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1525
	deg[it] = 0;
deba@1515
  1526
      }
deba@1515
  1527
    }
deba@1515
  1528
  private:
alpar@1506
  1529
    
deba@1515
  1530
    const _Graph& graph;
deba@1515
  1531
    AutoNodeMap deg;
alpar@1453
  1532
  };
alpar@1453
  1533
deba@1695
  1534
alpar@1402
  1535
  /// @}
alpar@1402
  1536
alpar@947
  1537
} //END OF NAMESPACE LEMON
klao@946
  1538
klao@946
  1539
#endif