7 #include <hugo/map_iterator.h>
8 #include <hugo/map_bits.h>
12 ///\brief Vector based graph maps.
16 /// \addtogroup graphmaps
19 /** The ArrayMap template class is graph map structure what
20 * automatically updates the map when a key is added to or erased from
21 * the map. This map factory uses the allocators to implement
22 * the container functionality. This map factory
23 * uses the std::vector to implement the container function.
25 * The template parameter is the MapRegistry that the maps
26 * will belong to and the ValueType.
28 * \todo It should use a faster initialization using the maxNodeId() or
29 * maxEdgeId() function of the graph instead of iterating through each
33 template <typename MapRegistry, typename Value>
34 class VectorMap : public MapRegistry::MapBase {
35 template <typename MR, typename T> friend class VectorMap;
38 /// The graph type of the maps.
39 typedef typename MapRegistry::Graph Graph;
40 /// The key type of the maps.
41 typedef typename MapRegistry::KeyType KeyType;
42 /// The iterator to iterate on the keys.
43 typedef typename MapRegistry::KeyIt KeyIt;
46 typedef VectorMap Map;
47 /// The MapBase of the Map which implements the core regisitry function.
48 typedef typename MapRegistry::MapBase MapBase;
52 /// The container type of the map.
53 typedef std::vector<Value> Container;
58 /// The value type of the map.
59 typedef Value ValueType;
60 /// The reference type of the map;
61 typedef typename Container::reference ReferenceType;
62 /// The pointer type of the map;
63 typedef typename Container::pointer PointerType;
65 /// The const value type of the map.
66 typedef const Value ConstValueType;
67 /// The const reference type of the map;
68 typedef typename Container::const_reference ConstReferenceType;
69 /// The pointer type of the map;
70 typedef typename Container::const_pointer ConstPointerType;
72 /** Default constructor for the map.
76 /** Graph and Registry initialized map constructor.
78 VectorMap(const Graph& g, MapRegistry& r)
79 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
81 /** Constructor to use default value to initialize the map.
83 VectorMap(const Graph& g, MapRegistry& r, const Value& v)
84 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
86 /** Assign operator to copy a map of an other map type.
88 template <typename TT>
89 VectorMap(const VectorMap<MapRegistry, TT>& c)
90 : MapBase(c), container(c.container.size()) {
91 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
92 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
93 container[id] = c.container[id];
97 /** Assign operator to copy a map of an other map type.
99 template <typename TT>
100 VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
101 container.resize(c.container.size());
102 MapBase::operator=(c);
103 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
104 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
105 container[id] = c.container[id];
110 * The subscript operator. The map can be subscripted by the
111 * actual keys of the graph.
113 ReferenceType operator[](const KeyType& key) {
114 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
115 return container[id];
119 * The const subscript operator. The map can be subscripted by the
120 * actual keys of the graph.
122 ConstReferenceType operator[](const KeyType& key) const {
123 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
124 return container[id];
127 /** Setter function of the map. Equivalent with map[key] = val.
128 * This is a compatibility feature with the not dereferable maps.
130 void set(const KeyType& key, const ValueType& val) {
131 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
135 /** Add a new key to the map. It called by the map registry.
137 void add(const KeyType& key) {
138 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
139 if (id >= (int)container.size()) {
140 container.resize(id + 1);
144 /** Erase a key from the map. It called by the map registry.
146 void erase(const KeyType& key) {}
148 /** Clear the data structure.
154 /// The stl compatible pair iterator of the map.
155 typedef MapIterator<VectorMap> Iterator;
156 /// The stl compatible const pair iterator of the map.
157 typedef MapConstIterator<VectorMap> ConstIterator;
159 /** Returns the begin iterator of the map.
162 return Iterator(*this, KeyIt(*MapBase::getGraph()));
165 /** Returns the end iterator of the map.
168 return Iterator(*this, INVALID);
171 /** Returns the begin ConstIterator of the map.
173 ConstIterator begin() const {
174 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
177 /** Returns the end const_iterator of the map.
179 ConstIterator end() const {
180 return ConstIterator(*this, INVALID);
183 /// The KeySet of the Map.
184 typedef MapConstKeySet<VectorMap> ConstKeySet;
186 /// KeySet getter function.
187 ConstKeySet keySet() const {
188 return ConstKeySet(*this);
191 /// The ConstValueSet of the Map.
192 typedef MapConstValueSet<VectorMap> ConstValueSet;
194 /// ConstValueSet getter function.
195 ConstValueSet valueSet() const {
196 return ConstValueSet(*this);
199 /// The ValueSet of the Map.
200 typedef MapValueSet<VectorMap> ValueSet;
202 /// ValueSet getter function.
203 ValueSet valueSet() {
204 return ValueSet(*this);
213 // STL compatibility typedefs.
214 typedef Iterator iterator;
215 typedef ConstIterator const_iterator;
216 typedef typename Iterator::PairValueType value_type;
217 typedef typename Iterator::KeyType key_type;
218 typedef typename Iterator::ValueType data_type;
219 typedef typename Iterator::PairReferenceType reference;
220 typedef typename Iterator::PairPointerType pointer;
221 typedef typename ConstIterator::PairReferenceType const_reference;
222 typedef typename ConstIterator::PairPointerType const_pointer;
223 typedef int difference_type;