phdMesh  Version of the Day
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
SimpleArrayOps.hpp
1 /*------------------------------------------------------------------------*/
2 /* phdMesh : Parallel Heterogneous Dynamic unstructured Mesh */
3 /* Copyright (2007) Sandia Corporation */
4 /* */
5 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6 /* license for use of this work by or on behalf of the U.S. Government. */
7 /* */
8 /* This library is free software; you can redistribute it and/or modify */
9 /* it under the terms of the GNU Lesser General Public License as */
10 /* published by the Free Software Foundation; either version 2.1 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* This library is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* Lesser General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU Lesser General Public */
19 /* License along with this library; if not, write to the Free Software */
20 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 */
21 /* USA */
22 /*------------------------------------------------------------------------*/
23 
24 #ifndef util_SimpleArrayOps_hpp
25 #define util_SimpleArrayOps_hpp
26 
27 namespace phdmesh {
28 
40 template< unsigned N , typename T > void Copy( T * , const T & );
41 template< unsigned N , typename T > void Copy( T * , const T * );
42 template< unsigned N , typename T > void Sum( T * , const T * );
43 template< unsigned N , typename T > void Sum( T * , const T & , const T * );
44 template< unsigned N , typename T > void Prod( T * , const T * );
45 template< unsigned N , typename T > void Min( T * , const T * );
46 template< unsigned N , typename T > void Max( T * , const T * );
47 
48 template< unsigned N , typename T > void BitOr( T * , const T * );
49 template< unsigned N , typename T > void BitAnd( T * , const T * );
50 
51 template< unsigned N , typename T > T InnerProduct( const T * , const T * );
52 
53 template< unsigned N , typename T > bool Equal( const T * , const T * );
54 template< unsigned N , typename T > bool NotEqual( const T * , const T * );
55 template< unsigned N , typename T > bool Less( const T * , const T * );
56 template< unsigned N , typename T > bool LessEqual( const T * , const T * );
57 template< unsigned N , typename T > bool Greater( const T * , const T * );
58 template< unsigned N , typename T > bool GreaterEqual( const T * , const T * );
59 
60 }
61 
62 //----------------------------------------------------------------------
63 //----------------------------------------------------------------------
64 
65 #ifndef DOXYGEN_COMPILE
66 
67 namespace phdmesh {
68 namespace impl {
69 
70 template< typename T, unsigned n , unsigned i = 0 > struct Copy ;
71 template< typename T, unsigned n , unsigned i = 0 > struct Sum ;
72 template< typename T, unsigned n , unsigned i = 0 > struct Prod ;
73 template< typename T, unsigned n , unsigned i = 0 > struct Max ;
74 template< typename T, unsigned n , unsigned i = 0 > struct Min ;
75 template< typename T, unsigned n , unsigned i = 0 > struct BitOr ;
76 template< typename T, unsigned n , unsigned i = 0 > struct BitAnd ;
77 
78 template< typename T, unsigned n, unsigned i = 0 > struct InnerProduct ;
79 template< typename T, unsigned n, unsigned i = 0 > struct Equal ;
80 template< typename T, unsigned n, unsigned i = 0 > struct NotEqual ;
81 template< typename T, unsigned n, unsigned i = 0 > struct Less ;
82 template< typename T, unsigned n, unsigned i = 0 > struct LessEqual ;
83 template< typename T, unsigned n, unsigned i = 0 > struct Greater ;
84 template< typename T, unsigned n, unsigned i = 0 > struct GreaterEqual ;
85 
86 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
88 
89 template< typename T , unsigned n >
90 struct Copy<T,n,n> {
91  typedef T type ;
92  enum { N = 0 };
93  static void op( T * , const T * ) {}
94  static void op( T * , const T & ) {}
95 
96  Copy() : ptr(0) {}
97  Copy( T * p ) : ptr(p) {}
98  Copy( const Copy & rhs ) : ptr( rhs.ptr ) {}
99  Copy & operator = ( const Copy & rhs ) { ptr = rhs.ptr ; return *this ; }
100 
101  T * ptr ;
102 };
103 
104 template< typename T , unsigned n >
105 struct Sum<T,n,n> {
106  typedef T type ;
107  enum { N = 0 };
108  static void op( T * , const T * ) {}
109  static void op( T * , const T & , const T * ) {}
110 
111  Sum() : ptr(0) {}
112  Sum( T * p ) : ptr(p) {}
113  Sum( const Sum & rhs ) : ptr( rhs.ptr ) {}
114  Sum & operator = ( const Sum & rhs ) { ptr = rhs.ptr ; return *this ; }
115 
116  T * ptr ;
117 };
118 
119 template< typename T , unsigned n >
120 struct Prod<T,n,n> {
121  typedef T type ;
122  enum { N = 0 };
123  static void op( T * , const T * ) {}
124 
125  Prod() : ptr(0) {}
126  Prod( T * p ) : ptr(p) {}
127  Prod( const Prod & rhs ) : ptr( rhs.ptr ) {}
128  Prod & operator = ( const Prod & rhs ) { ptr = rhs.ptr ; return *this ; }
129 
130  T * ptr ;
131 };
132 
133 template< typename T , unsigned n >
134 struct Max<T,n,n> {
135  typedef T type ;
136  enum { N = 0 };
137  static void op( T * , const T * ) {}
138 
139  Max() : ptr(0) {}
140  Max( T * p ) : ptr(p) {}
141  Max( const Max & rhs ) : ptr( rhs.ptr ) {}
142  Max & operator = ( const Max & rhs ) { ptr = rhs.ptr ; return *this ; }
143 
144  T * ptr ;
145 };
146 
147 template< typename T , unsigned n >
148 struct Min<T,n,n> {
149  typedef T type ;
150  enum { N = 0 };
151  static void op( T * , const T * ) {}
152 
153  Min() : ptr(0) {}
154  Min( T * p ) : ptr(p) {}
155  Min( const Min & rhs ) : ptr( rhs.ptr ) {}
156  Min & operator = ( const Min & rhs ) { ptr = rhs.ptr ; return *this ; }
157 
158  T * ptr ;
159 };
160 
161 template< typename T , unsigned n >
162 struct BitOr<T,n,n> {
163  typedef T type ;
164  enum { N = 0 };
165  static void op( T * , const T * ) {}
166 
167  BitOr() : ptr(0) {}
168  BitOr( T * p ) : ptr(p) {}
169  BitOr( const BitOr & rhs ) : ptr( rhs.ptr ) {}
170  BitOr & operator = ( const BitOr & rhs ) { ptr = rhs.ptr ; return *this ; }
171 
172  T * ptr ;
173 };
174 
175 template< typename T , unsigned n >
176 struct BitAnd<T,n,n> {
177  typedef T type ;
178  enum { N = 0 };
179  static void op( T * , const T * ) {}
180 
181  BitAnd() : ptr(0) {}
182  BitAnd( T * p ) : ptr(p) {}
183  BitAnd( const BitAnd & rhs ) : ptr( rhs.ptr ) {}
184  BitAnd & operator = ( const BitAnd & rhs ) { ptr = rhs.ptr ; return *this ; }
185 
186  T * ptr ;
187 };
188 
189 template< typename T , unsigned n >
190 struct Equal<T,n,n> {
191  static bool op( const T * , const T * ) { return true ; }
192 };
193 
194 template< typename T , unsigned n >
195 struct NotEqual<T,n,n> {
196  static bool op( const T * , const T * ) { return false ; }
197 };
198 
199 template< typename T , unsigned n >
200 struct InnerProduct<T,n,n> {
201  static T op( const T * , const T * ) { return 0 ; }
202 };
203 
204 template< typename T , unsigned n >
205 struct Less<T,n,n> {
206  static bool op( const T * , const T * ) { return false ; }
207 };
208 
209 template< typename T , unsigned n >
210 struct LessEqual<T,n,n> {
211  static bool op( const T * , const T * ) { return true ; }
212 };
213 
214 template< typename T , unsigned n >
215 struct Greater<T,n,n> {
216  static bool op( const T * , const T * ) { return false ; }
217 };
218 
219 template< typename T , unsigned n >
220 struct GreaterEqual<T,n,n> {
221  static bool op( const T * , const T * ) { return true ; }
222 };
223 
224 //----------------------------------------------------------------------
225 
226 template< typename T , unsigned n , unsigned i >
227 struct Copy {
228  typedef T type ;
229  enum { N = n };
230 
231  static void op( T * dst , const T * src )
232  { dst[i] = src[i] ; impl::Copy<T,N,i+1>::op(dst,src); }
233 
234  static void op( T * dst , const T & src )
235  { dst[i] = src ; impl::Copy<T,N,i+1>::op(dst,src); }
236 
237  Copy() : ptr(0) {}
238  Copy( T * p ) : ptr(p) {}
239  Copy( const Copy & rhs ) : ptr( rhs.ptr ) {}
240  Copy & operator = ( const Copy & rhs ) { ptr = rhs.ptr ; return *this ; }
241 
242  T * ptr ;
243 };
244 
245 template< typename T , unsigned n , unsigned i >
246 struct Sum {
247  typedef T type ;
248  enum { N = n };
249 
250  static void op( T * dst , const T * src )
251  { dst[i] += src[i] ; impl::Sum<T,N,i+1>::op(dst,src); }
252 
253  static void op( T * dst , const T & a , const T * src )
254  { dst[i] += a * src[i] ; impl::Sum<T,N,i+1>::op(dst,a,src); }
255 
256  Sum() : ptr(0) {}
257  Sum( T * p ) : ptr(p) {}
258  Sum( const Sum & rhs ) : ptr( rhs.ptr ) {}
259  Sum & operator = ( const Sum & rhs ) { ptr = rhs.ptr ; return *this ; }
260 
261  T * ptr ;
262 };
263 
264 template< typename T , unsigned n , unsigned i >
265 struct Prod {
266  typedef T type ;
267  enum { N = n };
268 
269  static void op( T * dst , const T * src )
270  { dst[i] *= src[i] ; impl::Prod<T,N,i+1>::op(dst,src); }
271 
272  Prod() : ptr(0) {}
273  Prod( T * p ) : ptr(p) {}
274  Prod( const Prod & rhs ) : ptr( rhs.ptr ) {}
275  Prod & operator = ( const Prod & rhs ) { ptr = rhs.ptr ; return *this ; }
276 
277  T * ptr ;
278 };
279 
280 template< typename T , unsigned n , unsigned i >
281 struct BitOr {
282  typedef T type ;
283  enum { N = n };
284 
285  static void op( T * dst , const T * src )
286  { dst[i] |= src[i] ; impl::BitOr<T,N,i+1>::op(dst,src); }
287 
288  BitOr() : ptr(0) {}
289  BitOr( T * p ) : ptr(p) {}
290  BitOr( const BitOr & rhs ) : ptr( rhs.ptr ) {}
291  BitOr & operator = ( const BitOr & rhs ) { ptr = rhs.ptr ; return *this ; }
292 
293  T * ptr ;
294 };
295 
296 template< typename T , unsigned n , unsigned i >
297 struct BitAnd {
298  typedef T type ;
299  enum { N = n };
300 
301  static void op( T * dst , const T * src )
302  { dst[i] |= src[i] ; impl::BitAnd<T,N,i+1>::op(dst,src); }
303 
304  BitAnd() : ptr(0) {}
305  BitAnd( T * p ) : ptr(p) {}
306  BitAnd( const BitAnd & rhs ) : ptr( rhs.ptr ) {}
307  BitAnd & operator = ( const BitAnd & rhs ) { ptr = rhs.ptr ; return *this ; }
308 
309  T * ptr ;
310 };
311 
312 template< typename T , unsigned n , unsigned i >
313 struct Max {
314  typedef T type ;
315  enum { N = n };
316 
317  static void op( T * dst , const T * src )
318  { if ( dst[i] < src[i] ) { dst[i] = src[i] ; }
319  impl::Max<T,N,i+1>::op(dst,src); }
320 
321  Max() : ptr(0) {}
322  Max( T * p ) : ptr(p) {}
323  Max( const Max & rhs ) : ptr( rhs.ptr ) {}
324  Max & operator = ( const Max & rhs ) { ptr = rhs.ptr ; return *this ; }
325 
326  T * ptr ;
327 };
328 
329 template< typename T , unsigned n , unsigned i >
330 struct Min {
331  typedef T type ;
332  enum { N = n };
333 
334  static void op( T * dst , const T * src )
335  { if ( src[i] < dst[i] ) { dst[i] = src[i] ; }
336  impl::Min<T,N,i+1>::op(dst,src); }
337 
338  Min() : ptr(0) {}
339  Min( T * p ) : ptr(p) {}
340  Min( const Min & rhs ) : ptr( rhs.ptr ) {}
341  Min & operator = ( const Min & rhs ) { ptr = rhs.ptr ; return *this ; }
342 
343  T * ptr ;
344 };
345 
346 
347 template< typename T , unsigned n , unsigned i >
348 struct InnerProduct {
349  static T op( const T * x , const T * y )
350  { return x[i] * y[i] + impl::InnerProduct<T,n,i+1>::op( x , y ); }
351 };
352 
353 template< typename T , unsigned n , unsigned i >
354 struct Equal {
355  static bool op( const T * x , const T * y )
356  { return x[i] == y[i] && impl::Equal<T,n,i+1>::op(x,y); }
357 };
358 
359 template< typename T , unsigned n , unsigned i >
360 struct NotEqual {
361  static bool op( const T * x , const T * y )
362  { return x[i] != y[i] || impl::NotEqual<T,n,i+1>::op(x,y); }
363 };
364 
365 template< typename T , unsigned n , unsigned i >
366 struct Less {
367  static bool op( const T * const lhs , const T * const rhs )
368  { return lhs[i] != rhs[i] ? lhs[i] < rhs[i]
369  : impl::Less<T,n,i+1>::op(lhs,rhs); }
370 };
371 
372 template< typename T , unsigned n , unsigned i >
373 struct LessEqual {
374  static bool op( const T * const lhs , const T * const rhs )
375  { return lhs[i] != rhs[i] ? lhs[i] < rhs[i]
376  : impl::LessEqual<T,n,i+1>::op(lhs,rhs); }
377 };
378 
379 template< typename T , unsigned n , unsigned i >
380 struct Greater {
381  static bool op( const T * const lhs , const T * const rhs )
382  { return lhs[i] != rhs[i] ? lhs[i] > rhs[i]
383  : impl::Greater<T,n,i+1>::op(lhs,rhs); }
384 };
385 
386 template< typename T , unsigned n , unsigned i >
387 struct GreaterEqual {
388  static bool op( const T * const lhs , const T * const rhs )
389  { return lhs[i] != rhs[i] ? lhs[i] > rhs[i]
390  : impl::GreaterEqual<T,n,i+1>::op(lhs,rhs); }
391 };
392 
393 } // namespace impl
394 
395 //-----------------------------------
396 
397 template< unsigned N , typename T >
398 inline
399 impl::Copy<T,N> Copy( T * dst )
400 { return impl::Copy<T,N,0>( dst ); }
401 
402 template< unsigned N , typename T >
403 inline
404 impl::Sum<T,N> Sum( T * dst )
405 { return impl::Sum<T,N,0>( dst ); }
406 
407 template< unsigned N , typename T >
408 inline
409 impl::Prod<T,N> Prod( T * dst )
410 { return impl::Prod<T,N,0>( dst ); }
411 
412 template< unsigned N , typename T >
413 inline
414 impl::Max<T,N> Max( T * dst )
415 { return impl::Max<T,N,0>( dst ); }
416 
417 template< unsigned N , typename T >
418 inline
419 impl::Min<T,N> Min( T * dst )
420 { return impl::Min<T,N,0>( dst ); }
421 
422 template< unsigned N , typename T >
423 inline
424 impl::BitOr<T,N> BitOr( T * dst )
425 { return impl::BitOr<T,N,0>( dst ); }
426 
427 template< unsigned N , typename T >
428 inline
429 impl::BitAnd<T,N> BitAnd( T * dst )
430 { return impl::BitAnd<T,N,0>( dst ); }
431 
432 } // namespace phdmesh
433 
434 #endif /* DOXYGEN_COMPILE */
435 
436 //----------------------------------------------------------------------
437 //----------------------------------------------------------------------
438 
439 namespace phdmesh {
440 
442 template< unsigned N , typename T >
443 inline
444 void Copy( T * dst , const T * src )
445 { impl::Copy<T,N,0>::op( dst , src ); }
446 
448 template< unsigned N , typename T >
449 inline
450 void Copy( T * dst , const T & src )
451 { impl::Copy<T,N,0>::op( dst , src ); }
452 
454 template< unsigned N , typename T >
455 inline
456 void Sum( T * dst , const T * src )
457 { impl::Sum<T,N,0>::op( dst , src ); }
458 
460 template< unsigned N , typename T >
461 inline
462 void Sum( T * dst , const T & a , const T * src )
463 { impl::Sum<T,N,0>::op( dst , a , src ); }
464 
466 template< unsigned N , typename T >
467 inline
468 void Prod( T * dst , const T * src )
469 { impl::Prod<T,N,0>::op( dst , src ); }
470 
472 template< unsigned N , typename T >
473 inline
474 void Max( T * dst , const T * src )
475 { impl::Max<T,N,0>::op( dst , src ); }
476 
478 template< unsigned N , typename T >
479 inline
480 void Min( T * dst , const T * src )
481 { impl::Min<T,N,0>::op( dst , src ); }
482 
484 template< unsigned N , typename T >
485 inline
486 void BitOr( T * dst , const T * src )
487 { impl::BitOr<T,N,0>::op( dst , src ); }
488 
490 template< unsigned N , typename T >
491 inline
492 void BitAnd( T * dst , const T * src )
493 { impl::BitAnd<T,N,0>::op( dst , src ); }
494 
495 //-----------------------------------
496 
498 template< unsigned N , typename T >
499 inline
500 T InnerProduct( const T * x , const T * y )
501 { return impl::InnerProduct<T,N,0>::op( x , y ); }
502 
504 template< unsigned N , typename T >
505 inline
506 bool Equal( const T * x , const T * y )
507 { return impl::Equal<T,N,0>::op( x , y ); }
508 
510 template< unsigned N , typename T >
511 inline
512 bool NotEqual( const T * x , const T * y )
513 { return impl::NotEqual<T,N,0>::op( x , y ); }
514 
516 template< unsigned N , typename T >
517 inline
518 bool Less( const T * x , const T * y )
519 { return impl::Less<T,N,0>::op( x , y ); }
520 
522 template< unsigned N , typename T >
523 inline
524 bool LessEqual( const T * x , const T * y )
525 { return impl::LessEqual<T,N,0>::op( x , y ); }
526 
528 template< unsigned N , typename T >
529 inline
530 bool Greater( const T * x , const T * y )
531 { return impl::Greater<T,N,0>::op( x , y ); }
532 
534 template< unsigned N , typename T >
535 inline
536 bool GreaterEqual( const T * x , const T * y )
537 { return impl::GreaterEqual<T,N,0>::op( x , y ); }
538 
539 } // namespace phdmesh
540 
543 //-----------------------------------
544 
545 #endif /* util_SimpleArrayOps_hpp */
546 
void Copy(T *, const T &)
dst[k] = src , k = 0..N-1
bool Less(const T *, const T *)
return x[k] < y[k] , first k such that x[k] != y[k]
bool Equal(const T *, const T *)
return x[k] == y[k] , k == 0..N-1
void Sum(T *, const T *)
dst[k] += src[k] , k = 0..N-1
void BitAnd(T *, const T *)
dst[k] &= src[k] , k = 0..N-1
void BitOr(T *, const T *)
dst[k] |= src[k] , k = 0..N-1
T InnerProduct(const T *, const T *)
return sum_k( x[k] * y[k] )
bool GreaterEqual(const T *, const T *)
return x[k] => y[k] , first k such that x[k] != y[k]
void Max(T *, const T *)
dst[k] = max( dst[k] , src[k] ) , k = 0..N-1
void Min(T *, const T *)
dst[k] = min( dst[k] , src[k] ) , k = 0..N-1
bool NotEqual(const T *, const T *)
return ! ( x[k] == y[k] , k == 0..N-1 )
bool Greater(const T *, const T *)
return x[k] > y[k] , first k such that x[k] != y[k]
void Prod(T *, const T *)
dst[k] *= src[k] , k = 0..N-1
bool LessEqual(const T *, const T *)
return x[k] <= y[k] , first k such that x[k] != y[k]