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