Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Stokhos_DynamicArrayTraits.hpp
Go to the documentation of this file.
1// $Id$
2// $Source$
3// @HEADER
4// ***********************************************************************
5//
6// Stokhos Package
7// Copyright (2009) Sandia Corporation
8//
9// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10// license for use of this work by or on behalf of the U.S. Government.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38//
39// Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
40//
41// ***********************************************************************
42// @HEADER
43
44#ifndef STOKHOS_DYNAMICARRAYTRAITS_HPP
45#define STOKHOS_DYNAMICARRAYTRAITS_HPP
46
47#include <new>
48#include <cstring>
49
50namespace Stokhos {
51
53
57 template <typename T> struct IsScalarType {
58 static const bool value = false;
59 };
60
62#define STOKHOS_BUILTIN_SPECIALIZATION(t) \
63 template <> struct IsScalarType< t > { \
64 static const bool value = true; \
65 };
66
71
72#undef STOKHOS_BUILTIN_SPECIALIZATION
73
74
78 template <typename T, bool isScalar = IsScalarType<T>::value>
79 struct ds_array {
80
82 static inline T* get_and_fill(int sz) {
83 T* m = static_cast<T* >(operator new(sz*sizeof(T)));
84 T* p = m;
85 for (int i=0; i<sz; ++i)
86 new (p++) T(0.0);
87 return m;
88 }
89
94 static inline T* get_and_fill(const T* src, int sz) {
95 T* m = static_cast<T* >(operator new(sz*sizeof(T)));
96 T* p = m;
97 for (int i=0; i<sz; ++i)
98 new (p++) T(*(src++));
99 return m;
100 }
101
103 static inline void copy(const T* src, T* dest, int sz) {
104 for (int i=0; i<sz; ++i)
105 *(dest++) = *(src++);
106 }
107
109 static inline void zero(T* dest, int sz) {
110 for (int i=0; i<sz; ++i)
111 *(dest++) = T(0.);
112 }
113
115 static inline void destroy_and_release(T* m, int sz) {
116 T* e = m+sz;
117 for (T* b = m; b!=e; b++)
118 b->~T();
119 operator delete((void*) m);
120 }
121 };
122
127 template <typename T>
128 struct ds_array<T,true> {
129
131 static inline T* get_and_fill(int sz) {
132 T* m = static_cast<T* >(operator new(sz*sizeof(T)));
133 std::memset(m,0,sz*sizeof(T));
134 return m;
135 }
136
141 static inline T* get_and_fill(const T* src, int sz) {
142 T* m = static_cast<T* >(operator new(sz*sizeof(T)));
143 for (int i=0; i<sz; ++i)
144 m[i] = src[i];
145 return m;
146 }
147
149 static inline void copy(const T* src, T* dest, int sz) {
150 std::memcpy(dest,src,sz*sizeof(T));
151 }
152
154 static inline void zero(T* dest, int sz) {
155 std::memset(dest,0,sz*sizeof(T));
156 }
157
159 static inline void destroy_and_release(T* m, int sz) {
160 operator delete((void*) m);
161 }
162 };
163
164} // namespace Stokhos
165
166#endif // STOKHOS_DYNAMICARRAYTRAITS_HPP
#define STOKHOS_BUILTIN_SPECIALIZATION(t)
Specialization of above classes to built-in types.
Top-level namespace for Stokhos classes and functions.
Base template specification for IsScalarType.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
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.
static T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
Dynamic array allocation class that works for any type.
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 zero(T *dest, int sz)
Zero out array dest of length sz.
static void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
static T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.