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