Scippy

SoPlex

Sequential object-oriented simPlex

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