STK++ 1.0
STK_Vector.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  Astkpp::rrays
00027  * Purpose:  Define the Vector class.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00032 
00038 #ifndef STK_VECTOR_H
00039 #define STK_VECTOR_H
00040 
00041 #include "../../STKernel/include/STK_Real.h"
00042 #include "STK_Array1D.h"
00043 
00044 namespace STK
00045 {
00046 
00053 typedef Array1D<Real> Vector;
00054 
00060 template<>
00061 class Array1D<Real> : public RecursiveArray1D< Real, Array1D<Real> >
00062 {
00064   typedef AllocatorBase<Real*> _AllocatorBaseType_;
00066   typedef RecursiveArray1D<Real, Array1D<Real> > _RecArray1DType_;
00067 
00068   public:
00072     Array1D( Range const& I = Range())
00073           : _RecArray1DType_(I)
00074     { ;}
00075 
00080     Array1D( Range const& I, Real const& v)
00081            : _RecArray1DType_(I)
00082     {
00083       for (Integer i=first(); i<=last(); i++)
00084         setData(i, v);
00085     }
00086 
00091     Array1D( const Array1D &T, bool ref =false)
00092            : _RecArray1DType_(T, ref)
00093     {
00094       // check if we want just a reference
00095       if (!ref)
00096       {
00097         for (Integer j=first(); j<=last(); j++)
00098           setData(j, T.data(j));
00099       }
00100     }
00101 
00106     Array1D( Array1D const& T, Range const& I)
00107            : _RecArray1DType_(T, I)
00108     { ;}
00109 
00115     Array1D( Real* q, Range const& I, Integer const& index =0)
00116            : _RecArray1DType_(q, I, index)
00117     { ;}
00118 
00124      Array1D( const _AllocatorBaseType_& T, Range const& I, Integer const& index)
00125             : _RecArray1DType_(T.data(index), I, index)
00126     { ;}
00127 
00131     virtual ~Array1D() { ;}
00132 
00136     inline Real& elt(Integer const& pos)
00137     { return this->data(pos);}
00138 
00142     inline Real const& elt(Integer const& pos) const
00143     { return this->data(pos);}
00144 
00148     inline Array1D elt(Range const& J) const
00149     {
00150 #ifdef STK_BOUNDS_CHECK
00151       if ((J.first()<this->first()))
00152       { throw out_of_range("Array1D::elt(J) "
00153                                 "J.first()<this->first()");
00154       }
00155       if ((J.last()>this->last()))
00156       { throw out_of_range("Array1D::elt(J) "
00157                                 "J.last()>this->last()");
00158       }
00159 #endif
00160       return Array1D(*this, J);
00161     }
00162 
00169     inline Array1D& operator=(Array1D const& T)
00170     {
00171       // check size
00172       if (size()!=T.size()) resize(T.range());
00173       // copy without ovelapping.
00174       if (first() < T.first())
00175       { for (Integer i=first(), j=T.first(); j<=T.last(); i++, j++)
00176           setData(i, T.data(j));
00177       }
00178       else
00179       { for (Integer i=last(), j=T.last(); j>=T.first(); i--, j--)
00180           setData(i, T.data(j));
00181       }
00182       return *this;
00183     }
00184 
00188     inline Array1D& operator=(Real const& v)
00189     {
00190       for (Integer i=first(); i<=last(); i++)
00191         setData(i, v);
00192       return *this;
00193     }
00194 
00198     inline Array1D& operator+=(Real const& v)
00199     {
00200       for (Integer i=first(); i<=last(); i++)
00201         elt(i) += v;
00202       return *this;
00203     }
00204 
00208     inline Array1D& operator-=(Real const& v)
00209     {
00210       for (Integer i=first(); i<=last(); i++)
00211         elt(i) -= v;
00212       return *this;
00213     }
00214 
00218     inline Array1D& operator/=(Real const& v)
00219     {
00220       for (Integer i=first(); i<=last(); i++)
00221         elt(i) /= v;
00222       return *this;
00223     }
00224 
00228     inline Array1D& operator*=(Real const& v)
00229     {
00230       for (Integer i=first(); i<=last(); i++)
00231         elt(i) *= v;
00232       return *this;
00233     }
00234 
00241     template <class LEAF>
00242     inline Array1D& operator=(const ITContainer1D<Real, LEAF> &T)
00243     { // We have to resize if this and T does not have the same size
00244       // but if they have the same size, we don't scale the index
00245       if (size()!=T.size()) resize(T.range());
00246 
00247       // copy without ovelapping
00248       if (first() < T.first())
00249       { for (Integer j=first(), i=T.first(); i<=T.last(); j++, i++)
00250           setData(j, T[i]);
00251       }
00252       else
00253       { for (Integer j=last(), i=T.last(); i>=T.first(); j--, i--)
00254           setData(j, T[i]);
00255       }
00256       return *this;
00257     }
00258 
00262     template< class Exp>
00263     inline Array1D& operator=(const Exp& rhs)
00264     {
00265       for (Integer i=first(); i<=last(); i++)
00266         setData(i, rhs[i]);
00267       return *this;
00268     }
00269 
00273     template< class Exp>
00274     inline Array1D& operator+=(const Exp& rhs)
00275     {
00276       for (Integer i=first(); i<=last(); i++)
00277         elt(i) += rhs[i];
00278       return *this;
00279     }
00280 
00284     template< class Exp>
00285     inline Array1D& operator-=(const Exp& rhs)
00286     {
00287       for (Integer i=first(); i<=last(); i++)
00288         elt(i) -= rhs[i];
00289       return *this;
00290     }
00291 
00295     template< class Exp>
00296     inline Array1D& operator/=(const Exp& rhs)
00297     {
00298       for (Integer i=first(); i<=last(); i++)
00299         elt(i) /= rhs[i];
00300       return *this;
00301     }
00302 
00306     template< class Exp>
00307     inline Array1D& operator*=(const Exp& rhs)
00308     {
00309       for (Integer i=first(); i<=last(); i++)
00310         elt(i) *= rhs[i];
00311       return *this;
00312     }
00313 };
00314 
00315 } // namespace STK
00316 
00317 #endif
00318 // STK_VECTOR_H