All Classes Namespaces Functions Variables Typedefs Enumerations Pages
var_row_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_row_vector_h
23 #define var_row_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 RowVector<Var,AT> : public Matrix<General,Fixed<1>,Var,AT> {
40  using Matrix<General,Fixed<1>,Var,AT>::N;
41  using Matrix<General,Fixed<1>,Var,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 RowVector<Row,AT> &x);
55 
57 
58  public:
59 
64  RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
65  }
66 
67 // template<class Ini=All<AT> >
68 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
69 
70  RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
71  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
72 
85  RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
86  }
87 
96  }
97 
98  template<class Row>
99  RowVector(const RowVector<Row,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
100  }
101 
102  template<class Type, class Row, class Col>
103  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
104  }
105 
106  RowVector<Var,AT>& resize() {
107  Matrix<General,Fixed<1>,Var,AT>::resize();
108  return *this;
109  }
110 
111  RowVector<Var,AT>& resize(int n, Noinit) {
112  Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
113  return *this;
114  }
115 
116  RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
117  Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
118  return *this;
119  }
120 
127  inline RowVector<Var,AT>& operator=(const RowVector<Var,AT> &x);
128 
129  template <class Row>
130  inline RowVector<Var,AT>& operator=(const RowVector<Row,AT> &x);
131 
138  template <class Row>
139  inline RowVector<Var,AT>& operator<<(const RowVector<Row,AT> &x);
140 
151  AT& operator()(int i) {
152 
153 #ifndef FMATVEC_NO_BOUNDS_CHECK
154  assert(i>=0);
155  assert(i<N);
156 #endif
157 
158  return e(i);
159  };
160 
165  const AT& operator()(int i) const {
166 
167 #ifndef FMATVEC_NO_BOUNDS_CHECK
168  assert(i>=0);
169  assert(i<N);
170 #endif
171 
172  return e(i);
173  };
174 
175  iterator begin() { return &ele[0]; }
176  iterator end() { return &ele[N]; }
177  const_iterator begin() const { return &ele[0]; }
178  const_iterator end() const { return &ele[N]; }
179  const_iterator cbegin() const noexcept { return &ele[0]; }
180  const_iterator cend() const noexcept { return &ele[N]; }
181 
182  AT& e(int i) {
183  return ele[i];
184  };
185 
190  const AT& e(int i) const {
191  return ele[i];
192  };
193 
201  inline RowVector<Var,AT>& init(const AT& val=0);
202  inline RowVector<Var,AT>& init(Init, const AT &a=0) { return init(a); }
203  inline RowVector<Var,AT>& init(Noinit, const AT &a=0) { return *this; }
204 
209  int size() const {return N;};
210 
217  int inc() const {return 1;};
218 
219  using Matrix<General,Fixed<1>,Var,AT>::operator();
220 
225  inline operator std::vector<AT>() const;
226 
231  inline RowVector(std::vector<AT> v);
232 
233  inline const Vector<Var,AT> T() const;
234 
235  inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
236 
237  template <class Row>
238  inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
239 
240  template <class Row>
241  inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
242  };
243 
244  template <class AT>
246  for(int i=0; i<N; i++)
247  e(i) = val;
248  return *this;
249  }
250 
251  template <class AT>
253 
254  if(!ele) {
255  delete[] ele;
256  N = x.size();
257  ele = new AT[N];
258  } else {
259 #ifndef FMATVEC_NO_SIZE_CHECK
260  assert(N == x.size());
261 #endif
262  }
263 
264  deepCopy(x);
265 
266  return *this;
267  }
268 
269  template <class AT> template<class Row>
271 
272  if(!ele) {
273  delete[] ele;
274  N = x.size();
275  ele = new AT[N];
276  } else {
277 #ifndef FMATVEC_NO_SIZE_CHECK
278  assert(N == x.size());
279 #endif
280  }
281 
282  deepCopy(x);
283 
284  return *this;
285  }
286 
287  template <class AT> template<class Row>
289 
290  if(N!=x.size()) {
291  delete[] ele;
292  N = x.size();
293  ele = new AT[N];
294  }
295 
296  deepCopy(x);
297 
298  return *this;
299  }
300 
301  template <class AT>
302  inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
303  Vector<Var,AT> x(N,NONINIT);
304  for(int i=0; i<N; i++)
305  x.e(i) = e(i);
306  return x;
307  }
308 
309  template <class AT>
310  inline RowVector<Var,AT>::operator std::vector<AT>() const {
311  std::vector<AT> ret(size());
312  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
313  return ret;
314  }
315 
316  template <class AT>
317  inline RowVector<Var,AT>::RowVector(std::vector<AT> v) : Matrix<General,Fixed<1>,Var,AT>(v.size(),1) {
318  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
319  }
320 
321  template <class AT>
322  inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
323 #ifndef FMATVEC_NO_BOUNDS_CHECK
324  assert(I.end()<N);
325 #endif
326  RowVector<Var,AT> x(I.end()-I.start()+1,NONINIT);
327 
328  for(int i=0; i<x.size(); i++)
329  x.e(i) = e(I.start()+i);
330 
331  return x;
332  }
333 
334  template <class AT> template <class Row>
335  inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
336 #ifndef FMATVEC_NO_BOUNDS_CHECK
337  assert(I.end()<size());
338  assert(I.size()==x.size());
339 #endif
340 
341  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
342  e(i) = x.e(ii);
343  }
344 
345  template <class AT> template <class Row>
346  inline void RowVector<Var,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
347 #ifndef FMATVEC_NO_BOUNDS_CHECK
348  assert(I.end()<size());
349  assert(I.size()==x.size());
350 #endif
351 
352  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
353  e(i) += x.e(ii);
354  }
355 
357 
358  template <class AT> template <class Row>
359  inline void RowVector<Var,AT>::deepCopy(const RowVector<Row,AT> &x) {
360  for(int i=0; i<N; i++)
361  e(i) = x.e(i);
362  }
363 
365 
366 }
367 
368 #endif
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:165
Definition: fmatvec.h:38
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:85
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
RowVector()
Standard constructor.
Definition: var_row_vector.h:64
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
AT & operator()(int i)
Element operator.
Definition: var_row_vector.h:151
Definition: matrix.h:43
Definition: types.h:65
Definition: fmatvec.h:50
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:95
Definition: matrix.h:42
int size() const
Size.
Definition: var_row_vector.h:209
Basic shape class for matrices.
Definition: types.h:100
const AT & e(int i) const
Element operator.
Definition: var_row_vector.h:190
int end() const
Last element.
Definition: range.h:95
int inc() const
Increment.
Definition: var_row_vector.h:217
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