Sacado_DynamicArrayTraits.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_DynamicArrayTraits.hpp,v 1.2 2007/07/24 00:08:04 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_DynamicArrayTraits.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_DYNAMICARRAYTRAITS_HPP
00033 #define SACADO_DYNAMICARRAYTRAITS_HPP
00034 
00035 #include <new>
00036 #include <cstring>
00037 
00038 #include "Sacado_Traits.hpp"
00039 
00040 namespace Sacado {
00041 
00045   template <typename T, bool isScalar = IsScalarType<T>::value>
00046   struct ds_array {
00047 
00049     static inline T* get_and_fill(int sz) {
00050       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00051       T* p = m;
00052       for (int i=0; i<sz; ++i)
00053   new (p++) T(0.0);
00054       return m;
00055     }
00056 
00061     static inline T* get_and_fill(const T* src, int sz) {
00062       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00063       T* p = m; 
00064       for (int i=0; i<sz; ++i)
00065   new (p++) T(*(src++));
00066       return m;
00067     }
00068 
00070     static inline void copy(const T* src, T*  dest, int sz) {
00071       for (int i=0; i<sz; ++i)
00072   *(dest++) = *(src++);
00073     }
00074 
00076     static inline void zero(T* dest, int sz) {
00077       for (int i=0; i<sz; ++i)
00078   *(dest++) = T(0.);
00079     }
00080 
00082     static inline void destroy_and_release(T* m, int sz) {
00083       T* e = m+sz;
00084       for (T* b = m; b!=e; b++)
00085   b->~T();
00086       operator delete((void*) m);
00087     }
00088   };
00089 
00094   template <typename T>
00095   struct ds_array<T,true> {
00096 
00098     static inline T* get_and_fill(int sz) {
00099       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00100       std::memset(m,0,sz*sizeof(T));
00101       return m;
00102     }
00103 
00108     static inline T* get_and_fill(const T* src, int sz) {
00109       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00110       for (int i=0; i<sz; ++i)
00111   m[i] = src[i];
00112       return m;
00113     }
00114 
00116     static inline void copy(const T* src, T* dest, int sz) {
00117       std::memcpy(dest,src,sz*sizeof(T));
00118     }
00119 
00121     static inline void zero(T* dest, int sz) {
00122       std::memset(dest,0,sz*sizeof(T));
00123     }
00124 
00126     static inline void destroy_and_release(T* m, int sz) {
00127       operator delete((void*) m);
00128       }
00129   };
00130 
00131 } // namespace Sacado
00132 
00133 #endif // SACADO_DYNAMICARRAY_HPP

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