Sacado_Fad_DynamicStorage.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_Fad_DynamicStorage.hpp,v 1.9 2008/06/16 17:06:35 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Fad_DynamicStorage.hpp,v $ 
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       void resize(int sz) { 
00098   if (sz > len_) {
00099     if (len_ != 0)
00100       ds_array<S>::destroy_and_release(dx_, len_);
00101     dx_ = ds_array<S>::get_and_fill(sz);
00102     len_ = sz;
00103   }
00104   sz_ = sz;
00105       }
00106 
00108       void zero() { 
00109   ds_array<S>::zero(dx_, sz_);
00110       }
00111 
00112     public:
00113 
00115       T val_;
00116 
00118       int sz_;
00119 
00121       int len_;
00122 
00124       S* dx_;
00125 
00126     }; // class DynamicStorage
00127 
00128   } // namespace Fad
00129 
00130 } // namespace Sacado
00131 
00132 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP

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