var_row_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_row_vector_h
00023 #define var_row_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 RowVector<Var,AT> : public Matrix<General,Fixed<1>,Var,AT> {
00040     using Matrix<General,Fixed<1>,Var,AT>::N;
00041     using Matrix<General,Fixed<1>,Var,AT>::ele;
00042 
00043     public:
00044 
00046 
00047     protected:
00048 
00049     template<class Row> inline void deepCopy(const RowVector<Row,AT> &x);
00050 
00052     
00053     public:
00054 
00059       RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
00060       }
00061 
00062 //      template<class Ini=All<AT> >
00063 //        RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { } 
00064 
00065       RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { } 
00066       RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { } 
00067 
00080       RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
00081       }
00082 
00090       RowVector(const RowVector<Var,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
00091       }
00092 
00093       template<class Row>
00094       RowVector(const RowVector<Row,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
00095       }
00096 
00097       template<class Type, class Row, class Col>
00098       explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
00099       }
00100 
00101       RowVector<Var,AT>& resize() {
00102         Matrix<General,Fixed<1>,Var,AT>::resize();
00103         return *this;
00104       }
00105 
00106       RowVector<Var,AT>& resize(int n, Noinit) {
00107         Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
00108         return *this;
00109       }
00110 
00111       RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
00112         Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
00113         return *this;
00114       }
00115 
00122       inline RowVector<Var,AT>& operator=(const RowVector<Var,AT> &x);
00123 
00124       template <class Row>
00125       inline RowVector<Var,AT>& operator=(const RowVector<Row,AT> &x);
00126 
00133       template <class Row>
00134         inline RowVector<Var,AT>& operator<<(const RowVector<Row,AT> &x);
00135 
00146       AT& operator()(int i) {
00147 
00148 #ifndef FMATVEC_NO_BOUNDS_CHECK
00149         assert(i>=0);
00150         assert(i<N);
00151 #endif
00152 
00153         return e(i);
00154       };
00155 
00160       const AT& operator()(int i) const {
00161 
00162 #ifndef FMATVEC_NO_BOUNDS_CHECK
00163         assert(i>=0);
00164         assert(i<N);
00165 #endif
00166 
00167         return e(i);
00168       };
00169 
00170       AT& e(int i) {
00171         return ele[i];
00172       };
00173 
00178       const AT& e(int i) const {
00179         return ele[i];
00180       };
00181 
00189       inline RowVector<Var,AT>& init(const AT& a=0);
00190       inline RowVector<Var,AT>& init(Init, const AT &a=0) { return init(a); }
00191       inline RowVector<Var,AT>& init(Noinit, const AT &a=0) { return *this; }
00192 
00197       int size() const {return N;};
00198 
00205       int inc() const {return 1;};
00206 
00207       using Matrix<General,Fixed<1>,Var,AT>::operator();
00208 
00213       inline operator std::vector<AT>();
00214 
00219       inline RowVector(std::vector<AT> v);
00220 
00221       inline const Vector<Var,AT> T() const;
00222 
00223       template <class Row>
00224       inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
00225   };
00226 
00227   template <class AT>
00228     inline RowVector<Var,AT>& RowVector<Var,AT>::init(const AT &val) {
00229       for(int i=0; i<N; i++) 
00230         e(i) = val; 
00231       return *this;
00232     }
00233 
00234   template <class AT>
00235     inline RowVector<Var,AT>& RowVector<Var,AT>::operator=(const RowVector<Var,AT> &x) { 
00236 
00237       if(!ele) {
00238         delete[] ele;
00239         N = x.size(); 
00240         ele = new AT[N];
00241       } else {
00242 #ifndef FMATVEC_NO_SIZE_CHECK
00243         assert(N == x.size());
00244 #endif
00245       }
00246 
00247       deepCopy(x);
00248 
00249       return *this;
00250     }
00251 
00252   template <class AT> template<class Row>
00253     inline RowVector<Var,AT>& RowVector<Var,AT>::operator=(const RowVector<Row,AT> &x) { 
00254 
00255       if(!ele) {
00256         delete[] ele;
00257         N = x.size(); 
00258         ele = new AT[N];
00259       } else {
00260 #ifndef FMATVEC_NO_SIZE_CHECK
00261         assert(N == x.size());
00262 #endif
00263       }
00264 
00265       deepCopy(x);
00266 
00267       return *this;
00268     }
00269 
00270   template <class AT> template<class Row>
00271     inline RowVector<Var,AT>& RowVector<Var,AT>::operator<<(const RowVector<Row,AT> &x) { 
00272 
00273       if(N!=x.size()) {
00274         delete[] ele;
00275         N = x.size(); 
00276         ele = new AT[N];
00277       }
00278 
00279       deepCopy(x);
00280 
00281       return *this;
00282     }
00283 
00284   template <class AT>
00285     inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
00286       Vector<Var,AT> x(N,NONINIT);
00287       for(int i=0; i<N; i++)
00288         x.e(i) = e(i);
00289       return x;
00290     }
00291 
00292   template <class AT>
00293     inline RowVector<Var,AT>::operator std::vector<AT>() {
00294       std::vector<AT> ret(size());
00295       if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
00296       return ret;
00297     }
00298 
00299   template <class AT>
00300     inline RowVector<Var,AT>::RowVector(std::vector<AT> v) : Matrix<General,Fixed<1>,Var,AT>(v.size(),1) {
00301       if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
00302     }
00303 
00304   template <class AT> template <class Row>
00305     inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
00306 #ifndef FMATVEC_NO_BOUNDS_CHECK
00307       assert(I.end()<size());
00308       assert(I.size()==x.size());
00309 #endif
00310 
00311       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00312         e(i) = x.e(ii);
00313     }
00314 
00316 
00317   template <class AT> template <class Row>
00318     inline void RowVector<Var,AT>::deepCopy(const RowVector<Row,AT> &x) {
00319       for(int i=0; i<N; i++)
00320         e(i) = x.e(i);
00321     }
00322 
00324 
00325 }
00326 
00327 #endif