7 #include <hugo/map_iterator.h>
11 ///\brief Vector based graph maps.
15 /// \addtogroup graphmaps
18 /** The ArrayMap template class is graph map structure what
19 * automatically updates the map when a key is added to or erased from
20 * the map. This map factory uses the allocators to implement
21 * the container functionality. This map factory
22 * uses the std::vector to implement the container function.
24 * The template parameter is the MapRegistry that the maps
25 * will belong to and the ValueType.
27 * \todo It should use a faster initialization using the maxNodeId() or
28 * maxEdgeId() function of the graph instead of iterating through each
32 template <typename MapRegistry, typename Value>
33 class VectorMap : public MapRegistry::MapBase {
36 /// The graph type of the maps.
37 typedef typename MapRegistry::Graph Graph;
38 /// The key type of the maps.
39 typedef typename MapRegistry::KeyType KeyType;
40 /// The iterator to iterate on the keys.
41 typedef typename MapRegistry::KeyIt KeyIt;
44 typedef VectorMap Map;
45 /// The MapBase of the Map which implements the core regisitry function.
46 typedef typename MapRegistry::MapBase MapBase;
50 /// The container type of the map.
51 typedef std::vector<Value> Container;
56 /// The value type of the map.
57 typedef Value ValueType;
58 /// The reference type of the map;
59 typedef typename Container::reference ReferenceType;
60 /// The pointer type of the map;
61 typedef typename Container::pointer PointerType;
63 /// The const value type of the map.
64 typedef const Value ConstValueType;
65 /// The const reference type of the map;
66 typedef typename Container::const_reference ConstReferenceType;
67 /// The pointer type of the map;
68 typedef typename Container::const_pointer ConstPointerType;
70 /** Default constructor for the map.
74 /** Graph and Registry initialized map constructor.
76 VectorMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
80 /** Constructor to use default value to initialize the map.
82 VectorMap(const Graph& g, MapRegistry& r, const Value& v)
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> VectorMap(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> VectorMap& 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.
128 virtual ~VectorMap() {
132 * The subscript operator. The map can be subscripted by the
133 * actual keys of the graph.
135 ReferenceType 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 ConstReferenceType 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 ValueType& 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 /// The stl compatible pair iterator of the map.
177 typedef MapIterator<VectorMap> Iterator;
178 /// The stl compatible const pair iterator of the map.
179 typedef MapConstIterator<VectorMap> ConstIterator;
181 /** Returns the begin iterator of the map.
184 return Iterator(*this, KeyIt(*MapBase::getGraph()));
187 /** Returns the end iterator of the map.
190 return Iterator(*this, INVALID);
193 /** Returns the begin ConstIterator of the map.
195 ConstIterator begin() const {
196 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
199 /** Returns the end const_iterator of the map.
201 ConstIterator end() const {
202 return ConstIterator(*this, INVALID);
205 /// The KeySet of the Map.
206 typedef MapConstKeySet<VectorMap> ConstKeySet;
208 /// KeySet getter function.
209 ConstKeySet keySet() const {
210 return ConstKeySet(*this);
213 /// The ConstValueSet of the Map.
214 typedef MapConstValueSet<VectorMap> ConstValueSet;
216 /// ConstValueSet getter function.
217 ConstValueSet valueSet() const {
218 return ConstValueSet(*this);
221 /// The ValueSet of the Map.
222 typedef MapValueSet<VectorMap> ValueSet;
224 /// ValueSet getter function.
225 ValueSet valueSet() {
226 return ValueSet(*this);