12 template <typename MapRegistry> class ArrayMapFactory {
16 typedef typename MapRegistry::Graph Graph;
17 typedef typename MapRegistry::Key Key;
18 typedef typename MapRegistry::KeyIt KeyIt;
20 typedef typename MapRegistry::MapBase MapBase;
22 template <typename V, typename A = std::allocator<V> > class Map : public MapBase {
30 Map() : values(0), capacity(0) {}
32 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
34 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
35 int id = getGraph()->id(it);
46 while (capacity <= max_id) {
49 values = allocator.allocate(capacity);
50 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
51 int id = getGraph()->id(it);
52 allocator.construct(&(values[id]), Value());
56 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
58 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
59 int id = getGraph()->id(it);
70 while (capacity <= max_id) {
73 values = allocator.allocate(capacity);
74 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
75 int id = getGraph()->id(it);
76 allocator.construct(&(values[id]), v);
80 Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
81 capacity = copy.capacity;
82 if (capacity == 0) return;
83 values = allocator.allocate(capacity);
84 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
85 int id = getGraph()->id(it);
86 allocator.construct(&(values[id]), copy.values[id]);
90 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
93 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
99 Map& operator=(const Map& copy) {
100 if (© == this) return;
103 allocator.deallocate(values, capacity);
105 capacity = copy.capacity;
106 if (capacity == 0) return;
107 values = allocator.allocate(capacity);
108 for (KeyIt it(getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
109 int id = getGraph()->id(it);
110 allocator.construct(&(values[id]), copy.values[id]);
114 template <typename CMap> Map& operator=(const CMap& copy) {
118 this->MapBase::operator=(copy);
121 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
132 allocator.deallocate(values, capacity);
137 Value& operator[](const Key& key) {
138 int id = getGraph()->id(key);
142 const Value& operator[](const Key& key) const {
143 int id = getGraph()->id(key);
147 const Value& get(const Key& key) const {
148 int id = getGraph()->id(key);
152 void set(const Key& key, const Value& val) {
153 int id = getGraph()->id(key);
157 void add(const Key& key) {
158 int id = getGraph()->id(key);
159 if (id >= capacity) {
160 int new_capacity = (capacity == 0 ? 1 : capacity);
161 while (new_capacity <= id) {
164 Value* new_values = allocator.allocate(new_capacity);;
165 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
166 int jd = getGraph()->id(it);
168 allocator.construct(&(new_values[jd]), values[jd]);
169 allocator.destroy(&(values[jd]));
172 if (capacity != 0) allocator.deallocate(values, capacity);
174 capacity = new_capacity;
176 allocator.construct(&(values[id]), Value());
179 void erase(const Key& key) {
180 int id = getGraph()->id(key);
181 allocator.destroy(&(values[id]));
184 /** Compatible iterator with the stl maps' iterators.
185 * It iterates on pairs of a key and a value.
189 friend class const_iterator;
192 /** Private constructor to initalize the the iterators returned
193 * by the begin() and end().
195 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
199 /** Default constructor.
203 /** Dereference operator for map.
205 std::pair<const Key, Value> operator*() {
206 return std::pair<const Key, Value>(it, (*map)[it]);
209 /** Arrow operator for map.
211 std::pair<const Key, Value>* operator->() {
212 static std::pair<const Key, Value> tmp = operator*();
216 /** The pre increment operator of the map.
218 iterator& operator++() {
219 map->getGraph()->next(it);
223 /** The post increment operator of the map.
225 iterator operator++(int) {
227 map.getGraph()->next(it);
231 /** The equality operator of the map.
233 bool operator==(const_iterator p_it) {
234 return p_it.it == it;
237 /** The not-equality operator of the map.
239 bool operator!=(const_iterator p_it) {
240 return !(*this == p_it);
248 /** Returns the begin iterator of the map.
251 return iterator(*this, KeyIt(*getGraph()));
254 /** Returns the end iterator of the map.
257 return iterator(*this, INVALID);
260 class const_iterator {
262 friend class iterator;
265 /** Private constructor to initalize the the iterators returned
266 * by the begin() and end().
268 const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
272 /** Default constructor.
276 /** Constructor to convert iterator to const_iterator.
278 const_iterator(iterator p_it) {
282 /** Dereference operator for map.
284 std::pair<const Key, const Value> operator*() const {
285 return std::pair<const Key, const Value>(it, (*map)[it]);
288 /** Arrow operator for map.
290 std::pair<const Key, const Value>* operator->() const {
291 static std::pair<const Key, const Value> tmp = operator*();
295 /** The pre increment operator of the map.
297 const_iterator& operator++() {
298 map->getGraph()->next(it);
302 /** The post increment operator of the map.
304 const_iterator operator++(int) {
305 const_iterator tmp(it);
306 map->getGraph()->next(it);
310 /** The equality operator of the map.
312 bool operator==(const_iterator p_it) {
313 return p_it.it == it;
316 /** The not-equality operator of the map.
318 bool operator!=(const_iterator p_it) {
319 return !(*this == p_it);
327 /** Returns the begin const_iterator of the map.
329 const_iterator begin() const {
330 return const_iterator(*this, KeyIt(*getGraph()));
333 /** Returns the end const_iterator of the map.
335 const_iterator end() const {
336 return const_iterator(*this, INVALID);