17 typedef typename MapRegistry::Key Key; |
16 typedef typename MapRegistry::Key Key; |
18 typedef typename MapRegistry::KeyIt KeyIt; |
17 typedef typename MapRegistry::KeyIt KeyIt; |
19 |
18 |
20 typedef typename MapRegistry::MapBase MapBase; |
19 typedef typename MapRegistry::MapBase MapBase; |
21 |
20 |
22 template <typename V, typename A = std::allocator<V> > class Map : public MapBase { |
21 template <typename V, typename A = std::allocator<V> > |
|
22 class Map : public MapBase { |
23 |
23 |
24 public: |
24 public: |
25 |
25 |
26 typedef V Value; |
26 typedef V Value; |
27 typedef A Allocator; |
27 typedef A Allocator; |
28 |
28 |
29 |
29 |
30 Map() : values(0), capacity(0) {} |
30 Map() : values(0), capacity(0) {} |
31 |
31 |
32 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) { |
32 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) { |
33 int max_id = -1; |
33 allocate_memory(); |
34 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
|
35 int id = getGraph()->id(it); |
|
36 if (id > max_id) { |
|
37 max_id = id; |
|
38 } |
|
39 } |
|
40 if (max_id == -1) { |
|
41 capacity = 0; |
|
42 values = 0; |
|
43 return; |
|
44 } |
|
45 capacity = 1; |
|
46 while (capacity <= max_id) { |
|
47 capacity <<= 1; |
|
48 } |
|
49 values = allocator.allocate(capacity); |
|
50 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
34 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
51 int id = getGraph()->id(it); |
35 int id = getGraph()->id(it); |
52 allocator.construct(&(values[id]), Value()); |
36 allocator.construct(&(values[id]), Value()); |
53 } |
37 } |
54 } |
38 } |
55 |
39 |
56 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) { |
40 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) { |
57 int max_id = -1; |
41 allocate_memory(); |
58 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
|
59 int id = getGraph()->id(it); |
|
60 if (id > max_id) { |
|
61 max_id = id; |
|
62 } |
|
63 } |
|
64 if (max_id == -1) { |
|
65 capacity = 0; |
|
66 values = 0; |
|
67 return; |
|
68 } |
|
69 capacity = 1; |
|
70 while (capacity <= max_id) { |
|
71 capacity <<= 1; |
|
72 } |
|
73 values = allocator.allocate(capacity); |
|
74 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
42 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
75 int id = getGraph()->id(it); |
43 int id = getGraph()->id(it); |
76 allocator.construct(&(values[id]), v); |
44 allocator.construct(&(values[id]), v); |
77 } |
45 } |
78 } |
46 } |
85 int id = getGraph()->id(it); |
53 int id = getGraph()->id(it); |
86 allocator.construct(&(values[id]), copy.values[id]); |
54 allocator.construct(&(values[id]), copy.values[id]); |
87 } |
55 } |
88 } |
56 } |
89 |
57 |
90 template <typename CMap> Map(const CMap& copy) : MapBase(copy) { |
58 template <typename CMap> Map(const CMap& copy) |
|
59 : capacity(0), values(0), MapBase(copy) { |
91 if (getGraph()) { |
60 if (getGraph()) { |
92 init(); |
61 allocate_memory(); |
93 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
62 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { |
94 set(it, copy[it]); |
63 set(it, copy[it]); |
95 } |
64 } |
96 } |
65 } |
97 } |
66 } |
179 void erase(const Key& key) { |
146 void erase(const Key& key) { |
180 int id = getGraph()->id(key); |
147 int id = getGraph()->id(key); |
181 allocator.destroy(&(values[id])); |
148 allocator.destroy(&(values[id])); |
182 } |
149 } |
183 |
150 |
184 /** Compatible iterator with the stl maps' iterators. |
|
185 * It iterates on pairs of a key and a value. |
|
186 */ |
|
187 class iterator { |
151 class iterator { |
188 friend class Map; |
152 friend class Map; |
189 friend class const_iterator; |
153 friend class const_iterator; |
190 private: |
154 private: |
191 |
155 |
192 /** Private constructor to initalize the the iterators returned |
156 /** Private constructor to initalize the the iterators returned |
193 * by the begin() and end(). |
157 * by the begin() and end(). |
194 */ |
158 */ |
195 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} |
159 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} |
196 |
160 |
197 public: |
161 public: |
198 |
162 |
199 /** Default constructor. |
163 /** Default constructor. |
200 */ |
164 */ |
201 iterator() {} |
165 iterator() {} |
|
166 |
|
167 typedef extended_pair<const Key&, const Key&, |
|
168 Value&, Value&> Reference; |
202 |
169 |
203 /** Dereference operator for map. |
170 /** Dereference operator for map. |
204 */ |
171 */ |
205 std::pair<const Key, Value> operator*() { |
172 Reference operator*() { |
206 return std::pair<const Key, Value>(it, (*map)[it]); |
173 return Reference(it, (*map)[it]); |
207 } |
174 } |
|
175 |
|
176 class Pointer { |
|
177 friend class iterator; |
|
178 private: |
|
179 Reference data; |
|
180 Pointer(const Key& key, Value& val) : data(key, val) {} |
|
181 public: |
|
182 Reference* operator->() {return &data;} |
|
183 }; |
208 |
184 |
209 /** Arrow operator for map. |
185 /** Arrow operator for map. |
210 */ |
186 */ |
211 std::pair<const Key, Value>* operator->() { |
187 Pointer operator->() { |
212 static std::pair<const Key, Value> tmp = operator*(); |
188 return Pointer(it, ((*map)[it])); |
213 return &tmp; |
|
214 } |
189 } |
215 |
190 |
216 /** The pre increment operator of the map. |
191 /** The pre increment operator of the map. |
217 */ |
192 */ |
218 iterator& operator++() { |
193 iterator& operator++() { |
258 } |
234 } |
259 |
235 |
260 class const_iterator { |
236 class const_iterator { |
261 friend class Map; |
237 friend class Map; |
262 friend class iterator; |
238 friend class iterator; |
263 private: |
239 private: |
264 |
240 |
265 /** Private constructor to initalize the the iterators returned |
241 /** Private constructor to initalize the the iterators returned |
266 * by the begin() and end(). |
242 * by the begin() and end(). |
267 */ |
243 */ |
268 const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} |
244 const_iterator (const Map& pmap, const KeyIt& pit) |
269 |
245 : map(&pmap), it(pit) {} |
270 public: |
246 |
|
247 public: |
271 |
248 |
272 /** Default constructor. |
249 /** Default constructor. |
273 */ |
250 */ |
274 const_iterator() {} |
251 const_iterator() {} |
275 |
252 |
276 /** Constructor to convert iterator to const_iterator. |
253 /** Constructor to convert iterator to const_iterator. |
277 */ |
254 */ |
278 const_iterator(iterator p_it) { |
255 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {} |
279 it = p_it.it; |
|
280 } |
|
281 |
256 |
|
257 typedef extended_pair<const Key&, const Key&, |
|
258 const Value&, const Value&> Reference; |
|
259 |
282 /** Dereference operator for map. |
260 /** Dereference operator for map. |
283 */ |
261 */ |
284 std::pair<const Key, const Value> operator*() const { |
262 Reference operator*() { |
285 return std::pair<const Key, const Value>(it, (*map)[it]); |
263 return Reference(it, (*map)[it]); |
286 } |
264 } |
|
265 |
|
266 |
|
267 class Pointer { |
|
268 friend class const_iterator; |
|
269 private: |
|
270 Reference data; |
|
271 Pointer(const Key& key, const Value& val) : data(key, val) {} |
|
272 public: |
|
273 Reference* operator->() {return &data;} |
|
274 }; |
287 |
275 |
288 /** Arrow operator for map. |
276 /** Arrow operator for map. |
289 */ |
277 */ |
290 std::pair<const Key, const Value>* operator->() const { |
278 Pointer operator->() { |
291 static std::pair<const Key, const Value> tmp = operator*(); |
279 return Pointer(it, ((*map)[it])); |
292 return &tmp; |
|
293 } |
280 } |
294 |
281 |
295 /** The pre increment operator of the map. |
282 /** The pre increment operator of the map. |
296 */ |
283 */ |
297 const_iterator& operator++() { |
284 const_iterator& operator++() { |