2 #ifndef ARRAY_MAP_FACTORY_H
3 #define ARRAY_MAP_FACTORY_H
7 #include <hugo/extended_pair.h>
11 template <typename MapRegistry> class ArrayMapFactory {
15 typedef typename MapRegistry::Graph Graph;
16 typedef typename MapRegistry::Key Key;
17 typedef typename MapRegistry::KeyIt KeyIt;
19 typedef typename MapRegistry::MapBase MapBase;
21 template <typename V, typename A = std::allocator<V> >
22 class Map : public MapBase {
30 Map() : values(0), capacity(0) {}
32 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
34 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
35 int id = MapBase::getGraph()->id(it);
36 allocator.construct(&(values[id]), Value());
40 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
42 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
43 int id = MapBase::getGraph()->id(it);
44 allocator.construct(&(values[id]), v);
48 Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
49 capacity = copy.capacity;
50 if (capacity == 0) return;
51 values = allocator.allocate(capacity);
52 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
53 int id = MapBase::getGraph()->id(it);
54 allocator.construct(&(values[id]), copy.values[id]);
58 template <typename CMap> Map(const CMap& copy)
59 : capacity(0), values(0), MapBase(copy) {
60 if (MapBase::getGraph()) {
62 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
68 Map& operator=(const Map& copy) {
69 if (© == this) return *this;
72 allocator.deallocate(values, capacity);
74 capacity = copy.capacity;
75 if (capacity == 0) return *this;
76 values = allocator.allocate(capacity);
77 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
78 int id = MapBase::getGraph()->id(it);
79 allocator.construct(&(values[id]), copy.values[id]);
84 template <typename CMap> Map& operator=(const CMap& copy) {
85 if (MapBase::getGraph()) {
88 MapBase::operator=(copy);
89 if (MapBase::getGraph()) {
91 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
101 allocator.deallocate(values, capacity);
106 Value& operator[](const Key& key) {
107 int id = MapBase::getGraph()->id(key);
111 const Value& operator[](const Key& key) const {
112 int id = MapBase::getGraph()->id(key);
116 const Value& get(const Key& key) const {
117 int id = MapBase::getGraph()->id(key);
121 void set(const Key& key, const Value& val) {
122 int id = MapBase::getGraph()->id(key);
126 void add(const Key& key) {
127 int id = MapBase::getGraph()->id(key);
128 if (id >= capacity) {
129 int new_capacity = (capacity == 0 ? 1 : capacity);
130 while (new_capacity <= id) {
133 Value* new_values = allocator.allocate(new_capacity);;
134 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
135 int jd = MapBase::getGraph()->id(it);
137 allocator.construct(&(new_values[jd]), values[jd]);
138 allocator.destroy(&(values[jd]));
141 if (capacity != 0) allocator.deallocate(values, capacity);
143 capacity = new_capacity;
145 allocator.construct(&(values[id]), Value());
148 void erase(const Key& key) {
149 int id = MapBase::getGraph()->id(key);
150 allocator.destroy(&(values[id]));
156 allocator.deallocate(values, capacity);
163 friend class const_iterator;
166 /** Private constructor to initalize the the iterators returned
167 * by the begin() and end().
169 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
173 /** Default constructor.
177 typedef extended_pair<const Key&, const Key&,
178 Value&, Value&> Reference;
180 /** Dereference operator for map.
182 Reference operator*() {
183 return Reference(it, (*map)[it]);
187 friend class iterator;
190 Pointer(const Key& key, Value& val) : data(key, val) {}
192 Reference* operator->() {return &data;}
195 /** Arrow operator for map.
197 Pointer operator->() {
198 return Pointer(it, ((*map)[it]));
201 /** The pre increment operator of the map.
203 iterator& operator++() {
208 /** The post increment operator of the map.
210 iterator operator++(int) {
216 /** The equality operator of the map.
218 bool operator==(const_iterator p_it) {
219 return p_it.it == it;
222 /** The not-equality operator of the map.
224 bool operator!=(const_iterator p_it) {
225 return !(*this == p_it);
234 /** Returns the begin iterator of the map.
237 return iterator(*this, KeyIt(*MapBase::getGraph()));
240 /** Returns the end iterator of the map.
243 return iterator(*this, INVALID);
246 class const_iterator {
248 friend class iterator;
251 /** Private constructor to initalize the the iterators returned
252 * by the begin() and end().
254 const_iterator (const Map& pmap, const KeyIt& pit)
255 : map(&pmap), it(pit) {}
259 /** Default constructor.
263 /** Constructor to convert iterator to const_iterator.
265 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
267 typedef extended_pair<const Key&, const Key&,
268 const Value&, const Value&> Reference;
270 /** Dereference operator for map.
272 Reference operator*() {
273 return Reference(it, (*map)[it]);
278 friend class const_iterator;
281 Pointer(const Key& key, const Value& val) : data(key, val) {}
283 Reference* operator->() {return &data;}
286 /** Arrow operator for map.
288 Pointer operator->() {
289 return Pointer(it, ((*map)[it]));
292 /** The pre increment operator of the map.
294 const_iterator& operator++() {
299 /** The post increment operator of the map.
301 const_iterator operator++(int) {
302 const_iterator tmp(it);
307 /** The equality operator of the map.
309 bool operator==(const_iterator p_it) {
310 return p_it.it == it;
313 /** The not-equality operator of the map.
315 bool operator!=(const_iterator p_it) {
316 return !(*this == p_it);
325 /** Returns the begin const_iterator of the map.
327 const_iterator begin() const {
328 return const_iterator(*this, KeyIt(*MapBase::getGraph()));
331 /** Returns the end const_iterator of the map.
333 const_iterator end() const {
334 return const_iterator(*this, INVALID);
339 void allocate_memory() {
341 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
342 int id = MapBase::getGraph()->id(it);
353 while (capacity <= max_id) {
356 values = allocator.allocate(capacity);