src/lemon/graph_utils.h
author alpar
Thu, 19 May 2005 11:46:42 +0000
changeset 1428 9ba88ddc629c
parent 1413 3f45d58969d4
permissions -rw-r--r--
A very simple xml parser
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 * src/lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
klao@946
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     6
 *
klao@946
     7
 * Permission to use, modify and distribute this software is granted
klao@946
     8
 * provided that this copyright notice appears in all copies. For
klao@946
     9
 * precise terms see the accompanying LICENSE file.
klao@946
    10
 *
klao@946
    11
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    12
 * express or implied, and with no claim as to its suitability for any
klao@946
    13
 * purpose.
klao@946
    14
 *
klao@946
    15
 */
klao@946
    16
klao@946
    17
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    18
#define LEMON_GRAPH_UTILS_H
klao@946
    19
klao@946
    20
#include <iterator>
deba@1419
    21
#include <vector>
alpar@1402
    22
#include <map>
klao@946
    23
klao@946
    24
#include <lemon/invalid.h>
klao@977
    25
#include <lemon/utility.h>
deba@1413
    26
#include <lemon/maps.h>
klao@946
    27
alpar@947
    28
///\ingroup gutils
klao@946
    29
///\file
alpar@947
    30
///\brief Graph utilities.
klao@946
    31
///
alpar@964
    32
///\todo Please
alpar@964
    33
///revise the documentation.
alpar@964
    34
///
klao@946
    35
klao@946
    36
klao@946
    37
namespace lemon {
klao@946
    38
deba@1267
    39
  /// \addtogroup gutils
deba@1267
    40
  /// @{
alpar@947
    41
klao@946
    42
  /// \brief Function to count the items in the graph.
klao@946
    43
  ///
klao@946
    44
  /// This function counts the items in the graph.
klao@946
    45
  /// The complexity of the function is O(n) because
klao@946
    46
  /// it iterates on all of the items.
klao@946
    47
klao@946
    48
  template <typename Graph, typename ItemIt>
klao@977
    49
  inline int countItems(const Graph& g) {
klao@946
    50
    int num = 0;
klao@977
    51
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
    52
      ++num;
klao@946
    53
    }
klao@946
    54
    return num;
klao@946
    55
  }
klao@946
    56
klao@977
    57
  // Node counting:
klao@977
    58
klao@977
    59
  template <typename Graph>
klao@977
    60
  inline
klao@977
    61
  typename enable_if<typename Graph::NodeNumTag, int>::type
klao@977
    62
  _countNodes(const Graph &g) {
klao@977
    63
    return g.nodeNum();
klao@977
    64
  }
klao@977
    65
klao@977
    66
  template <typename Graph>
klao@977
    67
  inline int _countNodes(Wrap<Graph> w) {
klao@977
    68
    return countItems<Graph, typename Graph::NodeIt>(w.value);
klao@977
    69
  }
klao@977
    70
klao@946
    71
  /// \brief Function to count the nodes in the graph.
klao@946
    72
  ///
klao@946
    73
  /// This function counts the nodes in the graph.
klao@946
    74
  /// The complexity of the function is O(n) but for some
alpar@964
    75
  /// graph structure it is specialized to run in O(1).
klao@977
    76
  ///
klao@977
    77
  /// \todo refer how to specialize it
klao@946
    78
klao@946
    79
  template <typename Graph>
klao@977
    80
  inline int countNodes(const Graph& g) {
klao@977
    81
    return _countNodes<Graph>(g);
klao@977
    82
  }
klao@977
    83
klao@977
    84
  // Edge counting:
klao@977
    85
klao@977
    86
  template <typename Graph>
klao@977
    87
  inline
klao@977
    88
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@977
    89
  _countEdges(const Graph &g) {
klao@977
    90
    return g.edgeNum();
klao@977
    91
  }
klao@977
    92
klao@977
    93
  template <typename Graph>
klao@977
    94
  inline int _countEdges(Wrap<Graph> w) {
klao@977
    95
    return countItems<Graph, typename Graph::EdgeIt>(w.value);
klao@946
    96
  }
klao@946
    97
klao@946
    98
  /// \brief Function to count the edges in the graph.
klao@946
    99
  ///
klao@946
   100
  /// This function counts the edges in the graph.
klao@946
   101
  /// The complexity of the function is O(e) but for some
alpar@964
   102
  /// graph structure it is specialized to run in O(1).
klao@977
   103
klao@946
   104
  template <typename Graph>
klao@977
   105
  inline int countEdges(const Graph& g) {
klao@977
   106
    return _countEdges<Graph>(g);
klao@946
   107
  }
klao@946
   108
klao@1053
   109
  // Undirected edge counting:
klao@1053
   110
klao@1053
   111
  template <typename Graph>
klao@1053
   112
  inline
klao@1053
   113
  typename enable_if<typename Graph::EdgeNumTag, int>::type
klao@1053
   114
  _countUndirEdges(const Graph &g) {
klao@1053
   115
    return g.undirEdgeNum();
klao@1053
   116
  }
klao@1053
   117
klao@1053
   118
  template <typename Graph>
klao@1053
   119
  inline int _countUndirEdges(Wrap<Graph> w) {
klao@1053
   120
    return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
klao@1053
   121
  }
klao@1053
   122
klao@1053
   123
  /// \brief Function to count the edges in the graph.
klao@946
   124
  ///
klao@1053
   125
  /// This function counts the edges in the graph.
klao@946
   126
  /// The complexity of the function is O(e) but for some
alpar@964
   127
  /// graph structure it is specialized to run in O(1).
klao@1053
   128
klao@946
   129
  template <typename Graph>
klao@1053
   130
  inline int countUndirEdges(const Graph& g) {
klao@1053
   131
    return _countUndirEdges<Graph>(g);
klao@946
   132
  }
klao@946
   133
klao@977
   134
klao@1053
   135
klao@946
   136
  template <typename Graph, typename DegIt>
klao@946
   137
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   138
    int num = 0;
klao@946
   139
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   140
      ++num;
klao@946
   141
    }
klao@946
   142
    return num;
klao@946
   143
  }
alpar@967
   144
alpar@967
   145
  /// Finds an edge between two nodes of a graph.
alpar@967
   146
alpar@967
   147
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   148
  ///
alpar@967
   149
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   150
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   151
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   152
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   153
  ///
alpar@967
   154
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@967
   155
  /// \code
alpar@967
   156
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   157
  ///   ...
alpar@967
   158
  /// }
alpar@967
   159
  /// \endcode
alpar@967
   160
  /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
alpar@967
   161
  /// interface here...
alpar@967
   162
  /// \bug Untested ...
alpar@967
   163
  template <typename Graph>
alpar@967
   164
  typename Graph::Edge findEdge(const Graph &g,
deba@1267
   165
				typename Graph::Node u, typename Graph::Node v,
deba@1267
   166
				typename Graph::Edge prev = INVALID) 
alpar@967
   167
  {
alpar@967
   168
    typename Graph::OutEdgeIt e(g,prev);
alpar@1079
   169
    //    if(prev==INVALID) g.first(e,u);
alpar@1079
   170
    if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
alpar@967
   171
    else ++e;
alpar@1079
   172
    while(e!=INVALID && g.target(e)!=v) ++e;
alpar@967
   173
    return e;
alpar@967
   174
  }
alpar@964
   175
  
alpar@964
   176
  ///\e
klao@946
   177
alpar@964
   178
  ///\todo Please document.
alpar@964
   179
  ///
klao@946
   180
  template <typename Graph>
klao@946
   181
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
   182
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
klao@946
   183
  }
klao@946
   184
alpar@964
   185
  ///\e
alpar@964
   186
alpar@964
   187
  ///\todo Please document.
alpar@964
   188
  ///
klao@946
   189
  template <typename Graph>
klao@946
   190
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
   191
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
klao@946
   192
  }
klao@946
   193
klao@946
   194
  // graph copy
klao@946
   195
klao@946
   196
  template <
klao@946
   197
    typename DestinationGraph, 
klao@946
   198
    typename SourceGraph, 
klao@946
   199
    typename NodeBijection>
klao@946
   200
  void copyNodes(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   201
		 NodeBijection& _nb) {    
klao@946
   202
    for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
klao@946
   203
      _nb[it] = _d.addNode();
klao@946
   204
    }
klao@946
   205
  }
klao@946
   206
klao@946
   207
  template <
klao@946
   208
    typename DestinationGraph, 
klao@946
   209
    typename SourceGraph, 
klao@946
   210
    typename NodeBijection,
klao@946
   211
    typename EdgeBijection>
klao@946
   212
  void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
klao@946
   213
		 const NodeBijection& _nb, EdgeBijection& _eb) {    
klao@946
   214
    for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
alpar@986
   215
      _eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
klao@946
   216
    }
klao@946
   217
  }
klao@946
   218
klao@946
   219
  template <
klao@946
   220
    typename DestinationGraph, 
klao@946
   221
    typename SourceGraph, 
klao@946
   222
    typename NodeBijection,
klao@946
   223
    typename EdgeBijection>
klao@946
   224
  void copyGraph(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   225
		 NodeBijection& _nb, EdgeBijection& _eb) {
klao@946
   226
    nodeCopy(_d, _s, _nb);
klao@946
   227
    edgeCopy(_d, _s, _nb, _eb);
klao@946
   228
  }
klao@946
   229
 
deba@1267
   230
  template <
klao@946
   231
    typename _DestinationGraph, 
klao@946
   232
    typename _SourceGraph, 
klao@946
   233
    typename _NodeBijection 
klao@946
   234
    =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
klao@946
   235
    typename _EdgeBijection 
deba@1267
   236
    = typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
deba@1267
   237
  >
deba@1267
   238
  class GraphCopy {
deba@1267
   239
  public:
deba@1267
   240
    
deba@1267
   241
    typedef _DestinationGraph DestinationGraph;
deba@1267
   242
    typedef _SourceGraph SourceGraph;
klao@946
   243
deba@1267
   244
    typedef _NodeBijection NodeBijection;
deba@1267
   245
    typedef _EdgeBijection EdgeBijection;
deba@1267
   246
    
deba@1267
   247
  protected:          
deba@1267
   248
    
deba@1267
   249
    NodeBijection node_bijection;
deba@1267
   250
    EdgeBijection edge_bijection;     
klao@946
   251
deba@1267
   252
  public:
deba@1267
   253
     
deba@1267
   254
    GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
deba@1267
   255
      copyGraph(_d, _s, node_bijection, edge_bijection);
deba@1267
   256
    }
deba@1267
   257
    
deba@1267
   258
    const NodeBijection& getNodeBijection() const {
deba@1267
   259
      return node_bijection;
deba@1267
   260
    }
klao@946
   261
deba@1267
   262
    const EdgeBijection& getEdgeBijection() const {
deba@1267
   263
      return edge_bijection;
deba@1267
   264
    }
deba@1267
   265
     
deba@1267
   266
  };
klao@946
   267
klao@946
   268
deba@1267
   269
  template <typename _Graph, typename _Item>
deba@1419
   270
  class ItemSetTraits {};
deba@1192
   271
  
deba@1192
   272
  template <typename _Graph>
deba@1267
   273
  class ItemSetTraits<_Graph, typename _Graph::Node> {
deba@1192
   274
  public:
deba@1192
   275
    
deba@1192
   276
    typedef _Graph Graph;
alpar@947
   277
deba@1192
   278
    typedef typename Graph::Node Item;
deba@1192
   279
    typedef typename Graph::NodeIt ItemIt;
deba@1192
   280
deba@1192
   281
    template <typename _Value>
deba@1192
   282
    class Map : public Graph::template NodeMap<_Value> {
deba@1192
   283
    public:
deba@1192
   284
      typedef typename Graph::template NodeMap<_Value> Parent; 
deba@1192
   285
      typedef typename Parent::Value Value;
deba@1192
   286
deba@1192
   287
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   288
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   289
	: Parent(_graph, _value) {}
deba@1192
   290
    };
deba@1192
   291
deba@1192
   292
  };
deba@1192
   293
deba@1192
   294
  template <typename _Graph>
deba@1267
   295
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
deba@1192
   296
  public:
deba@1192
   297
    
deba@1192
   298
    typedef _Graph Graph;
deba@1192
   299
deba@1192
   300
    typedef typename Graph::Edge Item;
deba@1192
   301
    typedef typename Graph::EdgeIt ItemIt;
deba@1192
   302
deba@1192
   303
    template <typename _Value>
deba@1192
   304
    class Map : public Graph::template EdgeMap<_Value> {
deba@1192
   305
    public:
deba@1192
   306
      typedef typename Graph::template EdgeMap<_Value> Parent; 
deba@1192
   307
      typedef typename Parent::Value Value;
deba@1192
   308
deba@1192
   309
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1192
   310
      Map(const Graph& _graph, const Value& _value) 
deba@1192
   311
	: Parent(_graph, _value) {}
deba@1192
   312
    };
deba@1192
   313
deba@1192
   314
  };
deba@1192
   315
deba@1267
   316
  template <typename _Graph>
deba@1267
   317
  class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
deba@1267
   318
  public:
deba@1267
   319
    
deba@1267
   320
    typedef _Graph Graph;
deba@1267
   321
deba@1267
   322
    typedef typename Graph::UndirEdge Item;
deba@1267
   323
    typedef typename Graph::UndirEdgeIt ItemIt;
deba@1267
   324
deba@1267
   325
    template <typename _Value>
deba@1267
   326
    class Map : public Graph::template UndirEdgeMap<_Value> {
deba@1267
   327
    public:
deba@1267
   328
      typedef typename Graph::template UndirEdgeMap<_Value> Parent; 
deba@1267
   329
      typedef typename Parent::Value Value;
deba@1267
   330
deba@1267
   331
      Map(const Graph& _graph) : Parent(_graph) {}
deba@1267
   332
      Map(const Graph& _graph, const Value& _value) 
deba@1267
   333
	: Parent(_graph, _value) {}
deba@1267
   334
    };
deba@1267
   335
deba@1267
   336
  };
deba@1192
   337
deba@1192
   338
  /// @}
alpar@1402
   339
alpar@1402
   340
  /// \addtogroup graph_maps
alpar@1402
   341
  /// @{
alpar@1402
   342
alpar@1402
   343
  template <typename Map, typename Enable = void>
alpar@1402
   344
  struct ReferenceMapTraits {
alpar@1402
   345
    typedef typename Map::Value Value;
alpar@1402
   346
    typedef typename Map::Value& Reference;
alpar@1402
   347
    typedef const typename Map::Value& ConstReference;
alpar@1402
   348
    typedef typename Map::Value* Pointer;
alpar@1402
   349
    typedef const typename Map::Value* ConstPointer;
alpar@1402
   350
  };
alpar@1402
   351
alpar@1402
   352
  template <typename Map>
alpar@1402
   353
  struct ReferenceMapTraits<
alpar@1402
   354
    Map, 
alpar@1402
   355
    typename enable_if<typename Map::FullTypeTag, void>::type
alpar@1402
   356
  > {
alpar@1402
   357
    typedef typename Map::Value Value;
alpar@1402
   358
    typedef typename Map::Reference Reference;
alpar@1402
   359
    typedef typename Map::ConstReference ConstReference;
alpar@1402
   360
    typedef typename Map::Pointer Pointer;
alpar@1402
   361
    typedef typename Map::ConstPointer ConstPointer;
alpar@1402
   362
  };
alpar@1402
   363
deba@1413
   364
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   365
deba@1413
   366
  /// The IdMap class provides an unique and immutable mapping for each item
deba@1413
   367
  /// in the graph.
deba@1413
   368
  ///
deba@1413
   369
  template <typename _Graph, typename _Item>
deba@1413
   370
  class IdMap {
deba@1413
   371
  public:
deba@1413
   372
    typedef _Graph Graph;
deba@1413
   373
    typedef int Value;
deba@1413
   374
    typedef _Item Item;
deba@1413
   375
    typedef _Item Key;
deba@1413
   376
deba@1419
   377
    typedef True NeedCopy;
deba@1419
   378
deba@1413
   379
    /// \brief Constructor.
deba@1413
   380
    ///
deba@1413
   381
    /// Constructor for creating id map.
deba@1413
   382
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   383
deba@1413
   384
    /// \brief Gives back the \e id of the item.
deba@1413
   385
    ///
deba@1413
   386
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   387
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   388
deba@1413
   389
deba@1413
   390
  private:
deba@1413
   391
    const Graph* graph;
deba@1413
   392
deba@1413
   393
  public:
deba@1413
   394
deba@1413
   395
    /// \brief The class represents the inverse of the map.
deba@1413
   396
    ///
deba@1413
   397
    /// The class represents the inverse of the map.
deba@1413
   398
    /// \see inverse()
deba@1413
   399
    class InverseMap {
deba@1413
   400
    public:
deba@1419
   401
deba@1419
   402
      typedef True NeedCopy;
deba@1419
   403
deba@1413
   404
      /// \brief Constructor.
deba@1413
   405
      ///
deba@1413
   406
      /// Constructor for creating an id-to-item map.
deba@1413
   407
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   408
deba@1413
   409
      /// \brief Constructor.
deba@1413
   410
      ///
deba@1413
   411
      /// Constructor for creating an id-to-item map.
deba@1413
   412
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   413
deba@1413
   414
      /// \brief Gives back the given item from its id.
deba@1413
   415
      ///
deba@1413
   416
      /// Gives back the given item from its id.
deba@1413
   417
      /// 
deba@1413
   418
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   419
    private:
deba@1413
   420
      const Graph* graph;
deba@1413
   421
    };
deba@1413
   422
deba@1413
   423
    /// \brief Gives back the inverse of the map.
deba@1413
   424
    ///
deba@1413
   425
    /// Gives back the inverse of the map.
deba@1413
   426
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   427
deba@1413
   428
  };
deba@1413
   429
deba@1413
   430
  
alpar@1402
   431
  /// \brief General inversable graph-map type.
alpar@1402
   432
alpar@1402
   433
  /// This type provides simple inversable map functions. 
alpar@1402
   434
  /// The InversableMap wraps an arbitrary ReadWriteMap 
alpar@1402
   435
  /// and if a key is setted to a new value then store it
alpar@1402
   436
  /// in the inverse map.
alpar@1402
   437
  /// \param _Graph The graph type.
alpar@1402
   438
  /// \param _Map The map to extend with inversable functionality. 
alpar@1402
   439
  template <
alpar@1402
   440
    typename _Graph,
alpar@1402
   441
    typename _Item, 
alpar@1402
   442
    typename _Value,
alpar@1402
   443
    typename _Map 
deba@1413
   444
    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
alpar@1402
   445
  >
deba@1413
   446
  class InvertableMap : protected _Map {
alpar@1402
   447
alpar@1402
   448
  public:
alpar@1402
   449
 
alpar@1402
   450
    typedef _Map Map;
alpar@1402
   451
    typedef _Graph Graph;
deba@1413
   452
deba@1413
   453
    /// The key type of InvertableMap (Node, Edge, UndirEdge).
alpar@1402
   454
    typedef typename _Map::Key Key;
deba@1413
   455
    /// The value type of the InvertableMap.
alpar@1402
   456
    typedef typename _Map::Value Value;
alpar@1402
   457
alpar@1402
   458
    /// \brief Constructor.
alpar@1402
   459
    ///
deba@1413
   460
    /// Construct a new InvertableMap for the graph.
alpar@1402
   461
    ///
deba@1413
   462
    InvertableMap(const Graph& graph) : Map(graph) {} 
alpar@1402
   463
    
alpar@1402
   464
    /// \brief The setter function of the map.
alpar@1402
   465
    ///
deba@1413
   466
    /// Sets the mapped value.
alpar@1402
   467
    void set(const Key& key, const Value& val) {
alpar@1402
   468
      Value oldval = Map::operator[](key);
deba@1413
   469
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
   470
      if (it != invMap.end() && it->second == key) {
alpar@1402
   471
	invMap.erase(it);
alpar@1402
   472
      }      
alpar@1402
   473
      invMap.insert(make_pair(val, key));
alpar@1402
   474
      Map::set(key, val);
alpar@1402
   475
    }
alpar@1402
   476
alpar@1402
   477
    /// \brief The getter function of the map.
alpar@1402
   478
    ///
alpar@1402
   479
    /// It gives back the value associated with the key.
deba@1413
   480
    const Value operator[](const Key& key) const {
alpar@1402
   481
      return Map::operator[](key);
alpar@1402
   482
    }
alpar@1402
   483
alpar@1402
   484
    /// \brief Add a new key to the map.
alpar@1402
   485
    ///
alpar@1402
   486
    /// Add a new key to the map. It is called by the
alpar@1402
   487
    /// \c AlterationNotifier.
alpar@1402
   488
    virtual void add(const Key& key) {
alpar@1402
   489
      Map::add(key);
alpar@1402
   490
    }
alpar@1402
   491
alpar@1402
   492
    /// \brief Erase the key from the map.
alpar@1402
   493
    ///
alpar@1402
   494
    /// Erase the key to the map. It is called by the
alpar@1402
   495
    /// \c AlterationNotifier.
alpar@1402
   496
    virtual void erase(const Key& key) {
alpar@1402
   497
      Value val = Map::operator[](key);
deba@1413
   498
      typename Container::iterator it = invMap.find(val);
alpar@1402
   499
      if (it != invMap.end() && it->second == key) {
alpar@1402
   500
	invMap.erase(it);
alpar@1402
   501
      }
alpar@1402
   502
      Map::erase(key);
alpar@1402
   503
    }
alpar@1402
   504
alpar@1402
   505
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
   506
    ///
alpar@1402
   507
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
   508
    /// \c AlterationNotifier.
alpar@1402
   509
    virtual void clear() {
alpar@1402
   510
      invMap.clear();
alpar@1402
   511
      Map::clear();
alpar@1402
   512
    }
alpar@1402
   513
deba@1413
   514
  private:
deba@1413
   515
    
deba@1413
   516
    typedef std::map<Value, Key> Container;
deba@1413
   517
    Container invMap;    
deba@1413
   518
deba@1413
   519
  public:
deba@1413
   520
deba@1413
   521
    /// \brief The inverse map type.
deba@1413
   522
    ///
deba@1413
   523
    /// The inverse of this map. The subscript operator of the map
deba@1413
   524
    /// gives back always the item what was last assigned to the value. 
deba@1413
   525
    class InverseMap {
deba@1413
   526
    public:
deba@1413
   527
      /// \brief Constructor of the InverseMap.
deba@1413
   528
      ///
deba@1413
   529
      /// Constructor of the InverseMap.
deba@1413
   530
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
   531
deba@1413
   532
      /// The value type of the InverseMap.
deba@1413
   533
      typedef typename InvertableMap::Key Value;
deba@1413
   534
      /// The key type of the InverseMap.
deba@1413
   535
      typedef typename InvertableMap::Value Key; 
deba@1413
   536
deba@1413
   537
      /// \brief Subscript operator. 
deba@1413
   538
      ///
deba@1413
   539
      /// Subscript operator. It gives back always the item 
deba@1413
   540
      /// what was last assigned to the value.
deba@1413
   541
      Value operator[](const Key& key) const {
deba@1413
   542
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
   543
	return it->second;
deba@1413
   544
      }
deba@1413
   545
      
deba@1413
   546
    private:
deba@1413
   547
      const InvertableMap& inverted;
deba@1413
   548
    };
deba@1413
   549
alpar@1402
   550
    /// \brief It gives back the just readeable inverse map.
alpar@1402
   551
    ///
alpar@1402
   552
    /// It gives back the just readeable inverse map.
deba@1413
   553
    InverseMap inverse() const {
deba@1413
   554
      return InverseMap(*this);
alpar@1402
   555
    } 
alpar@1402
   556
alpar@1402
   557
deba@1413
   558
    
alpar@1402
   559
  };
alpar@1402
   560
alpar@1402
   561
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
   562
  /// item in the graph.
alpar@1402
   563
  ///
alpar@1402
   564
  /// The DescriptorMap class provides a mutable, continuous and immutable
deba@1413
   565
  /// mapping for each item in the graph. The value for an item may mutated
deba@1413
   566
  /// on each operation when the an item erased or added to graph.
alpar@1402
   567
  ///
alpar@1402
   568
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
   569
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
alpar@1402
   570
  /// UndirEdge.
alpar@1402
   571
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
   572
  template <
alpar@1402
   573
    typename _Graph,   
alpar@1402
   574
    typename _Item,
deba@1413
   575
    typename _Map 
deba@1413
   576
    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
alpar@1402
   577
  >
alpar@1402
   578
  class DescriptorMap : protected _Map {
alpar@1402
   579
alpar@1402
   580
    typedef _Item Item;
alpar@1402
   581
    typedef _Map Map;
alpar@1402
   582
alpar@1402
   583
  public:
alpar@1402
   584
    /// The graph class of DescriptorMap.
alpar@1402
   585
    typedef _Graph Graph;
alpar@1402
   586
alpar@1402
   587
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
alpar@1402
   588
    typedef typename _Map::Key Key;
alpar@1402
   589
    /// The value type of DescriptorMap.
alpar@1402
   590
    typedef typename _Map::Value Value;
alpar@1402
   591
alpar@1402
   592
    /// \brief Constructor.
alpar@1402
   593
    ///
deba@1413
   594
    /// Constructor for descriptor map.
alpar@1402
   595
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
   596
      build();
alpar@1402
   597
    }
alpar@1402
   598
alpar@1402
   599
    /// \brief Add a new key to the map.
alpar@1402
   600
    ///
alpar@1402
   601
    /// Add a new key to the map. It is called by the
alpar@1402
   602
    /// \c AlterationNotifier.
alpar@1402
   603
    virtual void add(const Item& item) {
alpar@1402
   604
      Map::add(item);
alpar@1402
   605
      Map::set(item, invMap.size());
alpar@1402
   606
      invMap.push_back(item);
alpar@1402
   607
    }
alpar@1402
   608
alpar@1402
   609
    /// \brief Erase the key from the map.
alpar@1402
   610
    ///
alpar@1402
   611
    /// Erase the key to the map. It is called by the
alpar@1402
   612
    /// \c AlterationNotifier.
alpar@1402
   613
    virtual void erase(const Item& item) {
alpar@1402
   614
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
   615
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
   616
      invMap.pop_back();
alpar@1402
   617
      Map::erase(item);
alpar@1402
   618
    }
alpar@1402
   619
alpar@1402
   620
    /// \brief Build the unique map.
alpar@1402
   621
    ///
alpar@1402
   622
    /// Build the unique map. It is called by the
alpar@1402
   623
    /// \c AlterationNotifier.
alpar@1402
   624
    virtual void build() {
alpar@1402
   625
      Map::build();
alpar@1402
   626
      Item it;
alpar@1402
   627
      const typename Map::Graph* graph = Map::getGraph(); 
alpar@1402
   628
      for (graph->first(it); it != INVALID; graph->next(it)) {
alpar@1402
   629
	Map::set(it, invMap.size());
alpar@1402
   630
	invMap.push_back(it);	
alpar@1402
   631
      }      
alpar@1402
   632
    }
alpar@1402
   633
    
alpar@1402
   634
    /// \brief Clear the keys from the map.
alpar@1402
   635
    ///
alpar@1402
   636
    /// Clear the keys from the map. It is called by the
alpar@1402
   637
    /// \c AlterationNotifier.
alpar@1402
   638
    virtual void clear() {
alpar@1402
   639
      invMap.clear();
alpar@1402
   640
      Map::clear();
alpar@1402
   641
    }
alpar@1402
   642
alpar@1402
   643
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
   644
    ///
alpar@1402
   645
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
   646
    int operator[](const Item& item) const {
alpar@1402
   647
      return Map::operator[](item);
alpar@1402
   648
    }
alpar@1402
   649
    
deba@1413
   650
  private:
deba@1413
   651
deba@1413
   652
    typedef std::vector<Item> Container;
deba@1413
   653
    Container invMap;
deba@1413
   654
deba@1413
   655
  public:
deba@1413
   656
    /// \brief The inverse map type.
deba@1413
   657
    ///
deba@1413
   658
    /// The inverse map type.
deba@1413
   659
    class InverseMap {
deba@1413
   660
    public:
deba@1413
   661
      /// \brief Constructor of the InverseMap.
deba@1413
   662
      ///
deba@1413
   663
      /// Constructor of the InverseMap.
deba@1413
   664
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
   665
	: inverted(_inverted) {}
deba@1413
   666
deba@1413
   667
deba@1413
   668
      /// The value type of the InverseMap.
deba@1413
   669
      typedef typename DescriptorMap::Key Value;
deba@1413
   670
      /// The key type of the InverseMap.
deba@1413
   671
      typedef typename DescriptorMap::Value Key; 
deba@1413
   672
deba@1413
   673
      /// \brief Subscript operator. 
deba@1413
   674
      ///
deba@1413
   675
      /// Subscript operator. It gives back the item 
deba@1413
   676
      /// that the descriptor belongs to currently.
deba@1413
   677
      Value operator[](const Key& key) const {
deba@1413
   678
	return inverted.invMap[key];
deba@1413
   679
      }
deba@1413
   680
      
deba@1413
   681
    private:
deba@1413
   682
      const DescriptorMap& inverted;
deba@1413
   683
    };
deba@1413
   684
alpar@1402
   685
    /// \brief Gives back the inverse of the map.
alpar@1402
   686
    ///
alpar@1402
   687
    /// Gives back the inverse of the map.
alpar@1402
   688
    const InverseMap inverse() const {
deba@1413
   689
      return InverseMap(*this);
alpar@1402
   690
    }
alpar@1402
   691
  };
alpar@1402
   692
alpar@1402
   693
  /// \brief Returns the source of the given edge.
alpar@1402
   694
  ///
alpar@1402
   695
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
   696
  /// \author Balazs Dezso
alpar@1402
   697
  template <typename Graph>
alpar@1402
   698
  class SourceMap {
alpar@1402
   699
  public:
deba@1419
   700
deba@1419
   701
    typedef True NeedCopy;
deba@1419
   702
alpar@1402
   703
    typedef typename Graph::Node Value;
alpar@1402
   704
    typedef typename Graph::Edge Key;
alpar@1402
   705
alpar@1402
   706
    /// \brief Constructor
alpar@1402
   707
    ///
alpar@1402
   708
    /// Constructor
alpar@1402
   709
    /// \param _graph The graph that the map belongs to.
alpar@1402
   710
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   711
alpar@1402
   712
    /// \brief The subscript operator.
alpar@1402
   713
    ///
alpar@1402
   714
    /// The subscript operator.
alpar@1402
   715
    /// \param edge The edge 
alpar@1402
   716
    /// \return The source of the edge 
alpar@1402
   717
    Value operator[](const Key& edge) {
alpar@1402
   718
      return graph.source(edge);
alpar@1402
   719
    }
alpar@1402
   720
alpar@1402
   721
  private:
alpar@1402
   722
    const Graph& graph;
alpar@1402
   723
  };
alpar@1402
   724
alpar@1402
   725
  /// \brief Returns a \ref SourceMap class
alpar@1402
   726
  ///
alpar@1402
   727
  /// This function just returns an \ref SourceMap class.
alpar@1402
   728
  /// \relates SourceMap
alpar@1402
   729
  template <typename Graph>
alpar@1402
   730
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
   731
    return SourceMap<Graph>(graph);
alpar@1402
   732
  } 
alpar@1402
   733
alpar@1402
   734
  /// \brief Returns the target of the given edge.
alpar@1402
   735
  ///
alpar@1402
   736
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
   737
  /// \author Balazs Dezso
alpar@1402
   738
  template <typename Graph>
alpar@1402
   739
  class TargetMap {
alpar@1402
   740
  public:
deba@1419
   741
deba@1419
   742
    typedef True NeedCopy;
deba@1419
   743
alpar@1402
   744
    typedef typename Graph::Node Value;
alpar@1402
   745
    typedef typename Graph::Edge Key;
alpar@1402
   746
alpar@1402
   747
    /// \brief Constructor
alpar@1402
   748
    ///
alpar@1402
   749
    /// Constructor
alpar@1402
   750
    /// \param _graph The graph that the map belongs to.
alpar@1402
   751
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
   752
alpar@1402
   753
    /// \brief The subscript operator.
alpar@1402
   754
    ///
alpar@1402
   755
    /// The subscript operator.
alpar@1402
   756
    /// \param edge The edge 
alpar@1402
   757
    /// \return The target of the edge 
alpar@1402
   758
    Value operator[](const Key& key) {
alpar@1402
   759
      return graph.target(key);
alpar@1402
   760
    }
alpar@1402
   761
alpar@1402
   762
  private:
alpar@1402
   763
    const Graph& graph;
alpar@1402
   764
  };
alpar@1402
   765
alpar@1402
   766
  /// \brief Returns a \ref TargetMap class
alpar@1402
   767
alpar@1402
   768
  /// This function just returns an \ref TargetMap class.
alpar@1402
   769
  /// \relates TargetMap
alpar@1402
   770
  template <typename Graph>
alpar@1402
   771
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
   772
    return TargetMap<Graph>(graph);
alpar@1402
   773
  }
alpar@1402
   774
deba@1419
   775
  /// \brief Returns the "forward" directed edge view of undirected edge.
deba@1419
   776
  ///
deba@1419
   777
  /// Returns the "forward" directed edge view of undirected edge.
deba@1419
   778
  /// \author Balazs Dezso
deba@1419
   779
  template <typename Graph>
deba@1419
   780
  class ForwardMap {
deba@1419
   781
  public:
deba@1419
   782
deba@1419
   783
    typedef True NeedCopy;
deba@1419
   784
deba@1419
   785
    typedef typename Graph::Edge Value;
deba@1419
   786
    typedef typename Graph::UndirEdge Key;
deba@1419
   787
deba@1419
   788
    /// \brief Constructor
deba@1419
   789
    ///
deba@1419
   790
    /// Constructor
deba@1419
   791
    /// \param _graph The graph that the map belongs to.
deba@1419
   792
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   793
deba@1419
   794
    /// \brief The subscript operator.
deba@1419
   795
    ///
deba@1419
   796
    /// The subscript operator.
deba@1419
   797
    /// \param key An undirected edge 
deba@1419
   798
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
   799
    Value operator[](const Key& key) const {
deba@1419
   800
      return graph.edgeWithSource(key, graph.source(key));
deba@1419
   801
    }
deba@1419
   802
deba@1419
   803
  private:
deba@1419
   804
    const Graph& graph;
deba@1419
   805
  };
deba@1419
   806
deba@1419
   807
  /// \brief Returns a \ref ForwardMap class
deba@1419
   808
deba@1419
   809
  /// This function just returns an \ref ForwardMap class.
deba@1419
   810
  /// \relates ForwardMap
deba@1419
   811
  template <typename Graph>
deba@1419
   812
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
   813
    return ForwardMap<Graph>(graph);
deba@1419
   814
  }
deba@1419
   815
deba@1419
   816
  /// \brief Returns the "backward" directed edge view of undirected edge.
deba@1419
   817
  ///
deba@1419
   818
  /// Returns the "backward" directed edge view of undirected edge.
deba@1419
   819
  /// \author Balazs Dezso
deba@1419
   820
  template <typename Graph>
deba@1419
   821
  class BackwardMap {
deba@1419
   822
  public:
deba@1419
   823
    typedef True NeedCopy;
deba@1419
   824
deba@1419
   825
    typedef typename Graph::Edge Value;
deba@1419
   826
    typedef typename Graph::UndirEdge Key;
deba@1419
   827
deba@1419
   828
    /// \brief Constructor
deba@1419
   829
    ///
deba@1419
   830
    /// Constructor
deba@1419
   831
    /// \param _graph The graph that the map belongs to.
deba@1419
   832
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
   833
deba@1419
   834
    /// \brief The subscript operator.
deba@1419
   835
    ///
deba@1419
   836
    /// The subscript operator.
deba@1419
   837
    /// \param key An undirected edge 
deba@1419
   838
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
   839
    Value operator[](const Key& key) const {
deba@1419
   840
      return graph.edgeWithSource(key, graph.target(key));
deba@1419
   841
    }
deba@1419
   842
deba@1419
   843
  private:
deba@1419
   844
    const Graph& graph;
deba@1419
   845
  };
deba@1419
   846
deba@1419
   847
  /// \brief Returns a \ref BackwardMap class
deba@1419
   848
deba@1419
   849
  /// This function just returns an \ref BackwardMap class.
deba@1419
   850
  /// \relates BackwardMap
deba@1419
   851
  template <typename Graph>
deba@1419
   852
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
   853
    return BackwardMap<Graph>(graph);
deba@1419
   854
  }
deba@1419
   855
alpar@1402
   856
alpar@1402
   857
  /// @}
alpar@1402
   858
alpar@947
   859
} //END OF NAMESPACE LEMON
klao@946
   860
klao@946
   861
#endif