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 * use 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 {
47 /// The value type of the map.
50 typedef std::vector<Value> Container;
52 /** Default constructor for the map.
56 /** Graph and Registry initialized map constructor.
58 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
62 /** Constructor to use default value to initialize the map.
64 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
65 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
66 int id = getGraph()->id(it);
67 if (id >= container.size()) {
68 container.resize(id + 1);
74 /** Constructor to copy a map of an other map type.
76 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
78 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
79 int id = getGraph()->id(it);
80 if (id >= container.size()) {
81 container.resize(id + 1);
88 /** Assign operator to copy a map an other map type.
90 template <typename CMap> Map& operator=(const CMap& copy) {
94 this->MapBase::operator=(copy);
96 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
97 int id = getGraph()->id(it);
98 if (id >= container.size()) {
99 container.resize(id + 1);
106 /** The destructor of the map.
112 * The subscript operator. The map can be subscripted by the
113 * actual keys of the graph.
115 typename Container::reference operator[](const KeyType& key) {
116 int id = getGraph()->id(key);
117 return container[id];
121 * The const subscript operator. The map can be subscripted by the
122 * actual keys of the graph.
124 typename Container::const_reference operator[](const KeyType& key) const {
125 int id = getGraph()->id(key);
126 return container[id];
129 /** Setter function of the map. Equivalent with map[key] = val.
130 * This is a compatibility feature with the not dereferable maps.
132 void set(const KeyType& key, const Value& val) {
133 int id = getGraph()->id(key);
137 /** Add a new key to the map. It called by the map registry.
139 void add(const KeyType& key) {
140 int id = getGraph()->id(key);
141 if (id >= container.size()) {
142 container.resize(id + 1);
146 /** Erase a key from the map. It called by the map registry.
148 void erase(const KeyType& key) {}
150 /** Clear the data structure.
156 /** Compatible iterator with the stl maps' iterators.
157 * It iterates on pairs of a key and a value.
161 friend class const_iterator;
164 /** Private constructor to initalize the the iterators returned
165 * by the begin() and end().
167 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
171 /** Default constructor.
175 typedef extended_pair<const KeyType&, const KeyType&,
176 Value&, Value&> Reference;
178 /** Dereference operator for map.
180 Reference operator*() {
181 return Reference(it, (*map)[it]);
185 friend class iterator;
188 Pointer(const KeyType& key, Value& val) : data(key, val) {}
190 Reference* operator->() {return &data;}
193 /** Arrow operator for map.
195 Pointer operator->() {
196 return Pointer(it, ((*map)[it]));
199 /** The pre increment operator of the map.
201 iterator& operator++() {
206 /** The post increment operator of the map.
208 iterator operator++(int) {
214 /** The equality operator of the map.
216 bool operator==(const_iterator p_it) {
217 return p_it.it == it;
220 /** The not-equality operator of the map.
222 bool operator!=(const_iterator p_it) {
223 return !(*this == p_it);
232 /** Returns the begin iterator of the map.
235 return iterator(*this, KeyIt(*getGraph()));
238 /** Returns the end iterator of the map.
241 return iterator(*this, INVALID);
244 class const_iterator {
246 friend class iterator;
249 /** Private constructor to initalize the the iterators returned
250 * by the begin() and end().
252 const_iterator (const Map& pmap, const KeyIt& pit)
253 : map(&pmap), it(pit) {}
257 /** Default constructor.
261 /** Constructor to convert iterator to const_iterator.
263 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
265 typedef extended_pair<const KeyType&, const KeyType&,
266 const Value&, const Value&> Reference;
268 /** Dereference operator for map.
270 Reference operator*() {
271 return Reference(it, (*map)[it]);
276 friend class const_iterator;
279 Pointer(const KeyType& key, const Value& val) : data(key, val) {}
281 Reference* operator->() {return &data;}
284 /** Arrow operator for map.
286 Pointer operator->() {
287 return Pointer(it, ((*map)[it]));
290 /** The pre increment operator of the map.
292 const_iterator& operator++() {
297 /** The post increment operator of the map.
299 const_iterator operator++(int) {
300 const_iterator tmp(it);
305 /** The equality operator of the map.
307 bool operator==(const_iterator p_it) {
308 return p_it.it == it;
311 /** The not-equality operator of the map.
313 bool operator!=(const_iterator p_it) {
314 return !(*this == p_it);
323 /** Returns the begin const_iterator of the map.
325 const_iterator begin() const {
326 return const_iterator(*this, KeyIt(*getGraph()));
329 /** Returns the end const_iterator of the map.
331 const_iterator end() const {
332 return const_iterator(*this, INVALID);