SoPlex Doxygen Documentation
dvector.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-2012 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 dvector.h
17  * @brief Dynamic vectors.
18  */
19 #ifndef _DVECTOR_H_
20 #define _DVECTOR_H_
21 
22 #include <iostream>
23 #include <assert.h>
24 
25 #include "spxdefines.h"
26 #include "vector.h"
27 #include "svector.h"
28 
29 namespace soplex
30 {
31 class Vector_exact;
32 
33 /**@brief Dynamic vectors.
34  @ingroup Algebra
35 
36  Class DVector is a derived class of Vector adding automatic
37  memory management to such objects. This allows to implement maths
38  operations operator+() and operator-(). Further, it is possible
39  to reset the dimension of a DVector via method reDim(). However,
40  this may render all references to values of a #reDim()%ed DVector
41  invalid.
42 
43  For vectors that are often subject to reDim() it may be
44  unconvenient to reallocate the required memory every
45  time. Instead, an array of values of length memSize() is kept,
46  where only the first dim() elements are used. Initially,
47  memSize() == dim(). However, if the dimension is increased,
48  memSize() will be increased at least by a factor of 1.2 to be
49  prepared for future (small) #reDim()%s. Finally, one can explicitly
50  set memSize() with method reSize(), but not lower than
51  dim().
52 */
53 class DVector : public Vector
54 {
55  //-----------------------------------
56  /**@name Data */
57  //@{
58  int memsize; ///< length of array of values \ref soplex::DVector::mem "mem"
59  Real* mem; ///< value array to be used
60  //@}
61 
62 public:
63 
64  //-----------------------------------------
65  /**@name Arithmetic operations (friends) */
66  //@{
67  /// adding vectors.
68  friend DVector operator+(const Vector& v, const Vector& w);
69  /// adding vectors.
70  friend DVector operator+(const SVector& v, const Vector& w);
71  /// adding vectors.
72  friend DVector operator+(const Vector& v, const SVector& w);
73 
74  /// subtracting vectors.
75  friend DVector operator-(const Vector& v, const Vector& w);
76  /// subtracting vectors.
77  friend DVector operator-(const SVector& v, const Vector& w);
78  /// subtracting vectors.
79  friend DVector operator-(const Vector& v, const SVector& w);
80 
81  /// negation operator.
82  friend DVector operator-(const Vector& vec);
83  /// negation operator.
84  friend DVector operator-(const SVector& vec);
85 
86  /// scaling vectors with a real number.
87  friend DVector operator*(const Vector& v, Real x);
88  /// scaling vectors with a real number.
89  friend DVector operator*(Real x, const Vector& v);
90  //@}
91 
92  //-----------------------------------
93  /**@name I/O */
94  //@{
95  /// output operator.
96  friend std::istream& operator>>(std::istream& s, DVector& vec);
97  //@}
98 
99 #if 0
100  //--------------------------------------------------
101  /**@name Arithmetic operations (member functions) */
102  //@{
103  /**@todo Do we really have to reimplement the following operators
104  * (inheritance from Vector?). All the more as these methods are
105  * not virtual.
106  */
107  /// adds \p vec to %vector.
108  DVector& operator+=(const Vector& vec)
109  {
110  Vector::operator+=(vec);
111  return *this;
112  }
113  /// adds \p vec to %vector.
114  DVector& operator+=(const SVector& vec)
115  {
116  Vector::operator+=(vec);
117  return *this;
118  }
119 
120  /// subtracts \p vec from %vector.
121  DVector& operator-=(const Vector& vec)
122  {
123  Vector::operator-=(vec);
124  return *this;
125  }
126  /// subtracts \p vec from %vector.
127  DVector& operator-=(const SVector& vec)
128  {
129  Vector::operator-=(vec);
130  return *this;
131  }
132 
133  /// scales vector with factor \p x
135  {
137  return *this;
138  }
139  //@}
140 #endif
141 
142  //--------------------------------------------------
143  /**@name Access */
144  //@{
145  /// resets \ref soplex::DVector "DVector"'s dimension to \p newdim.
146  void reDim(int newdim);
147 
148  /// resets \ref soplex::DVector "DVector"'s memory size to \p newsize.
149  void reSize(int newsize);
150 
151  /// resets \ref soplex::DVector "DVector"'s memory size to \p newsize and dimension to \p newdim.
152  void reSize(int newsize, int newdim);
153 
154  /// returns \ref soplex::DVector "DVector"'s memory size.
155  int memSize() const
156  {
157  return memsize;
158  }
159 
160  /// consistency check.
161  bool isConsistent() const;
162  //@}
163 
164  //--------------------------------------------------
165  /**@name Construction / destruction */
166  //@{
167  /// default constructor. \p dim is the initial dimension.
168  explicit
169  DVector(int dim = 0);
170  /// copy constructor.
171  explicit DVector(const Vector& old);
172  /// cast from exact vector.
173  explicit DVector(const Vector_exact& old);
174  /// copy constructor.
175  DVector(const DVector& old);
176  /// assignment operator.
177  DVector& operator=(const Vector& vec)
178  {
179  if (vec.dim() != dim())
180  reDim(vec.dim());
181  Vector::operator=(vec);
182 
183  assert(isConsistent());
184 
185  return *this;
186  }
187  /// assignment operator.
188  DVector& operator=(const DVector& vec)
189  {
190  if (this != &vec)
191  {
192  if (vec.dim() != dim())
193  reDim(vec.dim());
194  Vector::operator=(vec);
195 
196  assert(isConsistent());
197  }
198  return *this;
199  }
200  /// assingment operator.
201  DVector& operator=(const SVector& vec)
202  {
203  if (vec.dim() != dim())
204  reDim(vec.dim());
205  Vector::operator=(vec);
206 
207  assert(isConsistent());
208 
209  return *this;
210  }
211 
212  /// destructor.
213  ~DVector();
214 };
215 } // namespace soplex
216 #endif // _DVECTOR_H_
217 
218 //-----------------------------------------------------------------------------
219 //Emacs Local Variables:
220 //Emacs mode:c++
221 //Emacs c-basic-offset:3
222 //Emacs tab-width:8
223 //Emacs indent-tabs-mode:nil
224 //Emacs End:
225 //-----------------------------------------------------------------------------