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