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       void resize(int sz) { 
00101   if (sz > len_) {
00102     if (len_ != 0)
00103       ds_array<S>::destroy_and_release(dx_, len_);
00104     dx_ = ds_array<S>::get_and_fill(sz);
00105     len_ = sz;
00106   }
00107   sz_ = sz;
00108       }
00109 
00111       void zero() { 
00112   ds_array<S>::zero(dx_, sz_);
00113       }
00114 
00116       const T& val() const { return val_; }
00117 
00119       T& val() { return val_; }
00120 
00122       const S* dx() const { return dx_;}
00123 
00125       S dx(int i) const { return sz_ ? dx_[i] : T(0.); }
00126     
00128       S& fastAccessDx(int i) { return dx_[i];}
00129 
00131       const S& fastAccessDx(int i) const { return dx_[i];}
00132 
00133     private:
00134 
00136       T val_;
00137 
00139       int sz_;
00140 
00142       int len_;
00143 
00145       S* dx_;
00146 
00147     }; // class DynamicStorage
00148 
00149   } // namespace Fad
00150 
00151 } // namespace Sacado
00152 
00153 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP

Generated on Wed May 12 21:39:34 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7