var_vector.h
00001 /* Copyright (C) 2003-2012 Martin Förg
00002 
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00016  *
00017  * Contact:
00018  *   martin.o.foerg@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef var_vector_h
00023 #define var_vector_h
00024 
00025 #include "var_general_matrix.h"
00026 #include <vector>
00027 #include <cstring>
00028 
00029 namespace fmatvec {
00030 
00039   template <class AT> class Vector<Var,AT> : public Matrix<General,Var,Fixed<1>,AT> {
00040     using Matrix<General,Var,Fixed<1>,AT>::M;
00041     using Matrix<General,Var,Fixed<1>,AT>::ele;
00042 
00043     public:
00044 
00046 
00047     protected:
00048 
00049     template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
00050 
00052     
00053     public:
00054 
00059       Vector() : Matrix<General,Var,Fixed<1>,AT>() { }
00060 
00061 //      template<class Ini=All<AT> >
00062 //        Vector(int m, Ini ini=All<AT>()) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { } 
00063 
00064       Vector(int m, Noinit ini) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { } 
00065       Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Var,Fixed<1>,AT>(m,ini,a) { } 
00066 
00079       Vector(const char *str) : Matrix<General,Var,Fixed<1>,AT>(str) {
00080       }
00081 
00089       Vector(const Vector<Var,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
00090       }
00091 
00092       template<class Row>
00093       Vector(const Vector<Row,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
00094       }
00095 
00096       template<class Type, class Row, class Col>
00097       explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
00098       }
00099 
00100       Vector<Var,AT>& resize() {
00101         Matrix<General,Var,Fixed<1>,AT>::resize();
00102         return *this;
00103       }
00104 
00105       Vector<Var,AT>& resize(int m, Noinit) {
00106         Matrix<General,Var,Fixed<1>,AT>::resize(m,Noinit());
00107         return *this;
00108       }
00109 
00110       Vector<Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
00111         Matrix<General,Var,Fixed<1>,AT>::resize(m,ini,a);
00112         return *this;
00113       }
00114 
00121       inline Vector<Var,AT>& operator=(const Vector<Var,AT> &x);
00122 
00123       template <class Row>
00124       inline Vector<Var,AT>& operator=(const Vector<Row,AT> &x);
00125 
00132       template <class Row>
00133         inline Vector<Var,AT>& operator<<(const Vector<Row,AT> &x);
00134 
00145       AT& operator()(int i) {
00146 
00147 #ifndef FMATVEC_NO_BOUNDS_CHECK
00148         assert(i>=0);
00149         assert(i<M);
00150 #endif
00151 
00152         return e(i);
00153       };
00154 
00159       const AT& operator()(int i) const {
00160 
00161 #ifndef FMATVEC_NO_BOUNDS_CHECK
00162         assert(i>=0);
00163         assert(i<M);
00164 #endif
00165 
00166         return e(i);
00167       };
00168 
00169       AT& e(int i) {
00170         return ele[i];
00171       };
00172 
00177       const AT& e(int i) const {
00178         return ele[i];
00179       };
00180 
00188       inline Vector<Var,AT>& init(const AT& a=0);
00189       inline Vector<Var,AT>& init(Init, const AT& a=0) { return init(a); }
00190       inline Vector<Var,AT>& init(Noinit, const AT& a=0) { return *this; }
00191 
00196       int size() const {return M;};
00197 
00204       int inc() const {return 1;};
00205 
00206       using Matrix<General,Var,Fixed<1>,AT>::operator();
00207 
00212       inline operator std::vector<AT>();
00213 
00218       inline Vector(std::vector<AT> v);
00219 
00220       inline const RowVector<Var,AT> T() const;
00221 
00222       template <class Row>
00223       inline void set(const Range<Var,Var> &I, const Vector<Row,AT> &x);
00224   };
00225 
00226   template <class AT>
00227     inline Vector<Var,AT>& Vector<Var,AT>::init(const AT &val) {
00228         for(int i=0; i<M; i++) 
00229           e(i) = val;
00230       return *this;
00231     }
00232 
00233   template <class AT>
00234     inline Vector<Var,AT>& Vector<Var,AT>::operator=(const Vector<Var,AT> &x) { 
00235 
00236       if(!ele) {
00237         delete[] ele;
00238         M = x.size(); 
00239         ele = new AT[M];
00240       } else {
00241 #ifndef FMATVEC_NO_SIZE_CHECK
00242         assert(M == x.size());
00243 #endif
00244       }
00245 
00246       deepCopy(x);
00247 
00248       return *this;
00249     }
00250 
00251   template <class AT> template<class Row>
00252     inline Vector<Var,AT>& Vector<Var,AT>::operator=(const Vector<Row,AT> &x) { 
00253 
00254       if(!ele) {
00255         delete[] ele;
00256         M = x.size(); 
00257         ele = new AT[M];
00258       } else {
00259 #ifndef FMATVEC_NO_SIZE_CHECK
00260         assert(M == x.size());
00261 #endif
00262       }
00263 
00264       deepCopy(x);
00265 
00266       return *this;
00267     }
00268 
00269   template <class AT> template<class Row>
00270     inline Vector<Var,AT>& Vector<Var,AT>::operator<<(const Vector<Row,AT> &x) { 
00271 
00272       if(M!=x.size()) {
00273         delete[] ele;
00274         M = x.size(); 
00275         ele = new AT[M];
00276       }
00277 
00278       deepCopy(x);
00279 
00280       return *this;
00281     }
00282 
00283   template <class AT>
00284     inline const RowVector<Var,AT> Vector<Var,AT>::T() const {
00285       RowVector<Var,AT> x(M,NONINIT);
00286       for(int i=0; i<M; i++)
00287         x.e(i) = e(i);
00288       return x;
00289     }
00290 
00291   template <class AT>
00292     inline Vector<Var,AT>::operator std::vector<AT>() {
00293       std::vector<AT> ret(size());
00294       if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
00295       return ret;
00296     }
00297 
00298   template <class AT>
00299     inline Vector<Var,AT>::Vector(std::vector<AT> v) : Matrix<General,Var,Fixed<1>,AT>(v.size(),1) {
00300       if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
00301     }
00302 
00303   template <class AT> template <class Row>
00304     inline void Vector<Var,AT>::set(const Range<Var,Var> &I, const Vector<Row,AT> &x) {
00305 #ifndef FMATVEC_NO_BOUNDS_CHECK
00306       assert(I.end()<size());
00307       assert(I.size()==x.size());
00308 #endif
00309 
00310       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00311         e(i) = x.e(ii);
00312     }
00313 
00315 
00316   template <class AT> template <class Row>
00317     inline void Vector<Var,AT>::deepCopy(const Vector<Row,AT> &x) {
00318       for(int i=0; i<M; i++)
00319         e(i) = x.e(i);
00320     }
00321 
00323 
00324 }
00325 
00326 #endif