alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_VECTOR_MAP_H
|
alpar@921
|
18 |
#define LEMON_VECTOR_MAP_H
|
deba@822
|
19 |
|
deba@822
|
20 |
#include <vector>
|
klao@946
|
21 |
#include <algorithm>
|
deba@822
|
22 |
|
klao@946
|
23 |
#include <lemon/alteration_observer_registry.h>
|
deba@822
|
24 |
|
deba@822
|
25 |
///\ingroup graphmaps
|
deba@822
|
26 |
///\file
|
deba@822
|
27 |
///\brief Vector based graph maps.
|
deba@822
|
28 |
|
alpar@921
|
29 |
namespace lemon {
|
deba@822
|
30 |
|
deba@822
|
31 |
/// \addtogroup graphmaps
|
deba@822
|
32 |
/// @{
|
deba@822
|
33 |
|
klao@946
|
34 |
/// The VectorMap template class is graph map structure what
|
klao@946
|
35 |
/// automatically updates the map when a key is added to or erased from
|
klao@946
|
36 |
/// the map. This map factory uses the allocators to implement
|
klao@946
|
37 |
/// the container functionality. This map factory
|
klao@946
|
38 |
/// uses the std::vector to implement the container function.
|
klao@946
|
39 |
///
|
klao@946
|
40 |
/// \param Registry The AlterationObserverRegistry that will notify this map.
|
klao@946
|
41 |
/// \param IdMap The IdMap type of the graph items.
|
klao@946
|
42 |
/// \param Value The value type of the map.
|
klao@946
|
43 |
///
|
klao@946
|
44 |
/// \author Balazs Dezso
|
klao@946
|
45 |
|
klao@946
|
46 |
|
klao@946
|
47 |
template <typename _Graph,
|
klao@946
|
48 |
typename _Item,
|
klao@946
|
49 |
typename _IdMap,
|
klao@946
|
50 |
typename _Value>
|
klao@946
|
51 |
class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase {
|
deba@822
|
52 |
public:
|
deba@822
|
53 |
|
klao@946
|
54 |
/// The graph type of the map.
|
klao@946
|
55 |
typedef _Graph Graph;
|
klao@946
|
56 |
/// The key type of the map.
|
klao@946
|
57 |
typedef _Item KeyType;
|
klao@946
|
58 |
/// The id map type of the map.
|
klao@946
|
59 |
typedef _IdMap IdMap;
|
klao@946
|
60 |
/// The registry type of the map.
|
klao@946
|
61 |
typedef AlterationObserverRegistry<_Item> Registry;
|
klao@946
|
62 |
/// The value type of the map.
|
klao@946
|
63 |
typedef _Value Value;
|
deba@822
|
64 |
|
deba@822
|
65 |
/// The map type.
|
deba@822
|
66 |
typedef VectorMap Map;
|
klao@946
|
67 |
/// The base class of the map.
|
klao@946
|
68 |
typedef typename Registry::ObserverBase Parent;
|
deba@822
|
69 |
|
deba@822
|
70 |
private:
|
deba@822
|
71 |
|
deba@822
|
72 |
/// The container type of the map.
|
deba@822
|
73 |
typedef std::vector<Value> Container;
|
deba@822
|
74 |
|
deba@822
|
75 |
public:
|
deba@822
|
76 |
|
deba@822
|
77 |
/// The value type of the map.
|
deba@822
|
78 |
typedef Value ValueType;
|
deba@822
|
79 |
/// The reference type of the map;
|
deba@822
|
80 |
typedef typename Container::reference ReferenceType;
|
deba@822
|
81 |
/// The pointer type of the map;
|
deba@822
|
82 |
typedef typename Container::pointer PointerType;
|
deba@822
|
83 |
|
deba@822
|
84 |
/// The const value type of the map.
|
deba@822
|
85 |
typedef const Value ConstValueType;
|
deba@822
|
86 |
/// The const reference type of the map;
|
deba@822
|
87 |
typedef typename Container::const_reference ConstReferenceType;
|
deba@822
|
88 |
/// The pointer type of the map;
|
deba@822
|
89 |
typedef typename Container::const_pointer ConstPointerType;
|
deba@822
|
90 |
|
klao@946
|
91 |
/// Constructor to attach the new map into the registry.
|
deba@937
|
92 |
|
klao@946
|
93 |
/// It construates a map and attachs it into the registry.
|
klao@946
|
94 |
/// It adds all the items of the graph to the map.
|
klao@946
|
95 |
|
klao@946
|
96 |
VectorMap(const Graph& _g, Registry& _r) : graph(&_g) {
|
klao@946
|
97 |
attach(_r);
|
klao@946
|
98 |
build();
|
klao@946
|
99 |
}
|
deba@822
|
100 |
|
deba@937
|
101 |
/// Constructor uses given value to initialize the map.
|
deba@937
|
102 |
|
klao@946
|
103 |
/// It construates a map uses a given value to initialize the map.
|
klao@946
|
104 |
/// It adds all the items of the graph to the map.
|
klao@946
|
105 |
|
klao@946
|
106 |
VectorMap(const Graph& g, Registry& r, const Value& v) : graph(&g) {
|
klao@946
|
107 |
attach(r);
|
klao@946
|
108 |
container.resize(IdMap(*graph).maxId() + 1, v);
|
klao@946
|
109 |
}
|
deba@822
|
110 |
|
klao@946
|
111 |
VectorMap(const VectorMap& copy) : graph(copy.getGraph()) {
|
klao@946
|
112 |
if (copy.attached()) {
|
klao@946
|
113 |
attach(*copy.getRegistry());
|
klao@946
|
114 |
container = copy.container;
|
deba@822
|
115 |
}
|
deba@822
|
116 |
}
|
deba@822
|
117 |
|
deba@937
|
118 |
|
klao@946
|
119 |
/** Assign operator to copy a map of the same map type.
|
deba@822
|
120 |
*/
|
klao@946
|
121 |
VectorMap& operator=(const VectorMap& copy) {
|
klao@946
|
122 |
if (© == this) return *this;
|
klao@946
|
123 |
|
klao@946
|
124 |
if (graph != copy.graph) {
|
klao@946
|
125 |
if (attached()) {
|
klao@946
|
126 |
detach();
|
klao@946
|
127 |
}
|
klao@946
|
128 |
if (copy.attached()) {
|
klao@946
|
129 |
attach(*copy.getRegistry());
|
klao@946
|
130 |
}
|
deba@897
|
131 |
}
|
klao@946
|
132 |
container = copy.container;
|
klao@946
|
133 |
|
klao@946
|
134 |
return *this;
|
klao@946
|
135 |
}
|
klao@946
|
136 |
|
klao@946
|
137 |
|
klao@946
|
138 |
virtual ~VectorMap() {
|
klao@946
|
139 |
if (attached()) {
|
klao@946
|
140 |
detach();
|
deba@822
|
141 |
}
|
klao@946
|
142 |
}
|
klao@946
|
143 |
|
klao@946
|
144 |
const Graph* getGraph() const {
|
klao@946
|
145 |
return graph;
|
deba@822
|
146 |
}
|
deba@937
|
147 |
|
deba@937
|
148 |
/// The subcript operator.
|
deba@937
|
149 |
|
klao@946
|
150 |
/// The subscript operator. The map can be subscripted by the
|
klao@946
|
151 |
/// actual items of the graph.
|
klao@946
|
152 |
|
deba@822
|
153 |
ReferenceType operator[](const KeyType& key) {
|
klao@946
|
154 |
return container[IdMap(*graph)[key]];
|
deba@822
|
155 |
}
|
deba@822
|
156 |
|
deba@937
|
157 |
/// The const subcript operator.
|
deba@937
|
158 |
|
klao@946
|
159 |
/// The const subscript operator. The map can be subscripted by the
|
klao@946
|
160 |
/// actual items of the graph.
|
klao@946
|
161 |
|
deba@822
|
162 |
ConstReferenceType operator[](const KeyType& key) const {
|
klao@946
|
163 |
return container[IdMap(*graph)[key]];
|
deba@822
|
164 |
}
|
deba@822
|
165 |
|
deba@937
|
166 |
|
klao@946
|
167 |
/// The setter function of the map.
|
klao@946
|
168 |
|
klao@946
|
169 |
/// It the same as operator[](key) = value expression.
|
klao@946
|
170 |
///
|
klao@946
|
171 |
|
klao@946
|
172 |
void set(const KeyType& key, const ValueType& value) {
|
klao@946
|
173 |
(*this)[key] = value;
|
deba@822
|
174 |
}
|
klao@946
|
175 |
|
deba@937
|
176 |
/// Adds a new key to the map.
|
deba@822
|
177 |
|
klao@946
|
178 |
/// It adds a new key to the map. It called by the observer registry
|
klao@946
|
179 |
/// and it overrides the add() member function of the observer base.
|
klao@946
|
180 |
|
deba@822
|
181 |
void add(const KeyType& key) {
|
klao@946
|
182 |
int id = IdMap(*graph)[key];
|
deba@822
|
183 |
if (id >= (int)container.size()) {
|
deba@822
|
184 |
container.resize(id + 1);
|
deba@822
|
185 |
}
|
deba@822
|
186 |
}
|
deba@937
|
187 |
|
deba@937
|
188 |
/// Erases a key from the map.
|
deba@822
|
189 |
|
klao@946
|
190 |
/// Erase a key from the map. It called by the observer registry
|
klao@946
|
191 |
/// and it overrides the erase() member function of the observer base.
|
klao@971
|
192 |
void erase(const KeyType&) {}
|
deba@822
|
193 |
|
klao@946
|
194 |
/// Buildes the map.
|
klao@946
|
195 |
|
klao@946
|
196 |
/// It buildes the map. It called by the observer registry
|
klao@946
|
197 |
/// and it overrides the build() member function of the observer base.
|
deba@937
|
198 |
|
klao@946
|
199 |
void build() {
|
klao@946
|
200 |
container.resize(IdMap(*graph).maxId() + 1);
|
klao@946
|
201 |
}
|
deba@937
|
202 |
|
klao@946
|
203 |
/// Clear the map.
|
klao@946
|
204 |
|
klao@946
|
205 |
/// It erase all items from the map. It called by the observer registry
|
klao@946
|
206 |
/// and it overrides the clear() member function of the observer base.
|
deba@822
|
207 |
void clear() {
|
deba@822
|
208 |
container.clear();
|
deba@822
|
209 |
}
|
deba@822
|
210 |
|
deba@822
|
211 |
private:
|
deba@822
|
212 |
|
deba@822
|
213 |
Container container;
|
klao@946
|
214 |
const Graph *graph;
|
deba@822
|
215 |
|
klao@946
|
216 |
};
|
klao@946
|
217 |
|
klao@946
|
218 |
|
klao@946
|
219 |
template <typename _Base>
|
klao@946
|
220 |
class VectorMappableGraphExtender : public _Base {
|
deba@844
|
221 |
public:
|
klao@946
|
222 |
|
klao@946
|
223 |
typedef VectorMappableGraphExtender<_Base> Graph;
|
klao@946
|
224 |
typedef _Base Parent;
|
klao@946
|
225 |
|
klao@946
|
226 |
typedef typename Parent::Node Node;
|
klao@946
|
227 |
typedef typename Parent::NodeIt NodeIt;
|
klao@946
|
228 |
typedef typename Parent::NodeIdMap NodeIdMap;
|
klao@946
|
229 |
typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
|
klao@946
|
230 |
|
klao@946
|
231 |
typedef typename Parent::Edge Edge;
|
klao@946
|
232 |
typedef typename Parent::EdgeIt EdgeIt;
|
klao@946
|
233 |
typedef typename Parent::EdgeIdMap EdgeIdMap;
|
klao@946
|
234 |
typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
|
klao@946
|
235 |
|
klao@946
|
236 |
|
klao@946
|
237 |
|
klao@946
|
238 |
template <typename _Value>
|
klao@946
|
239 |
class NodeMap : public VectorMap<Graph, Node, NodeIdMap, _Value> {
|
klao@946
|
240 |
public:
|
klao@946
|
241 |
typedef VectorMappableGraphExtender<_Base> Graph;
|
klao@946
|
242 |
|
klao@946
|
243 |
typedef typename Graph::Node Node;
|
klao@946
|
244 |
typedef typename Graph::NodeIdMap NodeIdMap;
|
klao@946
|
245 |
|
klao@946
|
246 |
typedef VectorMap<Graph, Node, NodeIdMap, _Value> Parent;
|
klao@946
|
247 |
|
klao@946
|
248 |
typedef typename Parent::Graph Graph;
|
klao@946
|
249 |
typedef typename Parent::Value Value;
|
klao@946
|
250 |
|
klao@946
|
251 |
NodeMap(const Graph& g)
|
klao@946
|
252 |
: Parent(g, g.getNodeObserverRegistry()) {}
|
klao@946
|
253 |
NodeMap(const Graph& g, const Value& v)
|
klao@946
|
254 |
: Parent(g, g.getNodeObserverRegistry(), v) {}
|
klao@946
|
255 |
|
klao@946
|
256 |
};
|
klao@946
|
257 |
|
klao@946
|
258 |
template <typename _Value>
|
klao@946
|
259 |
class EdgeMap : public VectorMap<Graph, Edge, EdgeIdMap, _Value> {
|
klao@946
|
260 |
public:
|
klao@946
|
261 |
typedef VectorMappableGraphExtender<_Base> Graph;
|
klao@946
|
262 |
|
klao@946
|
263 |
typedef typename Graph::Edge Edge;
|
klao@946
|
264 |
typedef typename Graph::EdgeIdMap EdgeIdMap;
|
klao@946
|
265 |
|
klao@946
|
266 |
typedef VectorMap<Graph, Edge, EdgeIdMap, _Value> Parent;
|
klao@946
|
267 |
|
klao@946
|
268 |
typedef typename Parent::Graph Graph;
|
klao@946
|
269 |
typedef typename Parent::Value Value;
|
klao@946
|
270 |
|
klao@946
|
271 |
EdgeMap(const Graph& g)
|
klao@946
|
272 |
: Parent(g, g.getEdgeObserverRegistry()) {}
|
klao@946
|
273 |
EdgeMap(const Graph& g, const Value& v)
|
klao@946
|
274 |
: Parent(g, g.getEdgeObserverRegistry(), v) {}
|
klao@946
|
275 |
|
klao@946
|
276 |
};
|
klao@946
|
277 |
|
deba@822
|
278 |
};
|
deba@822
|
279 |
|
deba@822
|
280 |
/// @}
|
deba@822
|
281 |
|
deba@822
|
282 |
}
|
deba@822
|
283 |
|
deba@822
|
284 |
#endif
|