7 #include <hugo/extended_pair.h>
9 ///\ingroup graphmapfactory
11 ///\brief Vector based graph maps.
15 /// \addtogroup graphmapfactory
18 /** The VectorMapFactory template class is a factory class
19 * to create maps for the edge and nodes. This map factory
20 * uses the std::vector to implement the container function.
22 * The template parameter is the MapRegistry that the maps
25 * \todo It should use a faster initialization using the maxNodeId() or
26 * maxEdgeId() function of the graph istead of iterating through each
30 template <typename MapRegistry>
31 class VectorMapFactory {
34 /// The graph type of the maps.
35 typedef typename MapRegistry::Graph Graph;
36 /// The key type of the maps.
37 typedef typename MapRegistry::KeyType KeyType;
38 /// The iterator to iterate on the keys.
39 typedef typename MapRegistry::KeyIt KeyIt;
41 /// The MapBase of the Map which imlements the core regisitry function.
42 typedef typename MapRegistry::MapBase MapBase;
45 /** The template Map type.
48 class Map : public MapBase {
50 typedef std::vector<V> Container;
54 /// The value type of the map.
57 /// The value type of the map.
59 /// The reference type of the map;
60 typedef typename Container::reference Reference;
61 /// The pointer type of the map;
62 typedef typename Container::pointer Pointer;
64 /// The const value type of the map.
65 typedef const Value ConstValue;
66 /// The const reference type of the map;
67 typedef typename Container::const_reference ConstReference;
68 /// The pointer type of the map;
69 typedef typename Container::const_pointer ConstPointer;
71 /** Default constructor for the map.
75 /** Graph and Registry initialized map constructor.
77 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
81 /** Constructor to use default value to initialize the map.
83 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
84 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
85 int id = getGraph()->id(it);
86 if (id >= (int)container.size()) {
87 container.resize(id + 1);
93 /** Constructor to copy a map of an other map type.
95 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
97 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
98 int id = getGraph()->id(it);
99 if (id >= (int)container.size()) {
100 container.resize(id + 1);
107 /** Assign operator to copy a map an other map type.
109 template <typename CMap> Map& operator=(const CMap& copy) {
113 this->MapBase::operator=(copy);
115 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
116 int id = getGraph()->id(it);
117 if (id >= (int)container.size()) {
118 container.resize(id + 1);
126 /** The destructor of the map.
132 * The subscript operator. The map can be subscripted by the
133 * actual keys of the graph.
135 Reference operator[](const KeyType& key) {
136 int id = getGraph()->id(key);
137 return container[id];
141 * The const subscript operator. The map can be subscripted by the
142 * actual keys of the graph.
144 ConstReference operator[](const KeyType& key) const {
145 int id = getGraph()->id(key);
146 return container[id];
149 /** Setter function of the map. Equivalent with map[key] = val.
150 * This is a compatibility feature with the not dereferable maps.
152 void set(const KeyType& key, const Value& val) {
153 int id = getGraph()->id(key);
157 /** Add a new key to the map. It called by the map registry.
159 void add(const KeyType& key) {
160 int id = getGraph()->id(key);
161 if (id >= (int)container.size()) {
162 container.resize(id + 1);
166 /** Erase a key from the map. It called by the map registry.
168 void erase(const KeyType& key) {}
170 /** Clear the data structure.
177 class const_iterator;
179 /** Compatible iterator with the stl maps' iterators.
180 * It iterates on pairs of a key and a value.
184 friend class const_iterator;
187 /** Private constructor to initalize the the iterators returned
188 * by the begin() and end().
190 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
194 /** Default constructor.
198 typedef extended_pair<const KeyType&, const KeyType&,
199 Map::Reference, Map::Reference> Reference;
201 /** Dereference operator for map.
203 Reference operator*() {
204 return Reference(it, (*map)[it]);
208 friend class iterator;
211 Pointer(const KeyType& key, Map::Reference val) : data(key, val) {}
213 Reference* operator->() {return &data;}
216 /** Arrow operator for map.
218 Pointer operator->() {
219 return Pointer(it, ((*map)[it]));
222 /** The pre increment operator of the map.
224 iterator& operator++() {
229 /** The post increment operator of the map.
231 iterator operator++(int) {
237 /** The equality operator of the map.
239 bool operator==(const_iterator p_it) {
240 return p_it.it == it;
243 /** The not-equality operator of the map.
245 bool operator!=(const_iterator p_it) {
246 return !(*this == p_it);
255 /** Returns the begin iterator of the map.
258 return iterator(*this, KeyIt(*getGraph()));
261 /** Returns the end iterator of the map.
264 return iterator(*this, INVALID);
267 class const_iterator {
269 friend class iterator;
272 /** Private constructor to initalize the the iterators returned
273 * by the begin() and end().
275 const_iterator (const Map& pmap, const KeyIt& pit)
276 : map(&pmap), it(pit) {}
280 /** Default constructor.
284 /** Constructor to convert iterator to const_iterator.
286 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
288 typedef extended_pair<const KeyType&, const KeyType&,
289 Map::ConstReference, Map::ConstReference> Reference;
291 /** Dereference operator for map.
293 Reference operator*() {
294 return Reference(it, (*map)[it]);
299 friend class const_iterator;
302 Pointer(const KeyType& key, Map::ConstReference val)
305 Reference* operator->() {return &data;}
308 /** Arrow operator for map.
310 Pointer operator->() {
311 return Pointer(it, ((*map)[it]));
314 /** The pre increment operator of the map.
316 const_iterator& operator++() {
321 /** The post increment operator of the map.
323 const_iterator operator++(int) {
324 const_iterator tmp(it);
329 /** The equality operator of the map.
331 bool operator==(const_iterator p_it) {
332 return p_it.it == it;
335 /** The not-equality operator of the map.
337 bool operator!=(const_iterator p_it) {
338 return !(*this == p_it);
347 /** Returns the begin const_iterator of the map.
349 const_iterator begin() const {
350 return const_iterator(*this, KeyIt(*getGraph()));
353 /** Returns the end const_iterator of the map.
355 const_iterator end() const {
356 return const_iterator(*this, INVALID);