FullVector.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_LINEARSOLVER_FULLVECTOR_H
00026 #define SOFA_COMPONENT_LINEARSOLVER_FULLVECTOR_H
00027 
00028 #include <sofa/defaulttype/BaseVector.h>
00029 #include <sofa/helper/rmath.h>
00030 
00031 namespace sofa
00032 {
00033 
00034 namespace component
00035 {
00036 
00037 namespace linearsolver
00038 {
00039 
00040 template<typename T>
00041 class FullVector : public defaulttype::BaseVector
00042 {
00043 public:
00044     typedef T Real;
00045     typedef int Index;
00046     typedef T* Iterator;
00047     typedef const T* ConstIterator;
00048     
00049     typedef Real value_type;
00050     typedef Index size_type;
00051     typedef Iterator iterator;
00052     typedef ConstIterator const_iterator;
00053 
00054 protected:
00055     T* data;
00056     Index cursize;
00057     Index allocsize;
00058 public:
00059 
00060     FullVector()
00061     : data(NULL), cursize(0), allocsize(0)
00062     {
00063     }
00064 
00065     explicit FullVector(Index n)
00066     : data(new T[n]), cursize(n), allocsize(n)
00067     {
00068     }
00069 
00070     FullVector(T* ptr, Index n)
00071     : data(ptr), cursize(n), allocsize(-n)
00072     {
00073     }
00074     
00075     FullVector(T* ptr, Index n, Index nmax)
00076     : data(ptr), cursize(n), allocsize(-nmax)
00077     {
00078     }
00079     
00080     virtual ~FullVector()
00081     {
00082         if (allocsize>0)
00083             delete[] data;
00084     }
00085     
00086     T* ptr() { return data; }
00087     const T* ptr() const { return data; }
00088     
00089     void setptr(T* p) { ptr = p; }
00090     
00091     Index capacity() const { if (allocsize < 0) return -allocsize; else return allocsize; }
00092 
00093     Iterator begin() { return data; }
00094     Iterator end()   { return data+cursize; }
00095 
00096     ConstIterator begin() const { return data; }
00097     ConstIterator end()   const { return data+cursize; }
00098 
00099     void fastResize(int dim)
00100     {
00101         if (dim == cursize) return;
00102         if (allocsize >= 0)
00103         {
00104             if (dim > allocsize)
00105             {
00106                 if (allocsize > 0)
00107                     delete[] data;
00108                 allocsize = dim;
00109                 data = new T[dim];
00110             }
00111         }
00112         else
00113         {
00114             if (dim > -allocsize)
00115             {
00116                 std::cerr << "ERROR: cannot resize preallocated vector to size "<<dim<<std::endl;
00117                 return;
00118             }
00119         }
00120         cursize = dim;
00121     }
00122 
00123     void resize(int dim)
00124     {
00125         fastResize(dim);
00126         clear();
00127     }
00128 
00129     void clear()
00130     {
00131         if (cursize > 0)
00132             std::fill( this->begin(), this->end(), T() );
00133     }
00134 
00135     T& operator[](Index i)
00136     {
00137         return data[i];
00138     }
00139 
00140     const T& operator[](Index i) const
00141     {
00142         return data[i];
00143     }
00144 
00145     SReal element(int i) const
00146     {
00147         return data[i];
00148     }
00149 
00150     void set(int i, SReal v)
00151     {
00152         data[i] = (Real)v;
00153     }
00154 
00155     void add(int i, SReal v)
00156     {
00157       data[i] +=  (Real)v;
00158     }
00159 
00160     int size() const
00161     {
00162         return cursize;
00163     }
00164 
00165     FullVector<T> sub(int i, int n)
00166     {
00167         return FullVector<T>(data+i,n);
00168     }
00169 
00170     template<class TV>
00171     void getsub(int i, int n, TV& v)
00172     {
00173         v = FullVector<T>(data+i,n);
00174     }
00175 
00176     template<class TV>
00177     void setsub(int i, int n, const TV& v)
00178     {
00179         FullVector<T>(data+i,n) = v;
00180     }
00181 
00183     void operator=(const FullVector<T>& a)
00184     {
00185         fastResize(a.size());
00186         std::copy(a.begin(), a.end(), begin());
00187     }
00188 
00189     void operator=(T a)
00190     {
00191         fastResize(a.size());
00192         std::fill(a.begin(), a.end(), a);
00193     }
00194 
00196     template<typename Real2>
00197     void operator+=(const FullVector<Real2>& a)
00198     {
00199         for(int i=0;i<cursize;++i)
00200             (*this)[i] += (Real)a[i];
00201     }
00202 
00204     template<typename Real2,typename Real3>
00205     void peq(const FullVector<Real2>& a, Real3 f)
00206     {
00207         for(int i=0;i<cursize;++i)
00208             (*this)[i] += (Real)(a[i]*f);
00209     }
00210 
00212     template<typename Real2>
00213     void operator*=(Real2 f)
00214     {
00215         for(int i=0;i<cursize;++i)
00216             (*this)[i] *= (Real)f;
00217     }
00218 
00220     Real dot(const FullVector<Real>& a) const
00221     {
00222         Real r = 0;
00223         for(int i=0;i<cursize;++i)
00224             r += (*this)[i]*a[i];
00225         return r;
00226     }
00227 
00229     double norm() const
00230     {
00231         return helper::rsqrt(dot(*this));
00232     }
00233 
00234     friend std::ostream& operator << (std::ostream& out, const FullVector<Real>& v )
00235     {
00236         for (int i=0,s=v.size();i<s;++i)
00237         {
00238             if (i) out << ' ';
00239             out << v[i];
00240         }
00241         return out;
00242     }
00243 };
00244 
00245 } // namespace linearsolver
00246 
00247 } // namespace component
00248 
00249 } // namespace sofa
00250 
00251 #endif

Generated on Sun Nov 23 03:25:11 2008 for Sofa by  doxygen 1.5.7.1