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 /** Graph and Registry initialized map constructor.
74 VectorMap(const Graph& g, MapRegistry& r)
75 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
77 /** Constructor to use default value to initialize the map.
79 VectorMap(const Graph& g, MapRegistry& r, const Value& v)
80 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
82 /** Assign operator to copy a map of an other map type.
84 template <typename TT>
85 VectorMap(const VectorMap<MapRegistry, TT>& c)
86 : MapBase(c), container(c.container.size()) {
87 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
88 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
89 container[id] = c.container[id];
93 /** Assign operator to copy a map of an other map type.
95 template <typename TT>
96 VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
97 if (MapBase::getGraph() != c.getGraph()) {
98 MapBase::operator=(c);
99 container.resize(c.container.size());
101 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
102 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
103 container[id] = c.container[id];
108 * The subscript operator. The map can be subscripted by the
109 * actual keys of the graph.
111 ReferenceType operator[](const KeyType& key) {
112 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
113 return container[id];
117 * The const subscript operator. The map can be subscripted by the
118 * actual keys of the graph.
120 ConstReferenceType operator[](const KeyType& key) const {
121 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
122 return container[id];
125 /** Setter function of the map. Equivalent with map[key] = val.
126 * This is a compatibility feature with the not dereferable maps.
128 void set(const KeyType& key, const ValueType& val) {
129 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
133 /** Add a new key to the map. It called by the map registry.
135 void add(const KeyType& key) {
136 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
137 if (id >= (int)container.size()) {
138 container.resize(id + 1);
142 /** Erase a key from the map. It called by the map registry.
144 void erase(const KeyType& key) {}
146 /** Clear the data structure.
152 /// The stl compatible pair iterator of the map.
153 typedef MapIterator<VectorMap> Iterator;
154 /// The stl compatible const pair iterator of the map.
155 typedef MapConstIterator<VectorMap> ConstIterator;
157 /** Returns the begin iterator of the map.
160 return Iterator(*this, KeyIt(*MapBase::getGraph()));
163 /** Returns the end iterator of the map.
166 return Iterator(*this, INVALID);
169 /** Returns the begin ConstIterator of the map.
171 ConstIterator begin() const {
172 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
175 /** Returns the end const_iterator of the map.
177 ConstIterator end() const {
178 return ConstIterator(*this, INVALID);
181 /// The KeySet of the Map.
182 typedef MapConstKeySet<VectorMap> ConstKeySet;
184 /// KeySet getter function.
185 ConstKeySet keySet() const {
186 return ConstKeySet(*this);
189 /// The ConstValueSet of the Map.
190 typedef MapConstValueSet<VectorMap> ConstValueSet;
192 /// ConstValueSet getter function.
193 ConstValueSet valueSet() const {
194 return ConstValueSet(*this);
197 /// The ValueSet of the Map.
198 typedef MapValueSet<VectorMap> ValueSet;
200 /// ValueSet getter function.
201 ValueSet valueSet() {
202 return ValueSet(*this);
211 // STL compatibility typedefs.
212 typedef Iterator iterator;
213 typedef ConstIterator const_iterator;
214 typedef typename Iterator::PairValueType value_type;
215 typedef typename Iterator::KeyType key_type;
216 typedef typename Iterator::ValueType data_type;
217 typedef typename Iterator::PairReferenceType reference;
218 typedef typename Iterator::PairPointerType pointer;
219 typedef typename ConstIterator::PairReferenceType const_reference;
220 typedef typename ConstIterator::PairPointerType const_pointer;
221 typedef int difference_type;