Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_DynamicArrayTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef STOKHOS_DYNAMICARRAYTRAITS_HPP
11 #define STOKHOS_DYNAMICARRAYTRAITS_HPP
12 
13 #include <new>
14 #include <cstring>
15 
16 namespace Stokhos {
17 
19 
23  template <typename T> struct IsScalarType {
24  static const bool value = false;
25  };
26 
28 #define STOKHOS_BUILTIN_SPECIALIZATION(t) \
29  template <> struct IsScalarType< t > { \
30  static const bool value = true; \
31  };
32 
37 
38 #undef STOKHOS_BUILTIN_SPECIALIZATION
39 
40 
44  template <typename T, bool isScalar = IsScalarType<T>::value>
45  struct ds_array {
46 
48  static inline T* get_and_fill(int sz) {
49  T* m = static_cast<T* >(operator new(sz*sizeof(T)));
50  T* p = m;
51  for (int i=0; i<sz; ++i)
52  new (p++) T(0.0);
53  return m;
54  }
55 
60  static inline T* get_and_fill(const T* src, int sz) {
61  T* m = static_cast<T* >(operator new(sz*sizeof(T)));
62  T* p = m;
63  for (int i=0; i<sz; ++i)
64  new (p++) T(*(src++));
65  return m;
66  }
67 
69  static inline void copy(const T* src, T* dest, int sz) {
70  for (int i=0; i<sz; ++i)
71  *(dest++) = *(src++);
72  }
73 
75  static inline void zero(T* dest, int sz) {
76  for (int i=0; i<sz; ++i)
77  *(dest++) = T(0.);
78  }
79 
81  static inline void destroy_and_release(T* m, int sz) {
82  T* e = m+sz;
83  for (T* b = m; b!=e; b++)
84  b->~T();
85  operator delete((void*) m);
86  }
87  };
88 
93  template <typename T>
94  struct ds_array<T,true> {
95 
97  static inline T* get_and_fill(int sz) {
98  T* m = static_cast<T* >(operator new(sz*sizeof(T)));
99  std::memset(m,0,sz*sizeof(T));
100  return m;
101  }
102 
107  static inline T* get_and_fill(const T* src, int sz) {
108  T* m = static_cast<T* >(operator new(sz*sizeof(T)));
109  for (int i=0; i<sz; ++i)
110  m[i] = src[i];
111  return m;
112  }
113 
115  static inline void copy(const T* src, T* dest, int sz) {
116  std::memcpy(dest,src,sz*sizeof(T));
117  }
118 
120  static inline void zero(T* dest, int sz) {
121  std::memset(dest,0,sz*sizeof(T));
122  }
123 
125  static inline void destroy_and_release(T* m, int sz) {
126  operator delete((void*) m);
127  }
128  };
129 
130 } // namespace Stokhos
131 
132 #endif // STOKHOS_DYNAMICARRAYTRAITS_HPP
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
static T * get_and_fill(const T *src, int sz)
Get memory for new array of length sz and fill with entries from src.
static void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
static T * get_and_fill(const T *src, int sz)
Get memory for new array of length sz and fill with entries from src.
#define STOKHOS_BUILTIN_SPECIALIZATION(t)
Specialization of above classes to built-in types.
static T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
static void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
static T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
Dynamic array allocation class that works for any type.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
Base template specification for IsScalarType.
static void zero(T *dest, int sz)
Zero out array dest of length sz.