2 * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
19 ///\brief Map utilities.
26 /// \addtogroup gutils
30 /// \brief General inversable map type.
32 /// This type provides simple inversable map functions.
33 /// The InversableMap wraps an arbitrary ReadWriteMap
34 /// and if a key is setted to a new value then store it
35 /// in the inverse map.
40 class InversableMap : protected _Map {
46 typedef typename _Map::Key Key;
47 typedef typename _Map::Value Value;
48 typedef std::map<Value, Key> InverseMap;
50 typedef typename _Map::ConstReference ConstReference;
54 /// Construct a new InversableMap for the graph.
56 InversableMap(const Graph& graph) : Map(graph) {}
58 /// The setter function of the map.
60 /// It sets the map and the inverse map
61 void set(const Key& key, const Value& val) {
62 Value oldval = Map::operator[](key);
63 typename InverseMap::iterator it = inv_map.find(oldval);
64 if (it != inv_map.end() && it->second == key) {
67 inv_map.insert(make_pair(val, key));
71 ConstReference operator[](const Key&) const {
72 return Map::operator[](key);
75 virtual void add(const Key&) {
79 virtual void erase(const Key&) {
80 Value val = Map::operator[](key);
81 typename InverseMap::iterator it = inv_map.find(val);
82 if (it != inv_map.end() && it->second == key) {
88 const InverseMap& inverse() const {
98 // unique, continous, mutable
106 class DescriptorMap : protected _Map {
108 typedef _Graph Graph;
110 typedef _ItemIt ItemIt;
114 typedef typename _Map::Key Key;
115 typedef typename _Map::Value Value;
117 typedef vector<Item> InverseMap;
119 DescriptorMap(const Graph& _graph) : Map(_graph) {
123 virtual void add(const Item& item) {
125 Map::set(item, inv_map.size());
126 inv_map.push_back(item);
129 virtual void erase(const Item& item) {
130 Map::set(inv_map.back(), Map::operator[](item));
131 inv_map[Map::operator[](item)] = inv_map.back();
135 virtual void build() {
137 for (ItemIt it(*Map::getGraph()); it != INVALID; ++it) {
138 Map::set(it, inv_map.size());
139 inv_map.push_back(it);
143 virtual void clear() {
148 int operator[](const Item& item) const {
149 return Map::operator[](item);
153 const InverseMap inverse() const {
158 vector<Item> inv_map;
161 // unique, immutable => IDMap