lemon/graph_utils.h
author alpar
Mon, 06 Feb 2006 09:10:43 +0000
changeset 1959 264811b995f3
parent 1946 17eb3eaad9f8
child 1981 81c8efe92706
permissions -rw-r--r--
Spellcheck
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     8
 *
klao@946
     9
 * Permission to use, modify and distribute this software is granted
klao@946
    10
 * provided that this copyright notice appears in all copies. For
klao@946
    11
 * precise terms see the accompanying LICENSE file.
klao@946
    12
 *
klao@946
    13
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    14
 * express or implied, and with no claim as to its suitability for any
klao@946
    15
 * purpose.
klao@946
    16
 *
klao@946
    17
 */
klao@946
    18
klao@946
    19
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    20
#define LEMON_GRAPH_UTILS_H
klao@946
    21
klao@946
    22
#include <iterator>
deba@1419
    23
#include <vector>
alpar@1402
    24
#include <map>
deba@1695
    25
#include <cmath>
klao@946
    26
klao@946
    27
#include <lemon/invalid.h>
klao@977
    28
#include <lemon/utility.h>
deba@1413
    29
#include <lemon/maps.h>
deba@1720
    30
#include <lemon/traits.h>
alpar@1459
    31
#include <lemon/bits/alteration_notifier.h>
klao@946
    32
alpar@947
    33
///\ingroup gutils
klao@946
    34
///\file
alpar@947
    35
///\brief Graph utilities.
klao@946
    36
///
alpar@964
    37
///
klao@946
    38
klao@946
    39
klao@946
    40
namespace lemon {
klao@946
    41
deba@1267
    42
  /// \addtogroup gutils
deba@1267
    43
  /// @{
alpar@947
    44
alpar@1756
    45
  ///Creates convenience typedefs for the graph types and iterators
alpar@1756
    46
alpar@1756
    47
  ///This \c \#define creates convenience typedefs for the following types
alpar@1756
    48
  ///of \c Graph: \c Node,  \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
alpar@1804
    49
  ///\c OutEdgeIt,  \c BoolNodeMap,  \c IntNodeMap,  \c DoubleNodeMap,
alpar@1804
    50
  ///\c BoolEdgeMap, \c IntEdgeMap,  \c DoubleEdgeMap.  
alpar@1756
    51
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    52
  ///\code
alpar@1756
    53
  ///  GRAPH_TYPEDEFS(typename G)
alpar@1756
    54
  ///\endcode
alpar@1756
    55
  ///
alpar@1756
    56
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    57
  ///template typedefs in C++.
alpar@1804
    58
#define GRAPH_TYPEDEFS(Graph)				\
alpar@1804
    59
  typedef Graph::     Node      Node;			\
alpar@1804
    60
    typedef Graph::   NodeIt    NodeIt;			\
alpar@1804
    61
    typedef Graph::   Edge      Edge;			\
alpar@1804
    62
    typedef Graph::   EdgeIt    EdgeIt;			\
alpar@1804
    63
    typedef Graph:: InEdgeIt  InEdgeIt;			\
alpar@1811
    64
    typedef Graph::OutEdgeIt OutEdgeIt;			
alpar@1811
    65
//     typedef Graph::template NodeMap<bool> BoolNodeMap;	       
alpar@1811
    66
//     typedef Graph::template NodeMap<int> IntNodeMap;	       
alpar@1811
    67
//     typedef Graph::template NodeMap<double> DoubleNodeMap;  
alpar@1811
    68
//     typedef Graph::template EdgeMap<bool> BoolEdgeMap;	       
alpar@1811
    69
//     typedef Graph::template EdgeMap<int> IntEdgeMap;	       
alpar@1811
    70
//     typedef Graph::template EdgeMap<double> DoubleEdgeMap;
alpar@1756
    71
  
alpar@1756
    72
  ///Creates convenience typedefs for the undirected graph types and iterators
alpar@1756
    73
alpar@1756
    74
  ///This \c \#define creates the same convenience typedefs as defined by
alpar@1756
    75
  ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
klao@1909
    76
  ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
klao@1909
    77
  ///\c BoolUEdgeMap, \c IntUEdgeMap,  \c DoubleUEdgeMap.  
alpar@1756
    78
  ///
alpar@1756
    79
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    80
  ///\code
alpar@1756
    81
  ///  UNDIRGRAPH_TYPEDEFS(typename G)
alpar@1756
    82
  ///\endcode
alpar@1756
    83
  ///
alpar@1756
    84
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    85
  ///template typedefs in C++.
alpar@1804
    86
#define UNDIRGRAPH_TYPEDEFS(Graph)				\
alpar@1804
    87
  GRAPH_TYPEDEFS(Graph)						\
klao@1909
    88
    typedef Graph:: UEdge   UEdge;			\
klao@1909
    89
    typedef Graph:: UEdgeIt UEdgeIt;			\
alpar@1811
    90
    typedef Graph:: IncEdgeIt   IncEdgeIt;		       
klao@1909
    91
//     typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;	 
klao@1909
    92
//     typedef Graph::template UEdgeMap<int> IntUEdgeMap;
klao@1909
    93
//     typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
alpar@1804
    94
  
alpar@1756
    95
alpar@1756
    96
klao@946
    97
  /// \brief Function to count the items in the graph.
klao@946
    98
  ///
athos@1540
    99
  /// This function counts the items (nodes, edges etc) in the graph.
klao@946
   100
  /// The complexity of the function is O(n) because
klao@946
   101
  /// it iterates on all of the items.
klao@946
   102
klao@946
   103
  template <typename Graph, typename ItemIt>
klao@977
   104
  inline int countItems(const Graph& g) {
klao@946
   105
    int num = 0;
klao@977
   106
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
   107
      ++num;
klao@946
   108
    }
klao@946
   109
    return num;
klao@946
   110
  }
klao@946
   111
klao@977
   112
  // Node counting:
klao@977
   113
klao@977
   114
  template <typename Graph>
deba@1829
   115
  inline typename enable_if<typename Graph::NodeNumTag, int>::type
klao@977
   116
  _countNodes(const Graph &g) {
klao@977
   117
    return g.nodeNum();
klao@977
   118
  }
klao@977
   119
klao@977
   120
  template <typename Graph>
klao@977
   121
  inline int _countNodes(Wrap<Graph> w) {
klao@977
   122
    return countItems<Graph, typename Graph::NodeIt>(w.value);
klao@977
   123
  }
klao@977
   124
klao@946
   125
  /// \brief Function to count the nodes in the graph.
klao@946
   126
  ///
klao@946
   127
  /// This function counts the nodes in the graph.
klao@946
   128
  /// The complexity of the function is O(n) but for some
athos@1526
   129
  /// graph structures it is specialized to run in O(1).
klao@977
   130
  ///
klao@977
   131
  /// \todo refer how to specialize it
klao@946
   132
klao@946
   133
  template <typename Graph>
klao@977
   134
  inline int countNodes(const Graph& g) {
klao@977
   135
    return _countNodes<Graph>(g);
klao@977
   136
  }
klao@977
   137
klao@977
   138
  // Edge counting:
klao@977
   139
klao@977
   140
  template <typename Graph>
deba@1829
   141
  inline typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@977
   142
  _countEdges(const Graph &g) {
klao@977
   143
    return g.edgeNum();
klao@977
   144
  }
klao@977
   145
klao@977
   146
  template <typename Graph>
klao@977
   147
  inline int _countEdges(Wrap<Graph> w) {
klao@977
   148
    return countItems<Graph, typename Graph::EdgeIt>(w.value);
klao@946
   149
  }
klao@946
   150
klao@946
   151
  /// \brief Function to count the edges in the graph.
klao@946
   152
  ///
klao@946
   153
  /// This function counts the edges in the graph.
klao@946
   154
  /// The complexity of the function is O(e) but for some
athos@1526
   155
  /// graph structures it is specialized to run in O(1).
klao@977
   156
klao@946
   157
  template <typename Graph>
klao@977
   158
  inline int countEdges(const Graph& g) {
klao@977
   159
    return _countEdges<Graph>(g);
klao@946
   160
  }
klao@946
   161
klao@1053
   162
  // Undirected edge counting:
klao@1053
   163
klao@1053
   164
  template <typename Graph>
klao@1053
   165
  inline
klao@1053
   166
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@1909
   167
  _countUEdges(const Graph &g) {
klao@1909
   168
    return g.uEdgeNum();
klao@1053
   169
  }
klao@1053
   170
klao@1053
   171
  template <typename Graph>
klao@1909
   172
  inline int _countUEdges(Wrap<Graph> w) {
klao@1909
   173
    return countItems<Graph, typename Graph::UEdgeIt>(w.value);
klao@1053
   174
  }
klao@1053
   175
athos@1526
   176
  /// \brief Function to count the undirected edges in the graph.
klao@946
   177
  ///
athos@1526
   178
  /// This function counts the undirected edges in the graph.
klao@946
   179
  /// The complexity of the function is O(e) but for some
athos@1540
   180
  /// graph structures it is specialized to run in O(1).
klao@1053
   181
klao@946
   182
  template <typename Graph>
klao@1909
   183
  inline int countUEdges(const Graph& g) {
klao@1909
   184
    return _countUEdges<Graph>(g);
klao@946
   185
  }
klao@946
   186
klao@977
   187
klao@1053
   188
klao@946
   189
  template <typename Graph, typename DegIt>
klao@946
   190
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   191
    int num = 0;
klao@946
   192
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   193
      ++num;
klao@946
   194
    }
klao@946
   195
    return num;
klao@946
   196
  }
alpar@967
   197
deba@1531
   198
  /// \brief Function to count the number of the out-edges from node \c n.
deba@1531
   199
  ///
deba@1531
   200
  /// This function counts the number of the out-edges from node \c n
deba@1531
   201
  /// in the graph.  
deba@1531
   202
  template <typename Graph>
deba@1531
   203
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   204
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
deba@1531
   205
  }
deba@1531
   206
deba@1531
   207
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1531
   208
  ///
deba@1531
   209
  /// This function counts the number of the in-edges to node \c n
deba@1531
   210
  /// in the graph.  
deba@1531
   211
  template <typename Graph>
deba@1531
   212
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   213
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
deba@1531
   214
  }
deba@1531
   215
deba@1704
   216
  /// \brief Function to count the number of the inc-edges to node \c n.
deba@1679
   217
  ///
deba@1704
   218
  /// This function counts the number of the inc-edges to node \c n
deba@1679
   219
  /// in the graph.  
deba@1679
   220
  template <typename Graph>
deba@1679
   221
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1679
   222
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
deba@1679
   223
  }
deba@1679
   224
deba@1531
   225
deba@1565
   226
  template <typename Graph>
deba@1565
   227
  inline
deba@1565
   228
  typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type 
deba@1565
   229
  _findEdge(const Graph &g,
deba@1565
   230
	    typename Graph::Node u, typename Graph::Node v,
deba@1565
   231
	    typename Graph::Edge prev = INVALID) {
deba@1565
   232
    return g.findEdge(u, v, prev);
deba@1565
   233
  }
alpar@967
   234
deba@1565
   235
  template <typename Graph>
deba@1565
   236
  inline typename Graph::Edge 
deba@1565
   237
  _findEdge(Wrap<Graph> w,
deba@1565
   238
	    typename Graph::Node u, 
deba@1565
   239
	    typename Graph::Node v,
deba@1565
   240
	    typename Graph::Edge prev = INVALID) {
deba@1565
   241
    const Graph& g = w.value;
deba@1565
   242
    if (prev == INVALID) {
deba@1565
   243
      typename Graph::OutEdgeIt e(g, u);
deba@1565
   244
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   245
      return e;
deba@1565
   246
    } else {
deba@1565
   247
      typename Graph::OutEdgeIt e(g, prev); ++e;
deba@1565
   248
      while (e != INVALID && g.target(e) != v) ++e;
deba@1565
   249
      return e;
deba@1565
   250
    }
deba@1565
   251
  }
deba@1565
   252
deba@1565
   253
  /// \brief Finds an edge between two nodes of a graph.
deba@1565
   254
  ///
alpar@967
   255
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   256
  ///
alpar@967
   257
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   258
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   259
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   260
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   261
  ///
alpar@967
   262
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   263
  ///\code
alpar@967
   264
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   265
  ///   ...
alpar@967
   266
  /// }
alpar@1946
   267
  ///\endcode
deba@1565
   268
  // /// \todo We may want to use the "GraphBase" 
deba@1565
   269
  // /// interface here...
alpar@967
   270
  template <typename Graph>
deba@1565
   271
  inline typename Graph::Edge findEdge(const Graph &g,
deba@1565
   272
				       typename Graph::Node u, 
deba@1565
   273
				       typename Graph::Node v,
deba@1565
   274
				       typename Graph::Edge prev = INVALID) {
deba@1565
   275
    return _findEdge<Graph>(g, u, v, prev);
alpar@967
   276
  }
deba@1531
   277
deba@1565
   278
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   279
  ///
deba@1565
   280
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   281
  /// higher level interface for the findEdge() function. You can
alpar@1591
   282
  /// use it the following way:
alpar@1946
   283
  ///\code
deba@1565
   284
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   285
  ///   ...
deba@1565
   286
  /// }
alpar@1946
   287
  ///\endcode
deba@1565
   288
  ///
deba@1565
   289
  /// \author Balazs Dezso 
deba@1565
   290
  template <typename _Graph>
deba@1565
   291
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   292
  public:
deba@1565
   293
deba@1565
   294
    typedef _Graph Graph;
deba@1565
   295
    typedef typename Graph::Edge Parent;
deba@1565
   296
deba@1565
   297
    typedef typename Graph::Edge Edge;
deba@1565
   298
    typedef typename Graph::Node Node;
deba@1565
   299
deba@1565
   300
    /// \brief Constructor.
deba@1565
   301
    ///
deba@1565
   302
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   303
    /// connects the \c u and \c v node.
deba@1565
   304
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   305
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   306
    }
deba@1565
   307
deba@1565
   308
    /// \brief Constructor.
deba@1565
   309
    ///
deba@1565
   310
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   311
    /// the \c e edge.
deba@1565
   312
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   313
    
deba@1565
   314
    /// \brief Increment operator.
deba@1565
   315
    ///
deba@1565
   316
    /// It increments the iterator and gives back the next edge.
deba@1565
   317
    ConEdgeIt& operator++() {
deba@1565
   318
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   319
				 graph.target(*this), *this));
deba@1565
   320
      return *this;
deba@1565
   321
    }
deba@1565
   322
  private:
deba@1565
   323
    const Graph& graph;
deba@1565
   324
  };
deba@1565
   325
deba@1704
   326
  template <typename Graph>
deba@1704
   327
  inline
deba@1704
   328
  typename enable_if<
deba@1704
   329
    typename Graph::FindEdgeTag, 
klao@1909
   330
    typename Graph::UEdge>::type 
klao@1909
   331
  _findUEdge(const Graph &g,
deba@1704
   332
	    typename Graph::Node u, typename Graph::Node v,
klao@1909
   333
	    typename Graph::UEdge prev = INVALID) {
klao@1909
   334
    return g.findUEdge(u, v, prev);
deba@1704
   335
  }
deba@1704
   336
deba@1704
   337
  template <typename Graph>
klao@1909
   338
  inline typename Graph::UEdge 
klao@1909
   339
  _findUEdge(Wrap<Graph> w,
deba@1704
   340
	    typename Graph::Node u, 
deba@1704
   341
	    typename Graph::Node v,
klao@1909
   342
	    typename Graph::UEdge prev = INVALID) {
deba@1704
   343
    const Graph& g = w.value;
deba@1704
   344
    if (prev == INVALID) {
deba@1704
   345
      typename Graph::OutEdgeIt e(g, u);
deba@1704
   346
      while (e != INVALID && g.target(e) != v) ++e;
deba@1704
   347
      return e;
deba@1704
   348
    } else {
deba@1704
   349
      typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
deba@1704
   350
      while (e != INVALID && g.target(e) != v) ++e;
deba@1704
   351
      return e;
deba@1704
   352
    }
deba@1704
   353
  }
deba@1704
   354
klao@1909
   355
  /// \brief Finds an uedge between two nodes of a graph.
deba@1704
   356
  ///
klao@1909
   357
  /// Finds an uedge from node \c u to node \c v in graph \c g.
deba@1704
   358
  ///
deba@1704
   359
  /// If \c prev is \ref INVALID (this is the default value), then
deba@1704
   360
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
deba@1704
   361
  /// the next edge from \c u to \c v after \c prev.
deba@1704
   362
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@1704
   363
  ///
deba@1704
   364
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   365
  ///\code
klao@1909
   366
  /// for(UEdge e = findUEdge(g,u,v); e != INVALID; 
klao@1909
   367
  ///     e = findUEdge(g,u,v,e)) {
deba@1704
   368
  ///   ...
deba@1704
   369
  /// }
alpar@1946
   370
  ///\endcode
deba@1704
   371
  // /// \todo We may want to use the "GraphBase" 
deba@1704
   372
  // /// interface here...
deba@1704
   373
  template <typename Graph>
klao@1909
   374
  inline typename Graph::UEdge 
klao@1909
   375
  findUEdge(const Graph &g,
deba@1704
   376
		typename Graph::Node u, 
deba@1704
   377
		typename Graph::Node v,
klao@1909
   378
		typename Graph::UEdge prev = INVALID) {
klao@1909
   379
    return _findUEdge<Graph>(g, u, v, prev);
deba@1704
   380
  }
deba@1704
   381
klao@1909
   382
  /// \brief Iterator for iterating on uedges connected the same nodes.
deba@1704
   383
  ///
klao@1909
   384
  /// Iterator for iterating on uedges connected the same nodes. It is 
klao@1909
   385
  /// higher level interface for the findUEdge() function. You can
deba@1704
   386
  /// use it the following way:
alpar@1946
   387
  ///\code
klao@1909
   388
  /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1704
   389
  ///   ...
deba@1704
   390
  /// }
alpar@1946
   391
  ///\endcode
deba@1704
   392
  ///
deba@1704
   393
  /// \author Balazs Dezso 
deba@1704
   394
  template <typename _Graph>
klao@1909
   395
  class ConUEdgeIt : public _Graph::UEdge {
deba@1704
   396
  public:
deba@1704
   397
deba@1704
   398
    typedef _Graph Graph;
klao@1909
   399
    typedef typename Graph::UEdge Parent;
deba@1704
   400
klao@1909
   401
    typedef typename Graph::UEdge UEdge;
deba@1704
   402
    typedef typename Graph::Node Node;
deba@1704
   403
deba@1704
   404
    /// \brief Constructor.
deba@1704
   405
    ///
klao@1909
   406
    /// Construct a new ConUEdgeIt iterating on the edges which
deba@1704
   407
    /// connects the \c u and \c v node.
klao@1909
   408
    ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
klao@1909
   409
      Parent::operator=(findUEdge(graph, u, v));
deba@1704
   410
    }
deba@1704
   411
deba@1704
   412
    /// \brief Constructor.
deba@1704
   413
    ///
klao@1909
   414
    /// Construct a new ConUEdgeIt which continues the iterating from 
deba@1704
   415
    /// the \c e edge.
klao@1909
   416
    ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
deba@1704
   417
    
deba@1704
   418
    /// \brief Increment operator.
deba@1704
   419
    ///
deba@1704
   420
    /// It increments the iterator and gives back the next edge.
klao@1909
   421
    ConUEdgeIt& operator++() {
klao@1909
   422
      Parent::operator=(findUEdge(graph, graph.source(*this), 
deba@1829
   423
				      graph.target(*this), *this));
deba@1704
   424
      return *this;
deba@1704
   425
    }
deba@1704
   426
  private:
deba@1704
   427
    const Graph& graph;
deba@1704
   428
  };
deba@1704
   429
athos@1540
   430
  /// \brief Copy a map.
alpar@964
   431
  ///
alpar@1547
   432
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   433
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   434
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   435
  template <typename Target, typename Source, 
deba@1531
   436
	    typename ItemIt, typename Ref>	    
deba@1531
   437
  void copyMap(Target& target, const Source& source, 
deba@1531
   438
	       ItemIt it, const Ref& ref) {
deba@1531
   439
    for (; it != INVALID; ++it) {
deba@1531
   440
      target[ref[it]] = source[it];
klao@946
   441
    }
klao@946
   442
  }
klao@946
   443
deba@1531
   444
  /// \brief Copy the source map to the target map.
deba@1531
   445
  ///
deba@1531
   446
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   447
  /// to iterate on the data structure.
deba@1830
   448
  template <typename Target, typename Source, typename ItemIt>	    
deba@1531
   449
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   450
    for (; it != INVALID; ++it) {
deba@1531
   451
      target[it] = source[it];
klao@946
   452
    }
klao@946
   453
  }
klao@946
   454
athos@1540
   455
  /// \brief Class to copy a graph.
deba@1531
   456
  ///
athos@1540
   457
  /// Class to copy a graph to an other graph (duplicate a graph). The
athos@1540
   458
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   459
  template <typename Target, typename Source>
deba@1267
   460
  class GraphCopy {
deba@1531
   461
  public: 
deba@1531
   462
    typedef typename Source::Node Node;
deba@1531
   463
    typedef typename Source::NodeIt NodeIt;
deba@1531
   464
    typedef typename Source::Edge Edge;
deba@1531
   465
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   466
deba@1531
   467
    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
deba@1531
   468
    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
klao@946
   469
deba@1531
   470
    /// \brief Constructor for the GraphCopy.
deba@1531
   471
    ///
deba@1531
   472
    /// It copies the content of the \c _source graph into the
deba@1531
   473
    /// \c _target graph. It creates also two references, one beetween
deba@1531
   474
    /// the two nodeset and one beetween the two edgesets.
deba@1531
   475
    GraphCopy(Target& _target, const Source& _source) 
deba@1531
   476
      : source(_source), target(_target), 
deba@1531
   477
	nodeRefMap(_source), edgeRefMap(_source) {
deba@1531
   478
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   479
	nodeRefMap[it] = target.addNode();
deba@1531
   480
      }
deba@1531
   481
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   482
	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1531
   483
					nodeRefMap[source.target(it)]);
deba@1531
   484
      }
deba@1267
   485
    }
klao@946
   486
deba@1531
   487
    /// \brief Copies the node references into the given map.
deba@1531
   488
    ///
deba@1531
   489
    /// Copies the node references into the given map.
deba@1531
   490
    template <typename NodeRef>
deba@1531
   491
    const GraphCopy& nodeRef(NodeRef& map) const {
deba@1531
   492
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   493
	map.set(it, nodeRefMap[it]);
deba@1531
   494
      }
deba@1531
   495
      return *this;
deba@1267
   496
    }
deba@1531
   497
deba@1531
   498
    /// \brief Reverse and copies the node references into the given map.
deba@1531
   499
    ///
deba@1531
   500
    /// Reverse and copies the node references into the given map.
deba@1531
   501
    template <typename NodeRef>
deba@1531
   502
    const GraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1531
   503
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   504
	map.set(nodeRefMap[it], it);
deba@1531
   505
      }
deba@1531
   506
      return *this;
deba@1531
   507
    }
deba@1531
   508
deba@1531
   509
    /// \brief Copies the edge references into the given map.
deba@1531
   510
    ///
deba@1531
   511
    /// Copies the edge references into the given map.
deba@1531
   512
    template <typename EdgeRef>
deba@1531
   513
    const GraphCopy& edgeRef(EdgeRef& map) const {
deba@1531
   514
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   515
	map.set(it, edgeRefMap[it]);
deba@1531
   516
      }
deba@1531
   517
      return *this;
deba@1531
   518
    }
deba@1531
   519
deba@1531
   520
    /// \brief Reverse and copies the edge references into the given map.
deba@1531
   521
    ///
deba@1531
   522
    /// Reverse and copies the edge references into the given map.
deba@1531
   523
    template <typename EdgeRef>
deba@1531
   524
    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1531
   525
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   526
	map.set(edgeRefMap[it], it);
deba@1531
   527
      }
deba@1531
   528
      return *this;
deba@1531
   529
    }
deba@1531
   530
deba@1531
   531
    /// \brief Make copy of the given map.
deba@1531
   532
    ///
deba@1531
   533
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   534
    /// The new map's key type is the target graph's node type,
deba@1531
   535
    /// and the copied map's key type is the source graph's node
deba@1531
   536
    /// type.  
deba@1531
   537
    template <typename TargetMap, typename SourceMap>
deba@1531
   538
    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   539
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1531
   540
      return *this;
deba@1531
   541
    }
deba@1531
   542
deba@1531
   543
    /// \brief Make copy of the given map.
deba@1531
   544
    ///
deba@1531
   545
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   546
    /// The new map's key type is the target graph's edge type,
deba@1531
   547
    /// and the copied map's key type is the source graph's edge
deba@1531
   548
    /// type.  
deba@1531
   549
    template <typename TargetMap, typename SourceMap>
deba@1531
   550
    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   551
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1531
   552
      return *this;
deba@1531
   553
    }
deba@1531
   554
deba@1531
   555
    /// \brief Gives back the stored node references.
deba@1531
   556
    ///
deba@1531
   557
    /// Gives back the stored node references.
deba@1531
   558
    const NodeRefMap& nodeRef() const {
deba@1531
   559
      return nodeRefMap;
deba@1531
   560
    }
deba@1531
   561
deba@1531
   562
    /// \brief Gives back the stored edge references.
deba@1531
   563
    ///
deba@1531
   564
    /// Gives back the stored edge references.
deba@1531
   565
    const EdgeRefMap& edgeRef() const {
deba@1531
   566
      return edgeRefMap;
deba@1531
   567
    }
deba@1531
   568
deba@1720
   569
    void run() {}
deba@1720
   570
deba@1531
   571
  private:
deba@1531
   572
    
deba@1531
   573
    const Source& source;
deba@1531
   574
    Target& target;
deba@1531
   575
deba@1531
   576
    NodeRefMap nodeRefMap;
deba@1531
   577
    EdgeRefMap edgeRefMap;
deba@1267
   578
  };
klao@946
   579
deba@1531
   580
  /// \brief Copy a graph to an other graph.
deba@1531
   581
  ///
deba@1531
   582
  /// Copy a graph to an other graph.
deba@1531
   583
  /// The usage of the function:
deba@1531
   584
  /// 
alpar@1946
   585
  ///\code
deba@1531
   586
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
alpar@1946
   587
  ///\endcode
deba@1531
   588
  /// 
deba@1531
   589
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   590
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   591
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   592
  /// edges.
deba@1531
   593
  template <typename Target, typename Source>
deba@1531
   594
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   595
    return GraphCopy<Target, Source>(target, source);
deba@1531
   596
  }
klao@946
   597
deba@1720
   598
  /// \brief Class to copy an undirected graph.
deba@1720
   599
  ///
deba@1720
   600
  /// Class to copy an undirected graph to an other graph (duplicate a graph).
klao@1909
   601
  /// The simplest way of using it is through the \c copyUGraph() function.
deba@1720
   602
  template <typename Target, typename Source>
klao@1909
   603
  class UGraphCopy {
deba@1720
   604
  public: 
deba@1720
   605
    typedef typename Source::Node Node;
deba@1720
   606
    typedef typename Source::NodeIt NodeIt;
deba@1720
   607
    typedef typename Source::Edge Edge;
deba@1720
   608
    typedef typename Source::EdgeIt EdgeIt;
klao@1909
   609
    typedef typename Source::UEdge UEdge;
klao@1909
   610
    typedef typename Source::UEdgeIt UEdgeIt;
deba@1720
   611
deba@1720
   612
    typedef typename Source::
deba@1720
   613
    template NodeMap<typename Target::Node> NodeRefMap;
deba@1720
   614
    
deba@1720
   615
    typedef typename Source::
klao@1909
   616
    template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
deba@1720
   617
deba@1720
   618
  private:
deba@1720
   619
deba@1720
   620
    struct EdgeRefMap {
klao@1909
   621
      EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
deba@1720
   622
      typedef typename Source::Edge Key;
deba@1720
   623
      typedef typename Target::Edge Value;
deba@1720
   624
deba@1720
   625
      Value operator[](const Key& key) {
klao@1909
   626
	return gc.target.direct(gc.uEdgeRef[key], 
deba@1720
   627
				gc.target.direction(key));
deba@1720
   628
      }
deba@1720
   629
      
klao@1909
   630
      UGraphCopy& gc;
deba@1720
   631
    };
deba@1720
   632
    
deba@1192
   633
  public:
deba@1720
   634
klao@1909
   635
    /// \brief Constructor for the UGraphCopy.
deba@1720
   636
    ///
deba@1720
   637
    /// It copies the content of the \c _source graph into the
deba@1720
   638
    /// \c _target graph. It creates also two references, one beetween
deba@1720
   639
    /// the two nodeset and one beetween the two edgesets.
klao@1909
   640
    UGraphCopy(Target& _target, const Source& _source) 
deba@1720
   641
      : source(_source), target(_target), 
klao@1909
   642
	nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
deba@1720
   643
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   644
	nodeRefMap[it] = target.addNode();
deba@1720
   645
      }
klao@1909
   646
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   647
	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1720
   648
					nodeRefMap[source.target(it)]);
deba@1720
   649
      }
deba@1720
   650
    }
deba@1720
   651
deba@1720
   652
    /// \brief Copies the node references into the given map.
deba@1720
   653
    ///
deba@1720
   654
    /// Copies the node references into the given map.
deba@1720
   655
    template <typename NodeRef>
klao@1909
   656
    const UGraphCopy& nodeRef(NodeRef& map) const {
deba@1720
   657
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   658
	map.set(it, nodeRefMap[it]);
deba@1720
   659
      }
deba@1720
   660
      return *this;
deba@1720
   661
    }
deba@1720
   662
deba@1720
   663
    /// \brief Reverse and copies the node references into the given map.
deba@1720
   664
    ///
deba@1720
   665
    /// Reverse and copies the node references into the given map.
deba@1720
   666
    template <typename NodeRef>
klao@1909
   667
    const UGraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1720
   668
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   669
	map.set(nodeRefMap[it], it);
deba@1720
   670
      }
deba@1720
   671
      return *this;
deba@1720
   672
    }
deba@1720
   673
deba@1720
   674
    /// \brief Copies the edge references into the given map.
deba@1720
   675
    ///
deba@1720
   676
    /// Copies the edge references into the given map.
deba@1720
   677
    template <typename EdgeRef>
klao@1909
   678
    const UGraphCopy& edgeRef(EdgeRef& map) const {
deba@1720
   679
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   680
	map.set(edgeRefMap[it], it);
deba@1720
   681
      }
deba@1720
   682
      return *this;
deba@1720
   683
    }
deba@1720
   684
deba@1720
   685
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   686
    /// given map.
deba@1720
   687
    ///
deba@1720
   688
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   689
    template <typename EdgeRef>
klao@1909
   690
    const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1720
   691
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   692
	map.set(it, edgeRefMap[it]);
deba@1720
   693
      }
deba@1720
   694
      return *this;
deba@1720
   695
    }
deba@1720
   696
deba@1720
   697
    /// \brief Copies the undirected edge references into the given map.
deba@1720
   698
    ///
deba@1720
   699
    /// Copies the undirected edge references into the given map.
deba@1720
   700
    template <typename EdgeRef>
klao@1909
   701
    const UGraphCopy& uEdgeRef(EdgeRef& map) const {
klao@1909
   702
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   703
	map.set(it, uEdgeRefMap[it]);
deba@1720
   704
      }
deba@1720
   705
      return *this;
deba@1720
   706
    }
deba@1720
   707
deba@1720
   708
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   709
    /// given map.
deba@1720
   710
    ///
deba@1720
   711
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   712
    template <typename EdgeRef>
klao@1909
   713
    const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
klao@1909
   714
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   715
	map.set(uEdgeRefMap[it], it);
deba@1720
   716
      }
deba@1720
   717
      return *this;
deba@1720
   718
    }
deba@1720
   719
deba@1720
   720
    /// \brief Make copy of the given map.
deba@1720
   721
    ///
deba@1720
   722
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   723
    /// The new map's key type is the target graph's node type,
deba@1720
   724
    /// and the copied map's key type is the source graph's node
deba@1720
   725
    /// type.  
deba@1720
   726
    template <typename TargetMap, typename SourceMap>
klao@1909
   727
    const UGraphCopy& nodeMap(TargetMap& tMap, 
deba@1720
   728
				  const SourceMap& sMap) const {
deba@1720
   729
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1720
   730
      return *this;
deba@1720
   731
    }
deba@1720
   732
deba@1720
   733
    /// \brief Make copy of the given map.
deba@1720
   734
    ///
deba@1720
   735
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   736
    /// The new map's key type is the target graph's edge type,
deba@1720
   737
    /// and the copied map's key type is the source graph's edge
deba@1720
   738
    /// type.  
deba@1720
   739
    template <typename TargetMap, typename SourceMap>
klao@1909
   740
    const UGraphCopy& edgeMap(TargetMap& tMap, 
deba@1720
   741
				  const SourceMap& sMap) const {
deba@1720
   742
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1720
   743
      return *this;
deba@1720
   744
    }
deba@1720
   745
deba@1720
   746
    /// \brief Make copy of the given map.
deba@1720
   747
    ///
deba@1720
   748
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   749
    /// The new map's key type is the target graph's edge type,
deba@1720
   750
    /// and the copied map's key type is the source graph's edge
deba@1720
   751
    /// type.  
deba@1720
   752
    template <typename TargetMap, typename SourceMap>
klao@1909
   753
    const UGraphCopy& uEdgeMap(TargetMap& tMap, 
deba@1720
   754
				  const SourceMap& sMap) const {
klao@1909
   755
      copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
deba@1720
   756
      return *this;
deba@1720
   757
    }
deba@1720
   758
deba@1720
   759
    /// \brief Gives back the stored node references.
deba@1720
   760
    ///
deba@1720
   761
    /// Gives back the stored node references.
deba@1720
   762
    const NodeRefMap& nodeRef() const {
deba@1720
   763
      return nodeRefMap;
deba@1720
   764
    }
deba@1720
   765
deba@1720
   766
    /// \brief Gives back the stored edge references.
deba@1720
   767
    ///
deba@1720
   768
    /// Gives back the stored edge references.
deba@1720
   769
    const EdgeRefMap& edgeRef() const {
deba@1720
   770
      return edgeRefMap;
deba@1720
   771
    }
deba@1720
   772
klao@1909
   773
    /// \brief Gives back the stored uedge references.
deba@1720
   774
    ///
klao@1909
   775
    /// Gives back the stored uedge references.
klao@1909
   776
    const UEdgeRefMap& uEdgeRef() const {
klao@1909
   777
      return uEdgeRefMap;
deba@1720
   778
    }
deba@1720
   779
deba@1720
   780
    void run() {}
deba@1720
   781
deba@1720
   782
  private:
deba@1192
   783
    
deba@1720
   784
    const Source& source;
deba@1720
   785
    Target& target;
alpar@947
   786
deba@1720
   787
    NodeRefMap nodeRefMap;
deba@1720
   788
    EdgeRefMap edgeRefMap;
klao@1909
   789
    UEdgeRefMap uEdgeRefMap;
deba@1192
   790
  };
deba@1192
   791
deba@1720
   792
  /// \brief Copy a graph to an other graph.
deba@1720
   793
  ///
deba@1720
   794
  /// Copy a graph to an other graph.
deba@1720
   795
  /// The usage of the function:
deba@1720
   796
  /// 
alpar@1946
   797
  ///\code
deba@1720
   798
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
alpar@1946
   799
  ///\endcode
deba@1720
   800
  /// 
deba@1720
   801
  /// After the copy the \c nr map will contain the mapping from the
deba@1720
   802
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@1720
   803
  /// contain the mapping from the target graph's edges to the source's
deba@1720
   804
  /// edges.
deba@1720
   805
  template <typename Target, typename Source>
klao@1909
   806
  UGraphCopy<Target, Source> 
klao@1909
   807
  copyUGraph(Target& target, const Source& source) {
klao@1909
   808
    return UGraphCopy<Target, Source>(target, source);
deba@1720
   809
  }
deba@1192
   810
deba@1192
   811
deba@1192
   812
  /// @}
alpar@1402
   813
alpar@1402
   814
  /// \addtogroup graph_maps
alpar@1402
   815
  /// @{
alpar@1402
   816
deba@1413
   817
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   818
athos@1540
   819
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
   820
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
   821
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
   822
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
   823
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
   824
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
   825
  /// class \c InverseMap.
deba@1413
   826
  ///
deba@1413
   827
  template <typename _Graph, typename _Item>
deba@1413
   828
  class IdMap {
deba@1413
   829
  public:
deba@1413
   830
    typedef _Graph Graph;
deba@1413
   831
    typedef int Value;
deba@1413
   832
    typedef _Item Item;
deba@1413
   833
    typedef _Item Key;
deba@1413
   834
deba@1413
   835
    /// \brief Constructor.
deba@1413
   836
    ///
deba@1413
   837
    /// Constructor for creating id map.
deba@1413
   838
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   839
deba@1413
   840
    /// \brief Gives back the \e id of the item.
deba@1413
   841
    ///
deba@1413
   842
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   843
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   844
deba@1413
   845
deba@1413
   846
  private:
deba@1413
   847
    const Graph* graph;
deba@1413
   848
deba@1413
   849
  public:
deba@1413
   850
athos@1540
   851
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
   852
    ///
athos@1540
   853
    /// The class represents the inverse of its owner (IdMap).
deba@1413
   854
    /// \see inverse()
deba@1413
   855
    class InverseMap {
deba@1413
   856
    public:
deba@1419
   857
deba@1413
   858
      /// \brief Constructor.
deba@1413
   859
      ///
deba@1413
   860
      /// Constructor for creating an id-to-item map.
deba@1413
   861
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   862
deba@1413
   863
      /// \brief Constructor.
deba@1413
   864
      ///
deba@1413
   865
      /// Constructor for creating an id-to-item map.
deba@1413
   866
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   867
deba@1413
   868
      /// \brief Gives back the given item from its id.
deba@1413
   869
      ///
deba@1413
   870
      /// Gives back the given item from its id.
deba@1413
   871
      /// 
deba@1413
   872
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   873
    private:
deba@1413
   874
      const Graph* graph;
deba@1413
   875
    };
deba@1413
   876
deba@1413
   877
    /// \brief Gives back the inverse of the map.
deba@1413
   878
    ///
athos@1540
   879
    /// Gives back the inverse of the IdMap.
deba@1413
   880
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   881
deba@1413
   882
  };
deba@1413
   883
deba@1413
   884
  
athos@1526
   885
  /// \brief General invertable graph-map type.
alpar@1402
   886
athos@1540
   887
  /// This type provides simple invertable graph-maps. 
athos@1526
   888
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
   889
  /// and if a key is set to a new value then store it
alpar@1402
   890
  /// in the inverse map.
deba@1931
   891
  ///
deba@1931
   892
  /// The values of the map can be accessed
deba@1931
   893
  /// with stl compatible forward iterator.
deba@1931
   894
  ///
alpar@1402
   895
  /// \param _Graph The graph type.
deba@1830
   896
  /// \param _Item The item type of the graph.
deba@1830
   897
  /// \param _Value The value type of the map.
deba@1931
   898
  ///
deba@1931
   899
  /// \see IterableValueMap
deba@1830
   900
#ifndef DOXYGEN
deba@1830
   901
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
   902
  template <
deba@1830
   903
    typename _Graph, typename _Item, typename _Value, typename _Map 
deba@1413
   904
    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
alpar@1402
   905
  >
deba@1830
   906
#else
deba@1830
   907
  template <typename _Graph, typename _Item, typename _Value>
deba@1830
   908
#endif
deba@1413
   909
  class InvertableMap : protected _Map {
alpar@1402
   910
  public:
deba@1413
   911
klao@1909
   912
    /// The key type of InvertableMap (Node, Edge, UEdge).
alpar@1402
   913
    typedef typename _Map::Key Key;
deba@1413
   914
    /// The value type of the InvertableMap.
alpar@1402
   915
    typedef typename _Map::Value Value;
alpar@1402
   916
deba@1931
   917
  private:
deba@1931
   918
    
deba@1931
   919
    typedef _Map Map;
deba@1931
   920
    typedef _Graph Graph;
deba@1931
   921
deba@1931
   922
    typedef std::map<Value, Key> Container;
deba@1931
   923
    Container invMap;    
deba@1931
   924
deba@1931
   925
  public:
deba@1931
   926
 
deba@1931
   927
deba@1931
   928
alpar@1402
   929
    /// \brief Constructor.
alpar@1402
   930
    ///
deba@1413
   931
    /// Construct a new InvertableMap for the graph.
alpar@1402
   932
    ///
deba@1413
   933
    InvertableMap(const Graph& graph) : Map(graph) {} 
deba@1931
   934
deba@1931
   935
    /// \brief Forward iterator for values.
deba@1931
   936
    ///
deba@1931
   937
    /// This iterator is an stl compatible forward
deba@1931
   938
    /// iterator on the values of the map. The values can
deba@1931
   939
    /// be accessed in the [beginValue, endValue) range.
deba@1931
   940
    ///
deba@1931
   941
    class ValueIterator 
deba@1931
   942
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
   943
      friend class InvertableMap;
deba@1931
   944
    private:
deba@1931
   945
      ValueIterator(typename Container::const_iterator _it) 
deba@1931
   946
        : it(_it) {}
deba@1931
   947
    public:
deba@1931
   948
      
deba@1931
   949
      ValueIterator() {}
deba@1931
   950
deba@1931
   951
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
   952
      ValueIterator operator++(int) { 
deba@1931
   953
        ValueIterator tmp(*this); 
deba@1931
   954
        operator++();
deba@1931
   955
        return tmp; 
deba@1931
   956
      }
deba@1931
   957
deba@1931
   958
      const Value& operator*() const { return it->first; }
deba@1931
   959
      const Value* operator->() const { return &(it->first); }
deba@1931
   960
deba@1931
   961
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
   962
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
   963
      
deba@1931
   964
    private:
deba@1931
   965
      typename Container::const_iterator it;
deba@1931
   966
    };
deba@1931
   967
deba@1931
   968
    /// \brief Returns an iterator to the first value.
deba@1931
   969
    ///
deba@1931
   970
    /// Returns an stl compatible iterator to the 
deba@1931
   971
    /// first value of the map. The values of the
deba@1931
   972
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   973
    /// range.
deba@1931
   974
    ValueIterator beginValue() const {
deba@1931
   975
      return ValueIterator(invMap.begin());
deba@1931
   976
    }
deba@1931
   977
deba@1931
   978
    /// \brief Returns an iterator after the last value.
deba@1931
   979
    ///
deba@1931
   980
    /// Returns an stl compatible iterator after the 
deba@1931
   981
    /// last value of the map. The values of the
deba@1931
   982
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   983
    /// range.
deba@1931
   984
    ValueIterator endValue() const {
deba@1931
   985
      return ValueIterator(invMap.end());
deba@1931
   986
    }
alpar@1402
   987
    
alpar@1402
   988
    /// \brief The setter function of the map.
alpar@1402
   989
    ///
deba@1413
   990
    /// Sets the mapped value.
alpar@1402
   991
    void set(const Key& key, const Value& val) {
alpar@1402
   992
      Value oldval = Map::operator[](key);
deba@1413
   993
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
   994
      if (it != invMap.end() && it->second == key) {
alpar@1402
   995
	invMap.erase(it);
alpar@1402
   996
      }      
alpar@1402
   997
      invMap.insert(make_pair(val, key));
alpar@1402
   998
      Map::set(key, val);
alpar@1402
   999
    }
alpar@1402
  1000
alpar@1402
  1001
    /// \brief The getter function of the map.
alpar@1402
  1002
    ///
alpar@1402
  1003
    /// It gives back the value associated with the key.
deba@1931
  1004
    typename MapTraits<Map>::ConstReturnValue 
deba@1931
  1005
    operator[](const Key& key) const {
alpar@1402
  1006
      return Map::operator[](key);
alpar@1402
  1007
    }
alpar@1402
  1008
deba@1515
  1009
  protected:
deba@1515
  1010
alpar@1402
  1011
    /// \brief Erase the key from the map.
alpar@1402
  1012
    ///
alpar@1402
  1013
    /// Erase the key to the map. It is called by the
alpar@1402
  1014
    /// \c AlterationNotifier.
alpar@1402
  1015
    virtual void erase(const Key& key) {
alpar@1402
  1016
      Value val = Map::operator[](key);
deba@1413
  1017
      typename Container::iterator it = invMap.find(val);
alpar@1402
  1018
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1019
	invMap.erase(it);
alpar@1402
  1020
      }
alpar@1402
  1021
      Map::erase(key);
alpar@1402
  1022
    }
alpar@1402
  1023
deba@1829
  1024
    /// \brief Erase more keys from the map.
deba@1829
  1025
    ///
deba@1829
  1026
    /// Erase more keys from the map. It is called by the
deba@1829
  1027
    /// \c AlterationNotifier.
deba@1829
  1028
    virtual void erase(const std::vector<Key>& keys) {
deba@1829
  1029
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1030
	Value val = Map::operator[](keys[i]);
deba@1829
  1031
	typename Container::iterator it = invMap.find(val);
deba@1829
  1032
	if (it != invMap.end() && it->second == keys[i]) {
deba@1829
  1033
	  invMap.erase(it);
deba@1829
  1034
	}
deba@1829
  1035
      }
deba@1829
  1036
      Map::erase(keys);
deba@1829
  1037
    }
deba@1829
  1038
alpar@1402
  1039
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
  1040
    ///
alpar@1402
  1041
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
  1042
    /// \c AlterationNotifier.
alpar@1402
  1043
    virtual void clear() {
alpar@1402
  1044
      invMap.clear();
alpar@1402
  1045
      Map::clear();
alpar@1402
  1046
    }
alpar@1402
  1047
deba@1413
  1048
  public:
deba@1413
  1049
deba@1413
  1050
    /// \brief The inverse map type.
deba@1413
  1051
    ///
deba@1413
  1052
    /// The inverse of this map. The subscript operator of the map
deba@1413
  1053
    /// gives back always the item what was last assigned to the value. 
deba@1413
  1054
    class InverseMap {
deba@1413
  1055
    public:
deba@1413
  1056
      /// \brief Constructor of the InverseMap.
deba@1413
  1057
      ///
deba@1413
  1058
      /// Constructor of the InverseMap.
deba@1413
  1059
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
  1060
deba@1413
  1061
      /// The value type of the InverseMap.
deba@1413
  1062
      typedef typename InvertableMap::Key Value;
deba@1413
  1063
      /// The key type of the InverseMap.
deba@1413
  1064
      typedef typename InvertableMap::Value Key; 
deba@1413
  1065
deba@1413
  1066
      /// \brief Subscript operator. 
deba@1413
  1067
      ///
deba@1413
  1068
      /// Subscript operator. It gives back always the item 
deba@1413
  1069
      /// what was last assigned to the value.
deba@1413
  1070
      Value operator[](const Key& key) const {
deba@1413
  1071
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
  1072
	return it->second;
deba@1413
  1073
      }
deba@1413
  1074
      
deba@1413
  1075
    private:
deba@1413
  1076
      const InvertableMap& inverted;
deba@1413
  1077
    };
deba@1413
  1078
alpar@1402
  1079
    /// \brief It gives back the just readeable inverse map.
alpar@1402
  1080
    ///
alpar@1402
  1081
    /// It gives back the just readeable inverse map.
deba@1413
  1082
    InverseMap inverse() const {
deba@1413
  1083
      return InverseMap(*this);
alpar@1402
  1084
    } 
alpar@1402
  1085
alpar@1402
  1086
deba@1413
  1087
    
alpar@1402
  1088
  };
alpar@1402
  1089
alpar@1402
  1090
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
  1091
  /// item in the graph.
alpar@1402
  1092
  ///
athos@1540
  1093
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
  1094
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
  1095
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
  1096
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
  1097
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
  1098
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
  1099
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
  1100
  /// with its member class \c InverseMap.
alpar@1402
  1101
  ///
alpar@1402
  1102
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
  1103
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
klao@1909
  1104
  /// UEdge.
deba@1830
  1105
#ifndef DOXYGEN
alpar@1402
  1106
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
  1107
  template <
deba@1830
  1108
    typename _Graph, typename _Item, typename _Map 
deba@1830
  1109
    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent 
alpar@1402
  1110
  >
deba@1830
  1111
#else
deba@1830
  1112
  template <typename _Graph, typename _Item>
deba@1830
  1113
#endif
alpar@1402
  1114
  class DescriptorMap : protected _Map {
alpar@1402
  1115
alpar@1402
  1116
    typedef _Item Item;
alpar@1402
  1117
    typedef _Map Map;
alpar@1402
  1118
alpar@1402
  1119
  public:
alpar@1402
  1120
    /// The graph class of DescriptorMap.
alpar@1402
  1121
    typedef _Graph Graph;
alpar@1402
  1122
klao@1909
  1123
    /// The key type of DescriptorMap (Node, Edge, UEdge).
alpar@1402
  1124
    typedef typename _Map::Key Key;
alpar@1402
  1125
    /// The value type of DescriptorMap.
alpar@1402
  1126
    typedef typename _Map::Value Value;
alpar@1402
  1127
alpar@1402
  1128
    /// \brief Constructor.
alpar@1402
  1129
    ///
deba@1413
  1130
    /// Constructor for descriptor map.
alpar@1402
  1131
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
  1132
      build();
alpar@1402
  1133
    }
alpar@1402
  1134
deba@1515
  1135
  protected:
deba@1515
  1136
alpar@1402
  1137
    /// \brief Add a new key to the map.
alpar@1402
  1138
    ///
alpar@1402
  1139
    /// Add a new key to the map. It is called by the
alpar@1402
  1140
    /// \c AlterationNotifier.
alpar@1402
  1141
    virtual void add(const Item& item) {
alpar@1402
  1142
      Map::add(item);
alpar@1402
  1143
      Map::set(item, invMap.size());
alpar@1402
  1144
      invMap.push_back(item);
alpar@1402
  1145
    }
alpar@1402
  1146
deba@1829
  1147
    /// \brief Add more new keys to the map.
deba@1829
  1148
    ///
deba@1829
  1149
    /// Add more new keys to the map. It is called by the
deba@1829
  1150
    /// \c AlterationNotifier.
deba@1829
  1151
    virtual void add(const std::vector<Item>& items) {
deba@1829
  1152
      Map::add(items);
deba@1829
  1153
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1154
	Map::set(items[i], invMap.size());
deba@1829
  1155
	invMap.push_back(items[i]);
deba@1829
  1156
      }
deba@1829
  1157
    }
deba@1829
  1158
alpar@1402
  1159
    /// \brief Erase the key from the map.
alpar@1402
  1160
    ///
deba@1829
  1161
    /// Erase the key from the map. It is called by the
alpar@1402
  1162
    /// \c AlterationNotifier.
alpar@1402
  1163
    virtual void erase(const Item& item) {
alpar@1402
  1164
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1165
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1166
      invMap.pop_back();
alpar@1402
  1167
      Map::erase(item);
alpar@1402
  1168
    }
alpar@1402
  1169
deba@1829
  1170
    /// \brief Erase more keys from the map.
deba@1829
  1171
    ///
deba@1829
  1172
    /// Erase more keys from the map. It is called by the
deba@1829
  1173
    /// \c AlterationNotifier.
deba@1829
  1174
    virtual void erase(const std::vector<Item>& items) {
deba@1829
  1175
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1176
	Map::set(invMap.back(), Map::operator[](items[i]));
deba@1829
  1177
	invMap[Map::operator[](items[i])] = invMap.back();
deba@1829
  1178
	invMap.pop_back();
deba@1829
  1179
      }
deba@1829
  1180
      Map::erase(items);
deba@1829
  1181
    }
deba@1829
  1182
alpar@1402
  1183
    /// \brief Build the unique map.
alpar@1402
  1184
    ///
alpar@1402
  1185
    /// Build the unique map. It is called by the
alpar@1402
  1186
    /// \c AlterationNotifier.
alpar@1402
  1187
    virtual void build() {
alpar@1402
  1188
      Map::build();
alpar@1402
  1189
      Item it;
alpar@1402
  1190
      const typename Map::Graph* graph = Map::getGraph(); 
alpar@1402
  1191
      for (graph->first(it); it != INVALID; graph->next(it)) {
alpar@1402
  1192
	Map::set(it, invMap.size());
alpar@1402
  1193
	invMap.push_back(it);	
alpar@1402
  1194
      }      
alpar@1402
  1195
    }
alpar@1402
  1196
    
alpar@1402
  1197
    /// \brief Clear the keys from the map.
alpar@1402
  1198
    ///
alpar@1402
  1199
    /// Clear the keys from the map. It is called by the
alpar@1402
  1200
    /// \c AlterationNotifier.
alpar@1402
  1201
    virtual void clear() {
alpar@1402
  1202
      invMap.clear();
alpar@1402
  1203
      Map::clear();
alpar@1402
  1204
    }
alpar@1402
  1205
deba@1538
  1206
  public:
deba@1538
  1207
deba@1931
  1208
    /// \brief Returns the maximal value plus one.
deba@1931
  1209
    ///
deba@1931
  1210
    /// Returns the maximal value plus one in the map.
deba@1931
  1211
    unsigned int size() const {
deba@1931
  1212
      return invMap.size();
deba@1931
  1213
    }
deba@1931
  1214
deba@1552
  1215
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1216
    ///
deba@1552
  1217
    /// Swaps the position of the two items in the map.
deba@1552
  1218
    void swap(const Item& p, const Item& q) {
deba@1552
  1219
      int pi = Map::operator[](p);
deba@1552
  1220
      int qi = Map::operator[](q);
deba@1552
  1221
      Map::set(p, qi);
deba@1552
  1222
      invMap[qi] = p;
deba@1552
  1223
      Map::set(q, pi);
deba@1552
  1224
      invMap[pi] = q;
deba@1552
  1225
    }
deba@1552
  1226
alpar@1402
  1227
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1228
    ///
alpar@1402
  1229
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1230
    int operator[](const Item& item) const {
alpar@1402
  1231
      return Map::operator[](item);
alpar@1402
  1232
    }
alpar@1402
  1233
    
deba@1413
  1234
  private:
deba@1413
  1235
deba@1413
  1236
    typedef std::vector<Item> Container;
deba@1413
  1237
    Container invMap;
deba@1413
  1238
deba@1413
  1239
  public:
athos@1540
  1240
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  1241
    ///
athos@1540
  1242
    /// The inverse map type of DescriptorMap.
deba@1413
  1243
    class InverseMap {
deba@1413
  1244
    public:
deba@1413
  1245
      /// \brief Constructor of the InverseMap.
deba@1413
  1246
      ///
deba@1413
  1247
      /// Constructor of the InverseMap.
deba@1413
  1248
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
  1249
	: inverted(_inverted) {}
deba@1413
  1250
deba@1413
  1251
deba@1413
  1252
      /// The value type of the InverseMap.
deba@1413
  1253
      typedef typename DescriptorMap::Key Value;
deba@1413
  1254
      /// The key type of the InverseMap.
deba@1413
  1255
      typedef typename DescriptorMap::Value Key; 
deba@1413
  1256
deba@1413
  1257
      /// \brief Subscript operator. 
deba@1413
  1258
      ///
deba@1413
  1259
      /// Subscript operator. It gives back the item 
deba@1413
  1260
      /// that the descriptor belongs to currently.
deba@1413
  1261
      Value operator[](const Key& key) const {
deba@1413
  1262
	return inverted.invMap[key];
deba@1413
  1263
      }
deba@1470
  1264
deba@1470
  1265
      /// \brief Size of the map.
deba@1470
  1266
      ///
deba@1470
  1267
      /// Returns the size of the map.
deba@1931
  1268
      unsigned int size() const {
deba@1470
  1269
	return inverted.invMap.size();
deba@1470
  1270
      }
deba@1413
  1271
      
deba@1413
  1272
    private:
deba@1413
  1273
      const DescriptorMap& inverted;
deba@1413
  1274
    };
deba@1413
  1275
alpar@1402
  1276
    /// \brief Gives back the inverse of the map.
alpar@1402
  1277
    ///
alpar@1402
  1278
    /// Gives back the inverse of the map.
alpar@1402
  1279
    const InverseMap inverse() const {
deba@1413
  1280
      return InverseMap(*this);
alpar@1402
  1281
    }
alpar@1402
  1282
  };
alpar@1402
  1283
alpar@1402
  1284
  /// \brief Returns the source of the given edge.
alpar@1402
  1285
  ///
alpar@1402
  1286
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
  1287
  /// \author Balazs Dezso
alpar@1402
  1288
  template <typename Graph>
alpar@1402
  1289
  class SourceMap {
alpar@1402
  1290
  public:
deba@1419
  1291
alpar@1402
  1292
    typedef typename Graph::Node Value;
alpar@1402
  1293
    typedef typename Graph::Edge Key;
alpar@1402
  1294
alpar@1402
  1295
    /// \brief Constructor
alpar@1402
  1296
    ///
alpar@1402
  1297
    /// Constructor
alpar@1402
  1298
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1299
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1300
alpar@1402
  1301
    /// \brief The subscript operator.
alpar@1402
  1302
    ///
alpar@1402
  1303
    /// The subscript operator.
alpar@1402
  1304
    /// \param edge The edge 
alpar@1402
  1305
    /// \return The source of the edge 
deba@1679
  1306
    Value operator[](const Key& edge) const {
alpar@1402
  1307
      return graph.source(edge);
alpar@1402
  1308
    }
alpar@1402
  1309
alpar@1402
  1310
  private:
alpar@1402
  1311
    const Graph& graph;
alpar@1402
  1312
  };
alpar@1402
  1313
alpar@1402
  1314
  /// \brief Returns a \ref SourceMap class
alpar@1402
  1315
  ///
alpar@1402
  1316
  /// This function just returns an \ref SourceMap class.
alpar@1402
  1317
  /// \relates SourceMap
alpar@1402
  1318
  template <typename Graph>
alpar@1402
  1319
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  1320
    return SourceMap<Graph>(graph);
alpar@1402
  1321
  } 
alpar@1402
  1322
alpar@1402
  1323
  /// \brief Returns the target of the given edge.
alpar@1402
  1324
  ///
alpar@1402
  1325
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
  1326
  /// \author Balazs Dezso
alpar@1402
  1327
  template <typename Graph>
alpar@1402
  1328
  class TargetMap {
alpar@1402
  1329
  public:
deba@1419
  1330
alpar@1402
  1331
    typedef typename Graph::Node Value;
alpar@1402
  1332
    typedef typename Graph::Edge Key;
alpar@1402
  1333
alpar@1402
  1334
    /// \brief Constructor
alpar@1402
  1335
    ///
alpar@1402
  1336
    /// Constructor
alpar@1402
  1337
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1338
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1339
alpar@1402
  1340
    /// \brief The subscript operator.
alpar@1402
  1341
    ///
alpar@1402
  1342
    /// The subscript operator.
alpar@1536
  1343
    /// \param e The edge 
alpar@1402
  1344
    /// \return The target of the edge 
deba@1679
  1345
    Value operator[](const Key& e) const {
alpar@1536
  1346
      return graph.target(e);
alpar@1402
  1347
    }
alpar@1402
  1348
alpar@1402
  1349
  private:
alpar@1402
  1350
    const Graph& graph;
alpar@1402
  1351
  };
alpar@1402
  1352
alpar@1402
  1353
  /// \brief Returns a \ref TargetMap class
deba@1515
  1354
  ///
athos@1540
  1355
  /// This function just returns a \ref TargetMap class.
alpar@1402
  1356
  /// \relates TargetMap
alpar@1402
  1357
  template <typename Graph>
alpar@1402
  1358
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  1359
    return TargetMap<Graph>(graph);
alpar@1402
  1360
  }
alpar@1402
  1361
athos@1540
  1362
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1363
  ///
athos@1540
  1364
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1365
  /// \author Balazs Dezso
deba@1419
  1366
  template <typename Graph>
deba@1419
  1367
  class ForwardMap {
deba@1419
  1368
  public:
deba@1419
  1369
deba@1419
  1370
    typedef typename Graph::Edge Value;
klao@1909
  1371
    typedef typename Graph::UEdge Key;
deba@1419
  1372
deba@1419
  1373
    /// \brief Constructor
deba@1419
  1374
    ///
deba@1419
  1375
    /// Constructor
deba@1419
  1376
    /// \param _graph The graph that the map belongs to.
deba@1419
  1377
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1378
deba@1419
  1379
    /// \brief The subscript operator.
deba@1419
  1380
    ///
deba@1419
  1381
    /// The subscript operator.
deba@1419
  1382
    /// \param key An undirected edge 
deba@1419
  1383
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  1384
    Value operator[](const Key& key) const {
deba@1627
  1385
      return graph.direct(key, true);
deba@1419
  1386
    }
deba@1419
  1387
deba@1419
  1388
  private:
deba@1419
  1389
    const Graph& graph;
deba@1419
  1390
  };
deba@1419
  1391
deba@1419
  1392
  /// \brief Returns a \ref ForwardMap class
deba@1515
  1393
  ///
deba@1419
  1394
  /// This function just returns an \ref ForwardMap class.
deba@1419
  1395
  /// \relates ForwardMap
deba@1419
  1396
  template <typename Graph>
deba@1419
  1397
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  1398
    return ForwardMap<Graph>(graph);
deba@1419
  1399
  }
deba@1419
  1400
athos@1540
  1401
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1402
  ///
athos@1540
  1403
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1404
  /// \author Balazs Dezso
deba@1419
  1405
  template <typename Graph>
deba@1419
  1406
  class BackwardMap {
deba@1419
  1407
  public:
deba@1419
  1408
deba@1419
  1409
    typedef typename Graph::Edge Value;
klao@1909
  1410
    typedef typename Graph::UEdge Key;
deba@1419
  1411
deba@1419
  1412
    /// \brief Constructor
deba@1419
  1413
    ///
deba@1419
  1414
    /// Constructor
deba@1419
  1415
    /// \param _graph The graph that the map belongs to.
deba@1419
  1416
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1417
deba@1419
  1418
    /// \brief The subscript operator.
deba@1419
  1419
    ///
deba@1419
  1420
    /// The subscript operator.
deba@1419
  1421
    /// \param key An undirected edge 
deba@1419
  1422
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  1423
    Value operator[](const Key& key) const {
deba@1627
  1424
      return graph.direct(key, false);
deba@1419
  1425
    }
deba@1419
  1426
deba@1419
  1427
  private:
deba@1419
  1428
    const Graph& graph;
deba@1419
  1429
  };
deba@1419
  1430
deba@1419
  1431
  /// \brief Returns a \ref BackwardMap class
deba@1419
  1432
athos@1540
  1433
  /// This function just returns a \ref BackwardMap class.
deba@1419
  1434
  /// \relates BackwardMap
deba@1419
  1435
  template <typename Graph>
deba@1419
  1436
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  1437
    return BackwardMap<Graph>(graph);
deba@1419
  1438
  }
deba@1419
  1439
deba@1695
  1440
  /// \brief Potential difference map
deba@1695
  1441
  ///
deba@1695
  1442
  /// If there is an potential map on the nodes then we
deba@1695
  1443
  /// can get an edge map as we get the substraction of the
deba@1695
  1444
  /// values of the target and source.
deba@1695
  1445
  template <typename Graph, typename NodeMap>
deba@1695
  1446
  class PotentialDifferenceMap {
deba@1515
  1447
  public:
deba@1695
  1448
    typedef typename Graph::Edge Key;
deba@1695
  1449
    typedef typename NodeMap::Value Value;
deba@1695
  1450
deba@1695
  1451
    /// \brief Constructor
deba@1695
  1452
    ///
deba@1695
  1453
    /// Contructor of the map
deba@1695
  1454
    PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) 
deba@1695
  1455
      : graph(_graph), potential(_potential) {}
deba@1695
  1456
deba@1695
  1457
    /// \brief Const subscription operator
deba@1695
  1458
    ///
deba@1695
  1459
    /// Const subscription operator
deba@1695
  1460
    Value operator[](const Key& edge) const {
deba@1695
  1461
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  1462
    }
deba@1695
  1463
deba@1695
  1464
  private:
deba@1695
  1465
    const Graph& graph;
deba@1695
  1466
    const NodeMap& potential;
deba@1695
  1467
  };
deba@1695
  1468
deba@1695
  1469
  /// \brief Just returns a PotentialDifferenceMap
deba@1695
  1470
  ///
deba@1695
  1471
  /// Just returns a PotentialDifferenceMap
deba@1695
  1472
  /// \relates PotentialDifferenceMap
deba@1695
  1473
  template <typename Graph, typename NodeMap>
deba@1695
  1474
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  1475
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  1476
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  1477
  }
deba@1695
  1478
deba@1515
  1479
  /// \brief Map of the node in-degrees.
alpar@1453
  1480
  ///
athos@1540
  1481
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  1482
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1483
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1484
  /// whenever the graph changes.
deba@1515
  1485
  ///
deba@1729
  1486
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1487
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1829
  1488
  /// is not guarantied if these additional features are used. For example
deba@1829
  1489
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1490
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1491
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1492
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1493
  ///
deba@1515
  1494
  /// \sa OutDegMap
deba@1515
  1495
alpar@1453
  1496
  template <typename _Graph>
deba@1515
  1497
  class InDegMap  
deba@1515
  1498
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1499
alpar@1453
  1500
  public:
deba@1515
  1501
    
deba@1515
  1502
    typedef _Graph Graph;
alpar@1453
  1503
    typedef int Value;
deba@1515
  1504
    typedef typename Graph::Node Key;
deba@1515
  1505
deba@1515
  1506
  private:
deba@1515
  1507
deba@1515
  1508
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1509
    public:
deba@1515
  1510
deba@1515
  1511
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1512
deba@1515
  1513
      typedef typename Parent::Key Key;
deba@1515
  1514
      typedef typename Parent::Value Value;
deba@1515
  1515
      
deba@1515
  1516
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1517
      
deba@1829
  1518
      virtual void add(const Key& key) {
deba@1515
  1519
	Parent::add(key);
deba@1515
  1520
	Parent::set(key, 0);
deba@1515
  1521
      }
deba@1931
  1522
deba@1829
  1523
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  1524
	Parent::add(keys);
deba@1829
  1525
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1526
	  Parent::set(keys[i], 0);
deba@1829
  1527
	}
deba@1829
  1528
      }
deba@1515
  1529
    };
deba@1515
  1530
deba@1515
  1531
  public:
alpar@1453
  1532
alpar@1453
  1533
    /// \brief Constructor.
alpar@1453
  1534
    ///
alpar@1453
  1535
    /// Constructor for creating in-degree map.
deba@1515
  1536
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1537
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1538
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1539
      
deba@1515
  1540
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1541
	deg[it] = countInEdges(graph, it);
deba@1515
  1542
      }
alpar@1453
  1543
    }
alpar@1453
  1544
deba@1515
  1545
    virtual ~InDegMap() {
alpar@1459
  1546
      AlterationNotifier<typename _Graph::Edge>::
alpar@1453
  1547
	ObserverBase::detach();
alpar@1453
  1548
    }
alpar@1453
  1549
    
alpar@1459
  1550
    /// Gives back the in-degree of a Node.
deba@1515
  1551
    int operator[](const Key& key) const {
deba@1515
  1552
      return deg[key];
alpar@1459
  1553
    }
alpar@1453
  1554
alpar@1453
  1555
  protected:
deba@1515
  1556
    
deba@1515
  1557
    typedef typename Graph::Edge Edge;
deba@1515
  1558
deba@1515
  1559
    virtual void add(const Edge& edge) {
deba@1515
  1560
      ++deg[graph.target(edge)];
alpar@1453
  1561
    }
alpar@1453
  1562
deba@1931
  1563
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  1564
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1565
        ++deg[graph.target(edges[i])];
deba@1931
  1566
      }
deba@1931
  1567
    }
deba@1931
  1568
deba@1515
  1569
    virtual void erase(const Edge& edge) {
deba@1515
  1570
      --deg[graph.target(edge)];
deba@1515
  1571
    }
deba@1515
  1572
deba@1931
  1573
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  1574
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1575
        --deg[graph.target(edges[i])];
deba@1931
  1576
      }
deba@1931
  1577
    }
deba@1931
  1578
deba@1515
  1579
    virtual void build() {
deba@1515
  1580
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1581
	deg[it] = countInEdges(graph, it);
deba@1515
  1582
      }      
deba@1515
  1583
    }
deba@1515
  1584
deba@1515
  1585
    virtual void clear() {
deba@1515
  1586
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1587
	deg[it] = 0;
deba@1515
  1588
      }
deba@1515
  1589
    }
deba@1515
  1590
  private:
alpar@1506
  1591
    
deba@1515
  1592
    const _Graph& graph;
deba@1515
  1593
    AutoNodeMap deg;
alpar@1459
  1594
  };
alpar@1459
  1595
deba@1515
  1596
  /// \brief Map of the node out-degrees.
deba@1515
  1597
  ///
athos@1540
  1598
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1599
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1600
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1601
  /// whenever the graph changes.
deba@1515
  1602
  ///
deba@1729
  1603
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1604
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1829
  1605
  /// is not guarantied if these additional features are used. For example
deba@1829
  1606
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1607
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1608
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1609
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1610
  ///
alpar@1555
  1611
  /// \sa InDegMap
alpar@1459
  1612
alpar@1459
  1613
  template <typename _Graph>
deba@1515
  1614
  class OutDegMap  
deba@1515
  1615
    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
deba@1515
  1616
alpar@1459
  1617
  public:
deba@1515
  1618
    
deba@1515
  1619
    typedef _Graph Graph;
alpar@1459
  1620
    typedef int Value;
deba@1515
  1621
    typedef typename Graph::Node Key;
deba@1515
  1622
deba@1515
  1623
  private:
deba@1515
  1624
deba@1515
  1625
    class AutoNodeMap : public Graph::template NodeMap<int> {
deba@1515
  1626
    public:
deba@1515
  1627
deba@1515
  1628
      typedef typename Graph::template NodeMap<int> Parent;
deba@1515
  1629
deba@1515
  1630
      typedef typename Parent::Key Key;
deba@1515
  1631
      typedef typename Parent::Value Value;
deba@1515
  1632
      
deba@1515
  1633
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1634
      
deba@1829
  1635
      virtual void add(const Key& key) {
deba@1515
  1636
	Parent::add(key);
deba@1515
  1637
	Parent::set(key, 0);
deba@1515
  1638
      }
deba@1829
  1639
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  1640
	Parent::add(keys);
deba@1829
  1641
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1642
	  Parent::set(keys[i], 0);
deba@1829
  1643
	}
deba@1829
  1644
      }
deba@1515
  1645
    };
deba@1515
  1646
deba@1515
  1647
  public:
alpar@1459
  1648
alpar@1459
  1649
    /// \brief Constructor.
alpar@1459
  1650
    ///
alpar@1459
  1651
    /// Constructor for creating out-degree map.
deba@1515
  1652
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
alpar@1459
  1653
      AlterationNotifier<typename _Graph::Edge>
alpar@1459
  1654
	::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1655
      
deba@1515
  1656
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1657
	deg[it] = countOutEdges(graph, it);
deba@1515
  1658
      }
alpar@1459
  1659
    }
alpar@1459
  1660
deba@1515
  1661
    virtual ~OutDegMap() {
alpar@1459
  1662
      AlterationNotifier<typename _Graph::Edge>::
alpar@1459
  1663
	ObserverBase::detach();
alpar@1459
  1664
    }
alpar@1459
  1665
    
alpar@1459
  1666
    /// Gives back the in-degree of a Node.
deba@1515
  1667
    int operator[](const Key& key) const {
deba@1515
  1668
      return deg[key];
alpar@1459
  1669
    }
alpar@1459
  1670
alpar@1459
  1671
  protected:
deba@1515
  1672
    
deba@1515
  1673
    typedef typename Graph::Edge Edge;
deba@1515
  1674
deba@1515
  1675
    virtual void add(const Edge& edge) {
deba@1515
  1676
      ++deg[graph.source(edge)];
alpar@1459
  1677
    }
alpar@1459
  1678
deba@1931
  1679
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  1680
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1681
        ++deg[graph.source(edges[i])];
deba@1931
  1682
      }
deba@1931
  1683
    }
deba@1931
  1684
deba@1515
  1685
    virtual void erase(const Edge& edge) {
deba@1515
  1686
      --deg[graph.source(edge)];
deba@1515
  1687
    }
deba@1515
  1688
deba@1931
  1689
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  1690
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1691
        --deg[graph.source(edges[i])];
deba@1931
  1692
      }
deba@1931
  1693
    }
deba@1931
  1694
deba@1515
  1695
    virtual void build() {
deba@1515
  1696
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1697
	deg[it] = countOutEdges(graph, it);
deba@1515
  1698
      }      
deba@1515
  1699
    }
deba@1515
  1700
deba@1515
  1701
    virtual void clear() {
deba@1515
  1702
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1703
	deg[it] = 0;
deba@1515
  1704
      }
deba@1515
  1705
    }
deba@1515
  1706
  private:
alpar@1506
  1707
    
deba@1515
  1708
    const _Graph& graph;
deba@1515
  1709
    AutoNodeMap deg;
alpar@1453
  1710
  };
alpar@1453
  1711
deba@1695
  1712
alpar@1402
  1713
  /// @}
alpar@1402
  1714
alpar@947
  1715
} //END OF NAMESPACE LEMON
klao@946
  1716
klao@946
  1717
#endif