deba@1720
|
1 |
/* -*- C++ -*-
|
deba@1720
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@1956
|
5 |
* Copyright (C) 2003-2006
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1720
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@1720
|
8 |
*
|
deba@1720
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@1720
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@1720
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@1720
|
12 |
*
|
deba@1720
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1720
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@1720
|
15 |
* purpose.
|
deba@1720
|
16 |
*
|
deba@1720
|
17 |
*/
|
deba@1720
|
18 |
|
deba@1720
|
19 |
#ifndef LEMON_MATRIX_MAPS_H
|
deba@1720
|
20 |
#define LEMON_MATRIX_MAPS_H
|
deba@1720
|
21 |
|
deba@1720
|
22 |
|
deba@1720
|
23 |
#include <vector>
|
deba@1993
|
24 |
#include <lemon/bits/utility.h>
|
alpar@2088
|
25 |
#include <lemon/bits/invalid.h>
|
deba@1720
|
26 |
#include <lemon/maps.h>
|
deba@1720
|
27 |
|
deba@2039
|
28 |
#include <lemon/concept/matrix_maps.h>
|
deba@1720
|
29 |
|
deba@1720
|
30 |
/// \file
|
alpar@2072
|
31 |
/// \ingroup matrices
|
deba@1720
|
32 |
/// \brief Maps indexed with pairs of items.
|
deba@1720
|
33 |
///
|
deba@1720
|
34 |
/// \todo This file has the same name as the concept file in concept/,
|
deba@1720
|
35 |
/// and this is not easily detectable in docs...
|
deba@1720
|
36 |
namespace lemon {
|
deba@1720
|
37 |
|
deba@1720
|
38 |
/// \brief Map for the coloumn view of the matrix
|
deba@1720
|
39 |
///
|
alpar@2072
|
40 |
/// \ingroup matrices
|
deba@1720
|
41 |
/// Map for the coloumn view of the matrix.
|
alpar@2072
|
42 |
///
|
deba@1720
|
43 |
template <typename _MatrixMap>
|
deba@2039
|
44 |
class MatrixRowMap : public MatrixMapTraits<_MatrixMap> {
|
deba@1720
|
45 |
public:
|
deba@1720
|
46 |
typedef _MatrixMap MatrixMap;
|
deba@1720
|
47 |
typedef typename MatrixMap::SecondKey Key;
|
deba@1720
|
48 |
typedef typename MatrixMap::Value Value;
|
deba@1720
|
49 |
|
deba@1720
|
50 |
|
deba@2084
|
51 |
/// \brief Constructor of the row map
|
deba@2084
|
52 |
///
|
deba@2084
|
53 |
/// Constructor of the row map.
|
deba@1751
|
54 |
MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _row)
|
deba@1720
|
55 |
: matrix(_matrix), row(_row) {}
|
deba@1720
|
56 |
|
deba@1720
|
57 |
/// \brief Subscription operator
|
deba@1720
|
58 |
///
|
deba@1720
|
59 |
/// Subscription operator.
|
deba@2039
|
60 |
typename MatrixMapTraits<MatrixMap>::ReturnValue
|
deba@1720
|
61 |
operator[](Key col) {
|
deba@1751
|
62 |
return matrix(row, col);
|
deba@1720
|
63 |
}
|
deba@1720
|
64 |
|
deba@1720
|
65 |
/// \brief Setter function
|
deba@1720
|
66 |
///
|
deba@1720
|
67 |
/// Setter function.
|
deba@1720
|
68 |
void set(Key col, const Value& val) {
|
deba@1751
|
69 |
matrix.set(row, col, val);
|
deba@1720
|
70 |
}
|
deba@1720
|
71 |
|
deba@1720
|
72 |
/// \brief Subscription operator
|
deba@1720
|
73 |
///
|
deba@1720
|
74 |
/// Subscription operator.
|
deba@2039
|
75 |
typename MatrixMapTraits<MatrixMap>::ConstReturnValue
|
deba@1720
|
76 |
operator[](Key col) const {
|
deba@1751
|
77 |
return matrix(row, col);
|
deba@1720
|
78 |
}
|
deba@1720
|
79 |
|
deba@1720
|
80 |
private:
|
deba@1720
|
81 |
MatrixMap& matrix;
|
deba@1751
|
82 |
typename MatrixMap::FirstKey row;
|
deba@1720
|
83 |
};
|
deba@1720
|
84 |
|
deba@1720
|
85 |
/// \brief Map for the row view of the matrix
|
deba@1720
|
86 |
///
|
alpar@2072
|
87 |
/// \ingroup matrices
|
deba@1720
|
88 |
/// Map for the row view of the matrix.
|
alpar@2072
|
89 |
///
|
deba@1720
|
90 |
template <typename _MatrixMap>
|
deba@2039
|
91 |
class ConstMatrixRowMap : public MatrixMapTraits<_MatrixMap> {
|
deba@1720
|
92 |
public:
|
deba@1720
|
93 |
typedef _MatrixMap MatrixMap;
|
deba@1751
|
94 |
typedef typename MatrixMap::SecondKey Key;
|
deba@1720
|
95 |
typedef typename MatrixMap::Value Value;
|
deba@1720
|
96 |
|
deba@1751
|
97 |
|
deba@2084
|
98 |
/// \brief Constructor of the row map
|
deba@2084
|
99 |
///
|
deba@2084
|
100 |
/// Constructor of the row map.
|
deba@1720
|
101 |
ConstMatrixRowMap(const MatrixMap& _matrix,
|
deba@1751
|
102 |
typename MatrixMap::FirstKey _row)
|
deba@1720
|
103 |
: matrix(_matrix), row(_row) {}
|
deba@1720
|
104 |
|
deba@1720
|
105 |
/// \brief Subscription operator
|
deba@1720
|
106 |
///
|
deba@1720
|
107 |
/// Subscription operator.
|
deba@2039
|
108 |
typename MatrixMapTraits<MatrixMap>::ConstReturnValue
|
deba@1720
|
109 |
operator[](Key col) const {
|
deba@1751
|
110 |
return matrix(row, col);
|
deba@1720
|
111 |
}
|
deba@1720
|
112 |
|
deba@1720
|
113 |
private:
|
deba@1720
|
114 |
const MatrixMap& matrix;
|
deba@1751
|
115 |
typename MatrixMap::FirstKey row;
|
deba@1720
|
116 |
};
|
deba@1720
|
117 |
|
deba@2084
|
118 |
/// \ingroup matrices
|
deba@2084
|
119 |
///
|
deba@1720
|
120 |
/// \brief Gives back a row view of the matrix map
|
deba@1720
|
121 |
///
|
deba@1720
|
122 |
/// Gives back a row view of the matrix map.
|
alpar@2072
|
123 |
///
|
deba@2084
|
124 |
/// \sa MatrixRowMap
|
deba@2084
|
125 |
/// \sa ConstMatrixRowMap
|
deba@1720
|
126 |
template <typename MatrixMap>
|
deba@1720
|
127 |
MatrixRowMap<MatrixMap> matrixRowMap(MatrixMap& matrixMap,
|
deba@1751
|
128 |
typename MatrixMap::FirstKey row) {
|
deba@1720
|
129 |
return MatrixRowMap<MatrixMap>(matrixMap, row);
|
deba@1720
|
130 |
}
|
deba@1720
|
131 |
|
deba@1720
|
132 |
template <typename MatrixMap>
|
deba@1751
|
133 |
ConstMatrixRowMap<MatrixMap>
|
deba@1751
|
134 |
matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey row) {
|
deba@1720
|
135 |
return ConstMatrixRowMap<MatrixMap>(matrixMap, row);
|
deba@1720
|
136 |
}
|
deba@1720
|
137 |
|
alpar@2072
|
138 |
/// \brief Map for the column view of the matrix
|
deba@1751
|
139 |
///
|
alpar@2072
|
140 |
/// \ingroup matrices
|
alpar@2072
|
141 |
/// Map for the column view of the matrix.
|
alpar@2072
|
142 |
///
|
deba@1751
|
143 |
template <typename _MatrixMap>
|
deba@2039
|
144 |
class MatrixColMap : public MatrixMapTraits<_MatrixMap> {
|
deba@1751
|
145 |
public:
|
deba@1751
|
146 |
typedef _MatrixMap MatrixMap;
|
deba@1751
|
147 |
typedef typename MatrixMap::FirstKey Key;
|
deba@1751
|
148 |
typedef typename MatrixMap::Value Value;
|
deba@1751
|
149 |
|
deba@2084
|
150 |
/// \brief Constructor of the column map
|
deba@2084
|
151 |
///
|
deba@2084
|
152 |
/// Constructor of the column map.
|
deba@1751
|
153 |
MatrixColMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _col)
|
deba@1751
|
154 |
: matrix(_matrix), col(_col) {}
|
deba@1751
|
155 |
|
deba@1751
|
156 |
/// \brief Subscription operator
|
deba@1751
|
157 |
///
|
deba@1751
|
158 |
/// Subscription operator.
|
deba@2039
|
159 |
typename MatrixMapTraits<MatrixMap>::ReturnValue
|
deba@1751
|
160 |
operator[](Key row) {
|
deba@1751
|
161 |
return matrix(row, col);
|
deba@1751
|
162 |
}
|
deba@1751
|
163 |
|
deba@1751
|
164 |
/// \brief Setter function
|
deba@1751
|
165 |
///
|
deba@1751
|
166 |
/// Setter function.
|
deba@1751
|
167 |
void set(Key row, const Value& val) {
|
deba@1751
|
168 |
matrix.set(row, col, val);
|
deba@1751
|
169 |
}
|
deba@1751
|
170 |
|
deba@1751
|
171 |
/// \brief Subscription operator
|
deba@1751
|
172 |
///
|
deba@1751
|
173 |
/// Subscription operator.
|
deba@2039
|
174 |
typename MatrixMapTraits<MatrixMap>::ConstReturnValue
|
deba@1751
|
175 |
operator[](Key row) const {
|
deba@1751
|
176 |
return matrix(row, col);
|
deba@1751
|
177 |
}
|
deba@1751
|
178 |
|
deba@1751
|
179 |
private:
|
deba@1751
|
180 |
MatrixMap& matrix;
|
deba@1751
|
181 |
typename MatrixMap::SecondKey col;
|
deba@1751
|
182 |
};
|
deba@1751
|
183 |
|
alpar@2072
|
184 |
/// \brief Map for the column view of the matrix
|
deba@1751
|
185 |
///
|
alpar@2072
|
186 |
/// \ingroup matrices
|
alpar@2072
|
187 |
/// Map for the column view of the matrix.
|
alpar@2072
|
188 |
///
|
deba@1751
|
189 |
template <typename _MatrixMap>
|
deba@2039
|
190 |
class ConstMatrixColMap : public MatrixMapTraits<_MatrixMap> {
|
deba@1751
|
191 |
public:
|
deba@1751
|
192 |
typedef _MatrixMap MatrixMap;
|
deba@1751
|
193 |
typedef typename MatrixMap::FirstKey Key;
|
deba@1751
|
194 |
typedef typename MatrixMap::Value Value;
|
deba@1751
|
195 |
|
deba@2084
|
196 |
/// \brief Constructor of the column map
|
deba@2084
|
197 |
///
|
deba@2084
|
198 |
/// Constructor of the column map.
|
deba@1751
|
199 |
ConstMatrixColMap(const MatrixMap& _matrix,
|
deba@1751
|
200 |
typename MatrixMap::SecondKey _col)
|
deba@1751
|
201 |
: matrix(_matrix), col(_col) {}
|
deba@1751
|
202 |
|
deba@1751
|
203 |
/// \brief Subscription operator
|
deba@1751
|
204 |
///
|
deba@1751
|
205 |
/// Subscription operator.
|
deba@2039
|
206 |
typename MatrixMapTraits<MatrixMap>::ConstReturnValue
|
deba@1751
|
207 |
operator[](Key row) const {
|
deba@1751
|
208 |
return matrix(row, col);
|
deba@1751
|
209 |
}
|
deba@1751
|
210 |
|
deba@1751
|
211 |
private:
|
deba@1751
|
212 |
const MatrixMap& matrix;
|
deba@1751
|
213 |
typename MatrixMap::SecondKey col;
|
deba@1751
|
214 |
};
|
deba@1751
|
215 |
|
deba@2084
|
216 |
/// \ingroup matrices
|
deba@2084
|
217 |
///
|
alpar@2072
|
218 |
/// \brief Gives back a column view of the matrix map
|
deba@1751
|
219 |
///
|
alpar@2072
|
220 |
/// Gives back a column view of the matrix map.
|
alpar@2072
|
221 |
///
|
deba@2084
|
222 |
/// \sa MatrixColMap
|
deba@2084
|
223 |
/// \sa ConstMatrixColMap
|
deba@1751
|
224 |
template <typename MatrixMap>
|
deba@1751
|
225 |
MatrixColMap<MatrixMap> matrixColMap(MatrixMap& matrixMap,
|
deba@1751
|
226 |
typename MatrixMap::SecondKey col) {
|
deba@1751
|
227 |
return MatrixColMap<MatrixMap>(matrixMap, col);
|
deba@1751
|
228 |
}
|
deba@1751
|
229 |
|
deba@1751
|
230 |
template <typename MatrixMap>
|
deba@1751
|
231 |
ConstMatrixColMap<MatrixMap>
|
deba@1751
|
232 |
matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey col) {
|
deba@1751
|
233 |
return ConstMatrixColMap<MatrixMap>(matrixMap, col);
|
deba@1751
|
234 |
}
|
deba@1751
|
235 |
|
deba@1720
|
236 |
/// \brief Container for store values for each ordered pair of graph items
|
deba@1720
|
237 |
///
|
alpar@2072
|
238 |
/// \ingroup matrices
|
alpar@1757
|
239 |
/// This data structure can strore for each pair of the same item
|
deba@1720
|
240 |
/// type a value. It increase the size of the container when the
|
deba@1720
|
241 |
/// associated graph modified, so it updated automaticly whenever
|
deba@1720
|
242 |
/// it is needed.
|
deba@1720
|
243 |
template <typename _Graph, typename _Item, typename _Value>
|
deba@1720
|
244 |
class DynamicMatrixMap
|
deba@1999
|
245 |
: protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
deba@1720
|
246 |
public:
|
deba@1999
|
247 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase
|
deba@1999
|
248 |
Parent;
|
deba@1720
|
249 |
|
deba@1720
|
250 |
typedef _Graph Graph;
|
deba@1720
|
251 |
typedef _Item Key;
|
deba@1720
|
252 |
|
deba@1720
|
253 |
typedef _Item FirstKey;
|
deba@1720
|
254 |
typedef _Item SecondKey;
|
deba@1720
|
255 |
typedef _Value Value;
|
deba@1720
|
256 |
|
deba@1720
|
257 |
typedef True ReferenceMapTag;
|
deba@1720
|
258 |
|
deba@1720
|
259 |
private:
|
deba@1720
|
260 |
|
deba@1720
|
261 |
typedef std::vector<Value> Container;
|
deba@1720
|
262 |
|
deba@1720
|
263 |
public:
|
deba@1720
|
264 |
|
deba@1720
|
265 |
typedef typename Container::reference Reference;
|
deba@1720
|
266 |
typedef typename Container::const_reference ConstReference;
|
deba@1720
|
267 |
|
deba@1720
|
268 |
/// \brief Creates an item matrix for the given graph
|
deba@1720
|
269 |
///
|
deba@1720
|
270 |
/// Creates an item matrix for the given graph.
|
deba@1720
|
271 |
DynamicMatrixMap(const Graph& _graph)
|
deba@1999
|
272 |
: values(size(_graph.maxId(Key()) + 1)) {
|
deba@1999
|
273 |
Parent::attach(_graph.getNotifier(Key()));
|
deba@1720
|
274 |
}
|
deba@1720
|
275 |
|
deba@1720
|
276 |
/// \brief Creates an item matrix for the given graph
|
deba@1720
|
277 |
///
|
deba@1720
|
278 |
/// Creates an item matrix for the given graph and assigns for each
|
deba@1720
|
279 |
/// pairs of keys the given parameter.
|
deba@1720
|
280 |
DynamicMatrixMap(const Graph& _graph, const Value& _val)
|
deba@1999
|
281 |
: values(size(_graph.maxId(Key()) + 1), _val) {
|
deba@1999
|
282 |
Parent::attach(_graph.getNotifier(Key()));
|
deba@1720
|
283 |
}
|
deba@1720
|
284 |
|
deba@2039
|
285 |
///\brief The assignement operator.
|
deba@2039
|
286 |
///
|
deba@2039
|
287 |
///It allow to assign a map to an other.
|
deba@2039
|
288 |
DynamicMatrixMap& operator=(const DynamicMatrixMap& _cmap){
|
deba@2039
|
289 |
return operator=<DynamicMatrixMap>(_cmap);
|
deba@2039
|
290 |
}
|
deba@2039
|
291 |
|
deba@2039
|
292 |
///\brief Template assignement operator.
|
deba@2039
|
293 |
///
|
deba@2039
|
294 |
///It copy the element of the given map to its own container. The
|
deba@2039
|
295 |
///type of the two map shall be the same.
|
deba@2039
|
296 |
template <typename CMap>
|
deba@2039
|
297 |
DynamicMatrixMap& operator=(const CMap& _cmap){
|
deba@2039
|
298 |
checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
|
deba@2039
|
299 |
typename Parent::Notifier* notifier = Parent::getNotifier();
|
deba@2039
|
300 |
Key first, second;
|
deba@2039
|
301 |
for(notifier->first(first); first != INVALID;
|
deba@2039
|
302 |
notifier->next(first)){
|
deba@2039
|
303 |
for(notifier->first(second); second != INVALID;
|
deba@2039
|
304 |
notifier->next(second)){
|
deba@2039
|
305 |
set(first, second, _cmap(first, second));
|
deba@2039
|
306 |
}
|
deba@2039
|
307 |
}
|
deba@2039
|
308 |
return *this;
|
deba@2039
|
309 |
}
|
deba@2039
|
310 |
|
deba@1720
|
311 |
/// \brief Gives back the value assigned to the \c first - \c second
|
deba@1720
|
312 |
/// ordered pair.
|
deba@1720
|
313 |
///
|
deba@1720
|
314 |
/// Gives back the value assigned to the \c first - \c second ordered pair.
|
deba@1720
|
315 |
ConstReference operator()(const Key& first, const Key& second) const {
|
deba@1999
|
316 |
return values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
317 |
Parent::getNotifier()->id(second))];
|
deba@1720
|
318 |
}
|
deba@1720
|
319 |
|
deba@1720
|
320 |
/// \brief Gives back the value assigned to the \c first - \c second
|
deba@1720
|
321 |
/// ordered pair.
|
deba@1720
|
322 |
///
|
deba@1720
|
323 |
/// Gives back the value assigned to the \c first - \c second ordered pair.
|
deba@1720
|
324 |
Reference operator()(const Key& first, const Key& second) {
|
deba@1999
|
325 |
return values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
326 |
Parent::getNotifier()->id(second))];
|
deba@1720
|
327 |
}
|
deba@1720
|
328 |
|
deba@1720
|
329 |
/// \brief Setter function for the matrix map.
|
deba@1720
|
330 |
///
|
deba@1720
|
331 |
/// Setter function for the matrix map.
|
deba@1720
|
332 |
void set(const Key& first, const Key& second, const Value& val) {
|
deba@1999
|
333 |
values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
334 |
Parent::getNotifier()->id(second))] = val;
|
deba@1720
|
335 |
}
|
deba@1720
|
336 |
|
deba@1720
|
337 |
protected:
|
deba@1720
|
338 |
|
deba@1720
|
339 |
static int index(int i, int j) {
|
deba@1720
|
340 |
if (i < j) {
|
deba@1720
|
341 |
return j * j + i;
|
deba@1720
|
342 |
} else {
|
deba@1720
|
343 |
return i * i + i + j;
|
deba@1720
|
344 |
}
|
deba@1720
|
345 |
}
|
deba@1720
|
346 |
|
deba@1720
|
347 |
static int size(int s) {
|
deba@1720
|
348 |
return s * s;
|
deba@1720
|
349 |
}
|
deba@1720
|
350 |
|
deba@1720
|
351 |
virtual void add(const Key& key) {
|
deba@1999
|
352 |
if (size(Parent::getNotifier()->id(key) + 1) >= (int)values.size()) {
|
deba@1999
|
353 |
values.resize(size(Parent::getNotifier()->id(key) + 1));
|
deba@1720
|
354 |
}
|
deba@1720
|
355 |
}
|
deba@1720
|
356 |
|
deba@1720
|
357 |
virtual void erase(const Key&) {}
|
deba@1720
|
358 |
|
deba@1720
|
359 |
virtual void build() {
|
deba@1999
|
360 |
values.resize(size(Parent::getNotifier()->maxId() + 1));
|
deba@1720
|
361 |
}
|
deba@1720
|
362 |
|
deba@1720
|
363 |
virtual void clear() {
|
deba@1720
|
364 |
values.clear();
|
deba@1720
|
365 |
}
|
deba@1720
|
366 |
|
deba@1720
|
367 |
private:
|
deba@1720
|
368 |
std::vector<Value> values;
|
deba@1720
|
369 |
};
|
deba@1720
|
370 |
|
deba@1720
|
371 |
/// \brief Container for store values for each unordered pair of graph items
|
deba@1720
|
372 |
///
|
alpar@2072
|
373 |
/// \ingroup matrices
|
alpar@1757
|
374 |
/// This data structure can strore for each pair of the same item
|
deba@1720
|
375 |
/// type a value. It increase the size of the container when the
|
deba@1720
|
376 |
/// associated graph modified, so it updated automaticly whenever
|
deba@1720
|
377 |
/// it is needed.
|
deba@1720
|
378 |
template <typename _Graph, typename _Item, typename _Value>
|
deba@1720
|
379 |
class DynamicSymMatrixMap
|
deba@1999
|
380 |
: protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
deba@1720
|
381 |
public:
|
deba@1999
|
382 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase
|
deba@1999
|
383 |
Parent;
|
deba@1720
|
384 |
|
deba@1720
|
385 |
typedef _Graph Graph;
|
deba@1720
|
386 |
typedef _Item Key;
|
deba@1720
|
387 |
|
deba@1720
|
388 |
typedef _Item FirstKey;
|
deba@1720
|
389 |
typedef _Item SecondKey;
|
deba@1720
|
390 |
typedef _Value Value;
|
deba@1720
|
391 |
|
deba@1720
|
392 |
typedef True ReferenceMapTag;
|
deba@1720
|
393 |
|
deba@1720
|
394 |
private:
|
deba@1720
|
395 |
|
deba@1720
|
396 |
typedef std::vector<Value> Container;
|
deba@1720
|
397 |
|
deba@1720
|
398 |
public:
|
deba@1720
|
399 |
|
deba@1720
|
400 |
typedef typename Container::reference Reference;
|
deba@1720
|
401 |
typedef typename Container::const_reference ConstReference;
|
deba@1720
|
402 |
|
deba@1720
|
403 |
/// \brief Creates an item matrix for the given graph
|
deba@1720
|
404 |
///
|
deba@1720
|
405 |
/// Creates an item matrix for the given graph.
|
deba@1720
|
406 |
DynamicSymMatrixMap(const Graph& _graph)
|
deba@1999
|
407 |
: values(size(_graph.maxId(Key()) + 1)) {
|
deba@1999
|
408 |
Parent::attach(_graph.getNotifier(Key()));
|
deba@1720
|
409 |
}
|
deba@1720
|
410 |
|
deba@1720
|
411 |
/// \brief Creates an item matrix for the given graph
|
deba@1720
|
412 |
///
|
deba@1720
|
413 |
/// Creates an item matrix for the given graph and assigns for each
|
deba@1720
|
414 |
/// pairs of keys the given parameter.
|
deba@1720
|
415 |
DynamicSymMatrixMap(const Graph& _graph, const Value& _val)
|
deba@1999
|
416 |
: values(size(_graph.maxId(Key()) + 1), _val) {
|
deba@1999
|
417 |
Parent::attach(_graph.getNotifier(Key()));
|
deba@1720
|
418 |
}
|
deba@1720
|
419 |
|
deba@2039
|
420 |
|
deba@2039
|
421 |
///\brief The assignement operator.
|
deba@2039
|
422 |
///
|
deba@2039
|
423 |
///It allow to assign a map to an other.
|
alpar@2072
|
424 |
///
|
deba@2039
|
425 |
DynamicSymMatrixMap& operator=(const DynamicSymMatrixMap& _cmap){
|
deba@2039
|
426 |
return operator=<DynamicSymMatrixMap>(_cmap);
|
deba@2039
|
427 |
}
|
deba@2039
|
428 |
|
deba@2039
|
429 |
///\brief Template assignement operator.
|
deba@2039
|
430 |
///
|
deba@2039
|
431 |
///It copy the element of the given map to its own container. The
|
deba@2039
|
432 |
///type of the two map shall be the same.
|
deba@2039
|
433 |
template <typename CMap>
|
deba@2039
|
434 |
DynamicSymMatrixMap& operator=(const CMap& _cmap){
|
deba@2039
|
435 |
checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
|
deba@2039
|
436 |
typename Parent::Notifier* notifier = Parent::getNotifier();
|
deba@2039
|
437 |
Key first, second;
|
deba@2039
|
438 |
for(notifier->first(first); first != INVALID;
|
deba@2039
|
439 |
notifier->next(first)){
|
deba@2039
|
440 |
for(notifier->first(second); second != first;
|
deba@2039
|
441 |
notifier->next(second)){
|
deba@2039
|
442 |
set(first, second, _cmap(first, second));
|
deba@2039
|
443 |
}
|
deba@2039
|
444 |
set(first, first, _cmap(first, first));
|
deba@2039
|
445 |
}
|
deba@2039
|
446 |
return *this;
|
deba@2039
|
447 |
}
|
deba@2039
|
448 |
|
deba@1720
|
449 |
/// \brief Gives back the value assigned to the \c first - \c second
|
deba@1720
|
450 |
/// unordered pair.
|
deba@1720
|
451 |
///
|
deba@1720
|
452 |
/// Gives back the value assigned to the \c first - \c second unordered
|
deba@1720
|
453 |
/// pair.
|
deba@1720
|
454 |
ConstReference operator()(const Key& first, const Key& second) const {
|
deba@1999
|
455 |
return values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
456 |
Parent::getNotifier()->id(second))];
|
deba@1720
|
457 |
}
|
deba@1720
|
458 |
|
deba@1720
|
459 |
/// \brief Gives back the value assigned to the \c first - \c second
|
deba@1720
|
460 |
/// unordered pair.
|
deba@1720
|
461 |
///
|
deba@1720
|
462 |
/// Gives back the value assigned to the \c first - \c second unordered
|
deba@1720
|
463 |
/// pair.
|
deba@1720
|
464 |
Reference operator()(const Key& first, const Key& second) {
|
deba@1999
|
465 |
return values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
466 |
Parent::getNotifier()->id(second))];
|
deba@1720
|
467 |
}
|
deba@1720
|
468 |
|
deba@1720
|
469 |
/// \brief Setter function for the matrix map.
|
deba@1720
|
470 |
///
|
deba@1720
|
471 |
/// Setter function for the matrix map.
|
alpar@2072
|
472 |
///
|
deba@1720
|
473 |
void set(const Key& first, const Key& second, const Value& val) {
|
deba@1999
|
474 |
values[index(Parent::getNotifier()->id(first),
|
deba@1999
|
475 |
Parent::getNotifier()->id(second))] = val;
|
deba@1720
|
476 |
}
|
deba@1720
|
477 |
|
deba@1720
|
478 |
protected:
|
deba@1720
|
479 |
|
deba@1720
|
480 |
static int index(int i, int j) {
|
deba@1720
|
481 |
if (i < j) {
|
deba@1720
|
482 |
return j * (j + 1) / 2 + i;
|
deba@1720
|
483 |
} else {
|
deba@1720
|
484 |
return i * (i + 1) / 2 + j;
|
deba@1720
|
485 |
}
|
deba@1720
|
486 |
}
|
deba@1720
|
487 |
|
deba@1720
|
488 |
static int size(int s) {
|
deba@1720
|
489 |
return s * (s + 1) / 2;
|
deba@1720
|
490 |
}
|
deba@1720
|
491 |
|
deba@1720
|
492 |
virtual void add(const Key& key) {
|
deba@1999
|
493 |
if (size(Parent::getNotifier()->id(key) + 1) >= (int)values.size()) {
|
deba@1999
|
494 |
values.resize(size(Parent::getNotifier()->id(key) + 1));
|
deba@1720
|
495 |
}
|
deba@1720
|
496 |
}
|
deba@1720
|
497 |
|
deba@1720
|
498 |
virtual void erase(const Key&) {}
|
deba@1720
|
499 |
|
deba@1720
|
500 |
virtual void build() {
|
deba@1999
|
501 |
values.resize(size(Parent::getNotifier()->maxId() + 1));
|
deba@1720
|
502 |
}
|
deba@1720
|
503 |
|
deba@1720
|
504 |
virtual void clear() {
|
deba@1720
|
505 |
values.clear();
|
deba@1720
|
506 |
}
|
deba@1720
|
507 |
|
deba@1720
|
508 |
private:
|
deba@1720
|
509 |
std::vector<Value> values;
|
deba@1720
|
510 |
};
|
deba@2039
|
511 |
|
deba@2039
|
512 |
///\brief Dynamic Asymmetric Matrix Map.
|
deba@2039
|
513 |
///
|
alpar@2072
|
514 |
///\ingroup matrices
|
deba@2039
|
515 |
///Dynamic Asymmetric Matrix Map. Container for store values for each
|
deba@2039
|
516 |
///ordered pair of containers items. This data structure can store
|
deba@2039
|
517 |
///data with different key types from different container types. It
|
deba@2039
|
518 |
///increases the size of the container if the linked containers
|
deba@2039
|
519 |
///content change, so it is updated automaticly whenever it is
|
deba@2039
|
520 |
///needed.
|
deba@2039
|
521 |
///
|
deba@2039
|
522 |
///This map meet with the concept::ReferenceMatrixMap<typename K1,
|
deba@2039
|
523 |
///typename K2, typename V, typename R, typename CR> called as
|
deba@2039
|
524 |
///"ReferenceMatrixMap".
|
deba@2039
|
525 |
///
|
deba@2039
|
526 |
///\param _FirstContainer the desired type of first container. It is
|
deba@2039
|
527 |
///ususally a Graph type, but can be any type with alteration
|
deba@2039
|
528 |
///property.
|
deba@2039
|
529 |
///
|
deba@2039
|
530 |
///\param _FirstContainerItem the nested type of the
|
deba@2039
|
531 |
///FirstContainer. It is usually a graph item as Node, Edge,
|
deba@2039
|
532 |
///etc. This type will be the FirstKey type.
|
deba@2039
|
533 |
///
|
deba@2039
|
534 |
///\param _SecondContainer the desired type of the second
|
deba@2039
|
535 |
///container. It is usualy a Graph type, but can be any type with
|
deba@2039
|
536 |
///alteration property.
|
deba@2039
|
537 |
///
|
deba@2039
|
538 |
///\param _SecondContainerItem the nested type of the
|
deba@2039
|
539 |
///SecondContainer. It is usually a graph item such as Node, Edge,
|
deba@2039
|
540 |
///UEdge, etc. This type will be the SecondKey type.
|
deba@2039
|
541 |
///
|
deba@2039
|
542 |
///\param _Value the type of the strored values in the container.
|
deba@2039
|
543 |
///
|
deba@2039
|
544 |
/// \author Janos Nagy
|
deba@2039
|
545 |
template <typename _FirstContainer, typename _FirstContainerItem,
|
deba@2039
|
546 |
typename _SecondContainer, typename _SecondContainerItem,
|
deba@2039
|
547 |
typename _Value>
|
deba@2039
|
548 |
class DynamicAsymMatrixMap{
|
deba@2039
|
549 |
public:
|
deba@2039
|
550 |
|
deba@2039
|
551 |
///The first key type.
|
deba@2039
|
552 |
typedef _FirstContainerItem FirstKey;
|
deba@2039
|
553 |
|
deba@2039
|
554 |
///The second key type.
|
deba@2039
|
555 |
typedef _SecondContainerItem SecondKey;
|
deba@2039
|
556 |
|
deba@2039
|
557 |
///The value type of the map.
|
deba@2039
|
558 |
typedef _Value Value;
|
deba@2039
|
559 |
|
deba@2039
|
560 |
///Indicates it is a reference map.
|
deba@2039
|
561 |
typedef True ReferenceMapTag;
|
deba@2039
|
562 |
|
deba@2039
|
563 |
protected:
|
deba@2039
|
564 |
|
deba@2039
|
565 |
///\brief Proxy class for the first key type.
|
deba@2039
|
566 |
///
|
deba@2039
|
567 |
///The proxy class belongs to the FirstKey type. It is necessary because
|
deba@2039
|
568 |
///if one want use the same conatainer types and same nested types but on
|
deba@2039
|
569 |
///other instances of containers than due to the type equiality of nested
|
deba@2039
|
570 |
///types it requires a proxy mechanism.
|
deba@2039
|
571 |
class FirstKeyProxy
|
deba@2039
|
572 |
: protected
|
deba@2039
|
573 |
ItemSetTraits<_FirstContainer,_FirstContainerItem>::
|
deba@2039
|
574 |
ItemNotifier::ObserverBase
|
deba@2039
|
575 |
{
|
deba@2039
|
576 |
|
deba@2039
|
577 |
public:
|
deba@2039
|
578 |
|
deba@2039
|
579 |
friend class DynamicAsymMatrixMap;
|
deba@2039
|
580 |
|
deba@2039
|
581 |
///Constructor.
|
deba@2039
|
582 |
FirstKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
|
deba@2039
|
583 |
protected:
|
deba@2039
|
584 |
|
deba@2039
|
585 |
///\brief Add a new FirstKey to the map.
|
deba@2039
|
586 |
///
|
deba@2039
|
587 |
///It adds a new FirstKey to the map. It is called by the
|
deba@2039
|
588 |
///observer notifier and it is ovverride the add() virtual
|
deba@2039
|
589 |
///member function in the observer base. It will call the
|
deba@2039
|
590 |
///maps addFirstKey() function.
|
deba@2039
|
591 |
virtual void add(const FirstKey& _firstKey){
|
deba@2039
|
592 |
_owner.addFirstKey(_firstKey);
|
deba@2039
|
593 |
}
|
deba@2039
|
594 |
|
deba@2039
|
595 |
///\brief Add more new FirstKey to the map.
|
deba@2039
|
596 |
///
|
deba@2039
|
597 |
///It adds more new FirstKey to the map. It is called by the
|
deba@2039
|
598 |
///observer notifier and it is ovverride the add() virtual
|
deba@2039
|
599 |
///member function in the observer base. It will call the
|
deba@2039
|
600 |
///map's addFirstKeys() function.
|
deba@2039
|
601 |
virtual void add(const std::vector<FirstKey>& _firstKeys){
|
deba@2039
|
602 |
_owner.addFirstKeys(_firstKeys);
|
deba@2039
|
603 |
}
|
deba@2039
|
604 |
|
deba@2039
|
605 |
///\brief Erase a FirstKey from the map.
|
deba@2039
|
606 |
///
|
deba@2039
|
607 |
///Erase a FirstKey from the map. It called by the observer
|
deba@2039
|
608 |
///notifier and it overrides the erase() virtual member
|
deba@2039
|
609 |
///function of the observer base. It will call the map's
|
deba@2039
|
610 |
///eraseFirstKey() function.
|
deba@2039
|
611 |
virtual void erase(const FirstKey& _firstKey){
|
deba@2039
|
612 |
_owner.eraseFirstKey(_firstKey);
|
deba@2039
|
613 |
}
|
deba@2039
|
614 |
|
deba@2039
|
615 |
///\brief Erase more FirstKey from the map.
|
deba@2039
|
616 |
///
|
deba@2039
|
617 |
///Erase more FirstKey from the map. It called by the
|
deba@2039
|
618 |
///observer notifier and it overrides the erase() virtual
|
deba@2039
|
619 |
///member function of the observer base. It will call the
|
deba@2039
|
620 |
///map's eraseFirstKeys() function.
|
deba@2039
|
621 |
virtual void erase(const std::vector<FirstKey>& _firstKeys){
|
deba@2039
|
622 |
_owner.eraseFirstKeys(_firstKeys);
|
deba@2039
|
623 |
}
|
deba@2039
|
624 |
|
deba@2039
|
625 |
///\brief Builds the map.
|
deba@2039
|
626 |
///
|
deba@2039
|
627 |
///It buildes the map. It called by the observer notifier
|
deba@2039
|
628 |
///and it overrides the build() virtual member function of
|
deba@2039
|
629 |
///the observer base. It will call the map's build()
|
deba@2039
|
630 |
///function.
|
deba@2039
|
631 |
virtual void build() {
|
deba@2039
|
632 |
_owner.build();
|
deba@2039
|
633 |
//_owner.buildFirst();
|
deba@2039
|
634 |
}
|
deba@2039
|
635 |
|
deba@2039
|
636 |
///\brief Clear the map.
|
deba@2039
|
637 |
///
|
deba@2039
|
638 |
///It erases all items from the map. It called by the
|
deba@2039
|
639 |
///observer notifier and it overrides the clear() virtual
|
deba@2039
|
640 |
///memeber function of the observer base. It will call the
|
deba@2039
|
641 |
///map's clear() function.
|
deba@2039
|
642 |
virtual void clear() {
|
deba@2039
|
643 |
_owner.clear();
|
deba@2039
|
644 |
//_owner.clearFirst();
|
deba@2039
|
645 |
}
|
deba@2039
|
646 |
private:
|
deba@2039
|
647 |
|
deba@2039
|
648 |
///The map type for it is linked.
|
deba@2039
|
649 |
DynamicAsymMatrixMap& _owner;
|
alpar@2072
|
650 |
};//END OF FIRSTKEYPROXY
|
deba@2039
|
651 |
|
deba@2039
|
652 |
///\brief Proxy class for the second key type.
|
deba@2039
|
653 |
///
|
ladanyi@2047
|
654 |
///The proxy class belongs to the SecondKey type. It is
|
deba@2039
|
655 |
///necessary because if one want use the same conatainer types
|
deba@2039
|
656 |
///and same nested types but on other instances of containers
|
deba@2039
|
657 |
///than due to the type equiality of nested types it requires a
|
deba@2039
|
658 |
///proxy mechanism.
|
deba@2039
|
659 |
class SecondKeyProxy
|
deba@2039
|
660 |
: protected
|
deba@2039
|
661 |
ItemSetTraits<_SecondContainer, _SecondContainerItem>::
|
deba@2039
|
662 |
ItemNotifier::ObserverBase {
|
deba@2039
|
663 |
|
deba@2039
|
664 |
public:
|
deba@2039
|
665 |
|
deba@2039
|
666 |
friend class DynamicAsymMatrixMap;
|
deba@2039
|
667 |
///Constructor.
|
deba@2039
|
668 |
SecondKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
|
deba@2039
|
669 |
|
deba@2039
|
670 |
protected:
|
deba@2039
|
671 |
|
deba@2039
|
672 |
///\brief Add a new SecondKey to the map.
|
deba@2039
|
673 |
///
|
deba@2039
|
674 |
///It adds a new SecondKey to the map. It is called by the
|
deba@2039
|
675 |
///observer notifier and it is ovverride the add() virtual
|
deba@2039
|
676 |
///member function in the observer base. It will call the
|
deba@2039
|
677 |
///maps addSecondKey() function.
|
deba@2039
|
678 |
virtual void add(const SecondKey& _secondKey){
|
deba@2039
|
679 |
_owner.addSecondKey(_secondKey);
|
deba@2039
|
680 |
}
|
deba@2039
|
681 |
|
deba@2039
|
682 |
///\brief Add more new SecondKey to the map.
|
deba@2039
|
683 |
///
|
deba@2039
|
684 |
///It adds more new SecondKey to the map. It is called by
|
deba@2039
|
685 |
///the observer notifier and it is ovverride the add()
|
deba@2039
|
686 |
///virtual member function in the observer base. It will
|
deba@2039
|
687 |
///call the maps addSecondKeys() function.
|
deba@2039
|
688 |
virtual void add(const std::vector<SecondKey>& _secondKeys){
|
deba@2039
|
689 |
_owner.addSecondKeys(_secondKeys);
|
deba@2039
|
690 |
}
|
deba@2039
|
691 |
|
deba@2039
|
692 |
///\brief Erase a SecondKey from the map.
|
deba@2039
|
693 |
///
|
deba@2039
|
694 |
///Erase a SecondKey from the map. It called by the observer
|
deba@2039
|
695 |
///notifier and it overrides the erase() virtual member
|
deba@2039
|
696 |
///function of the observer base. It will call the map's
|
deba@2039
|
697 |
///eraseSecondKey() function.
|
deba@2039
|
698 |
virtual void erase(const SecondKey& _secondKey){
|
deba@2039
|
699 |
_owner.eraseSecondKey(_secondKey);
|
deba@2039
|
700 |
}
|
deba@2039
|
701 |
|
deba@2039
|
702 |
///\brief Erase more SecondKeys from the map.
|
deba@2039
|
703 |
///
|
deba@2039
|
704 |
///Erase more SecondKey from the map. It called by the
|
deba@2039
|
705 |
///observer notifier and it overrides the erase() virtual
|
deba@2039
|
706 |
///member function of the observer base. It will call the
|
deba@2039
|
707 |
///map's eraseSecondKeys() function.
|
deba@2039
|
708 |
virtual void erase(const std::vector<SecondKey>& _secondKeys){
|
deba@2039
|
709 |
_owner.eraseSecondKeys(_secondKeys);
|
deba@2039
|
710 |
}
|
deba@2039
|
711 |
|
deba@2039
|
712 |
///\brief Builds the map.
|
deba@2039
|
713 |
///
|
deba@2039
|
714 |
///It buildes the map. It called by the observer notifier
|
deba@2039
|
715 |
///and it overrides the build() virtual member function of
|
deba@2039
|
716 |
///the observer base. It will call the map's build()
|
deba@2039
|
717 |
///function.
|
deba@2039
|
718 |
virtual void build() {
|
deba@2039
|
719 |
_owner.build();
|
deba@2039
|
720 |
}
|
deba@2039
|
721 |
|
deba@2039
|
722 |
///\brief Clear the map.
|
deba@2039
|
723 |
///
|
deba@2039
|
724 |
///It erases all items from the map. It called by the
|
deba@2039
|
725 |
///observer notifier and it overrides the clear() virtual
|
deba@2039
|
726 |
///memeber function of the observer base. It will call the
|
deba@2039
|
727 |
///map's clear() function.
|
deba@2039
|
728 |
virtual void clear() {
|
deba@2039
|
729 |
_owner.clear();
|
deba@2039
|
730 |
//_owner.clearFirst();
|
deba@2039
|
731 |
}
|
deba@2039
|
732 |
private:
|
deba@2039
|
733 |
|
deba@2039
|
734 |
///The type of map for which it is attached.
|
deba@2039
|
735 |
DynamicAsymMatrixMap& _owner;
|
alpar@2072
|
736 |
};//END OF SECONDKEYPROXY
|
deba@2039
|
737 |
|
deba@2039
|
738 |
private:
|
deba@2039
|
739 |
|
deba@2039
|
740 |
/// \e
|
deba@2039
|
741 |
typedef std::vector<Value> Container;
|
deba@2039
|
742 |
|
deba@2039
|
743 |
///The type of constainer which stores the values of the map.
|
deba@2039
|
744 |
typedef std::vector<Container> DContainer;
|
deba@2039
|
745 |
|
deba@2039
|
746 |
///The std:vector type which contains the data
|
deba@2039
|
747 |
DContainer values;
|
deba@2039
|
748 |
|
deba@2039
|
749 |
///Member for the first proxy class
|
deba@2039
|
750 |
FirstKeyProxy _first_key_proxy;
|
deba@2039
|
751 |
|
deba@2039
|
752 |
///Member for the second proxy class
|
deba@2039
|
753 |
SecondKeyProxy _second_key_proxy;
|
deba@2039
|
754 |
|
deba@2039
|
755 |
public:
|
deba@2039
|
756 |
|
deba@2039
|
757 |
///The refernce type of the map.
|
deba@2039
|
758 |
typedef typename Container::reference Reference;
|
deba@2039
|
759 |
|
deba@2039
|
760 |
///The const reference type of the constainer.
|
deba@2039
|
761 |
typedef typename Container::const_reference ConstReference;
|
deba@2039
|
762 |
|
deba@2039
|
763 |
///\brief Constructor what create the map for the two containers type.
|
deba@2039
|
764 |
///
|
deba@2039
|
765 |
///Creates the matrix map and initialize the values with Value()
|
deba@2039
|
766 |
DynamicAsymMatrixMap(const _FirstContainer& _firstContainer,
|
deba@2039
|
767 |
const _SecondContainer& _secondContainer)
|
deba@2039
|
768 |
: values(DContainer(_firstContainer.maxId(FirstKey())+1,
|
deba@2039
|
769 |
Container(_secondContainer.maxId(SecondKey())+1))),
|
deba@2039
|
770 |
_first_key_proxy(*this),
|
deba@2039
|
771 |
_second_key_proxy(*this)
|
deba@2039
|
772 |
{
|
deba@2039
|
773 |
_first_key_proxy.attach(_firstContainer.getNotifier(FirstKey()));
|
deba@2039
|
774 |
_second_key_proxy.attach(_secondContainer.getNotifier(SecondKey()));
|
deba@2039
|
775 |
}
|
deba@2039
|
776 |
|
deba@2039
|
777 |
///\brief Constructor what create the map for the two containers type.
|
deba@2039
|
778 |
///
|
deba@2039
|
779 |
///Creates the matrix map and initialize the values with the given _value
|
deba@2039
|
780 |
DynamicAsymMatrixMap(const _FirstContainer& _firstContainer,
|
deba@2039
|
781 |
const _SecondContainer& _secondContainer,
|
deba@2039
|
782 |
const Value& _value)
|
deba@2039
|
783 |
: values(DContainer(_firstContainer.maxId(FirstKey())+1,
|
deba@2039
|
784 |
Container(_secondContainer.maxId(SecondKey())+1,
|
deba@2039
|
785 |
_value))),
|
deba@2039
|
786 |
_first_key_proxy(*this),
|
deba@2039
|
787 |
_second_key_proxy(*this)
|
deba@2039
|
788 |
{
|
deba@2039
|
789 |
_first_key_proxy.attach(_firstContainer.getNotifier(FirstKey()));
|
deba@2039
|
790 |
_second_key_proxy.attach(_secondContainer.getNotifier(SecondKey()));
|
deba@2039
|
791 |
}
|
deba@2039
|
792 |
|
deba@2039
|
793 |
///\brief Copy constructor.
|
deba@2039
|
794 |
///
|
deba@2039
|
795 |
///The copy constructor of the map.
|
deba@2039
|
796 |
DynamicAsymMatrixMap(const DynamicAsymMatrixMap& _copy)
|
deba@2039
|
797 |
: _first_key_proxy(*this), _second_key_proxy(*this) {
|
deba@2039
|
798 |
if(_copy._first_key_proxy.attached() &&
|
deba@2039
|
799 |
_copy._second_key_proxy.attached()){
|
deba@2039
|
800 |
_first_key_proxy.attach(*_copy._first_key_proxy.getNotifier());
|
deba@2039
|
801 |
_second_key_proxy.attach(*_copy._second_key_proxy.getNotifier());
|
deba@2039
|
802 |
values = _copy.values;
|
deba@2039
|
803 |
}
|
deba@2039
|
804 |
}
|
deba@2039
|
805 |
|
deba@2039
|
806 |
///\brief Destructor
|
deba@2039
|
807 |
///
|
deba@2039
|
808 |
///Destructor what detach() from the attached objects. May this
|
deba@2039
|
809 |
///function is not necessary because the destructor of
|
deba@2039
|
810 |
///ObserverBase do the same.
|
deba@2039
|
811 |
~DynamicAsymMatrixMap() {
|
deba@2039
|
812 |
if(_first_key_proxy.attached()){
|
deba@2039
|
813 |
_first_key_proxy.detach();
|
deba@2039
|
814 |
}
|
deba@2039
|
815 |
if(_second_key_proxy.attached()){
|
deba@2039
|
816 |
_second_key_proxy.detach();
|
deba@2039
|
817 |
}
|
deba@2039
|
818 |
}
|
deba@2039
|
819 |
|
deba@2039
|
820 |
///\brief Gives back the value assigned to the \c first - \c
|
deba@2039
|
821 |
///second ordered pair.
|
deba@2039
|
822 |
///
|
deba@2039
|
823 |
///Gives back the value assigned to the \c first - \c second
|
deba@2039
|
824 |
///ordered pair.
|
deba@2039
|
825 |
Reference operator()(const FirstKey& _first, const SecondKey& _second) {
|
deba@2039
|
826 |
return values[_first_key_proxy.getNotifier()->id(_first)]
|
deba@2039
|
827 |
[_second_key_proxy.getNotifier()->id(_second)];
|
deba@2039
|
828 |
}
|
deba@2039
|
829 |
|
deba@2039
|
830 |
///\brief Gives back the value assigned to the \c first - \c
|
deba@2039
|
831 |
///second ordered pair.
|
deba@2039
|
832 |
///
|
deba@2039
|
833 |
///Gives back the value assigned to the \c first - \c second
|
deba@2039
|
834 |
///ordered pair.
|
deba@2039
|
835 |
ConstReference operator()(const FirstKey& _first,
|
deba@2039
|
836 |
const SecondKey& _second) const {
|
deba@2039
|
837 |
return values[_first_key_proxy.getNotifier()->id(_first)]
|
deba@2039
|
838 |
[_second_key_proxy.getNotifier()->id(_second)];
|
deba@2039
|
839 |
}
|
deba@2039
|
840 |
|
deba@2039
|
841 |
///\brief Setter function for this matrix map.
|
deba@2039
|
842 |
///
|
deba@2039
|
843 |
///Setter function for this matrix map.
|
deba@2039
|
844 |
void set(const FirstKey& first, const SecondKey& second,
|
deba@2039
|
845 |
const Value& value){
|
deba@2039
|
846 |
values[_first_key_proxy.getNotifier()->id(first)]
|
deba@2039
|
847 |
[_second_key_proxy.getNotifier()->id(second)] = value;
|
deba@2039
|
848 |
}
|
deba@2039
|
849 |
|
deba@2039
|
850 |
///\brief The assignement operator.
|
deba@2039
|
851 |
///
|
deba@2039
|
852 |
///It allow to assign a map to an other. It
|
deba@2039
|
853 |
DynamicAsymMatrixMap& operator=(const DynamicAsymMatrixMap& _cmap){
|
deba@2039
|
854 |
return operator=<DynamicAsymMatrixMap>(_cmap);
|
deba@2039
|
855 |
}
|
deba@2039
|
856 |
|
deba@2039
|
857 |
///\brief Template assignement operator.
|
deba@2039
|
858 |
///
|
deba@2039
|
859 |
///It copy the element of the given map to its own container. The
|
deba@2039
|
860 |
///type of the two map shall be the same.
|
deba@2039
|
861 |
template <typename CMap>
|
deba@2039
|
862 |
DynamicAsymMatrixMap& operator=(const CMap& _cdmap){
|
deba@2039
|
863 |
checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
|
deba@2039
|
864 |
const typename FirstKeyProxy::Notifier* notifierFirstKey =
|
deba@2039
|
865 |
_first_key_proxy.getNotifier();
|
deba@2039
|
866 |
const typename SecondKeyProxy::Notifier* notifierSecondKey =
|
deba@2039
|
867 |
_second_key_proxy.getNotifier();
|
deba@2039
|
868 |
FirstKey itemFirst;
|
deba@2039
|
869 |
SecondKey itemSecond;
|
deba@2039
|
870 |
for(notifierFirstKey->first(itemFirst); itemFirst != INVALID;
|
deba@2039
|
871 |
notifierFirstKey->next(itemFirst)){
|
deba@2039
|
872 |
for(notifierSecondKey->first(itemSecond); itemSecond != INVALID;
|
deba@2039
|
873 |
notifierSecondKey->next(itemSecond)){
|
deba@2039
|
874 |
set(itemFirst, itemSecond, _cdmap(itemFirst,itemSecond));
|
deba@2039
|
875 |
}
|
deba@2039
|
876 |
}
|
deba@2039
|
877 |
return *this;
|
deba@2039
|
878 |
}
|
deba@2039
|
879 |
|
deba@2039
|
880 |
protected:
|
deba@2039
|
881 |
|
deba@2039
|
882 |
///\brief Add a new FirstKey to the map.
|
deba@2039
|
883 |
///
|
deba@2039
|
884 |
///It adds a new FirstKey to the map. It is called by the observer
|
deba@2039
|
885 |
///class belongs to the FirstKey type.
|
deba@2039
|
886 |
void addFirstKey(const FirstKey& firstKey) {
|
deba@2039
|
887 |
int size = (int)values.size();
|
deba@2039
|
888 |
if( _first_key_proxy.getNotifier()->id(firstKey)+1 >= size ){
|
deba@2039
|
889 |
values.resize(_first_key_proxy.getNotifier()->id(firstKey)+1);
|
deba@2039
|
890 |
if( (int)values[0].size() != 0 ){
|
deba@2039
|
891 |
int innersize = (int)values[0].size();
|
deba@2039
|
892 |
for(int i=size; i!=(int)values.size();++i){
|
deba@2039
|
893 |
(values[i]).resize(innersize);
|
deba@2039
|
894 |
}
|
deba@2039
|
895 |
}else if(_second_key_proxy.getNotifier()->maxId() >= 0){
|
deba@2039
|
896 |
int innersize = _second_key_proxy.getNotifier()->maxId();
|
deba@2039
|
897 |
for(int i = 0; i != (int)values.size(); ++i){
|
deba@2039
|
898 |
values[0].resize(innersize);
|
deba@2039
|
899 |
}
|
deba@2039
|
900 |
}
|
deba@2039
|
901 |
}
|
deba@2039
|
902 |
}
|
deba@2039
|
903 |
|
deba@2039
|
904 |
///\brief Adds more new FirstKeys to the map.
|
deba@2039
|
905 |
///
|
deba@2039
|
906 |
///It adds more new FirstKeys to the map. It called by the
|
deba@2039
|
907 |
///observer class belongs to the FirstKey type.
|
deba@2039
|
908 |
void addFirstKeys(const std::vector<FirstKey>& firstKeys){
|
deba@2039
|
909 |
int max = values.size() - 1;
|
deba@2039
|
910 |
for(int i=0; i != (int)firstKeys.size(); ++i){
|
deba@2039
|
911 |
int id = _first_key_proxy.getNotifier()->id(firstKeys[i]);
|
deba@2039
|
912 |
if(max < id){
|
deba@2039
|
913 |
max = id;
|
deba@2039
|
914 |
}
|
deba@2039
|
915 |
}
|
deba@2039
|
916 |
int size = (int)values.size();
|
deba@2039
|
917 |
if(max >= size){
|
deba@2039
|
918 |
values.resize(max + 1);
|
deba@2039
|
919 |
if( (int)values[0].size() != 0){
|
deba@2039
|
920 |
int innersize = (int)values[0].size();
|
deba@2039
|
921 |
for(int i = size; i != (max + 1); ++i){
|
deba@2039
|
922 |
values[i].resize(innersize);
|
deba@2039
|
923 |
}
|
deba@2039
|
924 |
}else if(_second_key_proxy.getNotifier()->maxId() >= 0){
|
deba@2039
|
925 |
int innersize = _second_key_proxy.getNotifier()->maxId();
|
deba@2039
|
926 |
for(int i = 0; i != (int)values.size(); ++i){
|
deba@2039
|
927 |
values[i].resize(innersize);
|
deba@2039
|
928 |
}
|
deba@2039
|
929 |
}
|
deba@2039
|
930 |
}
|
deba@2039
|
931 |
}
|
deba@2039
|
932 |
|
deba@2039
|
933 |
///\brief Add a new SecondKey to the map.
|
deba@2039
|
934 |
///
|
deba@2039
|
935 |
///It adds a new SecondKey to the map. It is called by the
|
deba@2039
|
936 |
///observer class belongs to the SecondKey type.
|
deba@2039
|
937 |
void addSecondKey(const SecondKey& secondKey) {
|
deba@2039
|
938 |
if(values.size() == 0){
|
deba@2039
|
939 |
return;
|
deba@2039
|
940 |
}
|
deba@2039
|
941 |
int id = _second_key_proxy.getNotifier()->id(secondKey);
|
deba@2039
|
942 |
if(id >= (int)values[0].size()){
|
deba@2039
|
943 |
for(int i=0;i!=(int)values.size();++i){
|
deba@2039
|
944 |
values[i].resize(id+1);
|
deba@2039
|
945 |
}
|
deba@2039
|
946 |
}
|
deba@2039
|
947 |
}
|
deba@2039
|
948 |
|
deba@2039
|
949 |
///\brief Adds more new SecondKeys to the map.
|
deba@2039
|
950 |
///
|
deba@2039
|
951 |
///It adds more new SecondKeys to the map. It called by the
|
deba@2039
|
952 |
///observer class belongs to the SecondKey type.
|
deba@2039
|
953 |
void addSecondKeys(const std::vector<SecondKey>& secondKeys){
|
deba@2039
|
954 |
if(values.size() == 0){
|
deba@2039
|
955 |
return;
|
deba@2039
|
956 |
}
|
deba@2039
|
957 |
int max = values[0].size();
|
deba@2039
|
958 |
for(int i = 0; i != (int)secondKeys.size(); ++i){
|
deba@2039
|
959 |
int id = _second_key_proxy.getNotifier()->id(secondKeys[i]);
|
deba@2039
|
960 |
if(max < id){
|
deba@2039
|
961 |
max = id;
|
deba@2039
|
962 |
}
|
deba@2039
|
963 |
}
|
deba@2039
|
964 |
if(max > (int)values[0].size()){
|
deba@2039
|
965 |
for(int i = 0; i != (int)values.size(); ++i){
|
deba@2039
|
966 |
values[i].resize(max + 1);
|
deba@2039
|
967 |
}
|
deba@2039
|
968 |
}
|
deba@2039
|
969 |
}
|
deba@2039
|
970 |
|
deba@2039
|
971 |
///\brief Erase a FirstKey from the map.
|
deba@2039
|
972 |
///
|
deba@2039
|
973 |
///Erase a FirstKey from the map. It called by the observer
|
deba@2039
|
974 |
///class belongs to the FirstKey type.
|
deba@2039
|
975 |
void eraseFirstKey(const FirstKey& first) {
|
deba@2039
|
976 |
int id = _first_key_proxy.getNotifier()->id(first);
|
deba@2039
|
977 |
for(int i = 0; i != (int)values[id].size(); ++i){
|
deba@2039
|
978 |
values[id][i] = Value();
|
deba@2039
|
979 |
}
|
deba@2039
|
980 |
}
|
deba@2039
|
981 |
|
deba@2039
|
982 |
///\brief Erase more FirstKey from the map.
|
deba@2039
|
983 |
///
|
deba@2039
|
984 |
///Erase more FirstKey from the map. It called by the observer
|
deba@2039
|
985 |
///class belongs to the FirstKey type.
|
deba@2039
|
986 |
void eraseFirstKeys(const std::vector<FirstKey>& firstKeys) {
|
deba@2039
|
987 |
for(int j = 0; j != (int)firstKeys.size(); ++j){
|
deba@2039
|
988 |
int id = _first_key_proxy.getNotifier()->id(firstKeys[j]);
|
deba@2039
|
989 |
for(int i = 0; i != (int)values[id].size(); ++i){
|
deba@2039
|
990 |
values[id][i] = Value();
|
deba@2039
|
991 |
}
|
deba@2039
|
992 |
}
|
deba@2039
|
993 |
}
|
deba@2039
|
994 |
|
deba@2039
|
995 |
///\brief Erase a SecondKey from the map.
|
deba@2039
|
996 |
///
|
deba@2039
|
997 |
///Erase a SecondKey from the map. It called by the observer class
|
deba@2039
|
998 |
///belongs to the SecondKey type.
|
deba@2039
|
999 |
void eraseSecondKey(const SecondKey& second) {
|
deba@2039
|
1000 |
if(values.size() == 0){
|
deba@2039
|
1001 |
return;
|
deba@2039
|
1002 |
}
|
deba@2039
|
1003 |
int id = _second_key_proxy.getNotifier()->id(second);
|
deba@2039
|
1004 |
for(int i = 0; i != (int)values.size(); ++i){
|
deba@2039
|
1005 |
values[i][id] = Value();
|
deba@2039
|
1006 |
}
|
deba@2039
|
1007 |
}
|
deba@2039
|
1008 |
|
deba@2039
|
1009 |
///\brief Erase more SecondKey from the map.
|
deba@2039
|
1010 |
///
|
deba@2039
|
1011 |
///Erase more SecondKey from the map. It called by the observer
|
deba@2039
|
1012 |
///class belongs to the SecondKey type.
|
deba@2039
|
1013 |
void eraseSecondKeys(const std::vector<SecondKey>& secondKeys) {
|
deba@2039
|
1014 |
if(values.size() == 0){
|
deba@2039
|
1015 |
return;
|
deba@2039
|
1016 |
}
|
deba@2039
|
1017 |
for(int j = 0; j != (int)secondKeys.size(); ++j){
|
deba@2039
|
1018 |
int id = _second_key_proxy.getNotifier()->id(secondKeys[j]);
|
deba@2039
|
1019 |
for(int i = 0; i != (int)values.size(); ++i){
|
deba@2039
|
1020 |
values[i][id] = Value();
|
deba@2039
|
1021 |
}
|
deba@2039
|
1022 |
}
|
deba@2039
|
1023 |
}
|
deba@2039
|
1024 |
|
deba@2039
|
1025 |
///\brief Builds the map.
|
deba@2039
|
1026 |
///
|
deba@2039
|
1027 |
///It buildes the map. It is called by the observer class belongs
|
deba@2039
|
1028 |
///to the FirstKey or SecondKey type.
|
deba@2039
|
1029 |
void build() {
|
deba@2039
|
1030 |
values.resize(_first_key_proxy.getNotifier()->maxId());
|
deba@2039
|
1031 |
for(int i=0; i!=(int)values.size(); ++i){
|
deba@2039
|
1032 |
values[i].resize(_second_key_proxy.getNotifier()->maxId());
|
deba@2039
|
1033 |
}
|
deba@2039
|
1034 |
}
|
deba@2039
|
1035 |
|
deba@2039
|
1036 |
///\brief Clear the map.
|
deba@2039
|
1037 |
///
|
deba@2039
|
1038 |
///It erases all items from the map. It is called by the observer class
|
deba@2039
|
1039 |
///belongs to the FirstKey or SecondKey type.
|
deba@2039
|
1040 |
void clear() {
|
deba@2039
|
1041 |
for(int i=0; i!=(int)values.size(); ++i) {
|
deba@2039
|
1042 |
values[i].clear();
|
deba@2039
|
1043 |
}
|
deba@2039
|
1044 |
values.clear();
|
deba@2039
|
1045 |
}
|
deba@2039
|
1046 |
|
deba@2039
|
1047 |
};
|
deba@2039
|
1048 |
|
deba@2039
|
1049 |
|
deba@1720
|
1050 |
|
deba@1720
|
1051 |
}
|
deba@1720
|
1052 |
|
deba@1720
|
1053 |
#endif
|