src/work/deba/map_utils.h
author deba
Tue, 01 Feb 2005 15:56:37 +0000
changeset 1115 444f69240539
parent 1037 3eaff8d04171
child 1133 9fd485470fee
permissions -rw-r--r--
Some changes in the IO and map utilities.
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@1032
    26
  /// \addtogroup gutils
deba@1032
    27
  /// @{
deba@1032
    28
deba@1032
    29
deba@1032
    30
  /// \brief General inversable map type.
deba@1032
    31
deba@1032
    32
  /// This type provides simple inversable map functions. 
deba@1032
    33
  /// The InversableMap wraps an arbitrary ReadWriteMap 
deba@1032
    34
  /// and if a key is setted to a new value then store it
deba@1032
    35
  /// in the inverse map.
deba@1032
    36
  template <
deba@1032
    37
    typename _Graph, 
deba@1037
    38
    typename _Map
deba@1032
    39
  >
deba@1032
    40
  class InversableMap : protected _Map {
deba@1032
    41
deba@1032
    42
  public:
deba@1037
    43
    typedef _Graph Graph;
deba@1032
    44
deba@1037
    45
    typedef _Map Map;
deba@1037
    46
    typedef typename _Map::Key Key;
deba@1037
    47
    typedef typename _Map::Value Value;
deba@1037
    48
    typedef std::map<Value, Key> InverseMap;
deba@1032
    49
    
deba@1037
    50
    typedef typename _Map::ConstReference ConstReference;
deba@1032
    51
deba@1115
    52
    /// \brief Constructor.
deba@1115
    53
    ///
deba@1032
    54
    /// Construct a new InversableMap for the graph.
deba@1032
    55
    ///
deba@1032
    56
    InversableMap(const Graph& graph) : Map(graph) {} 
deba@1032
    57
    
deba@1115
    58
    /// \brief The setter function of the map.
deba@1115
    59
    ///
deba@1115
    60
    /// It sets the map and the inverse map to given key-value pair.
deba@1032
    61
    void set(const Key& key, const Value& val) {
deba@1032
    62
      Value oldval = Map::operator[](key);
deba@1037
    63
      typename InverseMap::iterator it = inv_map.find(oldval);
deba@1037
    64
      if (it != inv_map.end() && it->second == key) {
deba@1037
    65
	inv_map.erase(it);
deba@1032
    66
      }      
deba@1037
    67
      inv_map.insert(make_pair(val, key));
deba@1032
    68
      Map::set(key, val);
deba@1032
    69
    }
deba@1032
    70
deba@1032
    71
    ConstReference operator[](const Key&) const {
deba@1032
    72
      return Map::operator[](key);
deba@1032
    73
    }
deba@1032
    74
deba@1032
    75
    virtual void add(const Key&) {
deba@1032
    76
      Map::add(key);
deba@1032
    77
    }
deba@1032
    78
deba@1032
    79
    virtual void erase(const Key&) {
deba@1032
    80
      Value val = Map::operator[](key);
deba@1037
    81
      typename InverseMap::iterator it = inv_map.find(val);
deba@1037
    82
      if (it != inv_map.end() && it->second == key) {
deba@1032
    83
	invMap.erase(it);
deba@1032
    84
      }
deba@1032
    85
      Map::erase(key);
deba@1032
    86
    }
deba@1032
    87
deba@1032
    88
    const InverseMap& inverse() const {
deba@1037
    89
      return inv_map;
deba@1032
    90
    } 
deba@1032
    91
deba@1032
    92
deba@1032
    93
  private:
deba@1037
    94
    InverseMap inv_map;    
deba@1032
    95
  };
deba@1032
    96
deba@1037
    97
deba@1115
    98
  
deba@1115
    99
  /// \brief Provides a mutable, continous and unique descriptor for each 
deba@1115
   100
  /// item in the graph.
deba@1115
   101
  ///
deba@1115
   102
  /// The DescriptorMap class provides a mutable, continous and immutable
deba@1115
   103
  /// mapping for each item in the graph.
deba@1115
   104
  ///
deba@1115
   105
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
deba@1115
   106
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
deba@1115
   107
  /// UndirEdge.
deba@1115
   108
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
deba@1037
   109
deba@1037
   110
  template <
deba@1037
   111
    typename _Graph,   
deba@1037
   112
    typename _Item,
deba@1037
   113
    typename _Map
deba@1037
   114
  >
deba@1037
   115
  class DescriptorMap : protected _Map {
deba@1115
   116
deba@1037
   117
    typedef _Item Item;
deba@1037
   118
    typedef _Map Map;
deba@1037
   119
deba@1115
   120
  public:
deba@1115
   121
    /// The graph class of DescriptorMap.
deba@1115
   122
    typedef _Graph Graph;
deba@1037
   123
deba@1115
   124
    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
deba@1037
   125
    typedef typename _Map::Key Key;
deba@1115
   126
    /// The value type of DescriptorMap.
deba@1037
   127
    typedef typename _Map::Value Value;
deba@1037
   128
deba@1115
   129
    typedef std::vector<Item> InverseMap;
deba@1037
   130
deba@1115
   131
    /// \brief Constructor.
deba@1115
   132
    ///
deba@1115
   133
    /// Constructor for creating descriptor map.
deba@1037
   134
    DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@1037
   135
      build();
deba@1037
   136
    }
deba@1037
   137
deba@1037
   138
    virtual void add(const Item& item) {
deba@1037
   139
      Map::add(item);
deba@1037
   140
      Map::set(item, inv_map.size());
deba@1037
   141
      inv_map.push_back(item);
deba@1037
   142
    }
deba@1037
   143
deba@1037
   144
    virtual void erase(const Item& item) {
deba@1037
   145
      Map::set(inv_map.back(), Map::operator[](item));
deba@1037
   146
      inv_map[Map::operator[](item)] = inv_map.back();
deba@1037
   147
      Map::erase(item);
deba@1037
   148
    }
deba@1037
   149
deba@1037
   150
    virtual void build() {
deba@1037
   151
      Map::build();
deba@1115
   152
      Item it;
deba@1115
   153
      for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) {
deba@1037
   154
	Map::set(it, inv_map.size());
deba@1037
   155
	inv_map.push_back(it);	
deba@1037
   156
      }      
deba@1037
   157
    }
deba@1037
   158
    
deba@1037
   159
    virtual void clear() {
deba@1037
   160
      inv_map.clear();
deba@1037
   161
      Map::clear();
deba@1037
   162
    }
deba@1037
   163
deba@1115
   164
    /// \brief Gives back the \e descriptor of the item.
deba@1115
   165
    ///
deba@1115
   166
    /// Gives back the mutable and unique \e descriptor of the map.
deba@1037
   167
    int operator[](const Item& item) const {
deba@1037
   168
      return Map::operator[](item);
deba@1037
   169
    }
deba@1037
   170
    
deba@1115
   171
    /// \brief Gives back the inverse of the map.
deba@1115
   172
    ///
deba@1115
   173
    /// Gives back the inverse of the map.
deba@1037
   174
    const InverseMap inverse() const {
deba@1037
   175
      return inv_map;
deba@1037
   176
    }
deba@1037
   177
deba@1037
   178
  private:
deba@1037
   179
    vector<Item> inv_map;
deba@1037
   180
  };
deba@1115
   181
  
deba@1115
   182
  /// Provides an immutable and unique id for each item in the graph.
deba@1037
   183
deba@1115
   184
  /// The IdMap class provides an unique and immutable mapping for each item
deba@1115
   185
  /// in the graph.
deba@1115
   186
  ///
deba@1115
   187
  template <typename _Graph, typename _Item>
deba@1115
   188
  class IdMap {
deba@1115
   189
  public:
deba@1115
   190
    typedef _Graph Graph;
deba@1115
   191
    typedef int Value;
deba@1115
   192
    typedef _Item Item;
deba@1115
   193
deba@1115
   194
    /// \brief The class represents the inverse of the map.
deba@1115
   195
    ///
deba@1115
   196
    /// The class represents the inverse of the map.
deba@1115
   197
    /// \see inverse()
deba@1115
   198
    class InverseMap {
deba@1115
   199
    protected:
deba@1115
   200
      InverseMap(const Graph& _graph) : graph(_graph) {}
deba@1115
   201
    public:
deba@1115
   202
      /// \brief Gives back the given item by its id.
deba@1115
   203
      ///
deba@1115
   204
      /// Gives back the given item by its id.
deba@1115
   205
      /// 
deba@1115
   206
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1115
   207
    private:
deba@1115
   208
      Graph* graph;
deba@1115
   209
    };
deba@1115
   210
deba@1115
   211
    /// \brief Constructor.
deba@1115
   212
    ///
deba@1115
   213
    /// Constructor for creating id map.
deba@1115
   214
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1115
   215
deba@1115
   216
    /// \brief Gives back the \e id of the item.
deba@1115
   217
    ///
deba@1115
   218
    /// Gives back the immutable and unique \e id of the map.
deba@1115
   219
    int operator[](const Item& item) const { return graph->id(item);}
deba@1115
   220
deba@1115
   221
    /// \brief Gives back the inverse of the map.
deba@1115
   222
    ///
deba@1115
   223
    /// Gives back the inverse of the map.
deba@1115
   224
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1115
   225
deba@1115
   226
  private:
deba@1115
   227
    const Graph* graph;
deba@1115
   228
deba@1115
   229
  };
deba@1115
   230
deba@1115
   231
deba@1037
   232
deba@1032
   233
}
deba@1032
   234