Sacado_Fad_VectorDynamicStorage.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #ifndef SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
00033 #define SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
00034 
00035 #include "Sacado_Traits.hpp"
00036 #include "Sacado_DynamicArrayTraits.hpp"
00037 
00038 namespace Sacado {
00039 
00040   namespace Fad {
00041 
00043     template <typename T, typename S = T> 
00044     class VectorDynamicStorage {
00045 
00046     public:
00047 
00049       VectorDynamicStorage(const T & x) : 
00050   v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
00051       {}
00052 
00054 
00057       VectorDynamicStorage(const int sz, const T & x) : 
00058   v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
00059   dx_ = ds_array<S>::get_and_fill(sz_);
00060       }
00061 
00063       VectorDynamicStorage(const int sz, T* x, S* dx_p, const int stride,
00064          bool zero_out) : 
00065   v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride), 
00066   val_(x), dx_(dx_p) {
00067   if (zero_out)
00068     zero(dx_, sz_);
00069       }
00070 
00072       VectorDynamicStorage(const VectorDynamicStorage& x) : 
00073   v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_), 
00074   stride_(1), val_(&v_)  {
00075   dx_ = ds_array<S>::strided_get_and_fill(x.dx_, x.stride_, sz_);
00076       }
00077       
00079       ~VectorDynamicStorage() {
00080   if (owns_mem) {
00081     if (len_ != 0)
00082       ds_array<S>::destroy_and_release(dx_, len_);
00083   }
00084       }
00085 
00087       VectorDynamicStorage& operator=(const VectorDynamicStorage& x) { 
00088   *val_ = *x.val_;
00089   if (sz_ != x.sz_) {
00090     sz_ = x.sz_;
00091     if (x.sz_ > len_) {
00092       if (!owns_mem)
00093         throw "Can\'t resize beyond original size when memory isn't owned!";
00094       if (len_ != 0)
00095         ds_array<S>::destroy_and_release(dx_, len_);
00096       len_ = x.sz_;
00097       dx_ = ds_array<S>::strided_get_and_fill(x.dx_, x.stride_, sz_);
00098     }
00099     else 
00100       ds_array<S>::strided_copy(x.dx_, x.stride_, dx_, stride_, sz_);
00101   }
00102   else 
00103     ds_array<S>::strided_copy(x.dx_, x.stride_, dx_, stride_, sz_);
00104 
00105   return *this; 
00106       } 
00107 
00109       int size() const { return sz_;}
00110 
00112       int length() const { return len_; }
00113 
00115       void resize(int sz) { 
00116   if (sz > len_) {
00117     if (!owns_mem)
00118         throw "Can\'t resize beyond original size when memory isn't owned!";
00119     if (len_ != 0)
00120       ds_array<S>::destroy_and_release(dx_, len_);
00121     len_ = sz;
00122     dx_ = ds_array<S>::get_and_fill(len_);
00123   }
00124   sz_ = sz;
00125       }
00126 
00128       void zero() { 
00129   ds_array<S>::strided_zero(dx_, stride_, sz_);
00130       }
00131 
00133       void setMemory(int sz, T* x, S* dx_p, int stride) {
00134 
00135   // Destroy old memory
00136   if (owns_mem) {
00137     if (len_ != 0)
00138       ds_array<S>::destroy_and_release(dx_, len_);
00139   }
00140 
00141   // Set new values
00142   owns_mem = false;
00143   sz_ = sz;
00144   len_ = sz;
00145   stride_ = stride;
00146   val_ = x;
00147   dx_ = dx_p;
00148       }
00149 
00151       const T& val() const { return *val_; }
00152 
00154       T& val() { return *val_; }
00155 
00157       const S* dx() const { return dx_;}
00158 
00160       S dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
00161     
00163       S& fastAccessDx(int i) { return dx_[i*stride_];}
00164 
00166       const S& fastAccessDx(int i) const { return dx_[i*stride_];}
00167 
00168     private:
00169 
00170       T v_;
00171 
00172     private:
00173 
00175       bool owns_mem;
00176 
00178       int sz_;
00179 
00181       int len_;
00182 
00184       int stride_;
00185 
00187       T* val_;
00188 
00190       S* dx_;
00191 
00192     }; // class VectorDynamicStorage
00193 
00194   } // namespace Fad
00195 
00196 } // namespace Sacado
00197 
00198 #endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:33 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3