1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the class library */
/* SoPlex --- the Sequential object-oriented simPlex. */
/* */
/* Copyright 1996-2022 Zuse Institute Berlin */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* You should have received a copy of the Apache-2.0 license */
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file dsvectorbase.h
* @brief Dynamic sparse vectors.
*/
#ifndef _DSVECTORBASE_H_
#define _DSVECTORBASE_H_
#include <assert.h>
#include "soplex/svectorbase.h"
namespace soplex
{
template < class R > class VectorBase;
template < class S > class SSVectorBase;
template < class R > class SLinSolver;
/**@brief Dynamic sparse vectors.
* @ingroup Algebra
*
* Class DSVectorBase implements dynamic sparse vectors, i.e. #SVectorBase%s with an automatic memory management. This
* allows the user to freely add() as many nonzeros to a DSVectorBase as desired, without any precautions. For saving
* memory method setMax() allows to reduce memory consumption to the amount really required.
*
* @todo Both DSVectorBase and SVectorBase have a member variable that points to allocated memory. This does not seem to
* make too much sense. Why doesn't DSVectorBase use the element of its base class?
*/
template < class R >
class DSVectorBase : public SVectorBase<R>
{
friend class SLinSolver<R>;
private:
// ------------------------------------------------------------------------------------------------------------------
/**@name Data */
///@{
/// Memory.
Nonzero<R>* theelem;
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Private helpers */
///@{
/// Allocate memory for \p n nonzeros.
void allocMem(int n)
{
spx_alloc(theelem, n);
for(int i = 0; i < n; i++)
new(&(theelem[i])) Nonzero<R>();
SVectorBase<R>::setMem(n, theelem);
}
/// Ensure there is room for \p n new nonzeros.
void makeMem(int n)
{
assert(n >= 0);
if(SVectorBase<R>::max() - SVectorBase<R>::size() < n)
{
assert(SVectorBase<R>::size() + n > 0);
setMax(SVectorBase<R>::size() + n);
}
}
///@}
public:
// ------------------------------------------------------------------------------------------------------------------
/**@name Construction, assignment, and destruction */
///@{
/// Default constructor.
/** Creates a DSVectorBase ready to hold \p n nonzeros. However, the memory is automatically enlarged, if more
* nonzeros are added to the DSVectorBase.
*/
explicit DSVectorBase<R>(int n = 8)
: theelem(0)
{
allocMem((n < 1) ? 2 : n);
assert(isConsistent());
}
/// Copy constructor.
template < class S >
explicit DSVectorBase<R>(const SVectorBase<S>& old)
: theelem(0)
{
allocMem(old.size());
SVectorBase<R>::operator=(old);
assert(isConsistent());
}
/// Copy constructor.
/** The redundancy with the copy constructor below is necessary since otherwise the compiler doesn't realize that it
* could use the more general one with S = R and generates a shallow copy constructor.
*/
DSVectorBase<R>(const DSVectorBase<R>& old)
: SVectorBase<R>()
, theelem(0)
{
allocMem(old.size());
SVectorBase<R>::operator=(old);
assert(isConsistent());
}
/// Copy constructor.
template < class S >
DSVectorBase<R>(const DSVectorBase<S>& old)
: SVectorBase<R>()
, theelem(0)
{
allocMem(old.size());
SVectorBase<R>::operator=(old);
assert(isConsistent());
}
/// Copy constructor.
template < class S >
explicit DSVectorBase<R>(const VectorBase<S>& vec);
/// Copy constructor.
template < class S >
explicit DSVectorBase<R>(const SSVectorBase<S>& old);
/// Assignment operator.
template < class S >
DSVectorBase<R>& operator=(const SVectorBase<S>& vec)
{
if(this != &vec)
{
SVectorBase<R>::clear();
makeMem(vec.size());
SVectorBase<R>::operator=(vec);
}
return *this;
}
/// Assignment operator.
DSVectorBase<R>& operator=(const DSVectorBase<R>& vec)
{
if(this != &vec)
{
SVectorBase<R>::clear();
makeMem(vec.size());
SVectorBase<R>::operator=(vec);
}
return *this;
}
/// Assignment operator.
template < class S >
DSVectorBase<R>& operator=(const DSVectorBase<S>& vec)
{
if(this != (DSVectorBase<R>*)(&vec))
{
SVectorBase<R>::clear();
makeMem(vec.size());
SVectorBase<R>::operator=(vec);
}
return *this;
}
/// Assignment operator.
template < class S >
DSVectorBase<R>& operator=(const VectorBase<S>& vec);
/// Assignment operator.
template < class S >
DSVectorBase<R>& operator=(const SSVectorBase<S>& vec);
/// Destructor.
virtual ~DSVectorBase<R>()
{
if(theelem)
{
for(int i = SVectorBase<R>::max() - 1; i >= 0; i--)
theelem[i].~Nonzero<R>();
spx_free(theelem);
}
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Modification */
///@{
/// Append nonzeros of \p sv.
template < class S >
void add(const SVectorBase<S>& vec)
{
SVectorBase<R>::clear();
makeMem(vec.size());
SVectorBase<S>::add(vec);
}
/// Append one nonzero \p (i,v).
void add(int i, const R& v)
{
makeMem(1);
SVectorBase<R>::add(i, v);
}
/// Append one uninitialized nonzero.
void add(int i)
{
makeMem(1);
SVectorBase<R>::add(i);
}
/// Append \p n nonzeros.
void add(int n, const int i[], const R v[])
{
makeMem(n);
SVectorBase<R>::add(n, i, v);
}
/// Reset nonzero memory to >= \p newmax.
/** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
* reset to size() only.
*/
void setMax(int newmax = 1)
{
int siz = SVectorBase<R>::size();
int len = (newmax < siz) ? siz : newmax;
if(len == SVectorBase<R>::max())
return;
Nonzero<R>* newmem = 0;
/* allocate new memory */
spx_alloc(newmem, len);
/* call copy constructor for first elements */
int i;
for(i = 0; i < siz; i++)
new((&newmem[i])) Nonzero<R>(theelem[i]);
/* call default constructor for remaining elements */
for(; i < len; i++)
new((&newmem[i])) Nonzero<R>();
/* free old memory */
for(i = SVectorBase<R>::max() - 1; i >= 0; i--)
theelem[i].~Nonzero<R>();
if(theelem != 0)
spx_free(theelem);
/* assign new memory */
theelem = newmem;
SVectorBase<R>::setMem(len, theelem);
SVectorBase<R>::set_size(siz);
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Utilities */
///@{
/// Consistency check.
bool isConsistent() const
{
#ifdef ENABLE_CONSISTENCY_CHECKS
if(theelem != 0 && SVectorBase<R>::mem() != theelem)
return MSGinconsistent("DSVectorBase");
#endif
return true;
}
///@}
};
/// Allocate memory for \p n nonzeros (specialization for Real).
template<>
inline
void DSVectorBase<Real>::allocMem(int n)
{
spx_alloc(theelem, n);
SVectorBase<Real>::setMem(n, theelem);
}
/// Destructor (specialization for Real).
template<>
inline
DSVectorBase<Real>::~DSVectorBase()
{
if(theelem)
spx_free(theelem);
}
/// Reset nonzero memory to >= \p newmax.
/** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
* reset to size() only (specialization for Real).
*/
template<>
inline
void DSVectorBase<Real>::setMax(int newmax)
{
int siz = size();
int len = (newmax < siz) ? siz : newmax;
spx_realloc(theelem, len);
setMem(len, theelem);
// reset 'size' to old size since the above call to setMem() sets 'size' to 0
set_size(siz);
}
} // namespace soplex
#endif // _DSVECTORBASE_H_