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