lemon/graph_utils.h
author deba
Mon, 03 Apr 2006 09:45:23 +0000
changeset 2031 080d51024ac5
parent 2029 e00114f165f5
child 2064 2c5f81b35269
permissions -rw-r--r--
Correcting the structure of the graph's and adaptor's map.
The template assign operators and map iterators can be used for adaptors also.

Some bugfix in the adaptors

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