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