Scippy

SoPlex

Sequential object-oriented simPlex

dvectorbase.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 dvectorbase.h
17  * @brief Dynamic dense vectors.
18  */
19 #ifndef _DVECTORBASE_H_
20 #define _DVECTORBASE_H_
21 
22 #include <iostream>
23 #include <assert.h>
24 
25 #include "soplex/spxdefines.h"
26 #include "soplex/spxalloc.h"
27 #include "soplex/vectorbase.h"
28 
29 namespace soplex
30 {
31 template < class R > class SVectorBase;
32 
33 /**@brief Dynamic dense vectors.
34  * @ingroup Algebra
35  *
36  * Class DVectorBase is a derived class of VectorBase adding automatic memory management to such objects. This allows
37  * to implement maths operations operator+() and operator-(). Further, it is possible to reset the dimension of a
38  * DVectorBase via method reDim(). However, this may render all references to values of a #reDim()%ed DVectorBase
39  * invalid.
40  *
41  * For vectors that are often subject to reDim() it may be unconvenient to reallocate the required memory every time.
42  * Instead, an array of values of length memSize() is kept, where only the first dim() elements are used. Initially,
43  * memSize() == dim(). However, if the dimension is increased, memSize() will be increased at least by a factor of 1.2
44  * to be prepared for future (small) #reDim()%s. Finally, one can explicitly set memSize() with method reSize(), but
45  * not lower than dim().
46  */
47 template < class R >
48 class DVectorBase : public VectorBase<R>
49 {
50  template < class S > friend class DVectorBase;
51 
52 private:
53 
54  // ------------------------------------------------------------------------------------------------------------------
55  /**@name Data */
56  //@{
57 
58  /// Length of array of values \ref soplex::DVectorBase::mem "mem"
59  int memsize;
60 
61  /// Array of values.
62  R* mem;
63 
64  //@}
65 
66 public:
67 
68  // ------------------------------------------------------------------------------------------------------------------
69  /**@name Construction, destruction, and assignment */
70  //@{
71 
72  /// Default constructor. \p d is the initial dimension.
73  explicit DVectorBase<R>(int d = 0)
74  : VectorBase<R>(0, 0)
75  , mem(0)
76  {
77  memsize = (d > 0) ? d : 4;
78 
79  spx_alloc(mem, memsize);
80 
81  for(int i = 0; i < memsize; i++)
82  new(&(mem[i])) R();
83 
86 
87  assert(isConsistent());
88  }
89 
90  /// Copy constructor.
91  template < class S >
92  explicit DVectorBase<R>(const VectorBase<S>& old)
93  : VectorBase<R>(0, 0)
94  , mem(0)
95  {
96  VectorBase<R>::dimen = old.dim();
97  memsize = VectorBase<R>::dimen;
98 
99  spx_alloc(mem, memsize);
100 
101  for(int i = 0; i < VectorBase<R>::dimen; i++)
102  new(&(mem[i])) R(old[i]);
103 
104  for(int i = VectorBase<R>::dimen; i < memsize; i++)
105  new(&(mem[i])) R();
106 
108 
109  assert(isConsistent());
110  }
111 
112  /// Copy constructor.
113  /** The redundancy with the copy constructor below is necessary since otherwise the compiler doesn't realize that it
114  * could use the more general one with S = R and generates a shallow copy constructor.
115  */
117  : VectorBase<R>(0, 0)
118  , mem(0)
119  {
120  VectorBase<R>::dimen = old.dim();
121  memsize = old.memsize;
122 
123  spx_alloc(mem, memsize);
124 
125  for(int i = 0; i < VectorBase<R>::dimen; i++)
126  new(&(mem[i])) R(old[i]);
127 
128  for(int i = VectorBase<R>::dimen; i < memsize; i++)
129  new(&(mem[i])) R();
130 
132 
133  assert(isConsistent());
134  }
135 
136  /// Copy constructor.
137  template < class S >
139  : VectorBase<R>(0, 0)
140  , mem(0)
141  {
142  VectorBase<R>::dimen = old.dim();
143  memsize = old.memsize;
144 
145  spx_alloc(mem, memsize);
146 
147  for(int i = 0; i < VectorBase<R>::dimen; i++)
148  new(&(mem[i])) R(old[i]);
149 
150  for(int i = VectorBase<R>::dimen; i < memsize; i++)
151  new(&(mem[i])) R();
152 
154 
155  assert(isConsistent());
156  }
157 
158  /// Assignment operator.
160  {
161  if((VectorBase<R>*)this != &vec)
162  {
163  if(vec.dim() != VectorBase<R>::dim())
164  reDim(vec.dim());
165 
167 
168  assert(isConsistent());
169  }
170 
171  return *this;
172  }
173 
174  /// Assignment operator.
175  template < class S >
177  {
178  if((VectorBase<S>*)this != &vec)
179  {
180  if(vec.dim() != VectorBase<R>::dim())
181  reDim(vec.dim());
182 
184 
185  assert(isConsistent());
186  }
187 
188  return *this;
189  }
190 
191  /// Assignment operator.
193  {
194  if(this != &vec)
195  {
196  if(vec.dim() != VectorBase<R>::dim())
197  reDim(vec.dim());
198 
200 
201  assert(isConsistent());
202  }
203 
204  return *this;
205  }
206 
207  /// Assignment operator.
208  template < class S >
210  {
211  if(this != (const DVectorBase<R>*)&vec)
212  {
213  if(vec.dim() != VectorBase<R>::dim())
214  reDim(vec.dim());
215 
217 
218  assert(isConsistent());
219  }
220 
221  return *this;
222  }
223 
224  /// Assignment operator.
225  template < class S >
227 
228  /// Destructor.
229  virtual ~DVectorBase<R>()
230  {
231  if(mem != 0)
232  {
233  for(int i = memsize - 1; i >= 0; i--)
234  mem[i].~R();
235 
236  spx_free(mem);
237  }
238  }
239 
240  //@}
241 
242  // ------------------------------------------------------------------------------------------------------------------
243  /**@name Access and modification */
244  //@{
245 
246  /// Returns \ref soplex::DVectorBase "DVectorBase"'s memory size.
247  int memSize() const
248  {
249  return memsize;
250  }
251 
252  /// Resets \ref soplex::DVectorBase "DVectorBase"'s dimension to \p newdim.
253  void reDim(int newdim, const bool setZero = true)
254  {
255  assert(memsize >= 0);
256 
257  if(newdim > memsize)
258  reSize(int(newdim + 0.2 * memsize));
259 
260  if(setZero)
261  {
262  for(int i = VectorBase<R>::dimen; i < newdim; i++)
263  mem[i] = 0;
264  }
265 
266  VectorBase<R>::dimen = newdim;
267  }
268 
269  /// Resets \ref soplex::DVectorBase "DVectorBase"'s memory size to \p newsize.
270  void reSize(int newsize)
271  {
272  assert(newsize > VectorBase<R>::dim());
273 
274  /* allocate new memory */
275  R* newmem = 0;
276  spx_alloc(newmem, newsize);
277 
278  /* call copy constructor for first elements */
279  int i;
280 
281  for(i = 0; i < VectorBase<R>::dim(); i++)
282  new(&(newmem[i])) R(mem[i]);
283 
284  /* call default constructor for remaining elements */
285  for(; i < newsize; i++)
286  new(&(newmem[i])) R();
287 
288  /* free old memory */
289  for(i = memsize - 1; i >= 0; i--)
290  mem[i].~R();
291 
292  spx_free(mem);
293 
294  /* assign new memory */
295  mem = newmem;
296  memsize = newsize;
298  }
299 
300  //@}
301 
302  // ------------------------------------------------------------------------------------------------------------------
303  /**@name Utilities */
304  //@{
305 
306  /// Consistency check.
307  bool isConsistent() const
308  {
309 #ifdef ENABLE_CONSISTENCY_CHECKS
310 
311  if(VectorBase<R>::val != mem || VectorBase<R>::dimen > memsize || VectorBase<R>::dimen < 0)
312  return MSGinconsistent("DVectorBase");
313 
315 #else
316  return true;
317 #endif
318  }
319 
320  //@}
321 };
322 
323 
324 
325 /// Resets \ref soplex::DVectorBase "DVectorBase"'s memory size to \p newsize (specialization for Real).
326 template<>
327 inline
328 void DVectorBase<Real>::reSize(int newsize)
329 {
330  assert(newsize >= dim());
331 
332  spx_realloc(mem, newsize);
333 
334  val = mem;
335  memsize = newsize;
336 }
337 
338 
339 
340 /// Default constructor with \p d as the initial dimension (specialization for Real).
341 template<>
342 inline
344  : VectorBase<Real>(0, 0)
345  , mem(0)
346 {
347  memsize = (d > 0) ? d : 4;
348  spx_alloc(mem, memsize);
349  val = mem;
350  dimen = d;
351 
353 }
354 
355 
356 
357 /// Copy constructor (specialization for Real).
358 template<>
359 template<>
360 inline
362  : VectorBase<Real>(0, 0)
363  , mem(0)
364 {
365  dimen = old.dim();
366  memsize = dimen;
367  spx_alloc(mem, memsize);
368  val = mem;
369  *this = old;
370 
372 }
373 
374 
375 
376 /// Copy constructor (specialization for Real).
377 template<>
378 inline
380  : VectorBase<Real>(0, 0)
381  , mem(0)
382 {
383  dimen = old.dim();
384  memsize = old.memsize;
385  spx_alloc(mem, memsize);
386  val = mem;
387  *this = old;
388 
390 }
391 } // namespace soplex
392 #endif // _DVECTORBASE_H_
DVectorBase< R > & operator=(const VectorBase< R > &vec)
Assignment operator.
Definition: dvectorbase.h:159
void reDim(int newdim, const bool setZero=true)
Resets DVectorBase&#39;s dimension to newdim.
Definition: dvectorbase.h:253
DVectorBase< R > & operator=(const DVectorBase< R > &vec)
Assignment operator.
Definition: dvectorbase.h:192
bool isConsistent() const
Consistency check.
Definition: dvectorbase.h:307
VectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: vectorbase.h:112
Memory allocation routines.
Dynamic dense vectors.Class DVectorBase is a derived class of VectorBase adding automatic memory mana...
Definition: dvectorbase.h:48
Dense vector.Class VectorBase provides dense linear algebra vectors. It does not provide memory manag...
Definition: dsvectorbase.h:28
friend class DVectorBase
Definition: dvectorbase.h:50
int memsize
Length of array of values mem.
Definition: dvectorbase.h:59
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:48
R * mem
Array of values.
Definition: dvectorbase.h:62
void reSize(int newsize)
Resets DVectorBase&#39;s memory size to newsize.
Definition: dvectorbase.h:270
double Real
Definition: spxdefines.h:218
DVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: dvectorbase.h:176
R * val
Values of vector.
Definition: vectorbase.h:88
bool isConsistent() const
Consistency check.
Definition: vectorbase.h:461
Debugging, floating point type and parameter definitions.
DVectorBase< R > & operator=(const DVectorBase< S > &vec)
Assignment operator.
Definition: dvectorbase.h:209
void spx_realloc(T &p, int n)
Change amount of allocated memory.
Definition: spxalloc.h:79
int dim() const
Dimension of vector.
Definition: vectorbase.h:217
Everything should be within this namespace.
Dense vector.
int memSize() const
Returns DVectorBase&#39;s memory size.
Definition: dvectorbase.h:247
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