Changing first to iterators.
7 #include "extended_pair.h"
11 /** The VectorMapFactory template class is a factory class
12 * to create maps for the edge and nodes. This map factory
13 * use the std::vector to implement the container function.
15 * The template parameter is the MapRegistry that the maps
19 template <typename MapRegistry>
20 class VectorMapFactory {
23 /// The graph type of the maps.
24 typedef typename MapRegistry::Graph Graph;
25 /// The key type of the maps.
26 typedef typename MapRegistry::Key Key;
27 /// The iterator to iterate on the keys.
28 typedef typename MapRegistry::KeyIt KeyIt;
30 /// The MapBase of the Map which imlements the core regisitry function.
31 typedef typename MapRegistry::MapBase MapBase;
34 /** The template Map type.
37 class Map : public MapBase {
40 /// The value type of the map.
43 typedef std::vector<Value> Container;
45 /** Default constructor for the map.
49 /** Graph and Registry initialized map constructor.
51 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
55 /** Constructor to use default value to initialize the map.
57 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);
60 if (id >= container.size) {
61 container.resize(id + 1);
67 /** Constructor to copy a map of an other map type.
69 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
71 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
72 int id = getGraph->id(it);
73 if (id >= container.size) {
74 container.resize(id + 1);
81 /** Assign operator to copy a map an other map type.
83 template <typename CMap> Map& operator=(const CMap& copy) {
87 this->MapBase::operator=(copy);
89 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
90 int id = getGraph->id(it);
91 if (id >= container.size) {
92 container.resize(id + 1);
99 /** The destructor of the map.
105 * The subscript operator. The map can be subscripted by the
106 * actual keys of the graph.
108 typename Container::reference operator[](const Key& key) {
109 int id = getGraph()->id(key);
110 return container[id];
114 * The const subscript operator. The map can be subscripted by the
115 * actual keys of the graph.
117 typename Container::const_reference operator[](const Key& key) const {
118 int id = getGraph()->id(key);
119 return container[id];
122 /** Setter function of the map. Equivalent with map[key] = val.
123 * This is a compatibility feature with the not dereferable maps.
125 void set(const Key& key, const Value& val) {
126 int id = getGraph()->id(key);
130 /** Add a new key to the map. It called by the map registry.
132 void add(const Key& key) {
133 int id = getGraph()->id(key);
134 if (id >= container.size()) {
135 container.resize(id + 1);
139 /** Erease a key from the map. It called by the map registry.
141 void erase(const Key& key) {}
143 /** Compatible iterator with the stl maps' iterators.
144 * It iterates on pairs of a key and a value.
148 friend class const_iterator;
151 /** Private constructor to initalize the the iterators returned
152 * by the begin() and end().
154 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
158 /** Default constructor.
162 typedef extended_pair<const Key&, const Key&,
163 Value&, Value&> Reference;
165 /** Dereference operator for map.
167 Reference operator*() {
168 return Reference(it, (*map)[it]);
172 friend class iterator;
175 Pointer(const Key& key, Value& val) : data(key, val) {}
177 Reference* operator->() {return &data;}
180 /** Arrow operator for map.
182 Pointer operator->() {
183 return Pointer(it, ((*map)[it]));
186 /** The pre increment operator of the map.
188 iterator& operator++() {
189 map->getGraph()->next(it);
193 /** The post increment operator of the map.
195 iterator operator++(int) {
197 map.getGraph()->next(it);
201 /** The equality operator of the map.
203 bool operator==(const_iterator p_it) {
204 return p_it.it == it;
207 /** The not-equality operator of the map.
209 bool operator!=(const_iterator p_it) {
210 return !(*this == p_it);
219 /** Returns the begin iterator of the map.
222 return iterator(*this, KeyIt(*getGraph()));
225 /** Returns the end iterator of the map.
228 return iterator(*this, INVALID);
231 class const_iterator {
233 friend class iterator;
236 /** Private constructor to initalize the the iterators returned
237 * by the begin() and end().
239 const_iterator (const Map& pmap, const KeyIt& pit)
240 : map(&pmap), it(pit) {}
244 /** Default constructor.
248 /** Constructor to convert iterator to const_iterator.
250 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
252 typedef extended_pair<const Key&, const Key&,
253 const Value&, const Value&> Reference;
255 /** Dereference operator for map.
257 Reference operator*() {
258 return Reference(it, (*map)[it]);
263 friend class const_iterator;
266 Pointer(const Key& key, const Value& val) : data(key, val) {}
268 Reference* operator->() {return &data;}
271 /** Arrow operator for map.
273 Pointer operator->() {
274 return Pointer(it, ((*map)[it]));
277 /** The pre increment operator of the map.
279 const_iterator& operator++() {
280 map->getGraph()->next(it);
284 /** The post increment operator of the map.
286 const_iterator operator++(int) {
287 const_iterator tmp(it);
288 map->getGraph()->next(it);
292 /** The equality operator of the map.
294 bool operator==(const_iterator p_it) {
295 return p_it.it == it;
298 /** The not-equality operator of the map.
300 bool operator!=(const_iterator p_it) {
301 return !(*this == p_it);
310 /** Returns the begin const_iterator of the map.
312 const_iterator begin() const {
313 return const_iterator(*this, KeyIt(*getGraph()));
316 /** Returns the end const_iterator of the map.
318 const_iterator end() const {
319 return const_iterator(*this, INVALID);