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-2017 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 "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  for( int i = 0; i < n; i++ )
66  new (&(theelem[i])) Nonzero<R>();
67  SVectorBase<R>::setMem(n, theelem);
68  }
69 
70  /// Ensure there is room for \p n new nonzeros.
71  void makeMem(int n)
72  {
73  assert(n >= 0);
74 
76  {
77  assert(SVectorBase<R>::size() + n > 0);
79  }
80  }
81 
82  //@}
83 
84 public:
85 
86  // ------------------------------------------------------------------------------------------------------------------
87  /**@name Construction, assignment, and destruction */
88  //@{
89 
90  /// Default constructor.
91  /** Creates a DSVectorBase ready to hold \p n nonzeros. However, the memory is automatically enlarged, if more
92  * nonzeros are added to the DSVectorBase.
93  */
94  explicit DSVectorBase<R>(int n = 8)
95  : theelem(0)
96  {
97  allocMem((n < 1) ? 2 : n);
98 
99  assert(isConsistent());
100  }
101 
102  /// Copy constructor.
103  template < class S >
104  explicit DSVectorBase<R>(const SVectorBase<S>& old)
105  : theelem(0)
106  {
107  allocMem(old.size());
109 
110  assert(isConsistent());
111  }
112 
113  /// Copy constructor.
114  /** The redundancy with the copy constructor below is necessary since otherwise the compiler doesn't realize that it
115  * could use the more general one with S = R and generates a shallow copy constructor.
116  */
118  : SVectorBase<R>()
119  , theelem(0)
120  {
121  allocMem(old.size());
123 
124  assert(isConsistent());
125  }
126 
127  /// Copy constructor.
128  template < class S >
130  : SVectorBase<R>()
131  , theelem(0)
132  {
133  allocMem(old.size());
135 
136  assert(isConsistent());
137  }
138 
139  /// Copy constructor.
140  template < class S >
141  explicit DSVectorBase<R>(const VectorBase<S>& vec);
142 
143  /// Copy constructor.
144  template < class S >
145  explicit DSVectorBase<R>(const SSVectorBase<S>& old);
146 
147  /// Assignment operator.
148  template < class S >
150  {
151  if( this != &vec )
152  {
154  makeMem(vec.size());
156  }
157 
158  return *this;
159  }
160 
161  /// Assignment operator.
163  {
164  if( this != &vec )
165  {
167  makeMem(vec.size());
169  }
170 
171  return *this;
172  }
173 
174  /// Assignment operator.
175  template < class S >
177  {
178  if( this != (DSVectorBase<R>*)(&vec) )
179  {
181  makeMem(vec.size());
183  }
184 
185  return *this;
186  }
187 
188  /// Assignment operator.
189  template < class S >
191 
192  /// Assignment operator.
193  template < class S >
195 
196  /// Destructor.
197  virtual ~DSVectorBase<R>()
198  {
199  if( theelem )
200  {
201  for( int i = SVectorBase<R>::max() - 1; i >= 0; i-- )
202  theelem[i].~Nonzero<R>();
203 
204  spx_free(theelem);
205  }
206  }
207 
208  //@}
209 
210  // ------------------------------------------------------------------------------------------------------------------
211  /**@name Modification */
212  //@{
213 
214  /// Append nonzeros of \p sv.
215  template < class S >
216  void add(const SVectorBase<S>& vec)
217  {
219  makeMem(vec.size());
220  SVectorBase<S>::add(vec);
221  }
222 
223  /// Append one nonzero \p (i,v).
224  void add(int i, const R& v)
225  {
226  makeMem(1);
227  SVectorBase<R>::add(i, v);
228  }
229 
230  /// Append one uninitialized nonzero.
231  void add(int i)
232  {
233  makeMem(1);
235  }
236 
237  /// Append \p n nonzeros.
238  void add(int n, const int i[], const R v[])
239  {
240  makeMem(n);
241  SVectorBase<R>::add(n, i, v);
242  }
243 
244  /// Reset nonzero memory to >= \p newmax.
245  /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
246  * reset to size() only.
247  */
248  void setMax(int newmax = 1)
249  {
250  int siz = SVectorBase<R>::size();
251  int len = (newmax < siz) ? siz : newmax;
252 
253  if( len == SVectorBase<R>::max() )
254  return;
255 
256  Nonzero<R>* newmem = 0;
257 
258  /* allocate new memory */
259  spx_alloc(newmem, len);
260 
261  /* call copy constructor for first elements */
262  int i;
263  for( i = 0; i < siz; i++ )
264  new ((&newmem[i])) Nonzero<R>(theelem[i]);
265 
266  /* call default constructor for remaining elements */
267  for( ; i < len; i++ )
268  new ((&newmem[i])) Nonzero<R>();
269 
270  /* free old memory */
271  for( i = SVectorBase<R>::max()-1; i >= 0; i-- )
272  theelem[i].~Nonzero<R>();
273 
274  if( theelem != 0 )
275  spx_free(theelem);
276 
277  /* assign new memory */
278  theelem = newmem;
279  SVectorBase<R>::setMem(len, theelem);
281  }
282 
283  //@}
284 
285  // ------------------------------------------------------------------------------------------------------------------
286  /**@name Utilities */
287  //@{
288 
289  /// Consistency check.
290  bool isConsistent() const
291  {
292 #ifdef ENABLE_CONSISTENCY_CHECKS
293  if( theelem != 0 && SVectorBase<R>::mem() != theelem )
294  return MSGinconsistent("DSVectorBase");
295 #endif
296 
297  return true;
298  }
299 
300  //@}
301 };
302 
303 
304 
305 /// Allocate memory for \p n nonzeros (specialization for Real).
306 template<>
307 inline
309 {
310  spx_alloc(theelem, n);
312 }
313 
314 
315 
316 /// Destructor (specialization for Real).
317 template<>
318 inline
320 {
321  if( theelem )
322  spx_free(theelem);
323 }
324 
325 
326 
327 /// Reset nonzero memory to >= \p newmax.
328 /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is
329  * reset to size() only (specialization for Real).
330  */
331 template<>
332 inline
334 {
335  int siz = size();
336  int len = (newmax < siz) ? siz : newmax;
337 
338  spx_realloc(theelem, len);
339  setMem(len, theelem);
340  // reset 'size' to old size since the above call to setMem() sets 'size' to 0
341  set_size( siz );
342 }
343 } // namespace soplex
344 #endif // _DSVECTORBASE_H_
void makeMem(int n)
Ensure there is room for n new nonzeros.
Definition: dsvectorbase.h:71
Sparse vector nonzero element.
Definition: svectorbase.h:35
DSVectorBase< R > & operator=(const DSVectorBase< R > &vec)
Assignment operator.
Definition: dsvectorbase.h:162
DSVectorBase< R > & operator=(const SVectorBase< S > &vec)
Assignment operator.
Definition: dsvectorbase.h:149
SVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: basevectors.h:969
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:238
void setMem(int n, Nonzero< R > *elmem)
Set the memory area where the nonzeros will be stored.
Definition: svectorbase.h:762
int size() const
Number of used indices.
Definition: svectorbase.h:152
void set_size(int s)
Set size of the vector.
Definition: svectorbase.h:748
virtual ~DSVectorBase()
Destructor.
Definition: dsvectorbase.h:197
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:216
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:224
void add(int i)
Append one uninitialized nonzero.
Definition: dsvectorbase.h:231
bool isConsistent() const
Consistency check.
Definition: dsvectorbase.h:290
DSVectorBase< R > & operator=(const DSVectorBase< S > &vec)
Assignment operator.
Definition: dsvectorbase.h:176
void add(int i, const R &v)
Append one nonzero (i,v).
Definition: svectorbase.h:270
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:422
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:109
void setMax(int newmax=1)
Reset nonzero memory to >= newmax.
Definition: dsvectorbase.h:248