All Classes Namespaces Functions 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 
46 
47  protected:
48 
49  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
50 
52 
53  public:
54 
59  Vector() : Matrix<General,Var,Fixed<1>,AT>() { }
60 
61 // template<class Ini=All<AT> >
62 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
63 
64  Vector(int m, Noinit ini) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
65  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Var,Fixed<1>,AT>(m,ini,a) { }
66 
79  Vector(const char *str) : Matrix<General,Var,Fixed<1>,AT>(str) {
80  }
81 
89  Vector(const Vector<Var,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
90  }
91 
92  template<class Row>
93  Vector(const Vector<Row,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
94  }
95 
96  template<class Type, class Row, class Col>
97  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
98  }
99 
100  Vector<Var,AT>& resize() {
101  Matrix<General,Var,Fixed<1>,AT>::resize();
102  return *this;
103  }
104 
105  Vector<Var,AT>& resize(int m, Noinit) {
106  Matrix<General,Var,Fixed<1>,AT>::resize(m,Noinit());
107  return *this;
108  }
109 
110  Vector<Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
111  Matrix<General,Var,Fixed<1>,AT>::resize(m,ini,a);
112  return *this;
113  }
114 
121  inline Vector<Var,AT>& operator=(const Vector<Var,AT> &x);
122 
123  template <class Row>
124  inline Vector<Var,AT>& operator=(const Vector<Row,AT> &x);
125 
132  template <class Row>
133  inline Vector<Var,AT>& operator<<(const Vector<Row,AT> &x);
134 
145  AT& operator()(int i) {
146 
147 #ifndef FMATVEC_NO_BOUNDS_CHECK
148  assert(i>=0);
149  assert(i<M);
150 #endif
151 
152  return e(i);
153  };
154 
159  const AT& operator()(int i) const {
160 
161 #ifndef FMATVEC_NO_BOUNDS_CHECK
162  assert(i>=0);
163  assert(i<M);
164 #endif
165 
166  return e(i);
167  };
168 
169  AT& e(int i) {
170  return ele[i];
171  };
172 
177  const AT& e(int i) const {
178  return ele[i];
179  };
180 
188  inline Vector<Var,AT>& init(const AT& a=0);
189  inline Vector<Var,AT>& init(Init, const AT& a=0) { return init(a); }
190  inline Vector<Var,AT>& init(Noinit, const AT& a=0) { return *this; }
191 
196  int size() const {return M;};
197 
204  int inc() const {return 1;};
205 
206  using Matrix<General,Var,Fixed<1>,AT>::operator();
207 
212  inline operator std::vector<AT>();
213 
218  inline Vector(std::vector<AT> v);
219 
220  inline const RowVector<Var,AT> T() const;
221 
222  inline const Vector<Var,AT> operator()(const Range<Var,Var> &I) const;
223 
224  template <class Row>
225  inline void set(const Range<Var,Var> &I, const Vector<Row,AT> &x);
226  };
227 
228  template <class AT>
229  inline Vector<Var,AT>& Vector<Var,AT>::init(const AT &val) {
230  for(int i=0; i<M; i++)
231  e(i) = val;
232  return *this;
233  }
234 
235  template <class AT>
237 
238  if(!ele) {
239  delete[] ele;
240  M = x.size();
241  ele = new AT[M];
242  } else {
243 #ifndef FMATVEC_NO_SIZE_CHECK
244  assert(M == x.size());
245 #endif
246  }
247 
248  deepCopy(x);
249 
250  return *this;
251  }
252 
253  template <class AT> template<class Row>
255 
256  if(!ele) {
257  delete[] ele;
258  M = x.size();
259  ele = new AT[M];
260  } else {
261 #ifndef FMATVEC_NO_SIZE_CHECK
262  assert(M == x.size());
263 #endif
264  }
265 
266  deepCopy(x);
267 
268  return *this;
269  }
270 
271  template <class AT> template<class Row>
273 
274  if(M!=x.size()) {
275  delete[] ele;
276  M = x.size();
277  ele = new AT[M];
278  }
279 
280  deepCopy(x);
281 
282  return *this;
283  }
284 
285  template <class AT>
286  inline const RowVector<Var,AT> Vector<Var,AT>::T() const {
287  RowVector<Var,AT> x(M,NONINIT);
288  for(int i=0; i<M; i++)
289  x.e(i) = e(i);
290  return x;
291  }
292 
293  template <class AT>
294  inline Vector<Var,AT>::operator std::vector<AT>() {
295  std::vector<AT> ret(size());
296  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
297  return ret;
298  }
299 
300  template <class AT>
301  inline Vector<Var,AT>::Vector(std::vector<AT> v) : Matrix<General,Var,Fixed<1>,AT>(v.size(),1) {
302  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
303  }
304 
305  template <class AT>
306  inline const Vector<Var,AT> Vector<Var,AT>::operator()(const Range<Var,Var> &I) const {
307 #ifndef FMATVEC_NO_BOUNDS_CHECK
308  assert(I.end()<M);
309 #endif
310  Vector<Var,AT> x(I.end()-I.start()+1,NONINIT);
311 
312  for(int i=0; i<x.size(); i++)
313  x.e(i) = e(I.start()+i);
314 
315  return x;
316  }
317 
318  template <class AT> template <class Row>
319  inline void Vector<Var,AT>::set(const Range<Var,Var> &I, const Vector<Row,AT> &x) {
320 #ifndef FMATVEC_NO_BOUNDS_CHECK
321  assert(I.end()<size());
322  assert(I.size()==x.size());
323 #endif
324 
325  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
326  e(i) = x.e(ii);
327  }
328 
330 
331  template <class AT> template <class Row>
332  inline void Vector<Var,AT>::deepCopy(const Vector<Row,AT> &x) {
333  for(int i=0; i<M; i++)
334  e(i) = x.e(i);
335  }
336 
338 
339 }
340 
341 #endif
Vector()
Standard constructor.
Definition: var_vector.h:59
Definition: types.h:68
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
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:79
const AT & operator()(int i) const
Element operator.
Definition: var_vector.h:159
int inc() const
Increment.
Definition: var_vector.h:204
Definition: matrix.h:215
const AT & e(int i) const
Element operator.
Definition: var_vector.h:177
Definition: matrix.h:38
Basic shape class for matrices.
Definition: types.h:100
int size() const
Size.
Definition: var_vector.h:196
Vector(const Vector< Var, AT > &x)
Copy Constructor.
Definition: var_vector.h:89
int end() const
Last element.
Definition: range.h:95
AT & operator()(int i)
Element operator.
Definition: var_vector.h:145
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