All Classes Namespaces Functions Variables Typedefs Enumerations Pages
var_vector.h
1 /* Copyright (C) 2003-2012 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef var_vector_h
23 #define var_vector_h
24 
25 #include "var_general_matrix.h"
26 #include <vector>
27 #include <cstring>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Vector<Var,AT> : public Matrix<General,Var,Fixed<1>,AT> {
40  using Matrix<General,Var,Fixed<1>,AT>::M;
41  using Matrix<General,Var,Fixed<1>,AT>::ele;
42 
43  public:
44 
45  typedef AT* iterator;
46  typedef const AT* const_iterator;
47 
48  typedef AT value_type;
49 
51 
52  protected:
53 
54  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
55 
57 
58  public:
59 
64  Vector() : Matrix<General,Var,Fixed<1>,AT>() { }
65 
66 // template<class Ini=All<AT> >
67 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
68 
69  Vector(int m, Noinit ini) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
70  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Var,Fixed<1>,AT>(m,ini,a) { }
71 
84  Vector(const char *str) : Matrix<General,Var,Fixed<1>,AT>(str) {
85  }
86 
94  Vector(const Vector<Var,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
95  }
96 
97  template<class Row>
98  Vector(const Vector<Row,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
99  }
100 
101  template<class Type, class Row, class Col>
102  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
103  }
104 
105  Vector<Var,AT>& resize() {
106  Matrix<General,Var,Fixed<1>,AT>::resize();
107  return *this;
108  }
109 
110  Vector<Var,AT>& resize(int m, Noinit) {
111  Matrix<General,Var,Fixed<1>,AT>::resize(m,Noinit());
112  return *this;
113  }
114 
115  Vector<Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
116  Matrix<General,Var,Fixed<1>,AT>::resize(m,ini,a);
117  return *this;
118  }
119 
126  inline Vector<Var,AT>& operator=(const Vector<Var,AT> &x);
127 
128  template <class Row>
129  inline Vector<Var,AT>& operator=(const Vector<Row,AT> &x);
130 
137  template <class Row>
138  inline Vector<Var,AT>& operator<<(const Vector<Row,AT> &x);
139 
150  AT& operator()(int i) {
151 
152 #ifndef FMATVEC_NO_BOUNDS_CHECK
153  assert(i>=0);
154  assert(i<M);
155 #endif
156 
157  return e(i);
158  };
159 
164  const AT& operator()(int i) const {
165 
166 #ifndef FMATVEC_NO_BOUNDS_CHECK
167  assert(i>=0);
168  assert(i<M);
169 #endif
170 
171  return e(i);
172  };
173 
174  iterator begin() { return &ele[0]; }
175  iterator end() { return &ele[M]; }
176  const_iterator begin() const { return &ele[0]; }
177  const_iterator end() const { return &ele[M]; }
178  const_iterator cbegin() const noexcept { return &ele[0]; }
179  const_iterator cend() const noexcept { return &ele[M]; }
180 
181  AT& e(int i) {
182  return ele[i];
183  };
184 
189  const AT& e(int i) const {
190  return ele[i];
191  };
192 
200  inline Vector<Var,AT>& init(const AT& val=0);
201  inline Vector<Var,AT>& init(Init, const AT& a=0) { return init(a); }
202  inline Vector<Var,AT>& init(Noinit, const AT& a=0) { return *this; }
203 
208  int size() const {return M;};
209 
216  int inc() const {return 1;};
217 
218  using Matrix<General,Var,Fixed<1>,AT>::operator();
219 
224  inline operator std::vector<AT>() const;
225 
230  inline Vector(std::vector<AT> v);
231 
232  inline const RowVector<Var,AT> T() const;
233 
234  inline const Vector<Var,AT> operator()(const Range<Var,Var> &I) const;
235 
236  template <class Row>
237  inline void set(const Range<Var,Var> &I, const Vector<Row,AT> &x);
238 
239  template <class Row>
240  inline void add(const Range<Var,Var> &I, const Vector<Row,AT> &x);
241  };
242 
243  template <class AT>
244  inline Vector<Var,AT>& Vector<Var,AT>::init(const AT &val) {
245  for(int i=0; i<M; i++)
246  e(i) = val;
247  return *this;
248  }
249 
250  template <class AT>
252 
253  if(!ele) {
254  delete[] ele;
255  M = x.size();
256  ele = new AT[M];
257  } else {
258 #ifndef FMATVEC_NO_SIZE_CHECK
259  assert(M == x.size());
260 #endif
261  }
262 
263  deepCopy(x);
264 
265  return *this;
266  }
267 
268  template <class AT> template<class Row>
270 
271  if(!ele) {
272  delete[] ele;
273  M = x.size();
274  ele = new AT[M];
275  } else {
276 #ifndef FMATVEC_NO_SIZE_CHECK
277  assert(M == x.size());
278 #endif
279  }
280 
281  deepCopy(x);
282 
283  return *this;
284  }
285 
286  template <class AT> template<class Row>
288 
289  if(M!=x.size()) {
290  delete[] ele;
291  M = x.size();
292  ele = new AT[M];
293  }
294 
295  deepCopy(x);
296 
297  return *this;
298  }
299 
300  template <class AT>
301  inline const RowVector<Var,AT> Vector<Var,AT>::T() const {
302  RowVector<Var,AT> x(M,NONINIT);
303  for(int i=0; i<M; i++)
304  x.e(i) = e(i);
305  return x;
306  }
307 
308  template <class AT>
309  inline Vector<Var,AT>::operator std::vector<AT>() const {
310  std::vector<AT> ret(size());
311  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
312  return ret;
313  }
314 
315  template <class AT>
316  inline Vector<Var,AT>::Vector(std::vector<AT> v) : Matrix<General,Var,Fixed<1>,AT>(v.size(),1) {
317  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
318  }
319 
320  template <class AT>
321  inline const Vector<Var,AT> Vector<Var,AT>::operator()(const Range<Var,Var> &I) const {
322 #ifndef FMATVEC_NO_BOUNDS_CHECK
323  assert(I.end()<M);
324 #endif
325  Vector<Var,AT> x(I.end()-I.start()+1,NONINIT);
326 
327  for(int i=0; i<x.size(); i++)
328  x.e(i) = e(I.start()+i);
329 
330  return x;
331  }
332 
333  template <class AT> template <class Row>
334  inline void Vector<Var,AT>::set(const Range<Var,Var> &I, const Vector<Row,AT> &x) {
335 #ifndef FMATVEC_NO_BOUNDS_CHECK
336  assert(I.end()<size());
337  assert(I.size()==x.size());
338 #endif
339 
340  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
341  e(i) = x.e(ii);
342  }
343 
344  template <class AT> template <class Row>
345  inline void Vector<Var,AT>::add(const Range<Var,Var> &I, const Vector<Row,AT> &x) {
346 #ifndef FMATVEC_NO_BOUNDS_CHECK
347  assert(I.end()<size());
348  assert(I.size()==x.size());
349 #endif
350 
351  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
352  e(i) += x.e(ii);
353  }
354 
356 
357  template <class AT> template <class Row>
358  inline void Vector<Var,AT>::deepCopy(const Vector<Row,AT> &x) {
359  for(int i=0; i<M; i++)
360  e(i) = x.e(i);
361  }
362 
364 
365 }
366 
367 #endif
Vector()
Standard constructor.
Definition: var_vector.h:64
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
Definition: matrix.h:43
Definition: types.h:65
Vector(const char *str)
String Constructor.
Definition: var_vector.h:84
const AT & operator()(int i) const
Element operator.
Definition: var_vector.h:164
int inc() const
Increment.
Definition: var_vector.h:216
Definition: fmatvec.h:47
const AT & e(int i) const
Element operator.
Definition: var_vector.h:189
Definition: matrix.h:42
Basic shape class for matrices.
Definition: types.h:100
int size() const
Size.
Definition: var_vector.h:208
Vector(const Vector< Var, AT > &x)
Copy Constructor.
Definition: var_vector.h:94
int end() const
Last element.
Definition: range.h:95
AT & operator()(int i)
Element operator.
Definition: var_vector.h:150
This is an index class for creating submatrices.
Definition: range.h:44
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
int start() const
First element.
Definition: range.h:89

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home