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