7 #include <hugo/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()); it != INVALID; ++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()); it != INVALID; ++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()); it != INVALID; ++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 /** Erase a key from the map. It called by the map registry.
141 void erase(const Key& key) {}
143 /** Clear the data structure.
149 /** Compatible iterator with the stl maps' iterators.
150 * It iterates on pairs of a key and a value.
154 friend class const_iterator;
157 /** Private constructor to initalize the the iterators returned
158 * by the begin() and end().
160 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
164 /** Default constructor.
168 typedef extended_pair<const Key&, const Key&,
169 Value&, Value&> Reference;
171 /** Dereference operator for map.
173 Reference operator*() {
174 return Reference(it, (*map)[it]);
178 friend class iterator;
181 Pointer(const Key& key, Value& val) : data(key, val) {}
183 Reference* operator->() {return &data;}
186 /** Arrow operator for map.
188 Pointer operator->() {
189 return Pointer(it, ((*map)[it]));
192 /** The pre increment operator of the map.
194 iterator& operator++() {
199 /** The post increment operator of the map.
201 iterator operator++(int) {
207 /** The equality operator of the map.
209 bool operator==(const_iterator p_it) {
210 return p_it.it == it;
213 /** The not-equality operator of the map.
215 bool operator!=(const_iterator p_it) {
216 return !(*this == p_it);
225 /** Returns the begin iterator of the map.
228 return iterator(*this, KeyIt(*getGraph()));
231 /** Returns the end iterator of the map.
234 return iterator(*this, INVALID);
237 class const_iterator {
239 friend class iterator;
242 /** Private constructor to initalize the the iterators returned
243 * by the begin() and end().
245 const_iterator (const Map& pmap, const KeyIt& pit)
246 : map(&pmap), it(pit) {}
250 /** Default constructor.
254 /** Constructor to convert iterator to const_iterator.
256 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
258 typedef extended_pair<const Key&, const Key&,
259 const Value&, const Value&> Reference;
261 /** Dereference operator for map.
263 Reference operator*() {
264 return Reference(it, (*map)[it]);
269 friend class const_iterator;
272 Pointer(const Key& key, const Value& val) : data(key, val) {}
274 Reference* operator->() {return &data;}
277 /** Arrow operator for map.
279 Pointer operator->() {
280 return Pointer(it, ((*map)[it]));
283 /** The pre increment operator of the map.
285 const_iterator& operator++() {
290 /** The post increment operator of the map.
292 const_iterator operator++(int) {
293 const_iterator tmp(it);
298 /** The equality operator of the map.
300 bool operator==(const_iterator p_it) {
301 return p_it.it == it;
304 /** The not-equality operator of the map.
306 bool operator!=(const_iterator p_it) {
307 return !(*this == p_it);
316 /** Returns the begin const_iterator of the map.
318 const_iterator begin() const {
319 return const_iterator(*this, KeyIt(*getGraph()));
322 /** Returns the end const_iterator of the map.
324 const_iterator end() const {
325 return const_iterator(*this, INVALID);