src/lemon/map_utils.h
author deba
Fri, 04 Mar 2005 17:16:01 +0000
changeset 1192 aa4483befa56
parent 1137 83a48cfd8553
child 1197 a2cd33e6f61c
permissions -rw-r--r--
Adding GraphEdgeSet and GraphNodeSet classes to graph_utils.h.
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
deba@1137
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1137
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
deba@1137
     6
 *
deba@1137
     7
 * Permission to use, modify and distribute this software is granted
deba@1137
     8
 * provided that this copyright notice appears in all copies. For
deba@1137
     9
 * precise terms see the accompanying LICENSE file.
deba@1137
    10
 *
deba@1137
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1137
    12
 * express or implied, and with no claim as to its suitability for any
deba@1137
    13
 * purpose.
deba@1137
    14
 *
deba@1137
    15
 */
deba@1137
    16
deba@1137
    17
///\ingroup mutils
deba@1137
    18
///\file
deba@1137
    19
///\brief Map utilities.
deba@1137
    20
deba@1137
    21
#include <map>
deba@1137
    22
deba@1137
    23
deba@1137
    24
namespace lemon {
deba@1137
    25
deba@1137
    26
  /// \addtogroup mutils
deba@1137
    27
  /// @{
deba@1137
    28
deba@1137
    29
  /// \brief General inversable map type.
deba@1137
    30
deba@1137
    31
  /// This type provides simple inversable map functions. 
deba@1137
    32
  /// The InversableMap wraps an arbitrary ReadWriteMap 
deba@1137
    33
  /// and if a key is setted to a new value then store it
deba@1137
    34
  /// in the inverse map.
deba@1137
    35
  /// \param _Graph The graph type.
deba@1137
    36
  /// \param _Map The map to extend with inversable functionality. 
deba@1137
    37
  template <
deba@1137
    38
    typename _Graph, 
deba@1137
    39
    typename _Map
deba@1137
    40
  >
deba@1137
    41
  class InversableMap : protected _Map {
deba@1137
    42
deba@1137
    43
  public:
deba@1137
    44
    typedef _Graph Graph;
deba@1137
    45
deba@1137
    46
    typedef _Map Map;
deba@1137
    47
    /// The key type of InversableMap (Node, Edge, UndirEdge).
deba@1137
    48
    typedef typename _Map::Key Key;
deba@1137
    49
    /// The value type of the InversableMap.
deba@1137
    50
    typedef typename _Map::Value Value;
deba@1137
    51
    typedef std::map<Value, Key> InverseMap;
deba@1137
    52
    
deba@1137
    53
    typedef typename _Map::ConstReference ConstReference;
deba@1137
    54
deba@1137
    55
    /// \brief Constructor.
deba@1137
    56
    ///
deba@1137
    57
    /// Construct a new InversableMap for the graph.
deba@1137
    58
    ///
deba@1137
    59
    InversableMap(const Graph& graph) : Map(graph) {} 
deba@1137
    60
    
deba@1137
    61
    /// \brief The setter function of the map.
deba@1137
    62
    ///
deba@1137
    63
    /// It sets the map and the inverse map to given key-value pair.
deba@1137
    64
    void set(const Key& key, const Value& val) {
deba@1137
    65
      Value oldval = Map::operator[](key);
deba@1137
    66
      typename InverseMap::iterator it = invMap.find(oldval);
deba@1137
    67
      if (it != invMap.end() && it->second == key) {
deba@1137
    68
	invMap.erase(it);
deba@1137
    69
      }      
deba@1137
    70
      invMap.insert(make_pair(val, key));
deba@1137
    71
      Map::set(key, val);
deba@1137
    72
    }
deba@1137
    73
deba@1137
    74
    /// \brief The getter function of the map.
deba@1137
    75
    ///
deba@1137
    76
    /// It gives back the value associated with the key.
deba@1137
    77
    ConstReference operator[](const Key& key) const {
deba@1137
    78
      return Map::operator[](key);
deba@1137
    79
    }
deba@1137
    80
deba@1137
    81
    /// \brief Add a new key to the map.
deba@1137
    82
    ///
deba@1137
    83
    /// Add a new key to the map. It is called by the
deba@1137
    84
    /// \c AlterationNotifier.
deba@1137
    85
    virtual void add(const Key& key) {
deba@1137
    86
      Map::add(key);
deba@1137
    87
    }
deba@1137
    88
deba@1137
    89
    /// \brief Erase the key from the map.
deba@1137
    90
    ///
deba@1137
    91
    /// Erase the key to the map. It is called by the
deba@1137
    92
    /// \c AlterationNotifier.
deba@1137
    93
    virtual void erase(const Key& key) {
deba@1137
    94
      Value val = Map::operator[](key);
deba@1137
    95
      typename InverseMap::iterator it = invMap.find(val);
deba@1137
    96
      if (it != invMap.end() && it->second == key) {
deba@1137
    97
	invMap.erase(it);
deba@1137
    98
      }
deba@1137
    99
      Map::erase(key);
deba@1137
   100
    }
deba@1137
   101
deba@1137
   102
    /// \brief Clear the keys from the map and inverse map.
deba@1137
   103
    ///
deba@1137
   104
    /// Clear the keys from the map and inverse map. It is called by the
deba@1137
   105
    /// \c AlterationNotifier.
deba@1137
   106
    virtual void clear() {
deba@1137
   107
      invMap.clear();
deba@1137
   108
      Map::clear();
deba@1137
   109
    }
deba@1137
   110
deba@1137
   111
    /// \brief It gives back the just readeable inverse map.
deba@1137
   112
    ///
deba@1137
   113
    /// It gives back the just readeable inverse map.
deba@1137
   114
    const InverseMap& inverse() const {
deba@1137
   115
      return invMap;
deba@1137
   116
    } 
deba@1137
   117
deba@1137
   118
deba@1137
   119
  private:
deba@1137
   120
    InverseMap invMap;    
deba@1137
   121
  };
deba@1137
   122
deba@1137
   123
deba@1137
   124
  
deba@1137
   125
  /// \brief Provides a mutable, continous and unique descriptor for each 
deba@1137
   126
  /// item in the graph.
deba@1137
   127
  ///
deba@1137
   128
  /// The DescriptorMap class provides a mutable, continous and immutable
deba@1137
   129
  /// mapping for each item in the graph.
deba@1137
   130
  ///
deba@1137
   131
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
deba@1137
   132
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
deba@1137
   133
  /// UndirEdge.
deba@1137
   134
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
deba@1137
   135
deba@1137
   136
  template <
deba@1137
   137
    typename _Graph,   
deba@1137
   138
    typename _Item,
deba@1137
   139
    typename _Map
deba@1137
   140
  >
deba@1137
   141
  class DescriptorMap : protected _Map {
deba@1137
   142
deba@1137
   143
    typedef _Item Item;
deba@1137
   144
    typedef _Map Map;
deba@1137
   145
deba@1137
   146
  public:
deba@1137
   147
    /// The graph class of DescriptorMap.
deba@1137
   148
    typedef _Graph Graph;
deba@1137
   149
deba@1137
   150
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
deba@1137
   151
    typedef typename _Map::Key Key;
deba@1137
   152
    /// The value type of DescriptorMap.
deba@1137
   153
    typedef typename _Map::Value Value;
deba@1137
   154
deba@1137
   155
    typedef std::vector<Item> InverseMap;
deba@1137
   156
deba@1137
   157
    /// \brief Constructor.
deba@1137
   158
    ///
deba@1137
   159
    /// Constructor for creating descriptor map.
deba@1137
   160
    DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@1137
   161
      build();
deba@1137
   162
    }
deba@1137
   163
deba@1137
   164
    /// \brief Add a new key to the map.
deba@1137
   165
    ///
deba@1137
   166
    /// Add a new key to the map. It is called by the
deba@1137
   167
    /// \c AlterationNotifier.
deba@1137
   168
    virtual void add(const Item& item) {
deba@1137
   169
      Map::add(item);
deba@1137
   170
      Map::set(item, invMap.size());
deba@1137
   171
      invMap.push_back(item);
deba@1137
   172
    }
deba@1137
   173
deba@1137
   174
    /// \brief Erase the key from the map.
deba@1137
   175
    ///
deba@1137
   176
    /// Erase the key to the map. It is called by the
deba@1137
   177
    /// \c AlterationNotifier.
deba@1137
   178
    virtual void erase(const Item& item) {
deba@1137
   179
      Map::set(invMap.back(), Map::operator[](item));
deba@1137
   180
      invMap[Map::operator[](item)] = invMap.back();
deba@1137
   181
      Map::erase(item);
deba@1137
   182
    }
deba@1137
   183
deba@1137
   184
    /// \brief Build the unique map.
deba@1137
   185
    ///
deba@1137
   186
    /// Build the unique map. It is called by the
deba@1137
   187
    /// \c AlterationNotifier.
deba@1137
   188
    virtual void build() {
deba@1137
   189
      Map::build();
deba@1137
   190
      Item it;
deba@1137
   191
      for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) {
deba@1137
   192
	Map::set(it, invMap.size());
deba@1137
   193
	invMap.push_back(it);	
deba@1137
   194
      }      
deba@1137
   195
    }
deba@1137
   196
    
deba@1137
   197
    /// \brief Clear the keys from the map.
deba@1137
   198
    ///
deba@1137
   199
    /// Clear the keys from the map. It is called by the
deba@1137
   200
    /// \c AlterationNotifier.
deba@1137
   201
    virtual void clear() {
deba@1137
   202
      invMap.clear();
deba@1137
   203
      Map::clear();
deba@1137
   204
    }
deba@1137
   205
deba@1137
   206
    /// \brief Gives back the \e descriptor of the item.
deba@1137
   207
    ///
deba@1137
   208
    /// Gives back the mutable and unique \e descriptor of the map.
deba@1137
   209
    int operator[](const Item& item) const {
deba@1137
   210
      return Map::operator[](item);
deba@1137
   211
    }
deba@1137
   212
    
deba@1137
   213
    /// \brief Gives back the inverse of the map.
deba@1137
   214
    ///
deba@1137
   215
    /// Gives back the inverse of the map.
deba@1137
   216
    const InverseMap inverse() const {
deba@1137
   217
      return invMap;
deba@1137
   218
    }
deba@1137
   219
deba@1137
   220
  private:
deba@1137
   221
    vector<Item> invMap;
deba@1137
   222
  };
deba@1137
   223
  
deba@1137
   224
  /// Provides an immutable and unique id for each item in the graph.
deba@1137
   225
deba@1137
   226
  /// The IdMap class provides an unique and immutable mapping for each item
deba@1137
   227
  /// in the graph.
deba@1137
   228
  ///
deba@1137
   229
  template <typename _Graph, typename _Item>
deba@1137
   230
  class IdMap {
deba@1137
   231
  public:
deba@1137
   232
    typedef _Graph Graph;
deba@1137
   233
    typedef int Value;
deba@1137
   234
    typedef _Item Item;
deba@1137
   235
deba@1137
   236
    /// \brief The class represents the inverse of the map.
deba@1137
   237
    ///
deba@1137
   238
    /// The class represents the inverse of the map.
deba@1137
   239
    /// \see inverse()
deba@1137
   240
    class InverseMap {
deba@1137
   241
    protected:
deba@1137
   242
      InverseMap(const Graph& _graph) : graph(_graph) {}
deba@1137
   243
    public:
deba@1137
   244
      /// \brief Gives back the given item by its id.
deba@1137
   245
      ///
deba@1137
   246
      /// Gives back the given item by its id.
deba@1137
   247
      /// 
deba@1137
   248
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1137
   249
    private:
deba@1137
   250
      Graph* graph;
deba@1137
   251
    };
deba@1137
   252
deba@1137
   253
    /// \brief Constructor.
deba@1137
   254
    ///
deba@1137
   255
    /// Constructor for creating id map.
deba@1137
   256
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1137
   257
deba@1137
   258
    /// \brief Gives back the \e id of the item.
deba@1137
   259
    ///
deba@1137
   260
    /// Gives back the immutable and unique \e id of the map.
deba@1137
   261
    int operator[](const Item& item) const { return graph->id(item);}
deba@1137
   262
deba@1137
   263
    /// \brief Gives back the inverse of the map.
deba@1137
   264
    ///
deba@1137
   265
    /// Gives back the inverse of the map.
deba@1137
   266
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1137
   267
deba@1137
   268
  private:
deba@1137
   269
    const Graph* graph;
deba@1137
   270
deba@1137
   271
  };
deba@1137
   272
deba@1137
   273
deba@1137
   274
deba@1137
   275
}
deba@1137
   276