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