Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_Fad_DynamicStorage.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_DYNAMICSTORAGE_HPP
00033 #define SACADO_FAD_DYNAMICSTORAGE_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 DynamicStorage {
00045 
00046     public:
00047 
00049       DynamicStorage(const T & x) : 
00050   val_(x), sz_(0), len_(0), dx_(NULL) {}
00051 
00053 
00056       DynamicStorage(const int sz, const T & x) : 
00057   val_(x), sz_(sz), len_(sz) {
00058   dx_ = ds_array<S>::get_and_fill(sz_);
00059       }
00060 
00062       DynamicStorage(const DynamicStorage& x) : 
00063   val_(x.val_), sz_(x.sz_), len_(x.sz_) {
00064   dx_ = ds_array<S>::get_and_fill(x.dx_, sz_);
00065       }
00066       
00068       ~DynamicStorage() {
00069   if (len_ != 0)
00070     ds_array<S>::destroy_and_release(dx_, len_);
00071       }
00072 
00074       DynamicStorage& operator=(const DynamicStorage& x) { 
00075   val_ = x.val_;
00076   if (sz_ != x.sz_) {
00077     sz_ = x.sz_;
00078     if (x.sz_ > len_) {
00079       if (len_ != 0)
00080         ds_array<S>::destroy_and_release(dx_, len_);
00081       len_ = x.sz_;
00082       dx_ = ds_array<S>::get_and_fill(x.dx_, sz_);
00083     }
00084     else 
00085       ds_array<S>::copy(x.dx_, dx_, sz_);
00086   }
00087   else 
00088     ds_array<S>::copy(x.dx_, dx_, sz_);
00089 
00090   return *this; 
00091       } 
00092 
00094       int size() const { return sz_;}
00095 
00097       int length() const { return len_; }
00098 
00100 
00103       void resize(int sz) { 
00104   if (sz > len_) {
00105     if (len_ != 0)
00106       ds_array<S>::destroy_and_release(dx_, len_);
00107     dx_ = ds_array<S>::get_and_fill(sz);
00108     len_ = sz;
00109   }
00110   sz_ = sz;
00111       }
00112 
00114 
00118       void expand(int sz) {
00119         if (sz > len_) {
00120           S* dx_new = ds_array<S>::get_and_fill(sz);
00121           ds_array<S>::copy(dx_, dx_new, sz_);
00122           if (len_ > 0)
00123             ds_array<S>::destroy_and_release(dx_, len_);
00124           dx_ = dx_new;
00125           len_ = sz;
00126         }
00127         else if (sz > sz_) 
00128           ds_array<S>::zero(dx_+sz_, sz-sz_);
00129         sz_ = sz;
00130       }
00131 
00133       void zero() { 
00134   ds_array<S>::zero(dx_, sz_);
00135       }
00136 
00138       const T& val() const { return val_; }
00139 
00141       T& val() { return val_; }
00142 
00144       const S* dx() const { return dx_;}
00145 
00147       S dx(int i) const { return sz_ ? dx_[i] : T(0.); }
00148     
00150       S& fastAccessDx(int i) { return dx_[i];}
00151 
00153       const S& fastAccessDx(int i) const { return dx_[i];}
00154 
00155     private:
00156 
00158       T val_;
00159 
00161       int sz_;
00162 
00164       int len_;
00165 
00167       S* dx_;
00168 
00169     }; // class DynamicStorage
00170 
00171   } // namespace Fad
00172 
00173 } // namespace Sacado
00174 
00175 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines