src/work/deba/map_utils.h
author alpar
Tue, 04 Jan 2005 22:16:46 +0000
changeset 1050 bcc0766a7b86
parent 1032 9e903d3a1ef6
child 1115 444f69240539
permissions -rw-r--r--
Several new named parameters and documentation added to graphToEps().
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@1032
    17
///\ingroup gutils
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@1032
    52
    /// Constructor.
deba@1032
    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@1032
    58
    /// The setter function of the map.
deba@1032
    59
deba@1032
    60
    /// It sets the map and the inverse map 
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@1037
    98
  // unique, continous, mutable
deba@1037
    99
deba@1037
   100
  template <
deba@1037
   101
    typename _Graph,   
deba@1037
   102
    typename _Item,
deba@1037
   103
    typename _ItemIt,
deba@1037
   104
    typename _Map
deba@1037
   105
  >
deba@1037
   106
  class DescriptorMap : protected _Map {
deba@1037
   107
  public:
deba@1037
   108
    typedef _Graph Graph;
deba@1037
   109
    typedef _Item Item;
deba@1037
   110
    typedef _ItemIt ItemIt;
deba@1037
   111
    typedef _Map Map;
deba@1037
   112
deba@1037
   113
deba@1037
   114
    typedef typename _Map::Key Key;
deba@1037
   115
    typedef typename _Map::Value Value;
deba@1037
   116
deba@1037
   117
    typedef vector<Item> InverseMap;
deba@1037
   118
deba@1037
   119
    DescriptorMap(const Graph& _graph) : Map(_graph) {
deba@1037
   120
      build();
deba@1037
   121
    }
deba@1037
   122
deba@1037
   123
    virtual void add(const Item& item) {
deba@1037
   124
      Map::add(item);
deba@1037
   125
      Map::set(item, inv_map.size());
deba@1037
   126
      inv_map.push_back(item);
deba@1037
   127
    }
deba@1037
   128
deba@1037
   129
    virtual void erase(const Item& item) {
deba@1037
   130
      Map::set(inv_map.back(), Map::operator[](item));
deba@1037
   131
      inv_map[Map::operator[](item)] = inv_map.back();
deba@1037
   132
      Map::erase(item);
deba@1037
   133
    }
deba@1037
   134
deba@1037
   135
    virtual void build() {
deba@1037
   136
      Map::build();
deba@1037
   137
      for (ItemIt it(*Map::getGraph()); it != INVALID; ++it) {
deba@1037
   138
	Map::set(it, inv_map.size());
deba@1037
   139
	inv_map.push_back(it);	
deba@1037
   140
      }      
deba@1037
   141
    }
deba@1037
   142
    
deba@1037
   143
    virtual void clear() {
deba@1037
   144
      inv_map.clear();
deba@1037
   145
      Map::clear();
deba@1037
   146
    }
deba@1037
   147
deba@1037
   148
    int operator[](const Item& item) const {
deba@1037
   149
      return Map::operator[](item);
deba@1037
   150
    }
deba@1037
   151
deba@1037
   152
    
deba@1037
   153
    const InverseMap inverse() const {
deba@1037
   154
      return inv_map;
deba@1037
   155
    }
deba@1037
   156
deba@1037
   157
  private:
deba@1037
   158
    vector<Item> inv_map;
deba@1037
   159
  };
deba@1037
   160
deba@1037
   161
  // unique, immutable => IDMap
deba@1037
   162
  
deba@1037
   163
  
deba@1037
   164
deba@1032
   165
}
deba@1032
   166