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