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 {
37 /// The graph type of the maps.
38 typedef typename MapRegistry::Graph Graph;
39 /// The key type of the maps.
40 typedef typename MapRegistry::KeyType KeyType;
41 /// The iterator to iterate on the keys.
42 typedef typename MapRegistry::KeyIt KeyIt;
45 typedef VectorMap Map;
46 /// The MapBase of the Map which implements the core regisitry function.
47 typedef typename MapRegistry::MapBase MapBase;
51 /// The container type of the map.
52 typedef std::vector<Value> Container;
57 /// The value type of the map.
58 typedef Value ValueType;
59 /// The reference type of the map;
60 typedef typename Container::reference ReferenceType;
61 /// The pointer type of the map;
62 typedef typename Container::pointer PointerType;
64 /// The const value type of the map.
65 typedef const Value ConstValueType;
66 /// The const reference type of the map;
67 typedef typename Container::const_reference ConstReferenceType;
68 /// The pointer type of the map;
69 typedef typename Container::const_pointer ConstPointerType;
71 /** Default constructor for the map.
75 /** Graph and Registry initialized map constructor.
77 VectorMap(const Graph& g, MapRegistry& r)
78 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
80 /** Constructor to use default value to initialize the map.
82 VectorMap(const Graph& g, MapRegistry& r, const Value& v)
83 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
85 /** Constructor to copy a map of an other map type.
87 template <typename CMap> VectorMap(const CMap& copy) : MapBase(copy) {
88 if (MapBase::getGraph()) {
89 container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
90 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
96 /** Assign operator to copy a map an other map type.
98 template <typename CMap> VectorMap& operator=(const CMap& copy) {
100 this->MapBase::operator=(copy);
101 if (MapBase::getGraph()) {
102 container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
103 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
110 /** The destructor of the map.
112 virtual ~VectorMap() {
116 * The subscript operator. The map can be subscripted by the
117 * actual keys of the graph.
119 ReferenceType operator[](const KeyType& key) {
120 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
121 return container[id];
125 * The const subscript operator. The map can be subscripted by the
126 * actual keys of the graph.
128 ConstReferenceType operator[](const KeyType& key) const {
129 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
130 return container[id];
133 /** Setter function of the map. Equivalent with map[key] = val.
134 * This is a compatibility feature with the not dereferable maps.
136 void set(const KeyType& key, const ValueType& val) {
137 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
141 /** Add a new key to the map. It called by the map registry.
143 void add(const KeyType& key) {
144 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
145 if (id >= (int)container.size()) {
146 container.resize(id + 1);
150 /** Erase a key from the map. It called by the map registry.
152 void erase(const KeyType& key) {}
154 /** Clear the data structure.
160 /// The stl compatible pair iterator of the map.
161 typedef MapIterator<VectorMap> Iterator;
162 /// The stl compatible const pair iterator of the map.
163 typedef MapConstIterator<VectorMap> ConstIterator;
165 /** Returns the begin iterator of the map.
168 return Iterator(*this, KeyIt(*MapBase::getGraph()));
171 /** Returns the end iterator of the map.
174 return Iterator(*this, INVALID);
177 /** Returns the begin ConstIterator of the map.
179 ConstIterator begin() const {
180 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
183 /** Returns the end const_iterator of the map.
185 ConstIterator end() const {
186 return ConstIterator(*this, INVALID);
189 /// The KeySet of the Map.
190 typedef MapConstKeySet<VectorMap> ConstKeySet;
192 /// KeySet getter function.
193 ConstKeySet keySet() const {
194 return ConstKeySet(*this);
197 /// The ConstValueSet of the Map.
198 typedef MapConstValueSet<VectorMap> ConstValueSet;
200 /// ConstValueSet getter function.
201 ConstValueSet valueSet() const {
202 return ConstValueSet(*this);
205 /// The ValueSet of the Map.
206 typedef MapValueSet<VectorMap> ValueSet;
208 /// ValueSet getter function.
209 ValueSet valueSet() {
210 return ValueSet(*this);
219 // STL compatibility typedefs.
220 typedef Iterator iterator;
221 typedef ConstIterator const_iterator;
222 typedef typename Iterator::PairValueType value_type;
223 typedef typename Iterator::KeyType key_type;
224 typedef typename Iterator::ValueType data_type;
225 typedef typename Iterator::PairReferenceType reference;
226 typedef typename Iterator::PairPointerType pointer;
227 typedef typename ConstIterator::PairReferenceType const_reference;
228 typedef typename ConstIterator::PairPointerType const_pointer;
229 typedef int difference_type;