shards Version of the Day
Loading...
Searching...
No Matches
Shards_SimpleArrayOps.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5// Shards : Shared Discretization Tools
6// Copyright 2008 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Carter Edwards (hcedwar@sandia.gov),
39// Pavel Bochev (pbboche@sandia.gov), or
40// Denis Ridzal (dridzal@sandia.gov).
41//
42// ************************************************************************
43//@HEADER
44*/
45
46#ifndef Shards_SimpleArrayOps_hpp
47#define Shards_SimpleArrayOps_hpp
48
49namespace shards {
50
64template< unsigned n , unsigned i = 0 >
65struct Copy {
66 enum { N = n , I = i };
67
69 template<typename T>
70 inline Copy( T * const dst , const T * const src )
71 { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); }
72
74 template<typename T>
75 inline Copy( T * const dst , const T src )
76 { dst[I] = src ; Copy<N-1,I+1>(dst,src); }
77
78 Copy() {}
79};
80
84template< unsigned n , unsigned i = 0 >
85struct Sum {
86 enum { N = n , I = i };
87
89 template<typename T>
90 inline Sum( T * const dst , const T * const src )
91 { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); }
92
94 template<typename T>
95 inline Sum( T * const dst , const T a , const T * const src )
96 { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); }
97
98 Sum() {}
99};
100
104template< unsigned n , unsigned i = 0 >
105struct Prod {
106 enum { N = n , I = i };
107
109 template<typename T>
110 inline Prod( T * const dst , const T * const src )
111 { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); }
112 Prod() {}
113};
114
118template< unsigned n , unsigned i = 0 >
119struct BitOr {
120 enum { N = n , I = i };
121
123 template<typename T>
124 inline BitOr( T * const dst , const T * const src )
125 { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); }
126
127 BitOr() {}
128};
129
133template< unsigned n , unsigned i = 0 >
134struct BitAnd {
135 enum { N = n , I = i };
136
138 template<typename T>
139 inline BitAnd( T * const dst , const T * const src )
140 { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); }
141
142 BitAnd() {}
143};
144
148template< unsigned n , unsigned i = 0 >
149struct Max {
150 enum { N = n , I = i };
151
153 template<typename T>
154 inline Max( T * const dst , const T * const src )
155 { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); }
156
157 Max() {}
158};
159
163template< unsigned n , unsigned i = 0 >
164struct Min {
165 enum { N = n , I = i };
166
168 template<typename T>
169 inline Min( T * const dst , const T * const src )
170 { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); }
171
172 Min() {}
173};
174
178template< unsigned n , unsigned i = 0 >
180 enum { N = n , I = i };
181
183 template<typename T>
184 inline InnerProduct( T & value , const T * const x , const T * const y )
185 { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); }
186
187 InnerProduct() {}
188};
189
193template< unsigned n , unsigned i = 0 >
194struct Compare {
195 enum { N = n , I = i };
196
198 template<typename T>
199 inline static bool equal( const T * const x , const T * const y )
200 { return x[I] == y[I] && Compare<N-1,I+1>::equal(x,y); }
201
203 template<typename T>
204 inline static bool not_equal( const T * const x , const T * const y )
205 { return x[I] != y[I] || Compare<N-1,I+1>::not_equal(x,y); }
206
208 template<typename T>
209 inline static bool less( const T * const x , const T * const y )
210 {
211 return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less(x,y);
212 }
213
215 template<typename T>
216 inline static bool less_equal( const T * const x , const T * const y )
217 {
218 return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less_equal(x,y);
219 }
220
222 template<typename T>
223 inline static bool greater( const T * const x , const T * const y )
224 {
225 return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater(x,y);
226 }
227
229 template<typename T>
230 inline static bool greater_equal( const T * const x , const T * const y )
231 {
232 return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater_equal(x,y);
233 }
234
235 Compare() {}
236};
237
240//----------------------------------------------------------------------
241
242#ifndef DOXYGEN_COMPILE
243
244template<unsigned i>
245struct Copy<0,i> {
246 enum { N = 0 };
247 Copy() {}
248 template<typename T> inline Copy( T * const , const T * const ) {}
249 template<typename T> inline Copy( T * const , const T ) {}
250};
251
252template<unsigned i>
253struct Sum<0,i> {
254 enum { N = 0 };
255 Sum() {}
256 template<typename T> inline Sum( T * const , const T * const ) {}
257 template<typename T> inline Sum( T * const , const T , const T * const ) {}
258};
259
260template<unsigned i>
261struct Prod<0,i> {
262 enum { N = 0 };
263 Prod() {}
264 template<typename T> inline Prod( T * const , const T * const ) {}
265};
266
267template<unsigned i>
268struct Max<0,i> {
269 enum { N = 0 };
270 Max() {}
271 template<typename T> inline Max( T * const , const T * const ) {}
272};
273
274template<unsigned i>
275struct Min<0,i> {
276 enum { N = 0 };
277 Min() {}
278 template<typename T> inline Min( T * const , const T * const ) {}
279};
280
281template<unsigned i>
282struct BitOr<0,i> {
283 enum { N = 0 };
284 BitOr() {}
285 template<typename T> inline BitOr( T * const , const T * const ) {}
286};
287
288template<unsigned i>
289struct BitAnd<0,i> {
290 enum { N = 0 };
291 BitAnd() {}
292 template<typename T> inline BitAnd( T * const , const T * const ) {}
293};
294
295template<unsigned i>
296struct InnerProduct<0,i> {
297 enum { N = 0 };
298 InnerProduct() {}
299 template<typename T>
300 inline InnerProduct( T & , const T * const , const T * const ) {}
301};
302
303template<unsigned i>
304struct Compare<0,i> {
305 enum { N = 0 };
306 Compare() {}
307
308 template<typename T>
309 inline static bool equal( const T * const , const T * const )
310 { return true ; }
311
312 template<typename T>
313 inline static bool not_equal( const T * const , const T * const )
314 { return false ; }
315
316 template<typename T>
317 inline static bool less( const T * const , const T * const )
318 { return false ; }
319
320 template<typename T>
321 inline static bool less_equal( const T * const , const T * const )
322 { return true ; }
323
324 template<typename T>
325 inline static bool greater( const T * const , const T * const )
326 { return false ; }
327
328 template<typename T>
329 inline static bool greater_equal( const T * const , const T * const )
330 { return true ; }
331};
332
333
334#endif /* DOXYGEN_COMPILE */
335
336} // namespace shards
337
338#endif
339
Bitwise-and into an array.
BitAnd(T *const dst, const T *const src)
dst[0..N-1] &= src[0..N-1]
Bitwise-or into an array.
BitOr(T *const dst, const T *const src)
dst[0..N-1] |= src[0..N-1]
Lexicographical comparison of two arrays.
static bool greater_equal(const T *const x, const T *const y)
First non-equal members satisfies x[k] >= y[k].
static bool equal(const T *const x, const T *const y)
All members are equal.
static bool not_equal(const T *const x, const T *const y)
All members are not equal.
static bool less(const T *const x, const T *const y)
First non-equal members satisfy x[k] < y[k].
static bool greater(const T *const x, const T *const y)
First non-equal members satisfies x[k] > y[k].
static bool less_equal(const T *const x, const T *const y)
First non-equal members satisfies x[k] <= y[k].
Copy into an array.
Copy(T *const dst, const T src)
dst[0..N-1] = src
Copy(T *const dst, const T *const src)
dst[0..N-1] = src[0..N-1]
Inner product of two arrays.
InnerProduct(T &value, const T *const x, const T *const y)
value += sum[ k = 0..N-1 ]( x[k] * y[k] )
Take maximum value of each member of two arrays.
Max(T *const dst, const T *const src)
dst[0..N-1] = max( dst[0..N-1] , src[0..N-1] )
Take minimum value of each member of two arrays.
Min(T *const dst, const T *const src)
dst[0..N-1] = min( dst[0..N-1] , src[0..N-1] )
Scale into an array.
Prod(T *const dst, const T *const src)
dst[0..N-1] *= src[0..N-1]
Sum into an array.
Sum(T *const dst, const T *const src)
dst[0..N-1] += src[0..N-1]
Sum(T *const dst, const T a, const T *const src)
dst[0..N-1] += a * src[0..N-1]