21 #include <algorithm> |
21 #include <algorithm> |
22 |
22 |
23 #include <lemon/utility.h> |
23 #include <lemon/utility.h> |
24 #include <lemon/bits/map_iterator.h> |
24 #include <lemon/bits/map_iterator.h> |
25 #include <lemon/bits/alteration_notifier.h> |
25 #include <lemon/bits/alteration_notifier.h> |
26 |
26 #include <lemon/concept_check.h> |
27 ///\ingroup graphmapfactory |
27 #include <lemon/concept/maps.h> |
|
28 |
|
29 /// \ingroup graphmapfactory |
|
30 /// |
28 ///\file |
31 ///\file |
29 ///\brief Vector based graph maps. |
32 ///\brief Vector based graph maps. |
30 |
33 |
31 namespace lemon { |
34 namespace lemon { |
32 |
35 |
33 /// \addtogroup graphmapfactory |
36 /// \ingroup graphmapfactory |
34 /// @{ |
37 /// |
35 |
38 /// \brief Graph map based on the std::vector storage. |
|
39 /// |
36 /// The VectorMap template class is graph map structure what |
40 /// The VectorMap template class is graph map structure what |
37 /// automatically updates the map when a key is added to or erased from |
41 /// automatically updates the map when a key is added to or erased from |
38 /// the map. This map factory uses the allocators to implement |
42 /// the map. This map factory uses the allocators to implement |
39 /// the container functionality. This map factory |
43 /// the container functionality. This map factory |
40 /// uses the std::vector to implement the container function. |
44 /// uses the std::vector to implement the container function. |
115 attach(*_copy.getRegistry()); |
119 attach(*_copy.getRegistry()); |
116 container = _copy.container; |
120 container = _copy.container; |
117 } |
121 } |
118 } |
122 } |
119 |
123 |
120 using Parent::attach; |
|
121 using Parent::detach; |
|
122 using Parent::attached; |
|
123 |
|
124 /** Assign operator to copy a map of the same map type. |
|
125 */ |
|
126 VectorMap& operator=(const VectorMap& copy) { |
|
127 if (© == this) return *this; |
|
128 |
|
129 if (graph != copy.graph) { |
|
130 if (attached()) { |
|
131 detach(); |
|
132 } |
|
133 if (copy.attached()) { |
|
134 attach(*copy.getRegistry()); |
|
135 } |
|
136 } |
|
137 container = copy.container; |
|
138 |
|
139 return *this; |
|
140 } |
|
141 |
|
142 |
|
143 virtual ~VectorMap() { |
124 virtual ~VectorMap() { |
144 if (attached()) { |
125 if (attached()) { |
145 detach(); |
126 detach(); |
146 } |
127 } |
147 } |
128 } |
148 |
129 |
|
130 |
|
131 private: |
|
132 |
|
133 VectorMap& operator=(const VectorMap&); |
|
134 |
|
135 protected: |
|
136 |
|
137 using Parent::attach; |
|
138 using Parent::detach; |
|
139 using Parent::attached; |
|
140 |
149 const Graph* getGraph() const { |
141 const Graph* getGraph() const { |
150 return graph; |
142 return graph; |
151 } |
143 } |
|
144 |
|
145 public: |
152 |
146 |
153 /// The subcript operator. |
147 /// The subcript operator. |
154 |
148 |
155 /// The subscript operator. The map can be subscripted by the |
149 /// The subscript operator. The map can be subscripted by the |
156 /// actual items of the graph. |
150 /// actual items of the graph. |
171 |
165 |
172 /// The setter function of the map. |
166 /// The setter function of the map. |
173 |
167 |
174 /// It the same as operator[](key) = value expression. |
168 /// It the same as operator[](key) = value expression. |
175 /// |
169 /// |
176 |
|
177 void set(const Key& key, const Value& value) { |
170 void set(const Key& key, const Value& value) { |
178 (*this)[key] = value; |
171 (*this)[key] = value; |
179 } |
172 } |
180 |
173 |
181 /// Adds a new key to the map. |
174 protected: |
182 |
175 |
|
176 /// \brief Adds a new key to the map. |
|
177 /// |
183 /// It adds a new key to the map. It called by the observer registry |
178 /// It adds a new key to the map. It called by the observer registry |
184 /// and it overrides the add() member function of the observer base. |
179 /// and it overrides the add() member function of the observer base. |
185 |
180 |
186 void add(const Key& key) { |
181 void add(const Key& key) { |
187 int id = graph->id(key); |
182 int id = graph->id(key); |
218 Container container; |
213 Container container; |
219 const Graph *graph; |
214 const Graph *graph; |
220 |
215 |
221 }; |
216 }; |
222 |
217 |
223 |
|
224 template <typename _Base> |
|
225 class VectorMappableGraphExtender : public _Base { |
|
226 public: |
|
227 |
|
228 typedef VectorMappableGraphExtender<_Base> Graph; |
|
229 typedef _Base Parent; |
|
230 |
|
231 typedef typename Parent::Node Node; |
|
232 typedef typename Parent::NodeIt NodeIt; |
|
233 typedef typename Parent::NodeIdMap NodeIdMap; |
|
234 typedef typename Parent::NodeNotifier NodeObserverRegistry; |
|
235 |
|
236 typedef typename Parent::Edge Edge; |
|
237 typedef typename Parent::EdgeIt EdgeIt; |
|
238 typedef typename Parent::EdgeIdMap EdgeIdMap; |
|
239 typedef typename Parent::EdgeNotifier EdgeObserverRegistry; |
|
240 |
|
241 |
|
242 template <typename _Value> |
|
243 class NodeMap : |
|
244 public IterableMapExtender<VectorMap<Graph, Node, _Value> > { |
|
245 public: |
|
246 typedef VectorMappableGraphExtender<_Base> Graph; |
|
247 |
|
248 typedef typename Graph::Node Node; |
|
249 |
|
250 typedef IterableMapExtender<VectorMap<Graph, Node, _Value> > Parent; |
|
251 |
|
252 //typedef typename Parent::Graph Graph; |
|
253 typedef typename Parent::Value Value; |
|
254 |
|
255 NodeMap(const Graph& g) |
|
256 : Parent(g) {} |
|
257 NodeMap(const Graph& g, const Value& v) |
|
258 : Parent(g, v) {} |
|
259 |
|
260 }; |
|
261 |
|
262 template <typename _Value> |
|
263 class EdgeMap |
|
264 : public IterableMapExtender<VectorMap<Graph, Edge, _Value> > { |
|
265 public: |
|
266 typedef VectorMappableGraphExtender<_Base> Graph; |
|
267 |
|
268 typedef typename Graph::Edge Edge; |
|
269 |
|
270 typedef IterableMapExtender<VectorMap<Graph, Edge, _Value> > Parent; |
|
271 |
|
272 //typedef typename Parent::Graph Graph; |
|
273 typedef typename Parent::Value Value; |
|
274 |
|
275 EdgeMap(const Graph& g) |
|
276 : Parent(g) {} |
|
277 EdgeMap(const Graph& g, const Value& v) |
|
278 : Parent(g, v) {} |
|
279 |
|
280 }; |
|
281 |
|
282 }; |
|
283 |
|
284 /// @} |
|
285 |
|
286 } |
218 } |
287 |
219 |
288 #endif |
220 #endif |