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