lemon/graph_utils.h
author deba
Mon, 12 Sep 2005 11:24:54 +0000
changeset 1681 84e43c7ca1e3
parent 1674 648aa2f33dc8
child 1695 e6f99fe1723f
permissions -rw-r--r--
SubGraphAdaptors with edge checking functionality.

Improved grid_graph_demo
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@1679
   162
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1679
   163
  ///
deba@1679
   164
  /// This function counts the number of the in-edges to node \c n
deba@1679
   165
  /// in the graph.  
deba@1679
   166
  template <typename Graph>
deba@1679
   167
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1679
   168
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
deba@1679
   169
  }
deba@1679
   170
deba@1531
   171
deba@1565
   172
  template <typename Graph>
deba@1565
   173
  inline
deba@1565
   174
  typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type 
deba@1565
   175
  _findEdge(const Graph &g,
deba@1565
   176
	    typename Graph::Node u, typename Graph::Node v,
deba@1565
   177
	    typename Graph::Edge prev = INVALID) {
deba@1565
   178
    return g.findEdge(u, v, prev);
deba@1565
   179
  }
alpar@967
   180
deba@1565
   181
  template <typename Graph>
deba@1565
   182
  inline typename Graph::Edge 
deba@1565
   183
  _findEdge(Wrap<Graph> w,
deba@1565
   184
	    typename Graph::Node u, 
deba@1565
   185
	    typename Graph::Node v,
deba@1565
   186
	    typename Graph::Edge prev = INVALID) {
deba@1565
   187
    const Graph& g = w.value;
deba@1565
   188
    if (prev == INVALID) {
deba@1565
   189
      typename Graph::OutEdgeIt e(g, u);
deba@1565
   190
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   191
      return e;
deba@1565
   192
    } else {
deba@1565
   193
      typename Graph::OutEdgeIt e(g, prev); ++e;
deba@1565
   194
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   195
      return e;
deba@1565
   196
    }
deba@1565
   197
  }
deba@1565
   198
deba@1565
   199
  /// \brief Finds an edge between two nodes of a graph.
deba@1565
   200
  ///
alpar@967
   201
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   202
  ///
alpar@967
   203
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   204
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   205
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   206
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   207
  ///
alpar@967
   208
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@967
   209
  /// \code
alpar@967
   210
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   211
  ///   ...
alpar@967
   212
  /// }
alpar@967
   213
  /// \endcode
deba@1565
   214
  // /// \todo We may want to use the "GraphBase" 
deba@1565
   215
  // /// interface here...
deba@1565
   216
  // /// It would not work with the undirected graphs.
alpar@967
   217
  template <typename Graph>
deba@1565
   218
  inline typename Graph::Edge findEdge(const Graph &g,
deba@1565
   219
				       typename Graph::Node u, 
deba@1565
   220
				       typename Graph::Node v,
deba@1565
   221
				       typename Graph::Edge prev = INVALID) {
deba@1565
   222
    return _findEdge<Graph>(g, u, v, prev);
alpar@967
   223
  }
deba@1531
   224
deba@1565
   225
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   226
  ///
deba@1565
   227
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   228
  /// higher level interface for the findEdge() function. You can
alpar@1591
   229
  /// use it the following way:
deba@1565
   230
  /// \code
deba@1565
   231
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   232
  ///   ...
deba@1565
   233
  /// }
deba@1565
   234
  /// \endcode
deba@1565
   235
  ///
deba@1565
   236
  /// \author Balazs Dezso 
deba@1565
   237
  template <typename _Graph>
deba@1565
   238
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   239
  public:
deba@1565
   240
deba@1565
   241
    typedef _Graph Graph;
deba@1565
   242
    typedef typename Graph::Edge Parent;
deba@1565
   243
deba@1565
   244
    typedef typename Graph::Edge Edge;
deba@1565
   245
    typedef typename Graph::Node Node;
deba@1565
   246
deba@1565
   247
    /// \brief Constructor.
deba@1565
   248
    ///
deba@1565
   249
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   250
    /// connects the \c u and \c v node.
deba@1565
   251
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   252
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   253
    }
deba@1565
   254
deba@1565
   255
    /// \brief Constructor.
deba@1565
   256
    ///
deba@1565
   257
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   258
    /// the \c e edge.
deba@1565
   259
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   260
    
deba@1565
   261
    /// \brief Increment operator.
deba@1565
   262
    ///
deba@1565
   263
    /// It increments the iterator and gives back the next edge.
deba@1565
   264
    ConEdgeIt& operator++() {
deba@1565
   265
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   266
				 graph.target(*this), *this));
deba@1565
   267
      return *this;
deba@1565
   268
    }
deba@1565
   269
  private:
deba@1565
   270
    const Graph& graph;
deba@1565
   271
  };
deba@1565
   272
athos@1540
   273
  /// \brief Copy a map.
alpar@964
   274
  ///
alpar@1547
   275
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   276
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   277
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   278
  template <typename Target, typename Source, 
deba@1531
   279
	    typename ItemIt, typename Ref>	    
deba@1531
   280
  void copyMap(Target& target, const Source& source, 
deba@1531
   281
	       ItemIt it, const Ref& ref) {
deba@1531
   282
    for (; it != INVALID; ++it) {
deba@1531
   283
      target[ref[it]] = source[it];
klao@946
   284
    }
klao@946
   285
  }
klao@946
   286
deba@1531
   287
  /// \brief Copy the source map to the target map.
deba@1531
   288
  ///
deba@1531
   289
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   290
  /// to iterate on the data structure.
deba@1531
   291
  template <typename Target, typename Source, 
deba@1531
   292
	    typename ItemIt>	    
deba@1531
   293
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   294
    for (; it != INVALID; ++it) {
deba@1531
   295
      target[it] = source[it];
klao@946
   296
    }
klao@946
   297
  }
klao@946
   298
deba@1531
   299
athos@1540
   300
  /// \brief Class to copy a graph.
deba@1531
   301
  ///
athos@1540
   302
  /// Class to copy a graph to an other graph (duplicate a graph). The
athos@1540
   303
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   304
  template <typename Target, typename Source>
deba@1267
   305
  class GraphCopy {
deba@1531
   306
  public: 
deba@1531
   307
    typedef typename Source::Node Node;
deba@1531
   308
    typedef typename Source::NodeIt NodeIt;
deba@1531
   309
    typedef typename Source::Edge Edge;
deba@1531
   310
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   311
deba@1531
   312
    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
deba@1531
   313
    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
klao@946
   314
deba@1531
   315
    /// \brief Constructor for the GraphCopy.
deba@1531
   316
    ///
deba@1531
   317
    /// It copies the content of the \c _source graph into the
deba@1531
   318
    /// \c _target graph. It creates also two references, one beetween
deba@1531
   319
    /// the two nodeset and one beetween the two edgesets.
deba@1531
   320
    GraphCopy(Target& _target, const Source& _source) 
deba@1531
   321
      : source(_source), target(_target), 
deba@1531
   322
	nodeRefMap(_source), edgeRefMap(_source) {
deba@1531
   323
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   324
	nodeRefMap[it] = target.addNode();
deba@1531
   325
      }
deba@1531
   326
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   327
	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1531
   328
					nodeRefMap[source.target(it)]);
deba@1531
   329
      }
deba@1267
   330
    }
klao@946
   331
deba@1531
   332
    /// \brief Copies the node references into the given map.
deba@1531
   333
    ///
deba@1531
   334
    /// Copies the node references into the given map.
deba@1531
   335
    template <typename NodeRef>
deba@1531
   336
    const GraphCopy& nodeRef(NodeRef& map) const {
deba@1531
   337
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   338
	map.set(it, nodeRefMap[it]);
deba@1531
   339
      }
deba@1531
   340
      return *this;
deba@1267
   341
    }
deba@1531
   342
deba@1531
   343
    /// \brief Reverse and copies the node references into the given map.
deba@1531
   344
    ///
deba@1531
   345
    /// Reverse and copies the node references into the given map.
deba@1531
   346
    template <typename NodeRef>
deba@1531
   347
    const GraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1531
   348
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   349
	map.set(nodeRefMap[it], it);
deba@1531
   350
      }
deba@1531
   351
      return *this;
deba@1531
   352
    }
deba@1531
   353
deba@1531
   354
    /// \brief Copies the edge references into the given map.
deba@1531
   355
    ///
deba@1531
   356
    /// Copies the edge references into the given map.
deba@1531
   357
    template <typename EdgeRef>
deba@1531
   358
    const GraphCopy& edgeRef(EdgeRef& map) const {
deba@1531
   359
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   360
	map.set(it, edgeRefMap[it]);
deba@1531
   361
      }
deba@1531
   362
      return *this;
deba@1531
   363
    }
deba@1531
   364
deba@1531
   365
    /// \brief Reverse and copies the edge references into the given map.
deba@1531
   366
    ///
deba@1531
   367
    /// Reverse and copies the edge references into the given map.
deba@1531
   368
    template <typename EdgeRef>
deba@1531
   369
    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1531
   370
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   371
	map.set(edgeRefMap[it], it);
deba@1531
   372
      }
deba@1531
   373
      return *this;
deba@1531
   374
    }
deba@1531
   375
deba@1531
   376
    /// \brief Make copy of the given map.
deba@1531
   377
    ///
deba@1531
   378
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   379
    /// The new map's key type is the target graph's node type,
deba@1531
   380
    /// and the copied map's key type is the source graph's node
deba@1531
   381
    /// type.  
deba@1531
   382
    template <typename TargetMap, typename SourceMap>
deba@1531
   383
    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   384
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1531
   385
      return *this;
deba@1531
   386
    }
deba@1531
   387
deba@1531
   388
    /// \brief Make copy of the given map.
deba@1531
   389
    ///
deba@1531
   390
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   391
    /// The new map's key type is the target graph's edge type,
deba@1531
   392
    /// and the copied map's key type is the source graph's edge
deba@1531
   393
    /// type.  
deba@1531
   394
    template <typename TargetMap, typename SourceMap>
deba@1531
   395
    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   396
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1531
   397
      return *this;
deba@1531
   398
    }
deba@1531
   399
deba@1531
   400
    /// \brief Gives back the stored node references.
deba@1531
   401
    ///
deba@1531
   402
    /// Gives back the stored node references.
deba@1531
   403
    const NodeRefMap& nodeRef() const {
deba@1531
   404
      return nodeRefMap;
deba@1531
   405
    }
deba@1531
   406
deba@1531
   407
    /// \brief Gives back the stored edge references.
deba@1531
   408
    ///
deba@1531
   409
    /// Gives back the stored edge references.
deba@1531
   410
    const EdgeRefMap& edgeRef() const {
deba@1531
   411
      return edgeRefMap;
deba@1531
   412
    }
deba@1531
   413
deba@1531
   414
  private:
deba@1531
   415
    
deba@1531
   416
    const Source& source;
deba@1531
   417
    Target& target;
deba@1531
   418
deba@1531
   419
    NodeRefMap nodeRefMap;
deba@1531
   420
    EdgeRefMap edgeRefMap;
deba@1267
   421
  };
klao@946
   422
deba@1531
   423
  /// \brief Copy a graph to an other graph.
deba@1531
   424
  ///
deba@1531
   425
  /// Copy a graph to an other graph.
deba@1531
   426
  /// The usage of the function:
deba@1531
   427
  /// 
deba@1531
   428
  /// \code
deba@1531
   429
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
deba@1531
   430
  /// \endcode
deba@1531
   431
  /// 
deba@1531
   432
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   433
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   434
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   435
  /// edges.
deba@1531
   436
  template <typename Target, typename Source>
deba@1531
   437
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   438
    return GraphCopy<Target, Source>(target, source);
deba@1531
   439
  }
klao@946
   440
deba@1267
   441
  template <typename _Graph, typename _Item>
deba@1419
   442
  class ItemSetTraits {};
deba@1192
   443
  
deba@1192
   444
  template <typename _Graph>
deba@1267
   445
  class ItemSetTraits<_Graph, typename _Graph::Node> {
deba@1192
   446
  public:
deba@1192
   447
    
deba@1192
   448
    typedef _Graph Graph;
alpar@947
   449
deba@1192
   450
    typedef typename Graph::Node Item;
deba@1192
   451
    typedef typename Graph::NodeIt ItemIt;
deba@1192
   452
deba@1192
   453
    template <typename _Value>
deba@1192
   454
    class Map : public Graph::template NodeMap<_Value> {
deba@1192
   455
    public:
deba@1192
   456
      typedef typename Graph::template NodeMap<_Value> Parent; 
deba@1192
   457
      typedef typename Parent::Value Value;
deba@1192
   458
deba@1192
   459
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   460
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   461
	: Parent(_graph, _value) {}
deba@1192
   462
    };
deba@1192
   463
deba@1192
   464
  };
deba@1192
   465
deba@1192
   466
  template <typename _Graph>
deba@1267
   467
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
deba@1192
   468
  public:
deba@1192
   469
    
deba@1192
   470
    typedef _Graph Graph;
deba@1192
   471
deba@1192
   472
    typedef typename Graph::Edge Item;
deba@1192
   473
    typedef typename Graph::EdgeIt ItemIt;
deba@1192
   474
deba@1192
   475
    template <typename _Value>
deba@1192
   476
    class Map : public Graph::template EdgeMap<_Value> {
deba@1192
   477
    public:
deba@1192
   478
      typedef typename Graph::template EdgeMap<_Value> Parent; 
deba@1192
   479
      typedef typename Parent::Value Value;
deba@1192
   480
deba@1192
   481
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   482
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   483
	: Parent(_graph, _value) {}
deba@1192
   484
    };
deba@1192
   485
deba@1192
   486
  };
deba@1192
   487
deba@1267
   488
  template <typename _Graph>
deba@1267
   489
  class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
deba@1267
   490
  public:
deba@1267
   491
    
deba@1267
   492
    typedef _Graph Graph;
deba@1267
   493
deba@1267
   494
    typedef typename Graph::UndirEdge Item;
deba@1267
   495
    typedef typename Graph::UndirEdgeIt ItemIt;
deba@1267
   496
deba@1267
   497
    template <typename _Value>
deba@1267
   498
    class Map : public Graph::template UndirEdgeMap<_Value> {
deba@1267
   499
    public:
deba@1267
   500
      typedef typename Graph::template UndirEdgeMap<_Value> Parent; 
deba@1267
   501
      typedef typename Parent::Value Value;
deba@1267
   502
deba@1267
   503
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1267
   504
      Map(const Graph& _graph, const Value& _value) 
deba@1267
   505
	: Parent(_graph, _value) {}
deba@1267
   506
    };
deba@1267
   507
deba@1267
   508
  };
deba@1192
   509
deba@1192
   510
  /// @}
alpar@1402
   511
alpar@1402
   512
  /// \addtogroup graph_maps
alpar@1402
   513
  /// @{
alpar@1402
   514
alpar@1402
   515
  template <typename Map, typename Enable = void>
alpar@1402
   516
  struct ReferenceMapTraits {
alpar@1402
   517
    typedef typename Map::Value Value;
alpar@1402
   518
    typedef typename Map::Value& Reference;
alpar@1402
   519
    typedef const typename Map::Value& ConstReference;
alpar@1402
   520
    typedef typename Map::Value* Pointer;
alpar@1402
   521
    typedef const typename Map::Value* ConstPointer;
alpar@1402
   522
  };
alpar@1402
   523
alpar@1402
   524
  template <typename Map>
alpar@1402
   525
  struct ReferenceMapTraits<
alpar@1402
   526
    Map, 
alpar@1402
   527
    typename enable_if<typename Map::FullTypeTag, void>::type
alpar@1402
   528
  > {
alpar@1402
   529
    typedef typename Map::Value Value;
alpar@1402
   530
    typedef typename Map::Reference Reference;
alpar@1402
   531
    typedef typename Map::ConstReference ConstReference;
alpar@1402
   532
    typedef typename Map::Pointer Pointer;
alpar@1402
   533
    typedef typename Map::ConstPointer ConstPointer;
alpar@1402
   534
  };
alpar@1402
   535
deba@1413
   536
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   537
athos@1540
   538
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
   539
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
   540
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
   541
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
   542
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
   543
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
   544
  /// class \c InverseMap.
deba@1413
   545
  ///
deba@1413
   546
  template <typename _Graph, typename _Item>
deba@1413
   547
  class IdMap {
deba@1413
   548
  public:
deba@1413
   549
    typedef _Graph Graph;
deba@1413
   550
    typedef int Value;
deba@1413
   551
    typedef _Item Item;
deba@1413
   552
    typedef _Item Key;
deba@1413
   553
deba@1419
   554
    typedef True NeedCopy;
deba@1419
   555
deba@1413
   556
    /// \brief Constructor.
deba@1413
   557
    ///
deba@1413
   558
    /// Constructor for creating id map.
deba@1413
   559
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   560
deba@1413
   561
    /// \brief Gives back the \e id of the item.
deba@1413
   562
    ///
deba@1413
   563
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   564
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   565
deba@1413
   566
deba@1413
   567
  private:
deba@1413
   568
    const Graph* graph;
deba@1413
   569
deba@1413
   570
  public:
deba@1413
   571
athos@1540
   572
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
   573
    ///
athos@1540
   574
    /// The class represents the inverse of its owner (IdMap).
deba@1413
   575
    /// \see inverse()
deba@1413
   576
    class InverseMap {
deba@1413
   577
    public:
deba@1419
   578
deba@1419
   579
      typedef True NeedCopy;
deba@1419
   580
deba@1413
   581
      /// \brief Constructor.
deba@1413
   582
      ///
deba@1413
   583
      /// Constructor for creating an id-to-item map.
deba@1413
   584
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   585
deba@1413
   586
      /// \brief Constructor.
deba@1413
   587
      ///
deba@1413
   588
      /// Constructor for creating an id-to-item map.
deba@1413
   589
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   590
deba@1413
   591
      /// \brief Gives back the given item from its id.
deba@1413
   592
      ///
deba@1413
   593
      /// Gives back the given item from its id.
deba@1413
   594
      /// 
deba@1413
   595
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   596
    private:
deba@1413
   597
      const Graph* graph;
deba@1413
   598
    };
deba@1413
   599
deba@1413
   600
    /// \brief Gives back the inverse of the map.
deba@1413
   601
    ///
athos@1540
   602
    /// Gives back the inverse of the IdMap.
deba@1413
   603
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   604
deba@1413
   605
  };
deba@1413
   606
deba@1413
   607
  
athos@1526
   608
  /// \brief General invertable graph-map type.
alpar@1402
   609
athos@1540
   610
  /// This type provides simple invertable graph-maps. 
athos@1526
   611
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
   612
  /// and if a key is set to a new value then store it
alpar@1402
   613
  /// in the inverse map.
alpar@1402
   614
  /// \param _Graph The graph type.
athos@1526
   615
  /// \param _Map The map to extend with invertable functionality. 
alpar@1402
   616
  template <
alpar@1402
   617
    typename _Graph,
alpar@1402
   618
    typename _Item, 
alpar@1402
   619
    typename _Value,
alpar@1402
   620
    typename _Map 
deba@1413
   621
    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
alpar@1402
   622
  >
deba@1413
   623
  class InvertableMap : protected _Map {
alpar@1402
   624
alpar@1402
   625
  public:
alpar@1402
   626
 
alpar@1402
   627
    typedef _Map Map;
alpar@1402
   628
    typedef _Graph Graph;
deba@1413
   629
deba@1413
   630
    /// The key type of InvertableMap (Node, Edge, UndirEdge).
alpar@1402
   631
    typedef typename _Map::Key Key;
deba@1413
   632
    /// The value type of the InvertableMap.
alpar@1402
   633
    typedef typename _Map::Value Value;
alpar@1402
   634
alpar@1402
   635
    /// \brief Constructor.
alpar@1402
   636
    ///
deba@1413
   637
    /// Construct a new InvertableMap for the graph.
alpar@1402
   638
    ///
deba@1413
   639
    InvertableMap(const Graph& graph) : Map(graph) {} 
alpar@1402
   640
    
alpar@1402
   641
    /// \brief The setter function of the map.
alpar@1402
   642
    ///
deba@1413
   643
    /// Sets the mapped value.
alpar@1402
   644
    void set(const Key& key, const Value& val) {
alpar@1402
   645
      Value oldval = Map::operator[](key);
deba@1413
   646
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
   647
      if (it != invMap.end() && it->second == key) {
alpar@1402
   648
	invMap.erase(it);
alpar@1402
   649
      }      
alpar@1402
   650
      invMap.insert(make_pair(val, key));
alpar@1402
   651
      Map::set(key, val);
alpar@1402
   652
    }
alpar@1402
   653
alpar@1402
   654
    /// \brief The getter function of the map.
alpar@1402
   655
    ///
alpar@1402
   656
    /// It gives back the value associated with the key.
deba@1413
   657
    const Value operator[](const Key& key) const {
alpar@1402
   658
      return Map::operator[](key);
alpar@1402
   659
    }
alpar@1402
   660
deba@1515
   661
  protected:
deba@1515
   662
alpar@1402
   663
    /// \brief Add a new key to the map.
alpar@1402
   664
    ///
alpar@1402
   665
    /// Add a new key to the map. It is called by the
alpar@1402
   666
    /// \c AlterationNotifier.
alpar@1402
   667
    virtual void add(const Key& key) {
alpar@1402
   668
      Map::add(key);
alpar@1402
   669
    }
alpar@1402
   670
alpar@1402
   671
    /// \brief Erase the key from the map.
alpar@1402
   672
    ///
alpar@1402
   673
    /// Erase the key to the map. It is called by the
alpar@1402
   674
    /// \c AlterationNotifier.
alpar@1402
   675
    virtual void erase(const Key& key) {
alpar@1402
   676
      Value val = Map::operator[](key);
deba@1413
   677
      typename Container::iterator it = invMap.find(val);
alpar@1402
   678
      if (it != invMap.end() && it->second == key) {
alpar@1402
   679
	invMap.erase(it);
alpar@1402
   680
      }
alpar@1402
   681
      Map::erase(key);
alpar@1402
   682
    }
alpar@1402
   683
alpar@1402
   684
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
   685
    ///
alpar@1402
   686
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
   687
    /// \c AlterationNotifier.
alpar@1402
   688
    virtual void clear() {
alpar@1402
   689
      invMap.clear();
alpar@1402
   690
      Map::clear();
alpar@1402
   691
    }
alpar@1402
   692
deba@1413
   693
  private:
deba@1413
   694
    
deba@1413
   695
    typedef std::map<Value, Key> Container;
deba@1413
   696
    Container invMap;    
deba@1413
   697
deba@1413
   698
  public:
deba@1413
   699
deba@1413
   700
    /// \brief The inverse map type.
deba@1413
   701
    ///
deba@1413
   702
    /// The inverse of this map. The subscript operator of the map
deba@1413
   703
    /// gives back always the item what was last assigned to the value. 
deba@1413
   704
    class InverseMap {
deba@1413
   705
    public:
deba@1413
   706
      /// \brief Constructor of the InverseMap.
deba@1413
   707
      ///
deba@1413
   708
      /// Constructor of the InverseMap.
deba@1413
   709
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
   710
deba@1413
   711
      /// The value type of the InverseMap.
deba@1413
   712
      typedef typename InvertableMap::Key Value;
deba@1413
   713
      /// The key type of the InverseMap.
deba@1413
   714
      typedef typename InvertableMap::Value Key; 
deba@1413
   715
deba@1413
   716
      /// \brief Subscript operator. 
deba@1413
   717
      ///
deba@1413
   718
      /// Subscript operator. It gives back always the item 
deba@1413
   719
      /// what was last assigned to the value.
deba@1413
   720
      Value operator[](const Key& key) const {
deba@1413
   721
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
   722
	return it->second;
deba@1413
   723
      }
deba@1413
   724
      
deba@1413
   725
    private:
deba@1413
   726
      const InvertableMap& inverted;
deba@1413
   727
    };
deba@1413
   728
alpar@1402
   729
    /// \brief It gives back the just readeable inverse map.
alpar@1402
   730
    ///
alpar@1402
   731
    /// It gives back the just readeable inverse map.
deba@1413
   732
    InverseMap inverse() const {
deba@1413
   733
      return InverseMap(*this);
alpar@1402
   734
    } 
alpar@1402
   735
alpar@1402
   736
deba@1413
   737
    
alpar@1402
   738
  };
alpar@1402
   739
alpar@1402
   740
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
   741
  /// item in the graph.
alpar@1402
   742
  ///
athos@1540
   743
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
   744
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
   745
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
   746
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
   747
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
   748
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
   749
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
   750
  /// with its member class \c InverseMap.
alpar@1402
   751
  ///
alpar@1402
   752
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
   753
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
alpar@1402
   754
  /// UndirEdge.
alpar@1402
   755
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
   756
  template <
alpar@1402
   757
    typename _Graph,   
alpar@1402
   758
    typename _Item,
deba@1413
   759
    typename _Map 
deba@1413
   760
    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
alpar@1402
   761
  >
alpar@1402
   762
  class DescriptorMap : protected _Map {
alpar@1402
   763
alpar@1402
   764
    typedef _Item Item;
alpar@1402
   765
    typedef _Map Map;
alpar@1402
   766
alpar@1402
   767
  public:
alpar@1402
   768
    /// The graph class of DescriptorMap.
alpar@1402
   769
    typedef _Graph Graph;
alpar@1402
   770
alpar@1402
   771
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
alpar@1402
   772
    typedef typename _Map::Key Key;
alpar@1402
   773
    /// The value type of DescriptorMap.
alpar@1402
   774
    typedef typename _Map::Value Value;
alpar@1402
   775
alpar@1402
   776
    /// \brief Constructor.
alpar@1402
   777
    ///
deba@1413
   778
    /// Constructor for descriptor map.
alpar@1402
   779
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
   780
      build();
alpar@1402
   781
    }
alpar@1402
   782
deba@1515
   783
  protected:
deba@1515
   784
alpar@1402
   785
    /// \brief Add a new key to the map.
alpar@1402
   786
    ///
alpar@1402
   787
    /// Add a new key to the map. It is called by the
alpar@1402
   788
    /// \c AlterationNotifier.
alpar@1402
   789
    virtual void add(const Item& item) {
alpar@1402
   790
      Map::add(item);
alpar@1402
   791
      Map::set(item, invMap.size());
alpar@1402
   792
      invMap.push_back(item);
alpar@1402
   793
    }
alpar@1402
   794
alpar@1402
   795
    /// \brief Erase the key from the map.
alpar@1402
   796
    ///
alpar@1402
   797
    /// Erase the key to the map. It is called by the
alpar@1402
   798
    /// \c AlterationNotifier.
alpar@1402
   799
    virtual void erase(const Item& item) {
alpar@1402
   800
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
   801
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
   802
      invMap.pop_back();
alpar@1402
   803
      Map::erase(item);
alpar@1402
   804
    }
alpar@1402
   805
alpar@1402
   806
    /// \brief Build the unique map.
alpar@1402
   807
    ///
alpar@1402
   808
    /// Build the unique map. It is called by the
alpar@1402
   809
    /// \c AlterationNotifier.
alpar@1402
   810
    virtual void build() {
alpar@1402
   811
      Map::build();
alpar@1402
   812
      Item it;
alpar@1402
   813
      const typename Map::Graph* graph = Map::getGraph(); 
alpar@1402
   814
      for (graph->first(it); it != INVALID; graph->next(it)) {
alpar@1402
   815
	Map::set(it, invMap.size());
alpar@1402
   816
	invMap.push_back(it);	
alpar@1402
   817
      }      
alpar@1402
   818
    }
alpar@1402
   819
    
alpar@1402
   820
    /// \brief Clear the keys from the map.
alpar@1402
   821
    ///
alpar@1402
   822
    /// Clear the keys from the map. It is called by the
alpar@1402
   823
    /// \c AlterationNotifier.
alpar@1402
   824
    virtual void clear() {
alpar@1402
   825
      invMap.clear();
alpar@1402
   826
      Map::clear();
alpar@1402
   827
    }
alpar@1402
   828
deba@1538
   829
  public:
deba@1538
   830
deba@1552
   831
    /// \brief Swaps the position of the two items in the map.
deba@1552
   832
    ///
deba@1552
   833
    /// Swaps the position of the two items in the map.
deba@1552
   834
    void swap(const Item& p, const Item& q) {
deba@1552
   835
      int pi = Map::operator[](p);
deba@1552
   836
      int qi = Map::operator[](q);
deba@1552
   837
      Map::set(p, qi);
deba@1552
   838
      invMap[qi] = p;
deba@1552
   839
      Map::set(q, pi);
deba@1552
   840
      invMap[pi] = q;
deba@1552
   841
    }
deba@1552
   842
alpar@1402
   843
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
   844
    ///
alpar@1402
   845
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
   846
    int operator[](const Item& item) const {
alpar@1402
   847
      return Map::operator[](item);
alpar@1402
   848
    }
alpar@1402
   849
    
deba@1413
   850
  private:
deba@1413
   851
deba@1413
   852
    typedef std::vector<Item> Container;
deba@1413
   853
    Container invMap;
deba@1413
   854
deba@1413
   855
  public:
athos@1540
   856
    /// \brief The inverse map type of DescriptorMap.
deba@1413
   857
    ///
athos@1540
   858
    /// The inverse map type of DescriptorMap.
deba@1413
   859
    class InverseMap {
deba@1413
   860
    public:
deba@1413
   861
      /// \brief Constructor of the InverseMap.
deba@1413
   862
      ///
deba@1413
   863
      /// Constructor of the InverseMap.
deba@1413
   864
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
   865
	: inverted(_inverted) {}
deba@1413
   866
deba@1413
   867
deba@1413
   868
      /// The value type of the InverseMap.
deba@1413
   869
      typedef typename DescriptorMap::Key Value;
deba@1413
   870
      /// The key type of the InverseMap.
deba@1413
   871
      typedef typename DescriptorMap::Value Key; 
deba@1413
   872
deba@1413
   873
      /// \brief Subscript operator. 
deba@1413
   874
      ///
deba@1413
   875
      /// Subscript operator. It gives back the item 
deba@1413
   876
      /// that the descriptor belongs to currently.
deba@1413
   877
      Value operator[](const Key& key) const {
deba@1413
   878
	return inverted.invMap[key];
deba@1413
   879
      }
deba@1470
   880
deba@1470
   881
      /// \brief Size of the map.
deba@1470
   882
      ///
deba@1470
   883
      /// Returns the size of the map.
deba@1552
   884
      int size() const {
deba@1470
   885
	return inverted.invMap.size();
deba@1470
   886
      }
deba@1413
   887
      
deba@1413
   888
    private:
deba@1413
   889
      const DescriptorMap& inverted;
deba@1413
   890
    };
deba@1413
   891
alpar@1402
   892
    /// \brief Gives back the inverse of the map.
alpar@1402
   893
    ///
alpar@1402
   894
    /// Gives back the inverse of the map.
alpar@1402
   895
    const InverseMap inverse() const {
deba@1413
   896
      return InverseMap(*this);
alpar@1402
   897
    }
alpar@1402
   898
  };
alpar@1402
   899
alpar@1402
   900
  /// \brief Returns the source of the given edge.
alpar@1402
   901
  ///
alpar@1402
   902
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
   903
  /// \author Balazs Dezso
alpar@1402
   904
  template <typename Graph>
alpar@1402
   905
  class SourceMap {
alpar@1402
   906
  public:
deba@1419
   907
deba@1419
   908
    typedef True NeedCopy;
deba@1419
   909
alpar@1402
   910
    typedef typename Graph::Node Value;
alpar@1402
   911
    typedef typename Graph::Edge Key;
alpar@1402
   912
alpar@1402
   913
    /// \brief Constructor
alpar@1402
   914
    ///
alpar@1402
   915
    /// Constructor
alpar@1402
   916
    /// \param _graph The graph that the map belongs to.
alpar@1402
   917
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   918
alpar@1402
   919
    /// \brief The subscript operator.
alpar@1402
   920
    ///
alpar@1402
   921
    /// The subscript operator.
alpar@1402
   922
    /// \param edge The edge 
alpar@1402
   923
    /// \return The source of the edge 
deba@1679
   924
    Value operator[](const Key& edge) const {
alpar@1402
   925
      return graph.source(edge);
alpar@1402
   926
    }
alpar@1402
   927
alpar@1402
   928
  private:
alpar@1402
   929
    const Graph& graph;
alpar@1402
   930
  };
alpar@1402
   931
alpar@1402
   932
  /// \brief Returns a \ref SourceMap class
alpar@1402
   933
  ///
alpar@1402
   934
  /// This function just returns an \ref SourceMap class.
alpar@1402
   935
  /// \relates SourceMap
alpar@1402
   936
  template <typename Graph>
alpar@1402
   937
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
   938
    return SourceMap<Graph>(graph);
alpar@1402
   939
  } 
alpar@1402
   940
alpar@1402
   941
  /// \brief Returns the target of the given edge.
alpar@1402
   942
  ///
alpar@1402
   943
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
   944
  /// \author Balazs Dezso
alpar@1402
   945
  template <typename Graph>
alpar@1402
   946
  class TargetMap {
alpar@1402
   947
  public:
deba@1419
   948
deba@1419
   949
    typedef True NeedCopy;
deba@1419
   950
alpar@1402
   951
    typedef typename Graph::Node Value;
alpar@1402
   952
    typedef typename Graph::Edge Key;
alpar@1402
   953
alpar@1402
   954
    /// \brief Constructor
alpar@1402
   955
    ///
alpar@1402
   956
    /// Constructor
alpar@1402
   957
    /// \param _graph The graph that the map belongs to.
alpar@1402
   958
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   959
alpar@1402
   960
    /// \brief The subscript operator.
alpar@1402
   961
    ///
alpar@1402
   962
    /// The subscript operator.
alpar@1536
   963
    /// \param e The edge 
alpar@1402
   964
    /// \return The target of the edge 
deba@1679
   965
    Value operator[](const Key& e) const {
alpar@1536
   966
      return graph.target(e);
alpar@1402
   967
    }
alpar@1402
   968
alpar@1402
   969
  private:
alpar@1402
   970
    const Graph& graph;
alpar@1402
   971
  };
alpar@1402
   972
alpar@1402
   973
  /// \brief Returns a \ref TargetMap class
deba@1515
   974
  ///
athos@1540
   975
  /// This function just returns a \ref TargetMap class.
alpar@1402
   976
  /// \relates TargetMap
alpar@1402
   977
  template <typename Graph>
alpar@1402
   978
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
   979
    return TargetMap<Graph>(graph);
alpar@1402
   980
  }
alpar@1402
   981
athos@1540
   982
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
   983
  ///
athos@1540
   984
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
   985
  /// \author Balazs Dezso
deba@1419
   986
  template <typename Graph>
deba@1419
   987
  class ForwardMap {
deba@1419
   988
  public:
deba@1419
   989
deba@1419
   990
    typedef True NeedCopy;
deba@1419
   991
deba@1419
   992
    typedef typename Graph::Edge Value;
deba@1419
   993
    typedef typename Graph::UndirEdge Key;
deba@1419
   994
deba@1419
   995
    /// \brief Constructor
deba@1419
   996
    ///
deba@1419
   997
    /// Constructor
deba@1419
   998
    /// \param _graph The graph that the map belongs to.
deba@1419
   999
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1000
deba@1419
  1001
    /// \brief The subscript operator.
deba@1419
  1002
    ///
deba@1419
  1003
    /// The subscript operator.
deba@1419
  1004
    /// \param key An undirected edge 
deba@1419
  1005
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  1006
    Value operator[](const Key& key) const {
deba@1627
  1007
      return graph.direct(key, true);
deba@1419
  1008
    }
deba@1419
  1009
deba@1419
  1010
  private:
deba@1419
  1011
    const Graph& graph;
deba@1419
  1012
  };
deba@1419
  1013
deba@1419
  1014
  /// \brief Returns a \ref ForwardMap class
deba@1515
  1015
  ///
deba@1419
  1016
  /// This function just returns an \ref ForwardMap class.
deba@1419
  1017
  /// \relates ForwardMap
deba@1419
  1018
  template <typename Graph>
deba@1419
  1019
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  1020
    return ForwardMap<Graph>(graph);
deba@1419
  1021
  }
deba@1419
  1022
athos@1540
  1023
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1024
  ///
athos@1540
  1025
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1026
  /// \author Balazs Dezso
deba@1419
  1027
  template <typename Graph>
deba@1419
  1028
  class BackwardMap {
deba@1419
  1029
  public:
deba@1419
  1030
    typedef True NeedCopy;
deba@1419
  1031
deba@1419
  1032
    typedef typename Graph::Edge Value;
deba@1419
  1033
    typedef typename Graph::UndirEdge Key;
deba@1419
  1034
deba@1419
  1035
    /// \brief Constructor
deba@1419
  1036
    ///
deba@1419
  1037
    /// Constructor
deba@1419
  1038
    /// \param _graph The graph that the map belongs to.
deba@1419
  1039
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1040
deba@1419
  1041
    /// \brief The subscript operator.
deba@1419
  1042
    ///
deba@1419
  1043
    /// The subscript operator.
deba@1419
  1044
    /// \param key An undirected edge 
deba@1419
  1045
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  1046
    Value operator[](const Key& key) const {
deba@1627
  1047
      return graph.direct(key, false);
deba@1419
  1048
    }
deba@1419
  1049
deba@1419
  1050
  private:
deba@1419
  1051
    const Graph& graph;
deba@1419
  1052
  };
deba@1419
  1053
deba@1419
  1054
  /// \brief Returns a \ref BackwardMap class
deba@1419
  1055
athos@1540
  1056
  /// This function just returns a \ref BackwardMap class.
deba@1419
  1057
  /// \relates BackwardMap
deba@1419
  1058
  template <typename Graph>
deba@1419
  1059
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  1060
    return BackwardMap<Graph>(graph);
deba@1419
  1061
  }
deba@1419
  1062
deba@1515
  1063
  template <typename _Graph>
deba@1515
  1064
  class DegMapBase {
deba@1515
  1065
  public:
deba@1515
  1066
    
deba@1515
  1067
    typedef _Graph Graph;
alpar@1402
  1068
deba@1515
  1069
  protected:
alpar@1453
  1070
deba@1515
  1071
    typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@1515
  1072
    
deba@1515
  1073
  };
alpar@1453
  1074
deba@1515
  1075
  /// \brief Map of the node in-degrees.
alpar@1453
  1076
  ///
athos@1540
  1077
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  1078
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1079
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1080
  /// whenever the graph changes.
deba@1515
  1081
  ///
alpar@1674
  1082
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
alpar@1674
  1083
  /// alternative ways to mogify the graph. The correct behavior of InDegMap
alpar@1674
  1084
  /// is not guarantied if these additional featureas are used. For example
alpar@1674
  1085
  /// the funstions \ref ListGraph::changeSource() "changeSource()",
alpar@1674
  1086
  /// \ref ListGraph::changeTarget() "changeTarget()" and
alpar@1674
  1087
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
alpar@1674
  1088
  /// of \ref ListGraph will \e not update the degree values correctly.
alpar@1674
  1089
  ///
deba@1515
  1090
  /// \sa OutDegMap
deba@1515
  1091
alpar@1453
  1092
  template <typename _Graph>
deba@1515
  1093
  class InDegMap  
deba@1515
  1094
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1095
alpar@1453
  1096
  public:
deba@1515
  1097
    
deba@1515
  1098
    typedef _Graph Graph;
alpar@1453
  1099
    typedef int Value;
deba@1515
  1100
    typedef typename Graph::Node Key;
deba@1515
  1101
deba@1515
  1102
  private:
deba@1515
  1103
deba@1515
  1104
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1105
    public:
deba@1515
  1106
deba@1515
  1107
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1108
deba@1515
  1109
      typedef typename Parent::Key Key;
deba@1515
  1110
      typedef typename Parent::Value Value;
deba@1515
  1111
      
deba@1515
  1112
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1113
      
deba@1515
  1114
      void add(const Key& key) {
deba@1515
  1115
	Parent::add(key);
deba@1515
  1116
	Parent::set(key, 0);
deba@1515
  1117
      }
deba@1515
  1118
    };
deba@1515
  1119
deba@1515
  1120
  public:
alpar@1453
  1121
alpar@1453
  1122
    /// \brief Constructor.
alpar@1453
  1123
    ///
alpar@1453
  1124
    /// Constructor for creating in-degree map.
deba@1515
  1125
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1126
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1127
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1128
      
deba@1515
  1129
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1130
	deg[it] = countInEdges(graph, it);
deba@1515
  1131
      }
alpar@1453
  1132
    }
alpar@1453
  1133
deba@1515
  1134
    virtual ~InDegMap() {
alpar@1459
  1135
      AlterationNotifier<typename _Graph::Edge>::
alpar@1453
  1136
	ObserverBase::detach();
alpar@1453
  1137
    }
alpar@1453
  1138
    
alpar@1459
  1139
    /// Gives back the in-degree of a Node.
deba@1515
  1140
    int operator[](const Key& key) const {
deba@1515
  1141
      return deg[key];
alpar@1459
  1142
    }
alpar@1453
  1143
alpar@1453
  1144
  protected:
deba@1515
  1145
    
deba@1515
  1146
    typedef typename Graph::Edge Edge;
deba@1515
  1147
deba@1515
  1148
    virtual void add(const Edge& edge) {
deba@1515
  1149
      ++deg[graph.target(edge)];
alpar@1453
  1150
    }
alpar@1453
  1151
deba@1515
  1152
    virtual void erase(const Edge& edge) {
deba@1515
  1153
      --deg[graph.target(edge)];
deba@1515
  1154
    }
deba@1515
  1155
deba@1515
  1156
deba@1515
  1157
    virtual void build() {
deba@1515
  1158
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1159
	deg[it] = countInEdges(graph, it);
deba@1515
  1160
      }      
deba@1515
  1161
    }
deba@1515
  1162
deba@1515
  1163
    virtual void clear() {
deba@1515
  1164
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1165
	deg[it] = 0;
deba@1515
  1166
      }
deba@1515
  1167
    }
deba@1515
  1168
  private:
alpar@1506
  1169
    
deba@1515
  1170
    const _Graph& graph;
deba@1515
  1171
    AutoNodeMap deg;
alpar@1459
  1172
  };
alpar@1459
  1173
deba@1515
  1174
  /// \brief Map of the node out-degrees.
deba@1515
  1175
  ///
athos@1540
  1176
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1177
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1178
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1179
  /// whenever the graph changes.
deba@1515
  1180
  ///
alpar@1674
  1181
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
alpar@1674
  1182
  /// alternative ways to mogify the graph. The correct behavior of OutDegMap
alpar@1674
  1183
  /// is not guarantied if these additional featureas are used. For example
alpar@1674
  1184
  /// the funstions \ref ListGraph::changeSource() "changeSource()",
alpar@1674
  1185
  /// \ref ListGraph::changeTarget() "changeTarget()" and
alpar@1674
  1186
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
alpar@1674
  1187
  /// of \ref ListGraph will \e not update the degree values correctly.
alpar@1674
  1188
  ///
alpar@1555
  1189
  /// \sa InDegMap
alpar@1459
  1190
alpar@1459
  1191
  template <typename _Graph>
deba@1515
  1192
  class OutDegMap  
deba@1515
  1193
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1194
alpar@1459
  1195
  public:
deba@1515
  1196
    
deba@1515
  1197
    typedef _Graph Graph;
alpar@1459
  1198
    typedef int Value;
deba@1515
  1199
    typedef typename Graph::Node Key;
deba@1515
  1200
deba@1515
  1201
  private:
deba@1515
  1202
deba@1515
  1203
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1204
    public:
deba@1515
  1205
deba@1515
  1206
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1207
deba@1515
  1208
      typedef typename Parent::Key Key;
deba@1515
  1209
      typedef typename Parent::Value Value;
deba@1515
  1210
      
deba@1515
  1211
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1212
      
deba@1515
  1213
      void add(const Key& key) {
deba@1515
  1214
	Parent::add(key);
deba@1515
  1215
	Parent::set(key, 0);
deba@1515
  1216
      }
deba@1515
  1217
    };
deba@1515
  1218
deba@1515
  1219
  public:
alpar@1459
  1220
alpar@1459
  1221
    /// \brief Constructor.
alpar@1459
  1222
    ///
alpar@1459
  1223
    /// Constructor for creating out-degree map.
deba@1515
  1224
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1225
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1226
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1227
      
deba@1515
  1228
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1229
	deg[it] = countOutEdges(graph, it);
deba@1515
  1230
      }
alpar@1459
  1231
    }
alpar@1459
  1232
deba@1515
  1233
    virtual ~OutDegMap() {
alpar@1459
  1234
      AlterationNotifier<typename _Graph::Edge>::
alpar@1459
  1235
	ObserverBase::detach();
alpar@1459
  1236
    }
alpar@1459
  1237
    
alpar@1459
  1238
    /// Gives back the in-degree of a Node.
deba@1515
  1239
    int operator[](const Key& key) const {
deba@1515
  1240
      return deg[key];
alpar@1459
  1241
    }
alpar@1459
  1242
alpar@1459
  1243
  protected:
deba@1515
  1244
    
deba@1515
  1245
    typedef typename Graph::Edge Edge;
deba@1515
  1246
deba@1515
  1247
    virtual void add(const Edge& edge) {
deba@1515
  1248
      ++deg[graph.source(edge)];
alpar@1459
  1249
    }
alpar@1459
  1250
deba@1515
  1251
    virtual void erase(const Edge& edge) {
deba@1515
  1252
      --deg[graph.source(edge)];
deba@1515
  1253
    }
deba@1515
  1254
deba@1515
  1255
deba@1515
  1256
    virtual void build() {
deba@1515
  1257
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1258
	deg[it] = countOutEdges(graph, it);
deba@1515
  1259
      }      
deba@1515
  1260
    }
deba@1515
  1261
deba@1515
  1262
    virtual void clear() {
deba@1515
  1263
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1264
	deg[it] = 0;
deba@1515
  1265
      }
deba@1515
  1266
    }
deba@1515
  1267
  private:
alpar@1506
  1268
    
deba@1515
  1269
    const _Graph& graph;
deba@1515
  1270
    AutoNodeMap deg;
alpar@1453
  1271
  };
alpar@1453
  1272
alpar@1402
  1273
  /// @}
alpar@1402
  1274
alpar@947
  1275
} //END OF NAMESPACE LEMON
klao@946
  1276
klao@946
  1277
#endif