lemon/graph_utils.h
author alpar
Tue, 12 Jul 2005 16:16:19 +0000
changeset 1547 dd57a540ff5f
parent 1540 7d028a73d7f2
child 1552 5c7f270f8e25
permissions -rw-r--r--
Improve doc
klao@946
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
klao@946
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     6
 *
klao@946
     7
 * Permission to use, modify and distribute this software is granted
klao@946
     8
 * provided that this copyright notice appears in all copies. For
klao@946
     9
 * precise terms see the accompanying LICENSE file.
klao@946
    10
 *
klao@946
    11
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    12
 * express or implied, and with no claim as to its suitability for any
klao@946
    13
 * purpose.
klao@946
    14
 *
klao@946
    15
 */
klao@946
    16
klao@946
    17
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    18
#define LEMON_GRAPH_UTILS_H
klao@946
    19
klao@946
    20
#include <iterator>
deba@1419
    21
#include <vector>
alpar@1402
    22
#include <map>
klao@946
    23
klao@946
    24
#include <lemon/invalid.h>
klao@977
    25
#include <lemon/utility.h>
deba@1413
    26
#include <lemon/maps.h>
alpar@1459
    27
#include <lemon/bits/alteration_notifier.h>
klao@946
    28
alpar@947
    29
///\ingroup gutils
klao@946
    30
///\file
alpar@947
    31
///\brief Graph utilities.
klao@946
    32
///
alpar@964
    33
///
klao@946
    34
klao@946
    35
klao@946
    36
namespace lemon {
klao@946
    37
deba@1267
    38
  /// \addtogroup gutils
deba@1267
    39
  /// @{
alpar@947
    40
klao@946
    41
  /// \brief Function to count the items in the graph.
klao@946
    42
  ///
athos@1540
    43
  /// This function counts the items (nodes, edges etc) in the graph.
klao@946
    44
  /// The complexity of the function is O(n) because
klao@946
    45
  /// it iterates on all of the items.
klao@946
    46
klao@946
    47
  template <typename Graph, typename ItemIt>
klao@977
    48
  inline int countItems(const Graph& g) {
klao@946
    49
    int num = 0;
klao@977
    50
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
    51
      ++num;
klao@946
    52
    }
klao@946
    53
    return num;
klao@946
    54
  }
klao@946
    55
klao@977
    56
  // Node counting:
klao@977
    57
klao@977
    58
  template <typename Graph>
klao@977
    59
  inline
klao@977
    60
  typename enable_if<typename Graph::NodeNumTag, int>::type
klao@977
    61
  _countNodes(const Graph &g) {
klao@977
    62
    return g.nodeNum();
klao@977
    63
  }
klao@977
    64
klao@977
    65
  template <typename Graph>
klao@977
    66
  inline int _countNodes(Wrap<Graph> w) {
klao@977
    67
    return countItems<Graph, typename Graph::NodeIt>(w.value);
klao@977
    68
  }
klao@977
    69
klao@946
    70
  /// \brief Function to count the nodes in the graph.
klao@946
    71
  ///
klao@946
    72
  /// This function counts the nodes in the graph.
klao@946
    73
  /// The complexity of the function is O(n) but for some
athos@1526
    74
  /// graph structures it is specialized to run in O(1).
klao@977
    75
  ///
klao@977
    76
  /// \todo refer how to specialize it
klao@946
    77
klao@946
    78
  template <typename Graph>
klao@977
    79
  inline int countNodes(const Graph& g) {
klao@977
    80
    return _countNodes<Graph>(g);
klao@977
    81
  }
klao@977
    82
klao@977
    83
  // Edge counting:
klao@977
    84
klao@977
    85
  template <typename Graph>
klao@977
    86
  inline
klao@977
    87
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@977
    88
  _countEdges(const Graph &g) {
klao@977
    89
    return g.edgeNum();
klao@977
    90
  }
klao@977
    91
klao@977
    92
  template <typename Graph>
klao@977
    93
  inline int _countEdges(Wrap<Graph> w) {
klao@977
    94
    return countItems<Graph, typename Graph::EdgeIt>(w.value);
klao@946
    95
  }
klao@946
    96
klao@946
    97
  /// \brief Function to count the edges in the graph.
klao@946
    98
  ///
klao@946
    99
  /// This function counts the edges in the graph.
klao@946
   100
  /// The complexity of the function is O(e) but for some
athos@1526
   101
  /// graph structures it is specialized to run in O(1).
klao@977
   102
klao@946
   103
  template <typename Graph>
klao@977
   104
  inline int countEdges(const Graph& g) {
klao@977
   105
    return _countEdges<Graph>(g);
klao@946
   106
  }
klao@946
   107
klao@1053
   108
  // Undirected edge counting:
klao@1053
   109
klao@1053
   110
  template <typename Graph>
klao@1053
   111
  inline
klao@1053
   112
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@1053
   113
  _countUndirEdges(const Graph &g) {
klao@1053
   114
    return g.undirEdgeNum();
klao@1053
   115
  }
klao@1053
   116
klao@1053
   117
  template <typename Graph>
klao@1053
   118
  inline int _countUndirEdges(Wrap<Graph> w) {
klao@1053
   119
    return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
klao@1053
   120
  }
klao@1053
   121
athos@1526
   122
  /// \brief Function to count the undirected edges in the graph.
klao@946
   123
  ///
athos@1526
   124
  /// This function counts the undirected edges in the graph.
klao@946
   125
  /// The complexity of the function is O(e) but for some
athos@1540
   126
  /// graph structures it is specialized to run in O(1).
klao@1053
   127
klao@946
   128
  template <typename Graph>
klao@1053
   129
  inline int countUndirEdges(const Graph& g) {
klao@1053
   130
    return _countUndirEdges<Graph>(g);
klao@946
   131
  }
klao@946
   132
klao@977
   133
klao@1053
   134
klao@946
   135
  template <typename Graph, typename DegIt>
klao@946
   136
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   137
    int num = 0;
klao@946
   138
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   139
      ++num;
klao@946
   140
    }
klao@946
   141
    return num;
klao@946
   142
  }
alpar@967
   143
deba@1531
   144
  /// \brief Function to count the number of the out-edges from node \c n.
deba@1531
   145
  ///
deba@1531
   146
  /// This function counts the number of the out-edges from node \c n
deba@1531
   147
  /// in the graph.  
deba@1531
   148
  template <typename Graph>
deba@1531
   149
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   150
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
deba@1531
   151
  }
deba@1531
   152
deba@1531
   153
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1531
   154
  ///
deba@1531
   155
  /// This function counts the number of the in-edges to node \c n
deba@1531
   156
  /// in the graph.  
deba@1531
   157
  template <typename Graph>
deba@1531
   158
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   159
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
deba@1531
   160
  }
deba@1531
   161
deba@1531
   162
alpar@967
   163
  /// Finds an edge between two nodes of a graph.
alpar@967
   164
alpar@967
   165
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   166
  ///
alpar@967
   167
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   168
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   169
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   170
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   171
  ///
alpar@967
   172
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@967
   173
  /// \code
alpar@967
   174
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   175
  ///   ...
alpar@967
   176
  /// }
alpar@967
   177
  /// \endcode
alpar@1536
   178
  /// \todo We may want to use the "GraphBase"
alpar@967
   179
  /// interface here...
alpar@967
   180
  /// \bug Untested ...
alpar@967
   181
  template <typename Graph>
alpar@967
   182
  typename Graph::Edge findEdge(const Graph &g,
deba@1267
   183
				typename Graph::Node u, typename Graph::Node v,
deba@1267
   184
				typename Graph::Edge prev = INVALID) 
alpar@967
   185
  {
alpar@967
   186
    typename Graph::OutEdgeIt e(g,prev);
alpar@1079
   187
    //    if(prev==INVALID) g.first(e,u);
alpar@1079
   188
    if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
alpar@967
   189
    else ++e;
alpar@1079
   190
    while(e!=INVALID && g.target(e)!=v) ++e;
alpar@967
   191
    return e;
alpar@967
   192
  }
deba@1531
   193
athos@1540
   194
  /// \brief Copy a map.
alpar@964
   195
  ///
alpar@1547
   196
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   197
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   198
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   199
  template <typename Target, typename Source, 
deba@1531
   200
	    typename ItemIt, typename Ref>	    
deba@1531
   201
  void copyMap(Target& target, const Source& source, 
deba@1531
   202
	       ItemIt it, const Ref& ref) {
deba@1531
   203
    for (; it != INVALID; ++it) {
deba@1531
   204
      target[ref[it]] = source[it];
klao@946
   205
    }
klao@946
   206
  }
klao@946
   207
deba@1531
   208
  /// \brief Copy the source map to the target map.
deba@1531
   209
  ///
deba@1531
   210
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   211
  /// to iterate on the data structure.
deba@1531
   212
  template <typename Target, typename Source, 
deba@1531
   213
	    typename ItemIt>	    
deba@1531
   214
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   215
    for (; it != INVALID; ++it) {
deba@1531
   216
      target[it] = source[it];
klao@946
   217
    }
klao@946
   218
  }
klao@946
   219
deba@1531
   220
athos@1540
   221
  /// \brief Class to copy a graph.
deba@1531
   222
  ///
athos@1540
   223
  /// Class to copy a graph to an other graph (duplicate a graph). The
athos@1540
   224
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   225
  template <typename Target, typename Source>
deba@1267
   226
  class GraphCopy {
deba@1531
   227
  public: 
deba@1531
   228
    typedef typename Source::Node Node;
deba@1531
   229
    typedef typename Source::NodeIt NodeIt;
deba@1531
   230
    typedef typename Source::Edge Edge;
deba@1531
   231
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   232
deba@1531
   233
    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
deba@1531
   234
    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
klao@946
   235
deba@1531
   236
    /// \brief Constructor for the GraphCopy.
deba@1531
   237
    ///
deba@1531
   238
    /// It copies the content of the \c _source graph into the
deba@1531
   239
    /// \c _target graph. It creates also two references, one beetween
deba@1531
   240
    /// the two nodeset and one beetween the two edgesets.
deba@1531
   241
    GraphCopy(Target& _target, const Source& _source) 
deba@1531
   242
      : source(_source), target(_target), 
deba@1531
   243
	nodeRefMap(_source), edgeRefMap(_source) {
deba@1531
   244
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   245
	nodeRefMap[it] = target.addNode();
deba@1531
   246
      }
deba@1531
   247
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   248
	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1531
   249
					nodeRefMap[source.target(it)]);
deba@1531
   250
      }
deba@1267
   251
    }
klao@946
   252
deba@1531
   253
    /// \brief Copies the node references into the given map.
deba@1531
   254
    ///
deba@1531
   255
    /// Copies the node references into the given map.
deba@1531
   256
    template <typename NodeRef>
deba@1531
   257
    const GraphCopy& nodeRef(NodeRef& map) const {
deba@1531
   258
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   259
	map.set(it, nodeRefMap[it]);
deba@1531
   260
      }
deba@1531
   261
      return *this;
deba@1267
   262
    }
deba@1531
   263
deba@1531
   264
    /// \brief Reverse and copies the node references into the given map.
deba@1531
   265
    ///
deba@1531
   266
    /// Reverse and copies the node references into the given map.
deba@1531
   267
    template <typename NodeRef>
deba@1531
   268
    const GraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1531
   269
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   270
	map.set(nodeRefMap[it], it);
deba@1531
   271
      }
deba@1531
   272
      return *this;
deba@1531
   273
    }
deba@1531
   274
deba@1531
   275
    /// \brief Copies the edge references into the given map.
deba@1531
   276
    ///
deba@1531
   277
    /// Copies the edge references into the given map.
deba@1531
   278
    template <typename EdgeRef>
deba@1531
   279
    const GraphCopy& edgeRef(EdgeRef& map) const {
deba@1531
   280
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   281
	map.set(it, edgeRefMap[it]);
deba@1531
   282
      }
deba@1531
   283
      return *this;
deba@1531
   284
    }
deba@1531
   285
deba@1531
   286
    /// \brief Reverse and copies the edge references into the given map.
deba@1531
   287
    ///
deba@1531
   288
    /// Reverse and copies the edge references into the given map.
deba@1531
   289
    template <typename EdgeRef>
deba@1531
   290
    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1531
   291
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   292
	map.set(edgeRefMap[it], it);
deba@1531
   293
      }
deba@1531
   294
      return *this;
deba@1531
   295
    }
deba@1531
   296
deba@1531
   297
    /// \brief Make copy of the given map.
deba@1531
   298
    ///
deba@1531
   299
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   300
    /// The new map's key type is the target graph's node type,
deba@1531
   301
    /// and the copied map's key type is the source graph's node
deba@1531
   302
    /// type.  
deba@1531
   303
    template <typename TargetMap, typename SourceMap>
deba@1531
   304
    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   305
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1531
   306
      return *this;
deba@1531
   307
    }
deba@1531
   308
deba@1531
   309
    /// \brief Make copy of the given map.
deba@1531
   310
    ///
deba@1531
   311
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   312
    /// The new map's key type is the target graph's edge type,
deba@1531
   313
    /// and the copied map's key type is the source graph's edge
deba@1531
   314
    /// type.  
deba@1531
   315
    template <typename TargetMap, typename SourceMap>
deba@1531
   316
    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   317
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1531
   318
      return *this;
deba@1531
   319
    }
deba@1531
   320
deba@1531
   321
    /// \brief Gives back the stored node references.
deba@1531
   322
    ///
deba@1531
   323
    /// Gives back the stored node references.
deba@1531
   324
    const NodeRefMap& nodeRef() const {
deba@1531
   325
      return nodeRefMap;
deba@1531
   326
    }
deba@1531
   327
deba@1531
   328
    /// \brief Gives back the stored edge references.
deba@1531
   329
    ///
deba@1531
   330
    /// Gives back the stored edge references.
deba@1531
   331
    const EdgeRefMap& edgeRef() const {
deba@1531
   332
      return edgeRefMap;
deba@1531
   333
    }
deba@1531
   334
deba@1531
   335
  private:
deba@1531
   336
    
deba@1531
   337
    const Source& source;
deba@1531
   338
    Target& target;
deba@1531
   339
deba@1531
   340
    NodeRefMap nodeRefMap;
deba@1531
   341
    EdgeRefMap edgeRefMap;
deba@1267
   342
  };
klao@946
   343
deba@1531
   344
  /// \brief Copy a graph to an other graph.
deba@1531
   345
  ///
deba@1531
   346
  /// Copy a graph to an other graph.
deba@1531
   347
  /// The usage of the function:
deba@1531
   348
  /// 
deba@1531
   349
  /// \code
deba@1531
   350
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
deba@1531
   351
  /// \endcode
deba@1531
   352
  /// 
deba@1531
   353
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   354
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   355
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   356
  /// edges.
deba@1531
   357
  template <typename Target, typename Source>
deba@1531
   358
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   359
    return GraphCopy<Target, Source>(target, source);
deba@1531
   360
  }
klao@946
   361
deba@1267
   362
  template <typename _Graph, typename _Item>
deba@1419
   363
  class ItemSetTraits {};
deba@1192
   364
  
deba@1192
   365
  template <typename _Graph>
deba@1267
   366
  class ItemSetTraits<_Graph, typename _Graph::Node> {
deba@1192
   367
  public:
deba@1192
   368
    
deba@1192
   369
    typedef _Graph Graph;
alpar@947
   370
deba@1192
   371
    typedef typename Graph::Node Item;
deba@1192
   372
    typedef typename Graph::NodeIt ItemIt;
deba@1192
   373
deba@1192
   374
    template <typename _Value>
deba@1192
   375
    class Map : public Graph::template NodeMap<_Value> {
deba@1192
   376
    public:
deba@1192
   377
      typedef typename Graph::template NodeMap<_Value> Parent; 
deba@1192
   378
      typedef typename Parent::Value Value;
deba@1192
   379
deba@1192
   380
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   381
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   382
	: Parent(_graph, _value) {}
deba@1192
   383
    };
deba@1192
   384
deba@1192
   385
  };
deba@1192
   386
deba@1192
   387
  template <typename _Graph>
deba@1267
   388
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
deba@1192
   389
  public:
deba@1192
   390
    
deba@1192
   391
    typedef _Graph Graph;
deba@1192
   392
deba@1192
   393
    typedef typename Graph::Edge Item;
deba@1192
   394
    typedef typename Graph::EdgeIt ItemIt;
deba@1192
   395
deba@1192
   396
    template <typename _Value>
deba@1192
   397
    class Map : public Graph::template EdgeMap<_Value> {
deba@1192
   398
    public:
deba@1192
   399
      typedef typename Graph::template EdgeMap<_Value> Parent; 
deba@1192
   400
      typedef typename Parent::Value Value;
deba@1192
   401
deba@1192
   402
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   403
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   404
	: Parent(_graph, _value) {}
deba@1192
   405
    };
deba@1192
   406
deba@1192
   407
  };
deba@1192
   408
deba@1267
   409
  template <typename _Graph>
deba@1267
   410
  class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
deba@1267
   411
  public:
deba@1267
   412
    
deba@1267
   413
    typedef _Graph Graph;
deba@1267
   414
deba@1267
   415
    typedef typename Graph::UndirEdge Item;
deba@1267
   416
    typedef typename Graph::UndirEdgeIt ItemIt;
deba@1267
   417
deba@1267
   418
    template <typename _Value>
deba@1267
   419
    class Map : public Graph::template UndirEdgeMap<_Value> {
deba@1267
   420
    public:
deba@1267
   421
      typedef typename Graph::template UndirEdgeMap<_Value> Parent; 
deba@1267
   422
      typedef typename Parent::Value Value;
deba@1267
   423
deba@1267
   424
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1267
   425
      Map(const Graph& _graph, const Value& _value) 
deba@1267
   426
	: Parent(_graph, _value) {}
deba@1267
   427
    };
deba@1267
   428
deba@1267
   429
  };
deba@1192
   430
deba@1192
   431
  /// @}
alpar@1402
   432
alpar@1402
   433
  /// \addtogroup graph_maps
alpar@1402
   434
  /// @{
alpar@1402
   435
alpar@1402
   436
  template <typename Map, typename Enable = void>
alpar@1402
   437
  struct ReferenceMapTraits {
alpar@1402
   438
    typedef typename Map::Value Value;
alpar@1402
   439
    typedef typename Map::Value& Reference;
alpar@1402
   440
    typedef const typename Map::Value& ConstReference;
alpar@1402
   441
    typedef typename Map::Value* Pointer;
alpar@1402
   442
    typedef const typename Map::Value* ConstPointer;
alpar@1402
   443
  };
alpar@1402
   444
alpar@1402
   445
  template <typename Map>
alpar@1402
   446
  struct ReferenceMapTraits<
alpar@1402
   447
    Map, 
alpar@1402
   448
    typename enable_if<typename Map::FullTypeTag, void>::type
alpar@1402
   449
  > {
alpar@1402
   450
    typedef typename Map::Value Value;
alpar@1402
   451
    typedef typename Map::Reference Reference;
alpar@1402
   452
    typedef typename Map::ConstReference ConstReference;
alpar@1402
   453
    typedef typename Map::Pointer Pointer;
alpar@1402
   454
    typedef typename Map::ConstPointer ConstPointer;
alpar@1402
   455
  };
alpar@1402
   456
deba@1413
   457
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   458
athos@1540
   459
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
   460
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
   461
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
   462
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
   463
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
   464
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
   465
  /// class \c InverseMap.
deba@1413
   466
  ///
deba@1413
   467
  template <typename _Graph, typename _Item>
deba@1413
   468
  class IdMap {
deba@1413
   469
  public:
deba@1413
   470
    typedef _Graph Graph;
deba@1413
   471
    typedef int Value;
deba@1413
   472
    typedef _Item Item;
deba@1413
   473
    typedef _Item Key;
deba@1413
   474
deba@1419
   475
    typedef True NeedCopy;
deba@1419
   476
deba@1413
   477
    /// \brief Constructor.
deba@1413
   478
    ///
deba@1413
   479
    /// Constructor for creating id map.
deba@1413
   480
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   481
deba@1413
   482
    /// \brief Gives back the \e id of the item.
deba@1413
   483
    ///
deba@1413
   484
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   485
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   486
deba@1413
   487
deba@1413
   488
  private:
deba@1413
   489
    const Graph* graph;
deba@1413
   490
deba@1413
   491
  public:
deba@1413
   492
athos@1540
   493
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
   494
    ///
athos@1540
   495
    /// The class represents the inverse of its owner (IdMap).
deba@1413
   496
    /// \see inverse()
deba@1413
   497
    class InverseMap {
deba@1413
   498
    public:
deba@1419
   499
deba@1419
   500
      typedef True NeedCopy;
deba@1419
   501
deba@1413
   502
      /// \brief Constructor.
deba@1413
   503
      ///
deba@1413
   504
      /// Constructor for creating an id-to-item map.
deba@1413
   505
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   506
deba@1413
   507
      /// \brief Constructor.
deba@1413
   508
      ///
deba@1413
   509
      /// Constructor for creating an id-to-item map.
deba@1413
   510
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   511
deba@1413
   512
      /// \brief Gives back the given item from its id.
deba@1413
   513
      ///
deba@1413
   514
      /// Gives back the given item from its id.
deba@1413
   515
      /// 
deba@1413
   516
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   517
    private:
deba@1413
   518
      const Graph* graph;
deba@1413
   519
    };
deba@1413
   520
deba@1413
   521
    /// \brief Gives back the inverse of the map.
deba@1413
   522
    ///
athos@1540
   523
    /// Gives back the inverse of the IdMap.
deba@1413
   524
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   525
deba@1413
   526
  };
deba@1413
   527
deba@1413
   528
  
athos@1526
   529
  /// \brief General invertable graph-map type.
alpar@1402
   530
athos@1540
   531
  /// This type provides simple invertable graph-maps. 
athos@1526
   532
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
   533
  /// and if a key is set to a new value then store it
alpar@1402
   534
  /// in the inverse map.
alpar@1402
   535
  /// \param _Graph The graph type.
athos@1526
   536
  /// \param _Map The map to extend with invertable functionality. 
alpar@1402
   537
  template <
alpar@1402
   538
    typename _Graph,
alpar@1402
   539
    typename _Item, 
alpar@1402
   540
    typename _Value,
alpar@1402
   541
    typename _Map 
deba@1413
   542
    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
alpar@1402
   543
  >
deba@1413
   544
  class InvertableMap : protected _Map {
alpar@1402
   545
alpar@1402
   546
  public:
alpar@1402
   547
 
alpar@1402
   548
    typedef _Map Map;
alpar@1402
   549
    typedef _Graph Graph;
deba@1413
   550
deba@1413
   551
    /// The key type of InvertableMap (Node, Edge, UndirEdge).
alpar@1402
   552
    typedef typename _Map::Key Key;
deba@1413
   553
    /// The value type of the InvertableMap.
alpar@1402
   554
    typedef typename _Map::Value Value;
alpar@1402
   555
alpar@1402
   556
    /// \brief Constructor.
alpar@1402
   557
    ///
deba@1413
   558
    /// Construct a new InvertableMap for the graph.
alpar@1402
   559
    ///
deba@1413
   560
    InvertableMap(const Graph& graph) : Map(graph) {} 
alpar@1402
   561
    
alpar@1402
   562
    /// \brief The setter function of the map.
alpar@1402
   563
    ///
deba@1413
   564
    /// Sets the mapped value.
alpar@1402
   565
    void set(const Key& key, const Value& val) {
alpar@1402
   566
      Value oldval = Map::operator[](key);
deba@1413
   567
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
   568
      if (it != invMap.end() && it->second == key) {
alpar@1402
   569
	invMap.erase(it);
alpar@1402
   570
      }      
alpar@1402
   571
      invMap.insert(make_pair(val, key));
alpar@1402
   572
      Map::set(key, val);
alpar@1402
   573
    }
alpar@1402
   574
alpar@1402
   575
    /// \brief The getter function of the map.
alpar@1402
   576
    ///
alpar@1402
   577
    /// It gives back the value associated with the key.
deba@1413
   578
    const Value operator[](const Key& key) const {
alpar@1402
   579
      return Map::operator[](key);
alpar@1402
   580
    }
alpar@1402
   581
deba@1515
   582
  protected:
deba@1515
   583
alpar@1402
   584
    /// \brief Add a new key to the map.
alpar@1402
   585
    ///
alpar@1402
   586
    /// Add a new key to the map. It is called by the
alpar@1402
   587
    /// \c AlterationNotifier.
alpar@1402
   588
    virtual void add(const Key& key) {
alpar@1402
   589
      Map::add(key);
alpar@1402
   590
    }
alpar@1402
   591
alpar@1402
   592
    /// \brief Erase the key from the map.
alpar@1402
   593
    ///
alpar@1402
   594
    /// Erase the key to the map. It is called by the
alpar@1402
   595
    /// \c AlterationNotifier.
alpar@1402
   596
    virtual void erase(const Key& key) {
alpar@1402
   597
      Value val = Map::operator[](key);
deba@1413
   598
      typename Container::iterator it = invMap.find(val);
alpar@1402
   599
      if (it != invMap.end() && it->second == key) {
alpar@1402
   600
	invMap.erase(it);
alpar@1402
   601
      }
alpar@1402
   602
      Map::erase(key);
alpar@1402
   603
    }
alpar@1402
   604
alpar@1402
   605
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
   606
    ///
alpar@1402
   607
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
   608
    /// \c AlterationNotifier.
alpar@1402
   609
    virtual void clear() {
alpar@1402
   610
      invMap.clear();
alpar@1402
   611
      Map::clear();
alpar@1402
   612
    }
alpar@1402
   613
deba@1413
   614
  private:
deba@1413
   615
    
deba@1413
   616
    typedef std::map<Value, Key> Container;
deba@1413
   617
    Container invMap;    
deba@1413
   618
deba@1413
   619
  public:
deba@1413
   620
deba@1413
   621
    /// \brief The inverse map type.
deba@1413
   622
    ///
deba@1413
   623
    /// The inverse of this map. The subscript operator of the map
deba@1413
   624
    /// gives back always the item what was last assigned to the value. 
deba@1413
   625
    class InverseMap {
deba@1413
   626
    public:
deba@1413
   627
      /// \brief Constructor of the InverseMap.
deba@1413
   628
      ///
deba@1413
   629
      /// Constructor of the InverseMap.
deba@1413
   630
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
   631
deba@1413
   632
      /// The value type of the InverseMap.
deba@1413
   633
      typedef typename InvertableMap::Key Value;
deba@1413
   634
      /// The key type of the InverseMap.
deba@1413
   635
      typedef typename InvertableMap::Value Key; 
deba@1413
   636
deba@1413
   637
      /// \brief Subscript operator. 
deba@1413
   638
      ///
deba@1413
   639
      /// Subscript operator. It gives back always the item 
deba@1413
   640
      /// what was last assigned to the value.
deba@1413
   641
      Value operator[](const Key& key) const {
deba@1413
   642
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
   643
	return it->second;
deba@1413
   644
      }
deba@1413
   645
      
deba@1413
   646
    private:
deba@1413
   647
      const InvertableMap& inverted;
deba@1413
   648
    };
deba@1413
   649
alpar@1402
   650
    /// \brief It gives back the just readeable inverse map.
alpar@1402
   651
    ///
alpar@1402
   652
    /// It gives back the just readeable inverse map.
deba@1413
   653
    InverseMap inverse() const {
deba@1413
   654
      return InverseMap(*this);
alpar@1402
   655
    } 
alpar@1402
   656
alpar@1402
   657
deba@1413
   658
    
alpar@1402
   659
  };
alpar@1402
   660
alpar@1402
   661
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
   662
  /// item in the graph.
alpar@1402
   663
  ///
athos@1540
   664
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
   665
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
   666
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
   667
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
   668
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
   669
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
   670
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
   671
  /// with its member class \c InverseMap.
alpar@1402
   672
  ///
alpar@1402
   673
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
   674
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
alpar@1402
   675
  /// UndirEdge.
alpar@1402
   676
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
   677
  template <
alpar@1402
   678
    typename _Graph,   
alpar@1402
   679
    typename _Item,
deba@1413
   680
    typename _Map 
deba@1413
   681
    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
alpar@1402
   682
  >
alpar@1402
   683
  class DescriptorMap : protected _Map {
alpar@1402
   684
alpar@1402
   685
    typedef _Item Item;
alpar@1402
   686
    typedef _Map Map;
alpar@1402
   687
alpar@1402
   688
  public:
alpar@1402
   689
    /// The graph class of DescriptorMap.
alpar@1402
   690
    typedef _Graph Graph;
alpar@1402
   691
alpar@1402
   692
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
alpar@1402
   693
    typedef typename _Map::Key Key;
alpar@1402
   694
    /// The value type of DescriptorMap.
alpar@1402
   695
    typedef typename _Map::Value Value;
alpar@1402
   696
alpar@1402
   697
    /// \brief Constructor.
alpar@1402
   698
    ///
deba@1413
   699
    /// Constructor for descriptor map.
alpar@1402
   700
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
   701
      build();
alpar@1402
   702
    }
alpar@1402
   703
deba@1515
   704
  protected:
deba@1515
   705
alpar@1402
   706
    /// \brief Add a new key to the map.
alpar@1402
   707
    ///
alpar@1402
   708
    /// Add a new key to the map. It is called by the
alpar@1402
   709
    /// \c AlterationNotifier.
alpar@1402
   710
    virtual void add(const Item& item) {
alpar@1402
   711
      Map::add(item);
alpar@1402
   712
      Map::set(item, invMap.size());
alpar@1402
   713
      invMap.push_back(item);
alpar@1402
   714
    }
alpar@1402
   715
alpar@1402
   716
    /// \brief Erase the key from the map.
alpar@1402
   717
    ///
alpar@1402
   718
    /// Erase the key to the map. It is called by the
alpar@1402
   719
    /// \c AlterationNotifier.
alpar@1402
   720
    virtual void erase(const Item& item) {
alpar@1402
   721
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
   722
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
   723
      invMap.pop_back();
alpar@1402
   724
      Map::erase(item);
alpar@1402
   725
    }
alpar@1402
   726
alpar@1402
   727
    /// \brief Build the unique map.
alpar@1402
   728
    ///
alpar@1402
   729
    /// Build the unique map. It is called by the
alpar@1402
   730
    /// \c AlterationNotifier.
alpar@1402
   731
    virtual void build() {
alpar@1402
   732
      Map::build();
alpar@1402
   733
      Item it;
alpar@1402
   734
      const typename Map::Graph* graph = Map::getGraph(); 
alpar@1402
   735
      for (graph->first(it); it != INVALID; graph->next(it)) {
alpar@1402
   736
	Map::set(it, invMap.size());
alpar@1402
   737
	invMap.push_back(it);	
alpar@1402
   738
      }      
alpar@1402
   739
    }
alpar@1402
   740
    
alpar@1402
   741
    /// \brief Clear the keys from the map.
alpar@1402
   742
    ///
alpar@1402
   743
    /// Clear the keys from the map. It is called by the
alpar@1402
   744
    /// \c AlterationNotifier.
alpar@1402
   745
    virtual void clear() {
alpar@1402
   746
      invMap.clear();
alpar@1402
   747
      Map::clear();
alpar@1402
   748
    }
alpar@1402
   749
deba@1538
   750
  public:
deba@1538
   751
alpar@1402
   752
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
   753
    ///
alpar@1402
   754
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
   755
    int operator[](const Item& item) const {
alpar@1402
   756
      return Map::operator[](item);
alpar@1402
   757
    }
alpar@1402
   758
    
deba@1413
   759
  private:
deba@1413
   760
deba@1413
   761
    typedef std::vector<Item> Container;
deba@1413
   762
    Container invMap;
deba@1413
   763
deba@1413
   764
  public:
athos@1540
   765
    /// \brief The inverse map type of DescriptorMap.
deba@1413
   766
    ///
athos@1540
   767
    /// The inverse map type of DescriptorMap.
deba@1413
   768
    class InverseMap {
deba@1413
   769
    public:
deba@1413
   770
      /// \brief Constructor of the InverseMap.
deba@1413
   771
      ///
deba@1413
   772
      /// Constructor of the InverseMap.
deba@1413
   773
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
   774
	: inverted(_inverted) {}
deba@1413
   775
deba@1413
   776
deba@1413
   777
      /// The value type of the InverseMap.
deba@1413
   778
      typedef typename DescriptorMap::Key Value;
deba@1413
   779
      /// The key type of the InverseMap.
deba@1413
   780
      typedef typename DescriptorMap::Value Key; 
deba@1413
   781
deba@1413
   782
      /// \brief Subscript operator. 
deba@1413
   783
      ///
deba@1413
   784
      /// Subscript operator. It gives back the item 
deba@1413
   785
      /// that the descriptor belongs to currently.
deba@1413
   786
      Value operator[](const Key& key) const {
deba@1413
   787
	return inverted.invMap[key];
deba@1413
   788
      }
deba@1470
   789
deba@1470
   790
      /// \brief Size of the map.
deba@1470
   791
      ///
deba@1470
   792
      /// Returns the size of the map.
deba@1470
   793
      unsigned size() const {
deba@1470
   794
	return inverted.invMap.size();
deba@1470
   795
      }
deba@1413
   796
      
deba@1413
   797
    private:
deba@1413
   798
      const DescriptorMap& inverted;
deba@1413
   799
    };
deba@1413
   800
alpar@1402
   801
    /// \brief Gives back the inverse of the map.
alpar@1402
   802
    ///
alpar@1402
   803
    /// Gives back the inverse of the map.
alpar@1402
   804
    const InverseMap inverse() const {
deba@1413
   805
      return InverseMap(*this);
alpar@1402
   806
    }
alpar@1402
   807
  };
alpar@1402
   808
alpar@1402
   809
  /// \brief Returns the source of the given edge.
alpar@1402
   810
  ///
alpar@1402
   811
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
   812
  /// \author Balazs Dezso
alpar@1402
   813
  template <typename Graph>
alpar@1402
   814
  class SourceMap {
alpar@1402
   815
  public:
deba@1419
   816
deba@1419
   817
    typedef True NeedCopy;
deba@1419
   818
alpar@1402
   819
    typedef typename Graph::Node Value;
alpar@1402
   820
    typedef typename Graph::Edge Key;
alpar@1402
   821
alpar@1402
   822
    /// \brief Constructor
alpar@1402
   823
    ///
alpar@1402
   824
    /// Constructor
alpar@1402
   825
    /// \param _graph The graph that the map belongs to.
alpar@1402
   826
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   827
alpar@1402
   828
    /// \brief The subscript operator.
alpar@1402
   829
    ///
alpar@1402
   830
    /// The subscript operator.
alpar@1402
   831
    /// \param edge The edge 
alpar@1402
   832
    /// \return The source of the edge 
alpar@1402
   833
    Value operator[](const Key& edge) {
alpar@1402
   834
      return graph.source(edge);
alpar@1402
   835
    }
alpar@1402
   836
alpar@1402
   837
  private:
alpar@1402
   838
    const Graph& graph;
alpar@1402
   839
  };
alpar@1402
   840
alpar@1402
   841
  /// \brief Returns a \ref SourceMap class
alpar@1402
   842
  ///
alpar@1402
   843
  /// This function just returns an \ref SourceMap class.
alpar@1402
   844
  /// \relates SourceMap
alpar@1402
   845
  template <typename Graph>
alpar@1402
   846
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
   847
    return SourceMap<Graph>(graph);
alpar@1402
   848
  } 
alpar@1402
   849
alpar@1402
   850
  /// \brief Returns the target of the given edge.
alpar@1402
   851
  ///
alpar@1402
   852
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
   853
  /// \author Balazs Dezso
alpar@1402
   854
  template <typename Graph>
alpar@1402
   855
  class TargetMap {
alpar@1402
   856
  public:
deba@1419
   857
deba@1419
   858
    typedef True NeedCopy;
deba@1419
   859
alpar@1402
   860
    typedef typename Graph::Node Value;
alpar@1402
   861
    typedef typename Graph::Edge Key;
alpar@1402
   862
alpar@1402
   863
    /// \brief Constructor
alpar@1402
   864
    ///
alpar@1402
   865
    /// Constructor
alpar@1402
   866
    /// \param _graph The graph that the map belongs to.
alpar@1402
   867
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   868
alpar@1402
   869
    /// \brief The subscript operator.
alpar@1402
   870
    ///
alpar@1402
   871
    /// The subscript operator.
alpar@1536
   872
    /// \param e The edge 
alpar@1402
   873
    /// \return The target of the edge 
alpar@1536
   874
    Value operator[](const Key& e) {
alpar@1536
   875
      return graph.target(e);
alpar@1402
   876
    }
alpar@1402
   877
alpar@1402
   878
  private:
alpar@1402
   879
    const Graph& graph;
alpar@1402
   880
  };
alpar@1402
   881
alpar@1402
   882
  /// \brief Returns a \ref TargetMap class
deba@1515
   883
  ///
athos@1540
   884
  /// This function just returns a \ref TargetMap class.
alpar@1402
   885
  /// \relates TargetMap
alpar@1402
   886
  template <typename Graph>
alpar@1402
   887
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
   888
    return TargetMap<Graph>(graph);
alpar@1402
   889
  }
alpar@1402
   890
athos@1540
   891
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
   892
  ///
athos@1540
   893
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
   894
  /// \author Balazs Dezso
deba@1419
   895
  template <typename Graph>
deba@1419
   896
  class ForwardMap {
deba@1419
   897
  public:
deba@1419
   898
deba@1419
   899
    typedef True NeedCopy;
deba@1419
   900
deba@1419
   901
    typedef typename Graph::Edge Value;
deba@1419
   902
    typedef typename Graph::UndirEdge Key;
deba@1419
   903
deba@1419
   904
    /// \brief Constructor
deba@1419
   905
    ///
deba@1419
   906
    /// Constructor
deba@1419
   907
    /// \param _graph The graph that the map belongs to.
deba@1419
   908
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   909
deba@1419
   910
    /// \brief The subscript operator.
deba@1419
   911
    ///
deba@1419
   912
    /// The subscript operator.
deba@1419
   913
    /// \param key An undirected edge 
deba@1419
   914
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
   915
    Value operator[](const Key& key) const {
deba@1419
   916
      return graph.edgeWithSource(key, graph.source(key));
deba@1419
   917
    }
deba@1419
   918
deba@1419
   919
  private:
deba@1419
   920
    const Graph& graph;
deba@1419
   921
  };
deba@1419
   922
deba@1419
   923
  /// \brief Returns a \ref ForwardMap class
deba@1515
   924
  ///
deba@1419
   925
  /// This function just returns an \ref ForwardMap class.
deba@1419
   926
  /// \relates ForwardMap
deba@1419
   927
  template <typename Graph>
deba@1419
   928
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
   929
    return ForwardMap<Graph>(graph);
deba@1419
   930
  }
deba@1419
   931
athos@1540
   932
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
   933
  ///
athos@1540
   934
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
   935
  /// \author Balazs Dezso
deba@1419
   936
  template <typename Graph>
deba@1419
   937
  class BackwardMap {
deba@1419
   938
  public:
deba@1419
   939
    typedef True NeedCopy;
deba@1419
   940
deba@1419
   941
    typedef typename Graph::Edge Value;
deba@1419
   942
    typedef typename Graph::UndirEdge Key;
deba@1419
   943
deba@1419
   944
    /// \brief Constructor
deba@1419
   945
    ///
deba@1419
   946
    /// Constructor
deba@1419
   947
    /// \param _graph The graph that the map belongs to.
deba@1419
   948
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   949
deba@1419
   950
    /// \brief The subscript operator.
deba@1419
   951
    ///
deba@1419
   952
    /// The subscript operator.
deba@1419
   953
    /// \param key An undirected edge 
deba@1419
   954
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
   955
    Value operator[](const Key& key) const {
deba@1419
   956
      return graph.edgeWithSource(key, graph.target(key));
deba@1419
   957
    }
deba@1419
   958
deba@1419
   959
  private:
deba@1419
   960
    const Graph& graph;
deba@1419
   961
  };
deba@1419
   962
deba@1419
   963
  /// \brief Returns a \ref BackwardMap class
deba@1419
   964
athos@1540
   965
  /// This function just returns a \ref BackwardMap class.
deba@1419
   966
  /// \relates BackwardMap
deba@1419
   967
  template <typename Graph>
deba@1419
   968
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
   969
    return BackwardMap<Graph>(graph);
deba@1419
   970
  }
deba@1419
   971
deba@1515
   972
  template <typename _Graph>
deba@1515
   973
  class DegMapBase {
deba@1515
   974
  public:
deba@1515
   975
    
deba@1515
   976
    typedef _Graph Graph;
alpar@1402
   977
deba@1515
   978
  protected:
alpar@1453
   979
deba@1515
   980
    typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@1515
   981
    
deba@1515
   982
  };
alpar@1453
   983
deba@1515
   984
  /// \brief Map of the node in-degrees.
alpar@1453
   985
  ///
athos@1540
   986
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
   987
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
   988
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
   989
  /// whenever the graph changes.
deba@1515
   990
  ///
deba@1515
   991
  /// \sa OutDegMap
deba@1515
   992
alpar@1453
   993
  template <typename _Graph>
deba@1515
   994
  class InDegMap  
deba@1515
   995
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
   996
alpar@1453
   997
  public:
deba@1515
   998
    
deba@1515
   999
    typedef _Graph Graph;
alpar@1453
  1000
    typedef int Value;
deba@1515
  1001
    typedef typename Graph::Node Key;
deba@1515
  1002
deba@1515
  1003
  private:
deba@1515
  1004
deba@1515
  1005
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1006
    public:
deba@1515
  1007
deba@1515
  1008
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1009
deba@1515
  1010
      typedef typename Parent::Key Key;
deba@1515
  1011
      typedef typename Parent::Value Value;
deba@1515
  1012
      
deba@1515
  1013
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1014
      
deba@1515
  1015
      void add(const Key& key) {
deba@1515
  1016
	Parent::add(key);
deba@1515
  1017
	Parent::set(key, 0);
deba@1515
  1018
      }
deba@1515
  1019
    };
deba@1515
  1020
deba@1515
  1021
  public:
alpar@1453
  1022
alpar@1453
  1023
    /// \brief Constructor.
alpar@1453
  1024
    ///
alpar@1453
  1025
    /// Constructor for creating in-degree map.
deba@1515
  1026
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1027
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1028
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1029
      
deba@1515
  1030
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1031
	deg[it] = countInEdges(graph, it);
deba@1515
  1032
      }
alpar@1453
  1033
    }
alpar@1453
  1034
deba@1515
  1035
    virtual ~InDegMap() {
alpar@1459
  1036
      AlterationNotifier<typename _Graph::Edge>::
alpar@1453
  1037
	ObserverBase::detach();
alpar@1453
  1038
    }
alpar@1453
  1039
    
alpar@1459
  1040
    /// Gives back the in-degree of a Node.
deba@1515
  1041
    int operator[](const Key& key) const {
deba@1515
  1042
      return deg[key];
alpar@1459
  1043
    }
alpar@1453
  1044
alpar@1453
  1045
  protected:
deba@1515
  1046
    
deba@1515
  1047
    typedef typename Graph::Edge Edge;
deba@1515
  1048
deba@1515
  1049
    virtual void add(const Edge& edge) {
deba@1515
  1050
      ++deg[graph.target(edge)];
alpar@1453
  1051
    }
alpar@1453
  1052
deba@1515
  1053
    virtual void erase(const Edge& edge) {
deba@1515
  1054
      --deg[graph.target(edge)];
deba@1515
  1055
    }
deba@1515
  1056
deba@1515
  1057
deba@1515
  1058
    virtual void build() {
deba@1515
  1059
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1060
	deg[it] = countInEdges(graph, it);
deba@1515
  1061
      }      
deba@1515
  1062
    }
deba@1515
  1063
deba@1515
  1064
    virtual void clear() {
deba@1515
  1065
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1066
	deg[it] = 0;
deba@1515
  1067
      }
deba@1515
  1068
    }
deba@1515
  1069
  private:
alpar@1506
  1070
    
deba@1515
  1071
    const _Graph& graph;
deba@1515
  1072
    AutoNodeMap deg;
alpar@1459
  1073
  };
alpar@1459
  1074
alpar@1459
  1075
deba@1515
  1076
  /// \brief Map of the node out-degrees.
deba@1515
  1077
  ///
athos@1540
  1078
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1079
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1080
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1081
  /// whenever the graph changes.
deba@1515
  1082
  ///
deba@1515
  1083
  /// \sa OutDegMap
alpar@1459
  1084
alpar@1459
  1085
  template <typename _Graph>
deba@1515
  1086
  class OutDegMap  
deba@1515
  1087
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1088
alpar@1459
  1089
  public:
deba@1515
  1090
    
deba@1515
  1091
    typedef _Graph Graph;
alpar@1459
  1092
    typedef int Value;
deba@1515
  1093
    typedef typename Graph::Node Key;
deba@1515
  1094
deba@1515
  1095
  private:
deba@1515
  1096
deba@1515
  1097
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1098
    public:
deba@1515
  1099
deba@1515
  1100
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1101
deba@1515
  1102
      typedef typename Parent::Key Key;
deba@1515
  1103
      typedef typename Parent::Value Value;
deba@1515
  1104
      
deba@1515
  1105
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1106
      
deba@1515
  1107
      void add(const Key& key) {
deba@1515
  1108
	Parent::add(key);
deba@1515
  1109
	Parent::set(key, 0);
deba@1515
  1110
      }
deba@1515
  1111
    };
deba@1515
  1112
deba@1515
  1113
  public:
alpar@1459
  1114
alpar@1459
  1115
    /// \brief Constructor.
alpar@1459
  1116
    ///
alpar@1459
  1117
    /// Constructor for creating out-degree map.
deba@1515
  1118
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1119
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1120
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1121
      
deba@1515
  1122
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1123
	deg[it] = countOutEdges(graph, it);
deba@1515
  1124
      }
alpar@1459
  1125
    }
alpar@1459
  1126
deba@1515
  1127
    virtual ~OutDegMap() {
alpar@1459
  1128
      AlterationNotifier<typename _Graph::Edge>::
alpar@1459
  1129
	ObserverBase::detach();
alpar@1459
  1130
    }
alpar@1459
  1131
    
alpar@1459
  1132
    /// Gives back the in-degree of a Node.
deba@1515
  1133
    int operator[](const Key& key) const {
deba@1515
  1134
      return deg[key];
alpar@1459
  1135
    }
alpar@1459
  1136
alpar@1459
  1137
  protected:
deba@1515
  1138
    
deba@1515
  1139
    typedef typename Graph::Edge Edge;
deba@1515
  1140
deba@1515
  1141
    virtual void add(const Edge& edge) {
deba@1515
  1142
      ++deg[graph.source(edge)];
alpar@1459
  1143
    }
alpar@1459
  1144
deba@1515
  1145
    virtual void erase(const Edge& edge) {
deba@1515
  1146
      --deg[graph.source(edge)];
deba@1515
  1147
    }
deba@1515
  1148
deba@1515
  1149
deba@1515
  1150
    virtual void build() {
deba@1515
  1151
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1152
	deg[it] = countOutEdges(graph, it);
deba@1515
  1153
      }      
deba@1515
  1154
    }
deba@1515
  1155
deba@1515
  1156
    virtual void clear() {
deba@1515
  1157
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1158
	deg[it] = 0;
deba@1515
  1159
      }
deba@1515
  1160
    }
deba@1515
  1161
  private:
alpar@1506
  1162
    
deba@1515
  1163
    const _Graph& graph;
deba@1515
  1164
    AutoNodeMap deg;
alpar@1453
  1165
  };
alpar@1453
  1166
alpar@1402
  1167
  /// @}
alpar@1402
  1168
alpar@947
  1169
} //END OF NAMESPACE LEMON
klao@946
  1170
klao@946
  1171
#endif