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
26 template <typename MapRegistry>
27 class VectorMapFactory {
30 /// The graph type of the maps.
31 typedef typename MapRegistry::Graph Graph;
32 /// The key type of the maps.
33 typedef typename MapRegistry::KeyType KeyType;
34 /// The iterator to iterate on the keys.
35 typedef typename MapRegistry::KeyIt KeyIt;
37 /// The MapBase of the Map which imlements the core regisitry function.
38 typedef typename MapRegistry::MapBase MapBase;
41 /** The template Map type.
44 class Map : public MapBase {
46 typedef std::vector<V> Container;
50 /// The value type of the map.
53 /// The value type of the map.
55 /// The reference type of the map;
56 typedef typename Container::reference Reference;
57 /// The pointer type of the map;
58 typedef typename Container::pointer Pointer;
60 /// The const value type of the map.
61 typedef const Value ConstValue;
62 /// The const reference type of the map;
63 typedef typename Container::const_reference ConstReference;
64 /// The pointer type of the map;
65 typedef typename Container::const_pointer ConstPointer;
67 /** Default constructor for the map.
71 /** Graph and Registry initialized map constructor.
73 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
77 /** Constructor to use default value to initialize the map.
79 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
80 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
81 int id = getGraph()->id(it);
82 if (id >= (int)container.size()) {
83 container.resize(id + 1);
89 /** Constructor to copy a map of an other map type.
91 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
93 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
94 int id = getGraph()->id(it);
95 if (id >= (int)container.size()) {
96 container.resize(id + 1);
103 /** Assign operator to copy a map an other map type.
105 template <typename CMap> Map& operator=(const CMap& copy) {
109 this->MapBase::operator=(copy);
111 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
112 int id = getGraph()->id(it);
113 if (id >= (int)container.size()) {
114 container.resize(id + 1);
122 /** The destructor of the map.
128 * The subscript operator. The map can be subscripted by the
129 * actual keys of the graph.
132 Reference operator[](const Key& key) {
134 typename Container::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.
145 ConstReference operator[](const Key& key) const {
147 typename Container::const_reference operator[](const KeyType& key) const {
149 int id = getGraph()->id(key);
150 return container[id];
153 /** Setter function of the map. Equivalent with map[key] = val.
154 * This is a compatibility feature with the not dereferable maps.
156 void set(const KeyType& key, const Value& val) {
157 int id = getGraph()->id(key);
161 /** Add a new key to the map. It called by the map registry.
163 void add(const KeyType& key) {
164 int id = getGraph()->id(key);
165 if (id >= (int)container.size()) {
166 container.resize(id + 1);
170 /** Erase a key from the map. It called by the map registry.
172 void erase(const KeyType& key) {}
174 /** Clear the data structure.
180 /** Compatible iterator with the stl maps' iterators.
181 * It iterates on pairs of a key and a value.
185 friend class const_iterator;
188 /** Private constructor to initalize the the iterators returned
189 * by the begin() and end().
191 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
195 /** Default constructor.
199 typedef extended_pair<const KeyType&, const KeyType&,
200 Map::Reference, Map::Reference> Reference;
202 /** Dereference operator for map.
204 Reference operator*() {
205 return Reference(it, (*map)[it]);
209 friend class iterator;
212 Pointer(const KeyType& key, Value& val) : data(key, val) {}
214 Reference* operator->() {return &data;}
217 /** Arrow operator for map.
219 Pointer operator->() {
220 return Pointer(it, ((*map)[it]));
223 /** The pre increment operator of the map.
225 iterator& operator++() {
230 /** The post increment operator of the map.
232 iterator operator++(int) {
238 /** The equality operator of the map.
240 bool operator==(const_iterator p_it) {
241 return p_it.it == it;
244 /** The not-equality operator of the map.
246 bool operator!=(const_iterator p_it) {
247 return !(*this == p_it);
256 /** Returns the begin iterator of the map.
259 return iterator(*this, KeyIt(*getGraph()));
262 /** Returns the end iterator of the map.
265 return iterator(*this, INVALID);
268 class const_iterator {
270 friend class iterator;
273 /** Private constructor to initalize the the iterators returned
274 * by the begin() and end().
276 const_iterator (const Map& pmap, const KeyIt& pit)
277 : map(&pmap), it(pit) {}
281 /** Default constructor.
285 /** Constructor to convert iterator to const_iterator.
287 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
289 typedef extended_pair<const KeyType&, const KeyType&,
290 Map::ConstReference, Map::ConstReference> Reference;
292 /** Dereference operator for map.
294 Reference operator*() {
295 return Reference(it, (*map)[it]);
300 friend class const_iterator;
303 Pointer(const KeyType& key, const Value& val) : data(key, 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);