SoPlex
Loading...
Searching...
No Matches
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-2026 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 vec.
227 void add(const SVectorBase<R>& vec)
228 {
230 makeMem(vec.size());
232 }
233
234 /// Append one nonzero \p (i,v).
235 void add(int i, const R& v)
236 {
237 makeMem(1);
239 }
240
241 /// Append one uninitialized nonzero.
242 void add(int i)
243 {
244 makeMem(1);
246 }
247
248 /// Append \p n nonzeros.
249 void add(int n, const int i[], const R v[])
250 {
251 makeMem(n);
252 SVectorBase<R>::add(n, i, v);
253 }
254
255 /// Reset nonzero memory to >= \p newmax.
256 /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
257 * reset to size() only.
258 */
259 void setMax(int newmax = 1)
260 {
261 int siz = SVectorBase<R>::size();
262 int len = (newmax < siz) ? siz : newmax;
263
264 if(len == SVectorBase<R>::max())
265 return;
266
267 Nonzero<R>* newmem = nullptr;
268
269 /* allocate new memory */
270 spx_alloc(newmem, len);
271
272 /* call copy constructor for first elements */
273 int i;
274
275 for(i = 0; i < siz; i++)
276 new((&newmem[i])) Nonzero<R>(theelem[i]);
277
278 /* call default constructor for remaining elements */
279 for(; i < len; i++)
280 new((&newmem[i])) Nonzero<R>();
281
282 /* free old memory */
283 for(i = SVectorBase<R>::max() - 1; i >= 0; i--)
284 theelem[i].~Nonzero<R>();
285
286 if(theelem != nullptr)
288
289 /* assign new memory */
290 theelem = newmem;
293 }
294
295 ///@}
296
297 // ------------------------------------------------------------------------------------------------------------------
298 /**@name Utilities */
299 ///@{
300
301 /// Consistency check.
302 bool isConsistent() const
303 {
304#ifdef ENABLE_CONSISTENCY_CHECKS
305
306 if(theelem != nullptr && SVectorBase<R>::mem() != theelem)
307 return SPX_MSG_INCONSISTENT("DSVectorBase");
308
309#endif
310
311 return true;
312 }
313
314 ///@}
315};
316
317
318
319/// Allocate memory for \p n nonzeros (specialization for Real).
320template<>
321inline
327
328
329
330/// Destructor (specialization for Real).
331template<>
332inline
338
339
340
341/// Reset nonzero memory to >= \p newmax.
342/** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
343 * reset to size() only (specialization for Real).
344 */
345template<>
346inline
348{
349 int siz = size();
350 int len = (newmax < siz) ? siz : newmax;
351
352 spx_realloc(theelem, len);
353 setMem(len, theelem);
354 // reset 'size' to old size since the above call to setMem() sets 'size' to 0
355 set_size(siz);
356}
357} // namespace soplex
358#endif // _DSVECTORBASE_H_
void makeMem(int n)
Ensure there is room for n new nonzeros.
void add(int i)
Append one uninitialized nonzero.
void add(int n, const int i[], const R v[])
Append n nonzeros.
bool isConsistent() const
Consistency check.
void allocMem(int n)
Allocate memory for n nonzeros.
DSVectorBase(const VectorBase< S > &vec)
Copy constructor.
DSVectorBase(const SVectorBase< S > &old)
Copy constructor.
void add(int i, const R &v)
Append one nonzero (i,v).
DSVectorBase(const DSVectorBase< R > &old)
Copy constructor.
DSVectorBase(const DSVectorBase< S > &old)
Copy constructor.
DSVectorBase(int n=8)
Default constructor.
DSVectorBase< R > & operator=(const SSVectorBase< S > &vec)
Assignment operator.
DSVectorBase< R > & operator=(const DSVectorBase< R > &vec)
Assignment operator.
DSVectorBase< R > & operator=(const DSVectorBase< S > &vec)
Assignment operator.
DSVectorBase(const SSVectorBase< S > &old)
Copy constructor.
virtual ~DSVectorBase()
Destructor.
DSVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
void add(const SVectorBase< R > &vec)
Append nonzeros of vec.
void setMax(int newmax=1)
Reset nonzero memory to >= newmax.
DSVectorBase< R > & operator=(const SVectorBase< S > &vec)
Assignment operator.
Nonzero< Real > * theelem
Sparse vector nonzero element.
Definition svectorbase.h:47
Sparse Linear Solver virtual base class.
Definition slinsolver.h:53
Semi sparse vector.
void setMem(int n, Nonzero< R > *elmem)
Set the memory area where the nonzeros will be stored.
friend class SVectorBase
void add(int i, const R &v)
Append one nonzero (i,v).
int max() const
Maximal number of indices.
void set_size(int s)
Set size of the vector.
Nonzero< R > * mem() const
get pointer to internal memory.
SVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
void clear()
Remove all indices.
int size() const
Number of used indices.
Dense vector.
Definition vectorbase.h:86
Everything should be within this namespace.
void spx_alloc(T &p, size_t n=1)
Allocate memory.
Definition spxalloc.h:58
void spx_free(T &p)
Release memory.
Definition spxalloc.h:118
void spx_realloc(T &p, size_t n)
Change amount of allocated memory.
Definition spxalloc.h:89
#define SPX_MSG_INCONSISTENT(name)
Definition spxdefines.h:175
Sparse vectors.