7 #include "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) {
59 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
64 /** Constructor to copy a map of an other map type.
66 template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
69 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
75 /** Assign operator to copy a map an other map type.
77 template <typename CMap> Map& operator=(const CMap& copy) {
81 this->MapBase::operator=(copy);
84 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
90 /** The destructor of the map.
97 * The subscript operator. The map can be subscripted by the
98 * actual keys of the graph.
100 typename Container::reference operator[](const Key& key) {
101 int id = getGraph()->id(key);
102 return container[id];
106 * The const subscript operator. The map can be subscripted by the
107 * actual keys of the graph.
109 typename Container::const_reference operator[](const Key& key) const {
110 int id = getGraph()->id(key);
111 return container[id];
114 /** Setter function of the map. Equivalent with map[key] = val.
115 * This is a compatibility feature with the not dereferable maps.
117 void set(const Key& key, const Value& val) {
118 int id = getGraph()->id(key);
122 /** Add a new key to the map. It called by the map registry.
124 void add(const Key& key) {
125 int id = getGraph()->id(key);
126 if (id >= container.size()) {
127 container.resize(id + 1);
131 /** Erease a key from the map. It called by the map registry.
133 void erase(const Key& key) {}
135 /** Compatible iterator with the stl maps' iterators.
136 * It iterates on pairs of a key and a value.
140 friend class const_iterator;
143 /** Private constructor to initalize the the iterators returned
144 * by the begin() and end().
146 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
150 /** Default constructor.
154 /** Dereference operator for map.
156 std::pair<const Key, Value> operator*() {
157 return std::pair<const Key, Value>(it, (*map)[it]);
162 friend class iterator;
164 typedef extended_pair<const Key&, const Key&, Value&, Value&> Pair;
166 Pointer(const Key& key, Value& val) : data(key, val) {}
168 Pair* operator->() {return &data;}
171 /** Arrow operator for map.
173 Pointer operator->() {
174 return Pointer(it, ((*map)[it]));
177 /** The pre increment operator of the map.
179 iterator& operator++() {
180 map->getGraph()->next(it);
184 /** The post increment operator of the map.
186 iterator operator++(int) {
188 map.getGraph()->next(it);
192 /** The equality operator of the map.
194 bool operator==(const_iterator p_it) {
195 return p_it.it == it;
198 /** The not-equality operator of the map.
200 bool operator!=(const_iterator p_it) {
201 return !(*this == p_it);
210 /** Returns the begin iterator of the map.
213 return iterator(*this, KeyIt(*getGraph()));
216 /** Returns the end iterator of the map.
219 return iterator(*this, INVALID);
222 class const_iterator {
224 friend class iterator;
227 /** Private constructor to initalize the the iterators returned
228 * by the begin() and end().
230 const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
234 /** Default constructor.
238 /** Constructor to convert iterator to const_iterator.
240 const_iterator(iterator p_it) {
244 /** Dereference operator for map.
246 std::pair<const Key, const Value> operator*() const {
247 return std::pair<const Key, const Value>(it, (*map)[it]);
251 friend class const_iterator;
253 typedef extended_pair<const Key&, const Key&, const Value&, const Value&> Pair;
255 Pointer(const Key& key, const Value& val) : data(key, val) {}
257 Pair* operator->() {return &data;}
259 /** Arrow operator for map.
261 Pointer operator->() const {
262 return Pointer(it, (*map)[it]);
265 /** The pre increment operator of the map.
267 const_iterator& operator++() {
268 map->getGraph()->next(it);
272 /** The post increment operator of the map.
274 const_iterator operator++(int) {
275 const_iterator tmp(it);
276 map->getGraph()->next(it);
280 /** The equality operator of the map.
282 bool operator==(const_iterator p_it) {
283 return p_it.it == it;
286 /** The not-equality operator of the map.
288 bool operator!=(const_iterator p_it) {
289 return !(*this == p_it);
298 /** Returns the begin const_iterator of the map.
300 const_iterator begin() const {
301 return const_iterator(*this, KeyIt(*getGraph()));
304 /** Returns the end const_iterator of the map.
306 const_iterator end() const {
307 return const_iterator(*this, INVALID);