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