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