alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/array_map.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_ARRAY_MAP_H
|
alpar@921
|
18 |
#define LEMON_ARRAY_MAP_H
|
deba@822
|
19 |
|
deba@822
|
20 |
#include <memory>
|
deba@1267
|
21 |
#include <lemon/map_iterator.h>
|
deba@822
|
22 |
|
deba@822
|
23 |
///\ingroup graphmaps
|
deba@822
|
24 |
///\file
|
deba@822
|
25 |
///\brief Graph maps that construates and destruates
|
deba@822
|
26 |
///their elements dynamically.
|
deba@822
|
27 |
|
alpar@921
|
28 |
namespace lemon {
|
deba@822
|
29 |
|
deba@822
|
30 |
|
deba@822
|
31 |
/// \addtogroup graphmaps
|
deba@822
|
32 |
/// @{
|
deba@822
|
33 |
|
deba@822
|
34 |
/** The ArrayMap template class is graph map structure what
|
deba@822
|
35 |
* automatically updates the map when a key is added to or erased from
|
deba@822
|
36 |
* the map. This map factory uses the allocators to implement
|
deba@822
|
37 |
* the container functionality.
|
deba@822
|
38 |
*
|
deba@1267
|
39 |
* The template parameter is the AlterationNotifier that the maps
|
alpar@987
|
40 |
* will belong to and the Value.
|
deba@822
|
41 |
*/
|
deba@822
|
42 |
|
klao@946
|
43 |
template <typename _Graph,
|
klao@946
|
44 |
typename _Item,
|
klao@946
|
45 |
typename _Value>
|
deba@1039
|
46 |
class ArrayMap : public AlterationNotifier<_Item>::ObserverBase {
|
deba@897
|
47 |
|
deba@1267
|
48 |
typedef _Item Item;
|
deba@822
|
49 |
public:
|
deba@822
|
50 |
|
deba@822
|
51 |
/// The graph type of the maps.
|
klao@946
|
52 |
typedef _Graph Graph;
|
deba@822
|
53 |
/// The key type of the maps.
|
alpar@987
|
54 |
typedef _Item Key;
|
klao@946
|
55 |
|
deba@1039
|
56 |
typedef AlterationNotifier<_Item> Registry;
|
klao@946
|
57 |
|
deba@822
|
58 |
/// The MapBase of the Map which imlements the core regisitry function.
|
klao@946
|
59 |
typedef typename Registry::ObserverBase Parent;
|
deba@822
|
60 |
|
deba@822
|
61 |
/// The value type of the map.
|
alpar@987
|
62 |
typedef _Value Value;
|
deba@822
|
63 |
|
deba@822
|
64 |
|
klao@946
|
65 |
private:
|
deba@822
|
66 |
typedef std::allocator<Value> Allocator;
|
deba@822
|
67 |
|
klao@946
|
68 |
|
klao@946
|
69 |
public:
|
klao@946
|
70 |
|
deba@822
|
71 |
/** Graph and Registry initialized map constructor.
|
deba@822
|
72 |
*/
|
deba@980
|
73 |
ArrayMap(const Graph& _g) : graph(&_g) {
|
deba@1267
|
74 |
Item it;
|
deba@1267
|
75 |
attach(_g.getNotifier(Item()));
|
deba@822
|
76 |
allocate_memory();
|
deba@1267
|
77 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
78 |
int id = graph->id(it);;
|
deba@822
|
79 |
allocator.construct(&(values[id]), Value());
|
deba@822
|
80 |
}
|
deba@822
|
81 |
}
|
deba@822
|
82 |
|
klao@946
|
83 |
/// Constructor to use default value to initialize the map.
|
klao@946
|
84 |
|
klao@946
|
85 |
/// It constrates a map and initialize all of the the map.
|
klao@946
|
86 |
|
deba@980
|
87 |
ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
|
deba@1267
|
88 |
Item it;
|
deba@1039
|
89 |
attach(_g.getNotifier(_Item()));
|
deba@822
|
90 |
allocate_memory();
|
deba@1267
|
91 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
92 |
int id = graph->id(it);;
|
klao@946
|
93 |
allocator.construct(&(values[id]), _v);
|
deba@822
|
94 |
}
|
deba@822
|
95 |
}
|
deba@822
|
96 |
|
deba@822
|
97 |
/** Constructor to copy a map of the same map type.
|
deba@822
|
98 |
*/
|
klao@946
|
99 |
ArrayMap(const ArrayMap& copy) {
|
klao@946
|
100 |
if (copy.attached()) {
|
klao@946
|
101 |
attach(*copy.getRegistry());
|
klao@946
|
102 |
}
|
deba@822
|
103 |
capacity = copy.capacity;
|
deba@822
|
104 |
if (capacity == 0) return;
|
deba@822
|
105 |
values = allocator.allocate(capacity);
|
deba@1267
|
106 |
Item it;
|
deba@1267
|
107 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
108 |
int id = graph->id(it);;
|
deba@891
|
109 |
allocator.construct(&(values[id]), copy.values[id]);
|
deba@822
|
110 |
}
|
deba@822
|
111 |
}
|
deba@822
|
112 |
|
klao@978
|
113 |
using Parent::attach;
|
klao@978
|
114 |
using Parent::detach;
|
klao@978
|
115 |
using Parent::attached;
|
klao@978
|
116 |
|
deba@822
|
117 |
/** Assign operator to copy a map of the same map type.
|
deba@822
|
118 |
*/
|
deba@822
|
119 |
ArrayMap& operator=(const ArrayMap& copy) {
|
deba@822
|
120 |
if (© == this) return *this;
|
deba@897
|
121 |
|
klao@946
|
122 |
if (graph != copy.graph) {
|
klao@946
|
123 |
if (attached()) {
|
klao@946
|
124 |
clear();
|
klao@946
|
125 |
detach();
|
deba@897
|
126 |
}
|
klao@946
|
127 |
if (copy.attached()) {
|
klao@946
|
128 |
attach(*copy.getRegistry());
|
klao@946
|
129 |
}
|
deba@897
|
130 |
capacity = copy.capacity;
|
deba@897
|
131 |
if (capacity == 0) return *this;
|
deba@897
|
132 |
values = allocator.allocate(capacity);
|
deba@822
|
133 |
}
|
deba@891
|
134 |
|
deba@1267
|
135 |
Item it;
|
deba@1267
|
136 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
137 |
int id = graph->id(it);;
|
deba@822
|
138 |
allocator.construct(&(values[id]), copy.values[id]);
|
deba@822
|
139 |
}
|
deba@891
|
140 |
|
deba@822
|
141 |
return *this;
|
deba@822
|
142 |
}
|
deba@822
|
143 |
|
deba@822
|
144 |
/** The destructor of the map.
|
deba@822
|
145 |
*/
|
klao@946
|
146 |
virtual ~ArrayMap() {
|
klao@946
|
147 |
if (attached()) {
|
klao@946
|
148 |
clear();
|
klao@946
|
149 |
detach();
|
deba@822
|
150 |
}
|
deba@822
|
151 |
}
|
deba@822
|
152 |
|
deba@822
|
153 |
|
deba@822
|
154 |
/**
|
deba@822
|
155 |
* The subscript operator. The map can be subscripted by the
|
deba@822
|
156 |
* actual keys of the graph.
|
deba@822
|
157 |
*/
|
deba@1267
|
158 |
Value& operator[](const Key& key) {
|
deba@980
|
159 |
int id = graph->id(key);
|
deba@822
|
160 |
return values[id];
|
deba@822
|
161 |
}
|
deba@822
|
162 |
|
deba@822
|
163 |
/**
|
deba@822
|
164 |
* The const subscript operator. The map can be subscripted by the
|
deba@822
|
165 |
* actual keys of the graph.
|
deba@822
|
166 |
*/
|
deba@1267
|
167 |
const Value& operator[](const Key& key) const {
|
deba@980
|
168 |
int id = graph->id(key);
|
deba@822
|
169 |
return values[id];
|
deba@822
|
170 |
}
|
deba@822
|
171 |
|
deba@822
|
172 |
/** Setter function of the map. Equivalent with map[key] = val.
|
deba@822
|
173 |
* This is a compatibility feature with the not dereferable maps.
|
deba@822
|
174 |
*/
|
alpar@987
|
175 |
void set(const Key& key, const Value& val) {
|
klao@946
|
176 |
(*this)[key] = val;
|
deba@822
|
177 |
}
|
deba@822
|
178 |
|
deba@822
|
179 |
/** Add a new key to the map. It called by the map registry.
|
deba@822
|
180 |
*/
|
alpar@987
|
181 |
void add(const Key& key) {
|
deba@980
|
182 |
int id = graph->id(key);
|
deba@822
|
183 |
if (id >= capacity) {
|
deba@822
|
184 |
int new_capacity = (capacity == 0 ? 1 : capacity);
|
deba@822
|
185 |
while (new_capacity <= id) {
|
deba@822
|
186 |
new_capacity <<= 1;
|
deba@822
|
187 |
}
|
klao@946
|
188 |
Value* new_values = allocator.allocate(new_capacity);
|
deba@1267
|
189 |
Item it;
|
deba@1267
|
190 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
191 |
int jd = graph->id(it);;
|
deba@822
|
192 |
if (id != jd) {
|
deba@822
|
193 |
allocator.construct(&(new_values[jd]), values[jd]);
|
deba@822
|
194 |
allocator.destroy(&(values[jd]));
|
deba@822
|
195 |
}
|
deba@822
|
196 |
}
|
deba@822
|
197 |
if (capacity != 0) allocator.deallocate(values, capacity);
|
deba@822
|
198 |
values = new_values;
|
deba@822
|
199 |
capacity = new_capacity;
|
deba@822
|
200 |
}
|
deba@822
|
201 |
allocator.construct(&(values[id]), Value());
|
deba@822
|
202 |
}
|
deba@822
|
203 |
|
deba@822
|
204 |
/** Erase a key from the map. It called by the map registry.
|
deba@822
|
205 |
*/
|
alpar@987
|
206 |
void erase(const Key& key) {
|
deba@980
|
207 |
int id = graph->id(key);
|
deba@822
|
208 |
allocator.destroy(&(values[id]));
|
deba@822
|
209 |
}
|
deba@822
|
210 |
|
klao@946
|
211 |
void build() {
|
klao@946
|
212 |
allocate_memory();
|
deba@1267
|
213 |
Item it;
|
deba@1267
|
214 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
215 |
int id = graph->id(it);;
|
klao@946
|
216 |
allocator.construct(&(values[id]), Value());
|
klao@946
|
217 |
}
|
klao@946
|
218 |
}
|
klao@946
|
219 |
|
deba@822
|
220 |
void clear() {
|
deba@822
|
221 |
if (capacity != 0) {
|
deba@1267
|
222 |
Item it;
|
deba@1267
|
223 |
for (graph->first(it); it != INVALID; graph->next(it)) {
|
deba@980
|
224 |
int id = graph->id(it);;
|
klao@946
|
225 |
allocator.destroy(&(values[id]));
|
klao@946
|
226 |
}
|
deba@822
|
227 |
allocator.deallocate(values, capacity);
|
deba@822
|
228 |
capacity = 0;
|
deba@822
|
229 |
}
|
deba@822
|
230 |
}
|
deba@822
|
231 |
|
deba@1271
|
232 |
const Graph* getGraph() {
|
deba@1271
|
233 |
return graph;
|
deba@1271
|
234 |
}
|
deba@1271
|
235 |
|
klao@946
|
236 |
// /// The stl compatible pair iterator of the map.
|
klao@946
|
237 |
// typedef MapIterator<ArrayMap> Iterator;
|
klao@946
|
238 |
// /// The stl compatible const pair iterator of the map.
|
klao@946
|
239 |
// typedef MapConstIterator<ArrayMap> ConstIterator;
|
deba@822
|
240 |
|
klao@946
|
241 |
// /** Returns the begin iterator of the map.
|
klao@946
|
242 |
// */
|
klao@946
|
243 |
// Iterator begin() {
|
klao@946
|
244 |
// return Iterator(*this, KeyIt(*MapBase::getGraph()));
|
klao@946
|
245 |
// }
|
deba@822
|
246 |
|
klao@946
|
247 |
// /** Returns the end iterator of the map.
|
klao@946
|
248 |
// */
|
klao@946
|
249 |
// Iterator end() {
|
klao@946
|
250 |
// return Iterator(*this, INVALID);
|
klao@946
|
251 |
// }
|
deba@822
|
252 |
|
klao@946
|
253 |
// /** Returns the begin ConstIterator of the map.
|
klao@946
|
254 |
// */
|
klao@946
|
255 |
// ConstIterator begin() const {
|
klao@946
|
256 |
// return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
|
klao@946
|
257 |
// }
|
deba@822
|
258 |
|
klao@946
|
259 |
// /** Returns the end const_iterator of the map.
|
klao@946
|
260 |
// */
|
klao@946
|
261 |
// ConstIterator end() const {
|
klao@946
|
262 |
// return ConstIterator(*this, INVALID);
|
klao@946
|
263 |
// }
|
deba@822
|
264 |
|
klao@946
|
265 |
// /// The KeySet of the Map.
|
klao@946
|
266 |
// typedef MapConstKeySet<ArrayMap> ConstKeySet;
|
deba@830
|
267 |
|
klao@946
|
268 |
// /// KeySet getter function.
|
klao@946
|
269 |
// ConstKeySet keySet() const {
|
klao@946
|
270 |
// return ConstKeySet(*this);
|
klao@946
|
271 |
// }
|
deba@830
|
272 |
|
klao@946
|
273 |
// /// The ConstValueSet of the Map.
|
klao@946
|
274 |
// typedef MapConstValueSet<ArrayMap> ConstValueSet;
|
deba@830
|
275 |
|
klao@946
|
276 |
// /// ConstValueSet getter function.
|
klao@946
|
277 |
// ConstValueSet valueSet() const {
|
klao@946
|
278 |
// return ConstValueSet(*this);
|
klao@946
|
279 |
// }
|
deba@830
|
280 |
|
klao@946
|
281 |
// /// The ValueSet of the Map.
|
klao@946
|
282 |
// typedef MapValueSet<ArrayMap> ValueSet;
|
deba@830
|
283 |
|
klao@946
|
284 |
// /// ValueSet getter function.
|
klao@946
|
285 |
// ValueSet valueSet() {
|
klao@946
|
286 |
// return ValueSet(*this);
|
klao@946
|
287 |
// }
|
deba@830
|
288 |
|
deba@822
|
289 |
private:
|
deba@822
|
290 |
|
deba@822
|
291 |
void allocate_memory() {
|
deba@980
|
292 |
int max_id = graph->maxId(_Item());
|
deba@822
|
293 |
if (max_id == -1) {
|
deba@822
|
294 |
capacity = 0;
|
deba@822
|
295 |
values = 0;
|
deba@822
|
296 |
return;
|
deba@822
|
297 |
}
|
deba@822
|
298 |
capacity = 1;
|
deba@822
|
299 |
while (capacity <= max_id) {
|
deba@822
|
300 |
capacity <<= 1;
|
deba@822
|
301 |
}
|
deba@822
|
302 |
values = allocator.allocate(capacity);
|
deba@822
|
303 |
}
|
deba@822
|
304 |
|
klao@946
|
305 |
const Graph* graph;
|
deba@822
|
306 |
int capacity;
|
deba@822
|
307 |
Value* values;
|
deba@822
|
308 |
Allocator allocator;
|
deba@844
|
309 |
|
deba@822
|
310 |
};
|
deba@822
|
311 |
|
klao@946
|
312 |
template <typename _Base>
|
klao@946
|
313 |
class ArrayMappableGraphExtender : public _Base {
|
klao@946
|
314 |
public:
|
klao@946
|
315 |
|
klao@946
|
316 |
typedef ArrayMappableGraphExtender<_Base> Graph;
|
klao@946
|
317 |
typedef _Base Parent;
|
klao@946
|
318 |
|
klao@946
|
319 |
typedef typename Parent::Node Node;
|
klao@946
|
320 |
typedef typename Parent::NodeIt NodeIt;
|
deba@1039
|
321 |
typedef typename Parent::NodeNotifier NodeObserverRegistry;
|
klao@946
|
322 |
|
klao@946
|
323 |
typedef typename Parent::Edge Edge;
|
klao@946
|
324 |
typedef typename Parent::EdgeIt EdgeIt;
|
deba@1039
|
325 |
typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
|
klao@946
|
326 |
|
klao@946
|
327 |
|
klao@946
|
328 |
|
klao@946
|
329 |
template <typename _Value>
|
deba@1267
|
330 |
class NodeMap
|
deba@1267
|
331 |
: public IterableMapExtender<ArrayMap<Graph, Node, _Value> > {
|
klao@946
|
332 |
public:
|
klao@946
|
333 |
typedef ArrayMappableGraphExtender<_Base> Graph;
|
klao@946
|
334 |
|
klao@946
|
335 |
typedef typename Graph::Node Node;
|
klao@946
|
336 |
typedef typename Graph::NodeIt NodeIt;
|
klao@946
|
337 |
|
deba@1267
|
338 |
typedef IterableMapExtender<ArrayMap<Graph, Node, _Value> > Parent;
|
klao@946
|
339 |
|
klao@979
|
340 |
//typedef typename Parent::Graph Graph;
|
klao@946
|
341 |
typedef typename Parent::Value Value;
|
klao@946
|
342 |
|
klao@946
|
343 |
NodeMap(const Graph& g)
|
deba@980
|
344 |
: Parent(g) {}
|
klao@946
|
345 |
NodeMap(const Graph& g, const Value& v)
|
deba@980
|
346 |
: Parent(g, v) {}
|
klao@946
|
347 |
|
klao@946
|
348 |
};
|
klao@946
|
349 |
|
klao@946
|
350 |
template <typename _Value>
|
deba@1267
|
351 |
class EdgeMap
|
deba@1267
|
352 |
: public IterableMapExtender<ArrayMap<Graph, Edge, _Value> > {
|
klao@946
|
353 |
public:
|
klao@946
|
354 |
typedef ArrayMappableGraphExtender<_Base> Graph;
|
klao@946
|
355 |
|
klao@946
|
356 |
typedef typename Graph::Edge Edge;
|
klao@946
|
357 |
typedef typename Graph::EdgeIt EdgeIt;
|
klao@946
|
358 |
|
deba@1267
|
359 |
typedef IterableMapExtender<ArrayMap<Graph, Edge, _Value> > Parent;
|
klao@946
|
360 |
|
klao@979
|
361 |
//typedef typename Parent::Graph Graph;
|
klao@946
|
362 |
typedef typename Parent::Value Value;
|
klao@946
|
363 |
|
klao@946
|
364 |
EdgeMap(const Graph& g)
|
deba@980
|
365 |
: Parent(g) {}
|
klao@946
|
366 |
EdgeMap(const Graph& g, const Value& v)
|
deba@980
|
367 |
: Parent(g, v) {}
|
klao@946
|
368 |
|
klao@946
|
369 |
};
|
klao@946
|
370 |
|
klao@946
|
371 |
};
|
klao@946
|
372 |
|
deba@822
|
373 |
/// @}
|
deba@822
|
374 |
|
deba@822
|
375 |
}
|
deba@822
|
376 |
|
alpar@921
|
377 |
#endif //LEMON_ARRAY_MAP_H
|