92 |
89 |
93 public: |
90 public: |
94 |
91 |
95 /** Graph and Registry initialized map constructor. |
92 /** Graph and Registry initialized map constructor. |
96 */ |
93 */ |
97 ArrayMap(const Graph& _g, Registry& _r) : graph(&_g) { |
94 ArrayMap(const Graph& _g) : graph(&_g) { |
98 attach(_r); |
95 attach(_g.getObserverRegistry(_Item())); |
99 allocate_memory(); |
96 allocate_memory(); |
100 for (KeyIt it(*graph); it != INVALID; ++it) { |
97 for (KeyIt it(*graph); it != INVALID; ++it) { |
101 int id = IdMap(*graph)[it]; |
98 int id = graph->id(it);; |
102 allocator.construct(&(values[id]), Value()); |
99 allocator.construct(&(values[id]), Value()); |
103 } |
100 } |
104 } |
101 } |
105 |
102 |
106 /// Constructor to use default value to initialize the map. |
103 /// Constructor to use default value to initialize the map. |
107 |
104 |
108 /// It constrates a map and initialize all of the the map. |
105 /// It constrates a map and initialize all of the the map. |
109 |
106 |
110 ArrayMap(const Graph& _g, Registry& _r, const Value& _v) : graph(&_g) { |
107 ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) { |
111 attach(_r); |
108 attach(_g.getObserverRegistry(_Item())); |
112 allocate_memory(); |
109 allocate_memory(); |
113 for (KeyIt it(*graph); it != INVALID; ++it) { |
110 for (KeyIt it(*graph); it != INVALID; ++it) { |
114 int id = IdMap(*graph)[it]; |
111 int id = graph->id(it);; |
115 allocator.construct(&(values[id]), _v); |
112 allocator.construct(&(values[id]), _v); |
116 } |
113 } |
117 } |
114 } |
118 |
115 |
119 /** Constructor to copy a map of the same map type. |
116 /** Constructor to copy a map of the same map type. |
174 /** |
171 /** |
175 * The subscript operator. The map can be subscripted by the |
172 * The subscript operator. The map can be subscripted by the |
176 * actual keys of the graph. |
173 * actual keys of the graph. |
177 */ |
174 */ |
178 ReferenceType operator[](const KeyType& key) { |
175 ReferenceType operator[](const KeyType& key) { |
179 int id = IdMap(*graph)[key]; |
176 int id = graph->id(key); |
180 return values[id]; |
177 return values[id]; |
181 } |
178 } |
182 |
179 |
183 /** |
180 /** |
184 * The const subscript operator. The map can be subscripted by the |
181 * The const subscript operator. The map can be subscripted by the |
185 * actual keys of the graph. |
182 * actual keys of the graph. |
186 */ |
183 */ |
187 ConstReferenceType operator[](const KeyType& key) const { |
184 ConstReferenceType operator[](const KeyType& key) const { |
188 int id = IdMap(*graph)[key]; |
185 int id = graph->id(key); |
189 return values[id]; |
186 return values[id]; |
190 } |
187 } |
191 |
188 |
192 /** Setter function of the map. Equivalent with map[key] = val. |
189 /** Setter function of the map. Equivalent with map[key] = val. |
193 * This is a compatibility feature with the not dereferable maps. |
190 * This is a compatibility feature with the not dereferable maps. |
197 } |
194 } |
198 |
195 |
199 /** Add a new key to the map. It called by the map registry. |
196 /** Add a new key to the map. It called by the map registry. |
200 */ |
197 */ |
201 void add(const KeyType& key) { |
198 void add(const KeyType& key) { |
202 int id = IdMap(*graph)[key]; |
199 int id = graph->id(key); |
203 if (id >= capacity) { |
200 if (id >= capacity) { |
204 int new_capacity = (capacity == 0 ? 1 : capacity); |
201 int new_capacity = (capacity == 0 ? 1 : capacity); |
205 while (new_capacity <= id) { |
202 while (new_capacity <= id) { |
206 new_capacity <<= 1; |
203 new_capacity <<= 1; |
207 } |
204 } |
208 Value* new_values = allocator.allocate(new_capacity); |
205 Value* new_values = allocator.allocate(new_capacity); |
209 for (KeyIt it(*graph); it != INVALID; ++it) { |
206 for (KeyIt it(*graph); it != INVALID; ++it) { |
210 int jd = IdMap(*graph)[it]; |
207 int jd = graph->id(it);; |
211 if (id != jd) { |
208 if (id != jd) { |
212 allocator.construct(&(new_values[jd]), values[jd]); |
209 allocator.construct(&(new_values[jd]), values[jd]); |
213 allocator.destroy(&(values[jd])); |
210 allocator.destroy(&(values[jd])); |
214 } |
211 } |
215 } |
212 } |
221 } |
218 } |
222 |
219 |
223 /** Erase a key from the map. It called by the map registry. |
220 /** Erase a key from the map. It called by the map registry. |
224 */ |
221 */ |
225 void erase(const KeyType& key) { |
222 void erase(const KeyType& key) { |
226 int id = IdMap(*graph)[key]; |
223 int id = graph->id(key); |
227 allocator.destroy(&(values[id])); |
224 allocator.destroy(&(values[id])); |
228 } |
225 } |
229 |
226 |
230 void build() { |
227 void build() { |
231 allocate_memory(); |
228 allocate_memory(); |
232 for (KeyIt it(*graph); it != INVALID; ++it) { |
229 for (KeyIt it(*graph); it != INVALID; ++it) { |
233 int id = IdMap(*graph)[it]; |
230 int id = graph->id(it);; |
234 allocator.construct(&(values[id]), Value()); |
231 allocator.construct(&(values[id]), Value()); |
235 } |
232 } |
236 } |
233 } |
237 |
234 |
238 void clear() { |
235 void clear() { |
239 if (capacity != 0) { |
236 if (capacity != 0) { |
240 for (KeyIt it(*graph); it != INVALID; ++it) { |
237 for (KeyIt it(*graph); it != INVALID; ++it) { |
241 int id = IdMap(*graph)[it]; |
238 int id = graph->id(it);; |
242 allocator.destroy(&(values[id])); |
239 allocator.destroy(&(values[id])); |
243 } |
240 } |
244 allocator.deallocate(values, capacity); |
241 allocator.deallocate(values, capacity); |
245 capacity = 0; |
242 capacity = 0; |
246 } |
243 } |
341 typedef ArrayMappableGraphExtender<_Base> Graph; |
338 typedef ArrayMappableGraphExtender<_Base> Graph; |
342 typedef _Base Parent; |
339 typedef _Base Parent; |
343 |
340 |
344 typedef typename Parent::Node Node; |
341 typedef typename Parent::Node Node; |
345 typedef typename Parent::NodeIt NodeIt; |
342 typedef typename Parent::NodeIt NodeIt; |
346 typedef typename Parent::NodeIdMap NodeIdMap; |
|
347 typedef typename Parent::NodeObserverRegistry NodeObserverRegistry; |
343 typedef typename Parent::NodeObserverRegistry NodeObserverRegistry; |
348 |
344 |
349 typedef typename Parent::Edge Edge; |
345 typedef typename Parent::Edge Edge; |
350 typedef typename Parent::EdgeIt EdgeIt; |
346 typedef typename Parent::EdgeIt EdgeIt; |
351 typedef typename Parent::EdgeIdMap EdgeIdMap; |
|
352 typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry; |
347 typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry; |
353 |
348 |
354 |
349 |
355 |
350 |
356 template <typename _Value> |
351 template <typename _Value> |
357 class NodeMap : public ArrayMap<Graph, Node, NodeIt, NodeIdMap, _Value> { |
352 class NodeMap : public ArrayMap<Graph, Node, NodeIt, _Value> { |
358 public: |
353 public: |
359 typedef ArrayMappableGraphExtender<_Base> Graph; |
354 typedef ArrayMappableGraphExtender<_Base> Graph; |
360 |
355 |
361 typedef typename Graph::Node Node; |
356 typedef typename Graph::Node Node; |
362 typedef typename Graph::NodeIt NodeIt; |
357 typedef typename Graph::NodeIt NodeIt; |
363 typedef typename Graph::NodeIdMap NodeIdMap; |
358 |
364 |
359 typedef ArrayMap<Graph, Node, NodeIt, _Value> Parent; |
365 typedef ArrayMap<Graph, Node, NodeIt, NodeIdMap, _Value> Parent; |
|
366 |
360 |
367 //typedef typename Parent::Graph Graph; |
361 //typedef typename Parent::Graph Graph; |
368 typedef typename Parent::Value Value; |
362 typedef typename Parent::Value Value; |
369 |
363 |
370 NodeMap(const Graph& g) |
364 NodeMap(const Graph& g) |
371 : Parent(g, g.getNodeObserverRegistry()) {} |
365 : Parent(g) {} |
372 NodeMap(const Graph& g, const Value& v) |
366 NodeMap(const Graph& g, const Value& v) |
373 : Parent(g, g.getNodeObserverRegistry(), v) {} |
367 : Parent(g, v) {} |
374 |
368 |
375 }; |
369 }; |
376 |
370 |
377 template <typename _Value> |
371 template <typename _Value> |
378 class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> { |
372 class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, _Value> { |
379 public: |
373 public: |
380 typedef ArrayMappableGraphExtender<_Base> Graph; |
374 typedef ArrayMappableGraphExtender<_Base> Graph; |
381 |
375 |
382 typedef typename Graph::Edge Edge; |
376 typedef typename Graph::Edge Edge; |
383 typedef typename Graph::EdgeIt EdgeIt; |
377 typedef typename Graph::EdgeIt EdgeIt; |
384 typedef typename Graph::EdgeIdMap EdgeIdMap; |
378 |
385 |
379 typedef ArrayMap<Graph, Edge, EdgeIt, _Value> Parent; |
386 typedef ArrayMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> Parent; |
|
387 |
380 |
388 //typedef typename Parent::Graph Graph; |
381 //typedef typename Parent::Graph Graph; |
389 typedef typename Parent::Value Value; |
382 typedef typename Parent::Value Value; |
390 |
383 |
391 EdgeMap(const Graph& g) |
384 EdgeMap(const Graph& g) |
392 : Parent(g, g.getEdgeObserverRegistry()) {} |
385 : Parent(g) {} |
393 EdgeMap(const Graph& g, const Value& v) |
386 EdgeMap(const Graph& g, const Value& v) |
394 : Parent(g, g.getEdgeObserverRegistry(), v) {} |
387 : Parent(g, v) {} |
395 |
388 |
396 }; |
389 }; |
397 |
390 |
398 }; |
391 }; |
399 |
392 |