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