deba@822
|
1 |
// -*- c++ -*-
|
deba@822
|
2 |
#ifndef VECTOR_MAP_H
|
deba@822
|
3 |
#define VECTOR_MAP_H
|
deba@822
|
4 |
|
deba@822
|
5 |
#include <vector>
|
deba@822
|
6 |
|
deba@822
|
7 |
#include <hugo/map_iterator.h>
|
deba@844
|
8 |
#include <hugo/map_bits.h>
|
deba@822
|
9 |
|
deba@822
|
10 |
///\ingroup graphmaps
|
deba@822
|
11 |
///\file
|
deba@822
|
12 |
///\brief Vector based graph maps.
|
deba@822
|
13 |
|
deba@822
|
14 |
namespace hugo {
|
deba@822
|
15 |
|
deba@822
|
16 |
/// \addtogroup graphmaps
|
deba@822
|
17 |
/// @{
|
deba@822
|
18 |
|
deba@822
|
19 |
/** The ArrayMap template class is graph map structure what
|
deba@822
|
20 |
* automatically updates the map when a key is added to or erased from
|
deba@822
|
21 |
* the map. This map factory uses the allocators to implement
|
deba@822
|
22 |
* the container functionality. This map factory
|
deba@822
|
23 |
* uses the std::vector to implement the container function.
|
deba@822
|
24 |
*
|
deba@822
|
25 |
* The template parameter is the MapRegistry that the maps
|
deba@822
|
26 |
* will belong to and the ValueType.
|
deba@822
|
27 |
*
|
deba@822
|
28 |
* \todo It should use a faster initialization using the maxNodeId() or
|
deba@822
|
29 |
* maxEdgeId() function of the graph instead of iterating through each
|
deba@822
|
30 |
* edge/node.
|
deba@822
|
31 |
*/
|
deba@822
|
32 |
|
deba@822
|
33 |
template <typename MapRegistry, typename Value>
|
deba@822
|
34 |
class VectorMap : public MapRegistry::MapBase {
|
deba@822
|
35 |
public:
|
deba@822
|
36 |
|
deba@822
|
37 |
/// The graph type of the maps.
|
deba@822
|
38 |
typedef typename MapRegistry::Graph Graph;
|
deba@822
|
39 |
/// The key type of the maps.
|
deba@822
|
40 |
typedef typename MapRegistry::KeyType KeyType;
|
deba@822
|
41 |
/// The iterator to iterate on the keys.
|
deba@822
|
42 |
typedef typename MapRegistry::KeyIt KeyIt;
|
deba@822
|
43 |
|
deba@822
|
44 |
/// The map type.
|
deba@822
|
45 |
typedef VectorMap Map;
|
deba@822
|
46 |
/// The MapBase of the Map which implements the core regisitry function.
|
deba@822
|
47 |
typedef typename MapRegistry::MapBase MapBase;
|
deba@822
|
48 |
|
deba@822
|
49 |
private:
|
deba@822
|
50 |
|
deba@822
|
51 |
/// The container type of the map.
|
deba@822
|
52 |
typedef std::vector<Value> Container;
|
deba@822
|
53 |
|
deba@822
|
54 |
public:
|
deba@822
|
55 |
|
deba@822
|
56 |
|
deba@822
|
57 |
/// The value type of the map.
|
deba@822
|
58 |
typedef Value ValueType;
|
deba@822
|
59 |
/// The reference type of the map;
|
deba@822
|
60 |
typedef typename Container::reference ReferenceType;
|
deba@822
|
61 |
/// The pointer type of the map;
|
deba@822
|
62 |
typedef typename Container::pointer PointerType;
|
deba@822
|
63 |
|
deba@822
|
64 |
/// The const value type of the map.
|
deba@822
|
65 |
typedef const Value ConstValueType;
|
deba@822
|
66 |
/// The const reference type of the map;
|
deba@822
|
67 |
typedef typename Container::const_reference ConstReferenceType;
|
deba@822
|
68 |
/// The pointer type of the map;
|
deba@822
|
69 |
typedef typename Container::const_pointer ConstPointerType;
|
deba@822
|
70 |
|
deba@822
|
71 |
/** Default constructor for the map.
|
deba@822
|
72 |
*/
|
deba@822
|
73 |
VectorMap() {}
|
deba@822
|
74 |
|
deba@822
|
75 |
/** Graph and Registry initialized map constructor.
|
deba@822
|
76 |
*/
|
deba@844
|
77 |
VectorMap(const Graph& g, MapRegistry& r)
|
deba@844
|
78 |
: MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
|
deba@822
|
79 |
|
deba@822
|
80 |
/** Constructor to use default value to initialize the map.
|
deba@822
|
81 |
*/
|
deba@822
|
82 |
VectorMap(const Graph& g, MapRegistry& r, const Value& v)
|
deba@844
|
83 |
: MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
|
deba@822
|
84 |
|
deba@822
|
85 |
/** Constructor to copy a map of an other map type.
|
deba@822
|
86 |
*/
|
deba@822
|
87 |
template <typename CMap> VectorMap(const CMap& copy) : MapBase(copy) {
|
deba@844
|
88 |
if (MapBase::getGraph()) {
|
deba@844
|
89 |
container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
|
deba@844
|
90 |
for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
|
deba@822
|
91 |
set(it, copy[it]);
|
deba@822
|
92 |
}
|
deba@822
|
93 |
}
|
deba@822
|
94 |
}
|
deba@822
|
95 |
|
deba@822
|
96 |
/** Assign operator to copy a map an other map type.
|
deba@822
|
97 |
*/
|
deba@822
|
98 |
template <typename CMap> VectorMap& operator=(const CMap& copy) {
|
deba@844
|
99 |
container.clear();
|
deba@822
|
100 |
this->MapBase::operator=(copy);
|
deba@844
|
101 |
if (MapBase::getGraph()) {
|
deba@844
|
102 |
container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
|
deba@844
|
103 |
for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
|
deba@822
|
104 |
set(it, copy[it]);
|
deba@822
|
105 |
}
|
deba@822
|
106 |
}
|
deba@822
|
107 |
return *this;
|
deba@822
|
108 |
}
|
deba@822
|
109 |
|
deba@822
|
110 |
/** The destructor of the map.
|
deba@822
|
111 |
*/
|
deba@822
|
112 |
virtual ~VectorMap() {
|
deba@822
|
113 |
}
|
deba@822
|
114 |
|
deba@822
|
115 |
/**
|
deba@822
|
116 |
* The subscript operator. The map can be subscripted by the
|
deba@822
|
117 |
* actual keys of the graph.
|
deba@822
|
118 |
*/
|
deba@822
|
119 |
ReferenceType operator[](const KeyType& key) {
|
deba@844
|
120 |
int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
|
deba@822
|
121 |
return container[id];
|
deba@822
|
122 |
}
|
deba@822
|
123 |
|
deba@822
|
124 |
/**
|
deba@822
|
125 |
* The const subscript operator. The map can be subscripted by the
|
deba@822
|
126 |
* actual keys of the graph.
|
deba@822
|
127 |
*/
|
deba@822
|
128 |
ConstReferenceType operator[](const KeyType& key) const {
|
deba@844
|
129 |
int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
|
deba@822
|
130 |
return container[id];
|
deba@822
|
131 |
}
|
deba@822
|
132 |
|
deba@822
|
133 |
/** Setter function of the map. Equivalent with map[key] = val.
|
deba@822
|
134 |
* This is a compatibility feature with the not dereferable maps.
|
deba@822
|
135 |
*/
|
deba@822
|
136 |
void set(const KeyType& key, const ValueType& val) {
|
deba@844
|
137 |
int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
|
deba@822
|
138 |
container[id] = val;
|
deba@822
|
139 |
}
|
deba@822
|
140 |
|
deba@822
|
141 |
/** Add a new key to the map. It called by the map registry.
|
deba@822
|
142 |
*/
|
deba@822
|
143 |
void add(const KeyType& key) {
|
deba@844
|
144 |
int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
|
deba@822
|
145 |
if (id >= (int)container.size()) {
|
deba@822
|
146 |
container.resize(id + 1);
|
deba@822
|
147 |
}
|
deba@822
|
148 |
}
|
deba@822
|
149 |
|
deba@822
|
150 |
/** Erase a key from the map. It called by the map registry.
|
deba@822
|
151 |
*/
|
deba@822
|
152 |
void erase(const KeyType& key) {}
|
deba@822
|
153 |
|
deba@822
|
154 |
/** Clear the data structure.
|
deba@822
|
155 |
*/
|
deba@822
|
156 |
void clear() {
|
deba@822
|
157 |
container.clear();
|
deba@822
|
158 |
}
|
deba@822
|
159 |
|
deba@822
|
160 |
/// The stl compatible pair iterator of the map.
|
deba@822
|
161 |
typedef MapIterator<VectorMap> Iterator;
|
deba@822
|
162 |
/// The stl compatible const pair iterator of the map.
|
deba@822
|
163 |
typedef MapConstIterator<VectorMap> ConstIterator;
|
deba@822
|
164 |
|
deba@822
|
165 |
/** Returns the begin iterator of the map.
|
deba@822
|
166 |
*/
|
deba@822
|
167 |
Iterator begin() {
|
deba@822
|
168 |
return Iterator(*this, KeyIt(*MapBase::getGraph()));
|
deba@822
|
169 |
}
|
deba@822
|
170 |
|
deba@822
|
171 |
/** Returns the end iterator of the map.
|
deba@822
|
172 |
*/
|
deba@822
|
173 |
Iterator end() {
|
deba@822
|
174 |
return Iterator(*this, INVALID);
|
deba@822
|
175 |
}
|
deba@822
|
176 |
|
deba@822
|
177 |
/** Returns the begin ConstIterator of the map.
|
deba@822
|
178 |
*/
|
deba@822
|
179 |
ConstIterator begin() const {
|
deba@822
|
180 |
return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
|
deba@822
|
181 |
}
|
deba@822
|
182 |
|
deba@822
|
183 |
/** Returns the end const_iterator of the map.
|
deba@822
|
184 |
*/
|
deba@822
|
185 |
ConstIterator end() const {
|
deba@822
|
186 |
return ConstIterator(*this, INVALID);
|
deba@822
|
187 |
}
|
deba@822
|
188 |
|
deba@830
|
189 |
/// The KeySet of the Map.
|
deba@830
|
190 |
typedef MapConstKeySet<VectorMap> ConstKeySet;
|
deba@830
|
191 |
|
deba@830
|
192 |
/// KeySet getter function.
|
deba@830
|
193 |
ConstKeySet keySet() const {
|
deba@830
|
194 |
return ConstKeySet(*this);
|
deba@830
|
195 |
}
|
deba@830
|
196 |
|
deba@830
|
197 |
/// The ConstValueSet of the Map.
|
deba@830
|
198 |
typedef MapConstValueSet<VectorMap> ConstValueSet;
|
deba@830
|
199 |
|
deba@830
|
200 |
/// ConstValueSet getter function.
|
deba@830
|
201 |
ConstValueSet valueSet() const {
|
deba@830
|
202 |
return ConstValueSet(*this);
|
deba@830
|
203 |
}
|
deba@830
|
204 |
|
deba@830
|
205 |
/// The ValueSet of the Map.
|
deba@830
|
206 |
typedef MapValueSet<VectorMap> ValueSet;
|
deba@830
|
207 |
|
deba@830
|
208 |
/// ValueSet getter function.
|
deba@830
|
209 |
ValueSet valueSet() {
|
deba@830
|
210 |
return ValueSet(*this);
|
deba@830
|
211 |
}
|
deba@830
|
212 |
|
deba@830
|
213 |
|
deba@822
|
214 |
private:
|
deba@822
|
215 |
|
deba@822
|
216 |
Container container;
|
deba@822
|
217 |
|
deba@844
|
218 |
public:
|
deba@844
|
219 |
// STL compatibility typedefs.
|
deba@844
|
220 |
typedef Iterator iterator;
|
deba@844
|
221 |
typedef ConstIterator const_iterator;
|
deba@844
|
222 |
typedef typename Iterator::PairValueType value_type;
|
deba@844
|
223 |
typedef typename Iterator::KeyType key_type;
|
deba@844
|
224 |
typedef typename Iterator::ValueType data_type;
|
deba@844
|
225 |
typedef typename Iterator::PairReferenceType reference;
|
deba@844
|
226 |
typedef typename Iterator::PairPointerType pointer;
|
deba@844
|
227 |
typedef typename ConstIterator::PairReferenceType const_reference;
|
deba@844
|
228 |
typedef typename ConstIterator::PairPointerType const_pointer;
|
deba@844
|
229 |
typedef int difference_type;
|
deba@822
|
230 |
};
|
deba@822
|
231 |
|
deba@822
|
232 |
/// @}
|
deba@822
|
233 |
|
deba@822
|
234 |
}
|
deba@822
|
235 |
|
deba@822
|
236 |
#endif
|