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