.
7 #include "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(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
35 int id = 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(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
43 int id = 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(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
53 int id = 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) {
62 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
68 Map& operator=(const Map& copy) {
69 if (© == this) return;
72 allocator.deallocate(values, capacity);
74 capacity = copy.capacity;
75 if (capacity == 0) return;
76 values = allocator.allocate(capacity);
77 for (KeyIt it(getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
78 int id = getGraph()->id(it);
79 allocator.construct(&(values[id]), copy.values[id]);
83 template <typename CMap> Map& operator=(const CMap& copy) {
87 this->MapBase::operator=(copy);
90 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
99 allocator.deallocate(values, capacity);
104 Value& operator[](const Key& key) {
105 int id = getGraph()->id(key);
109 const Value& operator[](const Key& key) const {
110 int id = getGraph()->id(key);
114 const Value& get(const Key& key) const {
115 int id = getGraph()->id(key);
119 void set(const Key& key, const Value& val) {
120 int id = getGraph()->id(key);
124 void add(const Key& key) {
125 int id = getGraph()->id(key);
126 if (id >= capacity) {
127 int new_capacity = (capacity == 0 ? 1 : capacity);
128 while (new_capacity <= id) {
131 Value* new_values = allocator.allocate(new_capacity);;
132 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
133 int jd = getGraph()->id(it);
135 allocator.construct(&(new_values[jd]), values[jd]);
136 allocator.destroy(&(values[jd]));
139 if (capacity != 0) allocator.deallocate(values, capacity);
141 capacity = new_capacity;
143 allocator.construct(&(values[id]), Value());
146 void erase(const Key& key) {
147 int id = getGraph()->id(key);
148 allocator.destroy(&(values[id]));
153 friend class const_iterator;
156 /** Private constructor to initalize the the iterators returned
157 * by the begin() and end().
159 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
163 /** Default constructor.
167 typedef extended_pair<const Key&, const Key&,
168 Value&, Value&> Reference;
170 /** Dereference operator for map.
172 Reference operator*() {
173 return Reference(it, (*map)[it]);
177 friend class iterator;
180 Pointer(const Key& key, Value& val) : data(key, val) {}
182 Reference* operator->() {return &data;}
185 /** Arrow operator for map.
187 Pointer operator->() {
188 return Pointer(it, ((*map)[it]));
191 /** The pre increment operator of the map.
193 iterator& operator++() {
194 map->getGraph()->next(it);
198 /** The post increment operator of the map.
200 iterator operator++(int) {
202 map.getGraph()->next(it);
206 /** The equality operator of the map.
208 bool operator==(const_iterator p_it) {
209 return p_it.it == it;
212 /** The not-equality operator of the map.
214 bool operator!=(const_iterator p_it) {
215 return !(*this == p_it);
224 /** Returns the begin iterator of the map.
227 return iterator(*this, KeyIt(*getGraph()));
230 /** Returns the end iterator of the map.
233 return iterator(*this, INVALID);
236 class const_iterator {
238 friend class iterator;
241 /** Private constructor to initalize the the iterators returned
242 * by the begin() and end().
244 const_iterator (const Map& pmap, const KeyIt& pit)
245 : map(&pmap), it(pit) {}
249 /** Default constructor.
253 /** Constructor to convert iterator to const_iterator.
255 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
257 typedef extended_pair<const Key&, const Key&,
258 const Value&, const Value&> Reference;
260 /** Dereference operator for map.
262 Reference operator*() {
263 return Reference(it, (*map)[it]);
268 friend class const_iterator;
271 Pointer(const Key& key, const Value& val) : data(key, val) {}
273 Reference* operator->() {return &data;}
276 /** Arrow operator for map.
278 Pointer operator->() {
279 return Pointer(it, ((*map)[it]));
282 /** The pre increment operator of the map.
284 const_iterator& operator++() {
285 map->getGraph()->next(it);
289 /** The post increment operator of the map.
291 const_iterator operator++(int) {
292 const_iterator tmp(it);
293 map->getGraph()->next(it);
297 /** The equality operator of the map.
299 bool operator==(const_iterator p_it) {
300 return p_it.it == it;
303 /** The not-equality operator of the map.
305 bool operator!=(const_iterator p_it) {
306 return !(*this == p_it);
315 /** Returns the begin const_iterator of the map.
317 const_iterator begin() const {
318 return const_iterator(*this, KeyIt(*getGraph()));
321 /** Returns the end const_iterator of the map.
323 const_iterator end() const {
324 return const_iterator(*this, INVALID);
329 void allocate_memory() {
331 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
332 int id = getGraph()->id(it);
343 while (capacity <= max_id) {
346 values = allocator.allocate(capacity);