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-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file dsvectorbase.h
17  * @brief Dynamic sparse vectors.
18  */
19 #ifndef _DSVECTORBASE_H_
20 #define _DSVECTORBASE_H_
21 
22 #include <assert.h>
23 
24 #include "soplex/svectorbase.h"
25 
26 namespace soplex
27 {
28 template < class R > class VectorBase;
29 template < class S > class SSVectorBase;
30 template < class R > class SLinSolver;
31 
32 /**@brief Dynamic sparse vectors.
33  * @ingroup Algebra
34  *
35  * Class DSVectorBase implements dynamic sparse vectors, i.e. #SVectorBase%s with an automatic memory management. This
36  * allows the user to freely add() as many nonzeros to a DSVectorBase as desired, without any precautions. For saving
37  * memory method setMax() allows to reduce memory consumption to the amount really required.
38  *
39  * @todo Both DSVectorBase and SVectorBase have a member variable that points to allocated memory. This does not seem to
40  * make too much sense. Why doesn't DSVectorBase use the element of its base class?
41  */
42 template < class R >
43 class DSVectorBase : public SVectorBase<R>
44 {
45  friend class SLinSolver<R>;
46 
47 private:
48 
49  // ------------------------------------------------------------------------------------------------------------------
50  /**@name Data */
51  ///@{
52 
53  /// Memory.
55 
56  ///@}
57 
58  // ------------------------------------------------------------------------------------------------------------------
59  /**@name Private helpers */
60  ///@{
61 
62  /// Allocate memory for \p n nonzeros.
63  void allocMem(int n)
64  {
65  spx_alloc(theelem, n);
66 
67  for(int i = 0; i < n; i++)
68  new(&(theelem[i])) Nonzero<R>();
69 
70  SVectorBase<R>::setMem(n, theelem);
71  }
72 
73  /// Ensure there is room for \p n new nonzeros.
74  void makeMem(int n)
75  {
76  assert(n >= 0);
77 
79  {
80  assert(SVectorBase<R>::size() + n > 0);
82  }
83  }
84 
85  ///@}
86 
87 public:
88 
89  // ------------------------------------------------------------------------------------------------------------------
90  /**@name Construction, assignment, and destruction */
91  ///@{
92 
93  /// Default constructor.
94  /** Creates a DSVectorBase ready to hold \p n nonzeros. However, the memory is automatically enlarged, if more
95  * nonzeros are added to the DSVectorBase.
96  */
97  explicit DSVectorBase<R>(int n = 8)
98  : theelem(0)
99  {
100  allocMem((n < 1) ? 2 : n);
101 
102  assert(isConsistent());
103  }
104 
105  /// Copy constructor.
106  template < class S >
107  explicit DSVectorBase<R>(const SVectorBase<S>& old)
108  : theelem(0)
109  {
110  allocMem(old.size());
112 
113  assert(isConsistent());
114  }
115 
116  /// Copy constructor.
117  /** The redundancy with the copy constructor below is necessary since otherwise the compiler doesn't realize that it
118  * could use the more general one with S = R and generates a shallow copy constructor.
119  */
121  : SVectorBase<R>()
122  , theelem(0)
123  {
124  allocMem(old.size());
126 
127  assert(isConsistent());
128  }
129 
130  /// Copy constructor.
131  template < class S >
133  : SVectorBase<R>()
134  , theelem(0)
135  {
136  allocMem(old.size());
138 
139  assert(isConsistent());
140  }
141 
142  /// Copy constructor.
143  template < class S >
144  explicit DSVectorBase<R>(const VectorBase<S>& vec);
145 
146  /// Copy constructor.
147  template < class S >
148  explicit DSVectorBase<R>(const SSVectorBase<S>& old);
149 
150  /// Assignment operator.
151  template < class S >
153  {
154  if(this != &vec)
155  {
157  makeMem(vec.size());
159  }
160 
161  return *this;
162  }
163 
164  /// Assignment operator.
166  {
167  if(this != &vec)
168  {
170  makeMem(vec.size());
172  }
173 
174  return *this;
175  }
176 
177  /// Assignment operator.
178  template < class S >
180  {
181  if(this != (DSVectorBase<R>*)(&vec))
182  {
184  makeMem(vec.size());
186  }
187 
188  return *this;
189  }
190 
191  /// Assignment operator.
192  template < class S >
194 
195  /// Assignment operator.
196  template < class S >
198 
199  /// Destructor.
200  virtual ~DSVectorBase<R>()
201  {
202  if(theelem)
203  {
204  for(int i = SVectorBase<R>::max() - 1; i >= 0; i--)
205  theelem[i].~Nonzero<R>();
206 
207  spx_free(theelem);
208  }
209  }
210 
211  ///@}
212 
213  // ------------------------------------------------------------------------------------------------------------------
214  /**@name Modification */
215  ///@{
216 
217  /// Append nonzeros of \p sv.
218  template < class S >
219  void add(const SVectorBase<S>& vec)
220  {
222  makeMem(vec.size());
223  SVectorBase<S>::add(vec);
224  }
225 
226  /// Append one nonzero \p (i,v).
227  void add(int i, const R& v)
228  {
229  makeMem(1);
230  SVectorBase<R>::add(i, v);
231  }
232 
233  /// Append one uninitialized nonzero.
234  void add(int i)
235  {
236  makeMem(1);
238  }
239 
240  /// Append \p n nonzeros.
241  void add(int n, const int i[], const R v[])
242  {
243  makeMem(n);
244  SVectorBase<R>::add(n, i, v);
245  }
246 
247  /// Reset nonzero memory to >= \p newmax.
248  /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
249  * reset to size() only.
250  */
251  void setMax(int newmax = 1)
252  {
253  int siz = SVectorBase<R>::size();
254  int len = (newmax < siz) ? siz : newmax;
255 
256  if(len == SVectorBase<R>::max())
257  return;
258 
259  Nonzero<R>* newmem = 0;
260 
261  /* allocate new memory */
262  spx_alloc(newmem, len);
263 
264  /* call copy constructor for first elements */
265  int i;
266 
267  for(i = 0; i < siz; i++)
268  new((&newmem[i])) Nonzero<R>(theelem[i]);
269 
270  /* call default constructor for remaining elements */
271  for(; i < len; i++)
272  new((&newmem[i])) Nonzero<R>();
273 
274  /* free old memory */
275  for(i = SVectorBase<R>::max() - 1; i >= 0; i--)
276  theelem[i].~Nonzero<R>();
277 
278  if(theelem != 0)
279  spx_free(theelem);
280 
281  /* assign new memory */
282  theelem = newmem;
283  SVectorBase<R>::setMem(len, theelem);
285  }
286 
287  ///@}
288 
289  // ------------------------------------------------------------------------------------------------------------------
290  /**@name Utilities */
291  ///@{
292 
293  /// Consistency check.
294  bool isConsistent() const
295  {
296 #ifdef ENABLE_CONSISTENCY_CHECKS
297 
298  if(theelem != 0 && SVectorBase<R>::mem() != theelem)
299  return MSGinconsistent("DSVectorBase");
300 
301 #endif
302 
303  return true;
304  }
305 
306  ///@}
307 };
308 
309 
310 
311 /// Allocate memory for \p n nonzeros (specialization for Real).
312 template<>
313 inline
315 {
316  spx_alloc(theelem, n);
318 }
319 
320 
321 
322 /// Destructor (specialization for Real).
323 template<>
324 inline
326 {
327  if(theelem)
328  spx_free(theelem);
329 }
330 
331 
332 
333 /// Reset nonzero memory to >= \p newmax.
334 /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
335  * reset to size() only (specialization for Real).
336  */
337 template<>
338 inline
340 {
341  int siz = size();
342  int len = (newmax < siz) ? siz : newmax;
343 
344  spx_realloc(theelem, len);
345  setMem(len, theelem);
346  // reset 'size' to old size since the above call to setMem() sets 'size' to 0
347  set_size(siz);
348 }
349 } // namespace soplex
350 #endif // _DSVECTORBASE_H_
void makeMem(int n)
Ensure there is room for n new nonzeros.
Definition: dsvectorbase.h:74
Sparse vector nonzero element.
Definition: svectorbase.h:37
DSVectorBase< R > & operator=(const DSVectorBase< R > &vec)
Assignment operator.
Definition: dsvectorbase.h:165
DSVectorBase< R > & operator=(const SVectorBase< S > &vec)
Assignment operator.
Definition: dsvectorbase.h:152
SVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: basevectors.h:942
void allocMem(int n)
Allocate memory for n nonzeros.
Definition: dsvectorbase.h:63
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:28
void add(int n, const int i[], const R v[])
Append n nonzeros.
Definition: dsvectorbase.h:241
void setMem(int n, Nonzero< R > *elmem)
Set the memory area where the nonzeros will be stored.
Definition: svectorbase.h:804
int size() const
Number of used indices.
Definition: svectorbase.h:155
void set_size(int s)
Set size of the vector.
Definition: svectorbase.h:790
virtual ~DSVectorBase()
Destructor.
Definition: dsvectorbase.h:200
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:43
Sparse Linear Solver virtual base class.Class SLinSolver provides a class for solving sparse linear s...
Definition: dsvectorbase.h:30
Semi sparse vector.This class implements semi-sparse vectors. Such are VectorBases where the indices ...
Definition: dsvectorbase.h:29
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:219
Nonzero< R > * theelem
Memory.
Definition: dsvectorbase.h:54
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:49
void add(int i, const R &v)
Append one nonzero (i,v).
Definition: dsvectorbase.h:227
void add(int i)
Append one uninitialized nonzero.
Definition: dsvectorbase.h:234
bool isConsistent() const
Consistency check.
Definition: dsvectorbase.h:294
DSVectorBase< R > & operator=(const DSVectorBase< S > &vec)
Assignment operator.
Definition: dsvectorbase.h:179
void add(int i, const R &v)
Append one nonzero (i,v).
Definition: svectorbase.h:273
void spx_realloc(T &p, int n)
Change amount of allocated memory.
Definition: spxalloc.h:80
Everything should be within this namespace.
void clear()
Remove all indices.
Definition: svectorbase.h:434
Sparse vectors.
Sparse vectors.Class SVectorBase provides packed sparse vectors. Such are a sparse vectors...
Definition: ssvectorbase.h:34
#define MSGinconsistent(name)
Definition: spxdefines.h:164
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:111
void setMax(int newmax=1)
Reset nonzero memory to >= newmax.
Definition: dsvectorbase.h:251