Sacado Package Browser (Single Doxygen Collection) Version of the Day
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();
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 
00118       void resize(int sz) { 
00119   if (sz > len_) {
00120     if (!owns_mem)
00121         throw "Can\'t resize beyond original size when memory isn't owned!";
00122     if (len_ != 0)
00123       ds_array<S>::destroy_and_release(dx_, len_);
00124     len_ = sz;
00125     dx_ = ds_array<S>::get_and_fill(len_);
00126   }
00127   sz_ = sz;
00128       }
00129 
00131 
00135       void expand(int sz) {
00136         if (sz > len_) {
00137           if (!owns_mem)
00138         throw "Can\'t resize beyond original size when memory isn't owned!";
00139           S* dx_new = ds_array<S>::get_and_fill(sz);
00140           ds_array<S>::copy(dx_, dx_new, sz_);
00141           if (len_ > 0)
00142             ds_array<S>::destroy_and_release(dx_, len_);
00143           dx_ = dx_new;
00144           len_ = sz;
00145         }
00146         else if (sz > sz_) 
00147           ds_array<S>::strided_zero(dx_+stride_*sz_, stride_, sz-sz_);
00148         sz_ = sz;
00149       }
00150 
00152       void zero() { 
00153   ds_array<S>::strided_zero(dx_, stride_, sz_);
00154       }
00155 
00157       void setMemory(int sz, T* x, S* dx_p, int stride) {
00158 
00159   // Destroy old memory
00160   if (owns_mem) {
00161     if (len_ != 0)
00162       ds_array<S>::destroy_and_release(dx_, len_);
00163   }
00164 
00165   // Set new values
00166   owns_mem = false;
00167   sz_ = sz;
00168   len_ = sz;
00169   stride_ = stride;
00170   val_ = x;
00171   dx_ = dx_p;
00172       }
00173 
00175       const T& val() const { return *val_; }
00176 
00178       T& val() { return *val_; }
00179 
00181       const S* dx() const { return dx_;}
00182 
00184       S dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
00185     
00187       S& fastAccessDx(int i) { return dx_[i*stride_];}
00188 
00190       const S& fastAccessDx(int i) const { return dx_[i*stride_];}
00191 
00192     private:
00193 
00194       T v_;
00195 
00196     private:
00197 
00199       bool owns_mem;
00200 
00202       int sz_;
00203 
00205       int len_;
00206 
00208       int stride_;
00209 
00211       T* val_;
00212 
00214       S* dx_;
00215 
00216     }; // class VectorDynamicStorage
00217 
00218   } // namespace Fad
00219 
00220 } // namespace Sacado
00221 
00222 #endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines