lemon/graph_utils.h
author alpar
Fri, 15 Jul 2005 14:13:07 +0000
changeset 1558 69a922643b35
parent 1552 5c7f270f8e25
child 1565 96244ea562a3
permissions -rw-r--r--
Demos module added
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
deba@1552
   752
    /// \brief Swaps the position of the two items in the map.
deba@1552
   753
    ///
deba@1552
   754
    /// Swaps the position of the two items in the map.
deba@1552
   755
    void swap(const Item& p, const Item& q) {
deba@1552
   756
      int pi = Map::operator[](p);
deba@1552
   757
      int qi = Map::operator[](q);
deba@1552
   758
      Map::set(p, qi);
deba@1552
   759
      invMap[qi] = p;
deba@1552
   760
      Map::set(q, pi);
deba@1552
   761
      invMap[pi] = q;
deba@1552
   762
    }
deba@1552
   763
alpar@1402
   764
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
   765
    ///
alpar@1402
   766
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
   767
    int operator[](const Item& item) const {
alpar@1402
   768
      return Map::operator[](item);
alpar@1402
   769
    }
alpar@1402
   770
    
deba@1413
   771
  private:
deba@1413
   772
deba@1413
   773
    typedef std::vector<Item> Container;
deba@1413
   774
    Container invMap;
deba@1413
   775
deba@1413
   776
  public:
athos@1540
   777
    /// \brief The inverse map type of DescriptorMap.
deba@1413
   778
    ///
athos@1540
   779
    /// The inverse map type of DescriptorMap.
deba@1413
   780
    class InverseMap {
deba@1413
   781
    public:
deba@1413
   782
      /// \brief Constructor of the InverseMap.
deba@1413
   783
      ///
deba@1413
   784
      /// Constructor of the InverseMap.
deba@1413
   785
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
   786
	: inverted(_inverted) {}
deba@1413
   787
deba@1413
   788
deba@1413
   789
      /// The value type of the InverseMap.
deba@1413
   790
      typedef typename DescriptorMap::Key Value;
deba@1413
   791
      /// The key type of the InverseMap.
deba@1413
   792
      typedef typename DescriptorMap::Value Key; 
deba@1413
   793
deba@1413
   794
      /// \brief Subscript operator. 
deba@1413
   795
      ///
deba@1413
   796
      /// Subscript operator. It gives back the item 
deba@1413
   797
      /// that the descriptor belongs to currently.
deba@1413
   798
      Value operator[](const Key& key) const {
deba@1413
   799
	return inverted.invMap[key];
deba@1413
   800
      }
deba@1470
   801
deba@1470
   802
      /// \brief Size of the map.
deba@1470
   803
      ///
deba@1470
   804
      /// Returns the size of the map.
deba@1552
   805
      int size() const {
deba@1470
   806
	return inverted.invMap.size();
deba@1470
   807
      }
deba@1413
   808
      
deba@1413
   809
    private:
deba@1413
   810
      const DescriptorMap& inverted;
deba@1413
   811
    };
deba@1413
   812
alpar@1402
   813
    /// \brief Gives back the inverse of the map.
alpar@1402
   814
    ///
alpar@1402
   815
    /// Gives back the inverse of the map.
alpar@1402
   816
    const InverseMap inverse() const {
deba@1413
   817
      return InverseMap(*this);
alpar@1402
   818
    }
alpar@1402
   819
  };
alpar@1402
   820
alpar@1402
   821
  /// \brief Returns the source of the given edge.
alpar@1402
   822
  ///
alpar@1402
   823
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
   824
  /// \author Balazs Dezso
alpar@1402
   825
  template <typename Graph>
alpar@1402
   826
  class SourceMap {
alpar@1402
   827
  public:
deba@1419
   828
deba@1419
   829
    typedef True NeedCopy;
deba@1419
   830
alpar@1402
   831
    typedef typename Graph::Node Value;
alpar@1402
   832
    typedef typename Graph::Edge Key;
alpar@1402
   833
alpar@1402
   834
    /// \brief Constructor
alpar@1402
   835
    ///
alpar@1402
   836
    /// Constructor
alpar@1402
   837
    /// \param _graph The graph that the map belongs to.
alpar@1402
   838
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   839
alpar@1402
   840
    /// \brief The subscript operator.
alpar@1402
   841
    ///
alpar@1402
   842
    /// The subscript operator.
alpar@1402
   843
    /// \param edge The edge 
alpar@1402
   844
    /// \return The source of the edge 
alpar@1402
   845
    Value operator[](const Key& edge) {
alpar@1402
   846
      return graph.source(edge);
alpar@1402
   847
    }
alpar@1402
   848
alpar@1402
   849
  private:
alpar@1402
   850
    const Graph& graph;
alpar@1402
   851
  };
alpar@1402
   852
alpar@1402
   853
  /// \brief Returns a \ref SourceMap class
alpar@1402
   854
  ///
alpar@1402
   855
  /// This function just returns an \ref SourceMap class.
alpar@1402
   856
  /// \relates SourceMap
alpar@1402
   857
  template <typename Graph>
alpar@1402
   858
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
   859
    return SourceMap<Graph>(graph);
alpar@1402
   860
  } 
alpar@1402
   861
alpar@1402
   862
  /// \brief Returns the target of the given edge.
alpar@1402
   863
  ///
alpar@1402
   864
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
   865
  /// \author Balazs Dezso
alpar@1402
   866
  template <typename Graph>
alpar@1402
   867
  class TargetMap {
alpar@1402
   868
  public:
deba@1419
   869
deba@1419
   870
    typedef True NeedCopy;
deba@1419
   871
alpar@1402
   872
    typedef typename Graph::Node Value;
alpar@1402
   873
    typedef typename Graph::Edge Key;
alpar@1402
   874
alpar@1402
   875
    /// \brief Constructor
alpar@1402
   876
    ///
alpar@1402
   877
    /// Constructor
alpar@1402
   878
    /// \param _graph The graph that the map belongs to.
alpar@1402
   879
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   880
alpar@1402
   881
    /// \brief The subscript operator.
alpar@1402
   882
    ///
alpar@1402
   883
    /// The subscript operator.
alpar@1536
   884
    /// \param e The edge 
alpar@1402
   885
    /// \return The target of the edge 
alpar@1536
   886
    Value operator[](const Key& e) {
alpar@1536
   887
      return graph.target(e);
alpar@1402
   888
    }
alpar@1402
   889
alpar@1402
   890
  private:
alpar@1402
   891
    const Graph& graph;
alpar@1402
   892
  };
alpar@1402
   893
alpar@1402
   894
  /// \brief Returns a \ref TargetMap class
deba@1515
   895
  ///
athos@1540
   896
  /// This function just returns a \ref TargetMap class.
alpar@1402
   897
  /// \relates TargetMap
alpar@1402
   898
  template <typename Graph>
alpar@1402
   899
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
   900
    return TargetMap<Graph>(graph);
alpar@1402
   901
  }
alpar@1402
   902
athos@1540
   903
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
   904
  ///
athos@1540
   905
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
   906
  /// \author Balazs Dezso
deba@1419
   907
  template <typename Graph>
deba@1419
   908
  class ForwardMap {
deba@1419
   909
  public:
deba@1419
   910
deba@1419
   911
    typedef True NeedCopy;
deba@1419
   912
deba@1419
   913
    typedef typename Graph::Edge Value;
deba@1419
   914
    typedef typename Graph::UndirEdge Key;
deba@1419
   915
deba@1419
   916
    /// \brief Constructor
deba@1419
   917
    ///
deba@1419
   918
    /// Constructor
deba@1419
   919
    /// \param _graph The graph that the map belongs to.
deba@1419
   920
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   921
deba@1419
   922
    /// \brief The subscript operator.
deba@1419
   923
    ///
deba@1419
   924
    /// The subscript operator.
deba@1419
   925
    /// \param key An undirected edge 
deba@1419
   926
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
   927
    Value operator[](const Key& key) const {
deba@1419
   928
      return graph.edgeWithSource(key, graph.source(key));
deba@1419
   929
    }
deba@1419
   930
deba@1419
   931
  private:
deba@1419
   932
    const Graph& graph;
deba@1419
   933
  };
deba@1419
   934
deba@1419
   935
  /// \brief Returns a \ref ForwardMap class
deba@1515
   936
  ///
deba@1419
   937
  /// This function just returns an \ref ForwardMap class.
deba@1419
   938
  /// \relates ForwardMap
deba@1419
   939
  template <typename Graph>
deba@1419
   940
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
   941
    return ForwardMap<Graph>(graph);
deba@1419
   942
  }
deba@1419
   943
athos@1540
   944
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
   945
  ///
athos@1540
   946
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
   947
  /// \author Balazs Dezso
deba@1419
   948
  template <typename Graph>
deba@1419
   949
  class BackwardMap {
deba@1419
   950
  public:
deba@1419
   951
    typedef True NeedCopy;
deba@1419
   952
deba@1419
   953
    typedef typename Graph::Edge Value;
deba@1419
   954
    typedef typename Graph::UndirEdge Key;
deba@1419
   955
deba@1419
   956
    /// \brief Constructor
deba@1419
   957
    ///
deba@1419
   958
    /// Constructor
deba@1419
   959
    /// \param _graph The graph that the map belongs to.
deba@1419
   960
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   961
deba@1419
   962
    /// \brief The subscript operator.
deba@1419
   963
    ///
deba@1419
   964
    /// The subscript operator.
deba@1419
   965
    /// \param key An undirected edge 
deba@1419
   966
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
   967
    Value operator[](const Key& key) const {
deba@1419
   968
      return graph.edgeWithSource(key, graph.target(key));
deba@1419
   969
    }
deba@1419
   970
deba@1419
   971
  private:
deba@1419
   972
    const Graph& graph;
deba@1419
   973
  };
deba@1419
   974
deba@1419
   975
  /// \brief Returns a \ref BackwardMap class
deba@1419
   976
athos@1540
   977
  /// This function just returns a \ref BackwardMap class.
deba@1419
   978
  /// \relates BackwardMap
deba@1419
   979
  template <typename Graph>
deba@1419
   980
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
   981
    return BackwardMap<Graph>(graph);
deba@1419
   982
  }
deba@1419
   983
deba@1515
   984
  template <typename _Graph>
deba@1515
   985
  class DegMapBase {
deba@1515
   986
  public:
deba@1515
   987
    
deba@1515
   988
    typedef _Graph Graph;
alpar@1402
   989
deba@1515
   990
  protected:
alpar@1453
   991
deba@1515
   992
    typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@1515
   993
    
deba@1515
   994
  };
alpar@1453
   995
deba@1515
   996
  /// \brief Map of the node in-degrees.
alpar@1453
   997
  ///
athos@1540
   998
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
   999
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1000
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1001
  /// whenever the graph changes.
deba@1515
  1002
  ///
deba@1515
  1003
  /// \sa OutDegMap
deba@1515
  1004
alpar@1453
  1005
  template <typename _Graph>
deba@1515
  1006
  class InDegMap  
deba@1515
  1007
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1008
alpar@1453
  1009
  public:
deba@1515
  1010
    
deba@1515
  1011
    typedef _Graph Graph;
alpar@1453
  1012
    typedef int Value;
deba@1515
  1013
    typedef typename Graph::Node Key;
deba@1515
  1014
deba@1515
  1015
  private:
deba@1515
  1016
deba@1515
  1017
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1018
    public:
deba@1515
  1019
deba@1515
  1020
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1021
deba@1515
  1022
      typedef typename Parent::Key Key;
deba@1515
  1023
      typedef typename Parent::Value Value;
deba@1515
  1024
      
deba@1515
  1025
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1026
      
deba@1515
  1027
      void add(const Key& key) {
deba@1515
  1028
	Parent::add(key);
deba@1515
  1029
	Parent::set(key, 0);
deba@1515
  1030
      }
deba@1515
  1031
    };
deba@1515
  1032
deba@1515
  1033
  public:
alpar@1453
  1034
alpar@1453
  1035
    /// \brief Constructor.
alpar@1453
  1036
    ///
alpar@1453
  1037
    /// Constructor for creating in-degree map.
deba@1515
  1038
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1039
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1040
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1041
      
deba@1515
  1042
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1043
	deg[it] = countInEdges(graph, it);
deba@1515
  1044
      }
alpar@1453
  1045
    }
alpar@1453
  1046
deba@1515
  1047
    virtual ~InDegMap() {
alpar@1459
  1048
      AlterationNotifier<typename _Graph::Edge>::
alpar@1453
  1049
	ObserverBase::detach();
alpar@1453
  1050
    }
alpar@1453
  1051
    
alpar@1459
  1052
    /// Gives back the in-degree of a Node.
deba@1515
  1053
    int operator[](const Key& key) const {
deba@1515
  1054
      return deg[key];
alpar@1459
  1055
    }
alpar@1453
  1056
alpar@1453
  1057
  protected:
deba@1515
  1058
    
deba@1515
  1059
    typedef typename Graph::Edge Edge;
deba@1515
  1060
deba@1515
  1061
    virtual void add(const Edge& edge) {
deba@1515
  1062
      ++deg[graph.target(edge)];
alpar@1453
  1063
    }
alpar@1453
  1064
deba@1515
  1065
    virtual void erase(const Edge& edge) {
deba@1515
  1066
      --deg[graph.target(edge)];
deba@1515
  1067
    }
deba@1515
  1068
deba@1515
  1069
deba@1515
  1070
    virtual void build() {
deba@1515
  1071
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1072
	deg[it] = countInEdges(graph, it);
deba@1515
  1073
      }      
deba@1515
  1074
    }
deba@1515
  1075
deba@1515
  1076
    virtual void clear() {
deba@1515
  1077
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1078
	deg[it] = 0;
deba@1515
  1079
      }
deba@1515
  1080
    }
deba@1515
  1081
  private:
alpar@1506
  1082
    
deba@1515
  1083
    const _Graph& graph;
deba@1515
  1084
    AutoNodeMap deg;
alpar@1459
  1085
  };
alpar@1459
  1086
alpar@1459
  1087
deba@1515
  1088
  /// \brief Map of the node out-degrees.
deba@1515
  1089
  ///
athos@1540
  1090
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1091
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1092
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1093
  /// whenever the graph changes.
deba@1515
  1094
  ///
alpar@1555
  1095
  /// \sa InDegMap
alpar@1459
  1096
alpar@1459
  1097
  template <typename _Graph>
deba@1515
  1098
  class OutDegMap  
deba@1515
  1099
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1100
alpar@1459
  1101
  public:
deba@1515
  1102
    
deba@1515
  1103
    typedef _Graph Graph;
alpar@1459
  1104
    typedef int Value;
deba@1515
  1105
    typedef typename Graph::Node Key;
deba@1515
  1106
deba@1515
  1107
  private:
deba@1515
  1108
deba@1515
  1109
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1110
    public:
deba@1515
  1111
deba@1515
  1112
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1113
deba@1515
  1114
      typedef typename Parent::Key Key;
deba@1515
  1115
      typedef typename Parent::Value Value;
deba@1515
  1116
      
deba@1515
  1117
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1118
      
deba@1515
  1119
      void add(const Key& key) {
deba@1515
  1120
	Parent::add(key);
deba@1515
  1121
	Parent::set(key, 0);
deba@1515
  1122
      }
deba@1515
  1123
    };
deba@1515
  1124
deba@1515
  1125
  public:
alpar@1459
  1126
alpar@1459
  1127
    /// \brief Constructor.
alpar@1459
  1128
    ///
alpar@1459
  1129
    /// Constructor for creating out-degree map.
deba@1515
  1130
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1131
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1132
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1133
      
deba@1515
  1134
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1135
	deg[it] = countOutEdges(graph, it);
deba@1515
  1136
      }
alpar@1459
  1137
    }
alpar@1459
  1138
deba@1515
  1139
    virtual ~OutDegMap() {
alpar@1459
  1140
      AlterationNotifier<typename _Graph::Edge>::
alpar@1459
  1141
	ObserverBase::detach();
alpar@1459
  1142
    }
alpar@1459
  1143
    
alpar@1459
  1144
    /// Gives back the in-degree of a Node.
deba@1515
  1145
    int operator[](const Key& key) const {
deba@1515
  1146
      return deg[key];
alpar@1459
  1147
    }
alpar@1459
  1148
alpar@1459
  1149
  protected:
deba@1515
  1150
    
deba@1515
  1151
    typedef typename Graph::Edge Edge;
deba@1515
  1152
deba@1515
  1153
    virtual void add(const Edge& edge) {
deba@1515
  1154
      ++deg[graph.source(edge)];
alpar@1459
  1155
    }
alpar@1459
  1156
deba@1515
  1157
    virtual void erase(const Edge& edge) {
deba@1515
  1158
      --deg[graph.source(edge)];
deba@1515
  1159
    }
deba@1515
  1160
deba@1515
  1161
deba@1515
  1162
    virtual void build() {
deba@1515
  1163
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1164
	deg[it] = countOutEdges(graph, it);
deba@1515
  1165
      }      
deba@1515
  1166
    }
deba@1515
  1167
deba@1515
  1168
    virtual void clear() {
deba@1515
  1169
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1170
	deg[it] = 0;
deba@1515
  1171
      }
deba@1515
  1172
    }
deba@1515
  1173
  private:
alpar@1506
  1174
    
deba@1515
  1175
    const _Graph& graph;
deba@1515
  1176
    AutoNodeMap deg;
alpar@1453
  1177
  };
alpar@1453
  1178
alpar@1402
  1179
  /// @}
alpar@1402
  1180
alpar@947
  1181
} //END OF NAMESPACE LEMON
klao@946
  1182
klao@946
  1183
#endif