shards  Version of the Day
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups
Shards_SimpleArrayOps.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Shards : Shared Discretization Tools
4 //
5 // Copyright 2008-2011 NTESS and the Shards contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef Shards_SimpleArrayOps_hpp
11 #define Shards_SimpleArrayOps_hpp
12 
13 namespace shards {
14 
28 template< unsigned n , unsigned i = 0 >
29 struct Copy {
30  enum { N = n , I = i };
31 
33  template<typename T>
34  inline Copy( T * const dst , const T * const src )
35  { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); }
36 
38  template<typename T>
39  inline Copy( T * const dst , const T src )
40  { dst[I] = src ; Copy<N-1,I+1>(dst,src); }
41 
42  Copy() {}
43 };
44 
48 template< unsigned n , unsigned i = 0 >
49 struct Sum {
50  enum { N = n , I = i };
51 
53  template<typename T>
54  inline Sum( T * const dst , const T * const src )
55  { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); }
56 
58  template<typename T>
59  inline Sum( T * const dst , const T a , const T * const src )
60  { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); }
61 
62  Sum() {}
63 };
64 
68 template< unsigned n , unsigned i = 0 >
69 struct Prod {
70  enum { N = n , I = i };
71 
73  template<typename T>
74  inline Prod( T * const dst , const T * const src )
75  { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); }
76  Prod() {}
77 };
78 
82 template< unsigned n , unsigned i = 0 >
83 struct BitOr {
84  enum { N = n , I = i };
85 
87  template<typename T>
88  inline BitOr( T * const dst , const T * const src )
89  { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); }
90 
91  BitOr() {}
92 };
93 
97 template< unsigned n , unsigned i = 0 >
98 struct BitAnd {
99  enum { N = n , I = i };
100 
102  template<typename T>
103  inline BitAnd( T * const dst , const T * const src )
104  { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); }
105 
106  BitAnd() {}
107 };
108 
112 template< unsigned n , unsigned i = 0 >
113 struct Max {
114  enum { N = n , I = i };
115 
117  template<typename T>
118  inline Max( T * const dst , const T * const src )
119  { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); }
120 
121  Max() {}
122 };
123 
127 template< unsigned n , unsigned i = 0 >
128 struct Min {
129  enum { N = n , I = i };
130 
132  template<typename T>
133  inline Min( T * const dst , const T * const src )
134  { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); }
135 
136  Min() {}
137 };
138 
142 template< unsigned n , unsigned i = 0 >
143 struct InnerProduct {
144  enum { N = n , I = i };
145 
147  template<typename T>
148  inline InnerProduct( T & value , const T * const x , const T * const y )
149  { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); }
150 
151  InnerProduct() {}
152 };
153 
157 template< unsigned n , unsigned i = 0 >
158 struct Compare {
159  enum { N = n , I = i };
160 
162  template<typename T>
163  inline static bool equal( const T * const x , const T * const y )
164  { return x[I] == y[I] && Compare<N-1,I+1>::equal(x,y); }
165 
167  template<typename T>
168  inline static bool not_equal( const T * const x , const T * const y )
169  { return x[I] != y[I] || Compare<N-1,I+1>::not_equal(x,y); }
170 
172  template<typename T>
173  inline static bool less( const T * const x , const T * const y )
174  {
175  return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less(x,y);
176  }
177 
179  template<typename T>
180  inline static bool less_equal( const T * const x , const T * const y )
181  {
182  return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less_equal(x,y);
183  }
184 
186  template<typename T>
187  inline static bool greater( const T * const x , const T * const y )
188  {
189  return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater(x,y);
190  }
191 
193  template<typename T>
194  inline static bool greater_equal( const T * const x , const T * const y )
195  {
196  return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater_equal(x,y);
197  }
198 
199  Compare() {}
200 };
201 
204 //----------------------------------------------------------------------
205 
206 #ifndef DOXYGEN_COMPILE
207 
208 template<unsigned i>
209 struct Copy<0,i> {
210  enum { N = 0 };
211  Copy() {}
212  template<typename T> inline Copy( T * const , const T * const ) {}
213  template<typename T> inline Copy( T * const , const T ) {}
214 };
215 
216 template<unsigned i>
217 struct Sum<0,i> {
218  enum { N = 0 };
219  Sum() {}
220  template<typename T> inline Sum( T * const , const T * const ) {}
221  template<typename T> inline Sum( T * const , const T , const T * const ) {}
222 };
223 
224 template<unsigned i>
225 struct Prod<0,i> {
226  enum { N = 0 };
227  Prod() {}
228  template<typename T> inline Prod( T * const , const T * const ) {}
229 };
230 
231 template<unsigned i>
232 struct Max<0,i> {
233  enum { N = 0 };
234  Max() {}
235  template<typename T> inline Max( T * const , const T * const ) {}
236 };
237 
238 template<unsigned i>
239 struct Min<0,i> {
240  enum { N = 0 };
241  Min() {}
242  template<typename T> inline Min( T * const , const T * const ) {}
243 };
244 
245 template<unsigned i>
246 struct BitOr<0,i> {
247  enum { N = 0 };
248  BitOr() {}
249  template<typename T> inline BitOr( T * const , const T * const ) {}
250 };
251 
252 template<unsigned i>
253 struct BitAnd<0,i> {
254  enum { N = 0 };
255  BitAnd() {}
256  template<typename T> inline BitAnd( T * const , const T * const ) {}
257 };
258 
259 template<unsigned i>
260 struct InnerProduct<0,i> {
261  enum { N = 0 };
262  InnerProduct() {}
263  template<typename T>
264  inline InnerProduct( T & , const T * const , const T * const ) {}
265 };
266 
267 template<unsigned i>
268 struct Compare<0,i> {
269  enum { N = 0 };
270  Compare() {}
271 
272  template<typename T>
273  inline static bool equal( const T * const , const T * const )
274  { return true ; }
275 
276  template<typename T>
277  inline static bool not_equal( const T * const , const T * const )
278  { return false ; }
279 
280  template<typename T>
281  inline static bool less( const T * const , const T * const )
282  { return false ; }
283 
284  template<typename T>
285  inline static bool less_equal( const T * const , const T * const )
286  { return true ; }
287 
288  template<typename T>
289  inline static bool greater( const T * const , const T * const )
290  { return false ; }
291 
292  template<typename T>
293  inline static bool greater_equal( const T * const , const T * const )
294  { return true ; }
295 };
296 
297 
298 #endif /* DOXYGEN_COMPILE */
299 
300 } // namespace shards
301 
302 #endif
303 
static bool greater(const T *const x, const T *const y)
First non-equal members satisfies x[k] &gt; y[k].
Min(T *const dst, const T *const src)
dst[0..N-1] = min( dst[0..N-1] , src[0..N-1] )
BitOr(T *const dst, const T *const src)
dst[0..N-1] |= src[0..N-1]
Bitwise-or into an array.
Take maximum value of each member of two arrays.
static bool not_equal(const T *const x, const T *const y)
All members are not equal.
static bool less_equal(const T *const x, const T *const y)
First non-equal members satisfies x[k] &lt;= y[k].
Take minimum value of each member of two arrays.
Copy(T *const dst, const T *const src)
dst[0..N-1] = src[0..N-1]
Sum into an array.
Copy(T *const dst, const T src)
dst[0..N-1] = src
Inner product of two arrays.
static bool greater_equal(const T *const x, const T *const y)
First non-equal members satisfies x[k] &gt;= y[k].
Lexicographical comparison of two arrays.
static bool less(const T *const x, const T *const y)
First non-equal members satisfy x[k] &lt; y[k].
BitAnd(T *const dst, const T *const src)
dst[0..N-1] &amp;= src[0..N-1]
InnerProduct(T &value, const T *const x, const T *const y)
value += sum[ k = 0..N-1 ]( x[k] * y[k] )
Sum(T *const dst, const T *const src)
dst[0..N-1] += src[0..N-1]
static bool equal(const T *const x, const T *const y)
All members are equal.
Prod(T *const dst, const T *const src)
dst[0..N-1] *= src[0..N-1]
Bitwise-and into an array.
Sum(T *const dst, const T a, const T *const src)
dst[0..N-1] += a * src[0..N-1]
Scale into an array.
Max(T *const dst, const T *const src)
dst[0..N-1] = max( dst[0..N-1] , src[0..N-1] )
Copy into an array.