Sacado_DynamicArrayTraits.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_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 
00073     static inline T* strided_get_and_fill(const T* src, int stride, int sz) {
00074       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00075       T* p = m; 
00076       for (int i=0; i<sz; ++i) {
00077   new (p++) T(*(src));
00078   src += stride;
00079       }
00080       return m;
00081     }
00082 
00084     static inline void copy(const T* src, T*  dest, int sz) {
00085       for (int i=0; i<sz; ++i)
00086   *(dest++) = *(src++);
00087     }
00088 
00090     static inline void strided_copy(const T* src, int src_stride, 
00091             T* dest, int dest_stride, int sz) {
00092       for (int i=0; i<sz; ++i) {
00093   *(dest) = *(src);
00094   dest += dest_stride;
00095   src += src_stride;
00096       }
00097     }
00098 
00100     static inline void zero(T* dest, int sz) {
00101       for (int i=0; i<sz; ++i)
00102   *(dest++) = T(0.);
00103     }
00104 
00106     static inline void strided_zero(T* dest, int stride, int sz) {
00107       for (int i=0; i<sz; ++i) {
00108   *(dest) = T(0.);
00109   dest += stride;
00110       }
00111     }
00112 
00114     static inline void destroy_and_release(T* m, int sz) {
00115       T* e = m+sz;
00116       for (T* b = m; b!=e; b++)
00117   b->~T();
00118       operator delete((void*) m);
00119     }
00120   };
00121 
00126   template <typename T>
00127   struct ds_array<T,true> {
00128 
00130     static inline T* get_and_fill(int sz) {
00131       if (sz > 0) {
00132   T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00133   std::memset(m,0,sz*sizeof(T));
00134   return m;
00135       }
00136       else
00137   return NULL;
00138     }
00139 
00144     static inline T* get_and_fill(const T* src, int sz) {
00145       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00146       for (int i=0; i<sz; ++i)
00147   m[i] = src[i];
00148       return m;
00149     }
00150 
00155     static inline T* strided_get_and_fill(const T* src, int stride, int sz) {
00156       T* m = static_cast<T* >(operator new(sz*sizeof(T)));
00157       for (int i=0; i<sz; ++i)
00158   m[i] = src[i*stride];
00159       return m;
00160     }
00161 
00163     static inline void copy(const T* src, T* dest, int sz) {
00164       std::memcpy(dest,src,sz*sizeof(T));
00165     }
00166 
00168     static inline void strided_copy(const T* src, int src_stride, 
00169             T* dest, int dest_stride, int sz) {
00170       for (int i=0; i<sz; ++i) {
00171   *(dest) = *(src);
00172   dest += dest_stride;
00173   src += src_stride;
00174       }
00175     }
00176 
00178     static inline void zero(T* dest, int sz) {
00179       if (sz > 0)
00180   std::memset(dest,0,sz*sizeof(T));
00181     }
00182 
00184     static inline void strided_zero(T* dest, int stride, int sz) {
00185       for (int i=0; i<sz; ++i) {
00186   *(dest) = T(0.);
00187   dest += stride;
00188       }
00189     }
00190 
00192     static inline void destroy_and_release(T* m, int sz) {
00193       operator delete((void*) m);
00194     }
00195   };
00196 
00197 } // namespace Sacado
00198 
00199 #endif // SACADO_DYNAMICARRAY_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:31 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3