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