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 AtomicType;
46 
48 
49  protected:
50 
51  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
52 
54 
55  public:
56 
61  Vector() : Matrix<General,Var,Fixed<1>,AT>() { }
62 
63 // template<class Ini=All<AT> >
64 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
65 
66  Vector(int m, Noinit ini) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
67  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Var,Fixed<1>,AT>(m,ini,a) { }
68 
81  Vector(const char *str) : Matrix<General,Var,Fixed<1>,AT>(str) {
82  }
83 
91  Vector(const Vector<Var,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
92  }
93 
94  template<class Row>
95  Vector(const Vector<Row,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
96  }
97 
98  template<class Type, class Row, class Col>
99  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
100  }
101 
102  Vector<Var,AT>& resize() {
103  Matrix<General,Var,Fixed<1>,AT>::resize();
104  return *this;
105  }
106 
107  Vector<Var,AT>& resize(int m, Noinit) {
108  Matrix<General,Var,Fixed<1>,AT>::resize(m,Noinit());
109  return *this;
110  }
111 
112  Vector<Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
113  Matrix<General,Var,Fixed<1>,AT>::resize(m,ini,a);
114  return *this;
115  }
116 
123  inline Vector<Var,AT>& operator=(const Vector<Var,AT> &x);
124 
125  template <class Row>
126  inline Vector<Var,AT>& operator=(const Vector<Row,AT> &x);
127 
134  template <class Row>
135  inline Vector<Var,AT>& operator<<(const Vector<Row,AT> &x);
136 
147  AT& operator()(int i) {
148 
149 #ifndef FMATVEC_NO_BOUNDS_CHECK
150  assert(i>=0);
151  assert(i<M);
152 #endif
153 
154  return e(i);
155  };
156 
161  const AT& operator()(int i) const {
162 
163 #ifndef FMATVEC_NO_BOUNDS_CHECK
164  assert(i>=0);
165  assert(i<M);
166 #endif
167 
168  return e(i);
169  };
170 
171  AT& e(int i) {
172  return ele[i];
173  };
174 
179  const AT& e(int i) const {
180  return ele[i];
181  };
182 
190  inline Vector<Var,AT>& init(const AT& a=0);
191  inline Vector<Var,AT>& init(Init, const AT& a=0) { return init(a); }
192  inline Vector<Var,AT>& init(Noinit, const AT& a=0) { return *this; }
193 
198  int size() const {return M;};
199 
206  int inc() const {return 1;};
207 
208  using Matrix<General,Var,Fixed<1>,AT>::operator();
209 
214  inline operator std::vector<AT>();
215 
220  inline Vector(std::vector<AT> v);
221 
222  inline const RowVector<Var,AT> T() const;
223 
224  inline const Vector<Var,AT> operator()(const Range<Var,Var> &I) const;
225 
226  template <class Row>
227  inline void set(const Range<Var,Var> &I, const Vector<Row,AT> &x);
228 
229  template <class Row>
230  inline void add(const Range<Var,Var> &I, const Vector<Row,AT> &x);
231  };
232 
233  template <class AT>
234  inline Vector<Var,AT>& Vector<Var,AT>::init(const AT &val) {
235  for(int i=0; i<M; i++)
236  e(i) = val;
237  return *this;
238  }
239 
240  template <class AT>
242 
243  if(!ele) {
244  delete[] ele;
245  M = x.size();
246  ele = new AT[M];
247  } else {
248 #ifndef FMATVEC_NO_SIZE_CHECK
249  assert(M == x.size());
250 #endif
251  }
252 
253  deepCopy(x);
254 
255  return *this;
256  }
257 
258  template <class AT> template<class Row>
260 
261  if(!ele) {
262  delete[] ele;
263  M = x.size();
264  ele = new AT[M];
265  } else {
266 #ifndef FMATVEC_NO_SIZE_CHECK
267  assert(M == x.size());
268 #endif
269  }
270 
271  deepCopy(x);
272 
273  return *this;
274  }
275 
276  template <class AT> template<class Row>
278 
279  if(M!=x.size()) {
280  delete[] ele;
281  M = x.size();
282  ele = new AT[M];
283  }
284 
285  deepCopy(x);
286 
287  return *this;
288  }
289 
290  template <class AT>
291  inline const RowVector<Var,AT> Vector<Var,AT>::T() const {
292  RowVector<Var,AT> x(M,NONINIT);
293  for(int i=0; i<M; i++)
294  x.e(i) = e(i);
295  return x;
296  }
297 
298  template <class AT>
299  inline Vector<Var,AT>::operator std::vector<AT>() {
300  std::vector<AT> ret(size());
301  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
302  return ret;
303  }
304 
305  template <class AT>
306  inline Vector<Var,AT>::Vector(std::vector<AT> v) : Matrix<General,Var,Fixed<1>,AT>(v.size(),1) {
307  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
308  }
309 
310  template <class AT>
311  inline const Vector<Var,AT> Vector<Var,AT>::operator()(const Range<Var,Var> &I) const {
312 #ifndef FMATVEC_NO_BOUNDS_CHECK
313  assert(I.end()<M);
314 #endif
315  Vector<Var,AT> x(I.end()-I.start()+1,NONINIT);
316 
317  for(int i=0; i<x.size(); i++)
318  x.e(i) = e(I.start()+i);
319 
320  return x;
321  }
322 
323  template <class AT> template <class Row>
324  inline void Vector<Var,AT>::set(const Range<Var,Var> &I, const Vector<Row,AT> &x) {
325 #ifndef FMATVEC_NO_BOUNDS_CHECK
326  assert(I.end()<size());
327  assert(I.size()==x.size());
328 #endif
329 
330  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
331  e(i) = x.e(ii);
332  }
333 
334  template <class AT> template <class Row>
335  inline void Vector<Var,AT>::add(const Range<Var,Var> &I, const Vector<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 
346 
347  template <class AT> template <class Row>
348  inline void Vector<Var,AT>::deepCopy(const Vector<Row,AT> &x) {
349  for(int i=0; i<M; i++)
350  e(i) = x.e(i);
351  }
352 
354 
355 }
356 
357 #endif
Vector()
Standard constructor.
Definition: var_vector.h:61
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:39
Definition: types.h:65
Vector(const char *str)
String Constructor.
Definition: var_vector.h:81
const AT & operator()(int i) const
Element operator.
Definition: var_vector.h:161
int inc() const
Increment.
Definition: var_vector.h:206
Definition: fmatvec.h:47
const AT & e(int i) const
Element operator.
Definition: var_vector.h:179
Definition: matrix.h:38
Basic shape class for matrices.
Definition: types.h:100
int size() const
Size.
Definition: var_vector.h:198
Vector(const Vector< Var, AT > &x)
Copy Constructor.
Definition: var_vector.h:91
int end() const
Last element.
Definition: range.h:95
AT & operator()(int i)
Element operator.
Definition: var_vector.h:147
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 Valid HTML