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.
176 /** Compatible iterator with the stl maps' iterators.
177 * It iterates on pairs of a key and a value.
181 friend class const_iterator;
184 /** Private constructor to initalize the the iterators returned
185 * by the begin() and end().
187 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
191 /** Default constructor.
195 typedef extended_pair<const KeyType&, const KeyType&,
196 Map::Reference, Map::Reference> Reference;
198 /** Dereference operator for map.
200 Reference operator*() {
201 return Reference(it, (*map)[it]);
205 friend class iterator;
208 Pointer(const KeyType& key, Map::Reference val) : data(key, val) {}
210 Reference* operator->() {return &data;}
213 /** Arrow operator for map.
215 Pointer operator->() {
216 return Pointer(it, ((*map)[it]));
219 /** The pre increment operator of the map.
221 iterator& operator++() {
226 /** The post increment operator of the map.
228 iterator operator++(int) {
234 /** The equality operator of the map.
236 bool operator==(const_iterator p_it) {
237 return p_it.it == it;
240 /** The not-equality operator of the map.
242 bool operator!=(const_iterator p_it) {
243 return !(*this == p_it);
252 /** Returns the begin iterator of the map.
255 return iterator(*this, KeyIt(*getGraph()));
258 /** Returns the end iterator of the map.
261 return iterator(*this, INVALID);
264 class const_iterator {
266 friend class iterator;
269 /** Private constructor to initalize the the iterators returned
270 * by the begin() and end().
272 const_iterator (const Map& pmap, const KeyIt& pit)
273 : map(&pmap), it(pit) {}
277 /** Default constructor.
281 /** Constructor to convert iterator to const_iterator.
283 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
285 typedef extended_pair<const KeyType&, const KeyType&,
286 Map::ConstReference, Map::ConstReference> Reference;
288 /** Dereference operator for map.
290 Reference operator*() {
291 return Reference(it, (*map)[it]);
296 friend class const_iterator;
299 Pointer(const KeyType& key, Map::ConstReference val)
302 Reference* operator->() {return &data;}
305 /** Arrow operator for map.
307 Pointer operator->() {
308 return Pointer(it, ((*map)[it]));
311 /** The pre increment operator of the map.
313 const_iterator& operator++() {
318 /** The post increment operator of the map.
320 const_iterator operator++(int) {
321 const_iterator tmp(it);
326 /** The equality operator of the map.
328 bool operator==(const_iterator p_it) {
329 return p_it.it == it;
332 /** The not-equality operator of the map.
334 bool operator!=(const_iterator p_it) {
335 return !(*this == p_it);
344 /** Returns the begin const_iterator of the map.
346 const_iterator begin() const {
347 return const_iterator(*this, KeyIt(*getGraph()));
350 /** Returns the end const_iterator of the map.
352 const_iterator end() const {
353 return const_iterator(*this, INVALID);