24 #ifndef util_SimpleArrayOps_hpp
25 #define util_SimpleArrayOps_hpp
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 * );
48 template<
unsigned N ,
typename T >
void BitOr( T * ,
const T * );
49 template<
unsigned N ,
typename T >
void BitAnd( T * ,
const T * );
51 template<
unsigned N ,
typename T > T
InnerProduct(
const T * ,
const T * );
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 * );
65 #ifndef DOXYGEN_COMPILE
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 ;
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 ;
89 template<
typename T ,
unsigned n >
93 static void op( T * ,
const T * ) {}
94 static void op( T * ,
const T & ) {}
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 ; }
104 template<
typename T ,
unsigned n >
108 static void op( T * ,
const T * ) {}
109 static void op( T * ,
const T & ,
const T * ) {}
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 ; }
119 template<
typename T ,
unsigned n >
123 static void op( T * ,
const T * ) {}
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 ; }
133 template<
typename T ,
unsigned n >
137 static void op( T * ,
const T * ) {}
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 ; }
147 template<
typename T ,
unsigned n >
151 static void op( T * ,
const T * ) {}
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 ; }
161 template<
typename T ,
unsigned n >
162 struct BitOr<T,n,n> {
165 static void op( T * ,
const T * ) {}
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 ; }
175 template<
typename T ,
unsigned n >
179 static void op( T * ,
const T * ) {}
182 BitAnd( T * p ) : ptr(p) {}
184 BitAnd & operator = (
const BitAnd & rhs ) { ptr = rhs.ptr ;
return *this ; }
189 template<
typename T ,
unsigned n >
190 struct Equal<T,n,n> {
191 static bool op(
const T * ,
const T * ) {
return true ; }
194 template<
typename T ,
unsigned n >
196 static bool op(
const T * ,
const T * ) {
return false ; }
199 template<
typename T ,
unsigned n >
201 static T op(
const T * ,
const T * ) {
return 0 ; }
204 template<
typename T ,
unsigned n >
206 static bool op(
const T * ,
const T * ) {
return false ; }
209 template<
typename T ,
unsigned n >
211 static bool op(
const T * ,
const T * ) {
return true ; }
214 template<
typename T ,
unsigned n >
216 static bool op(
const T * ,
const T * ) {
return false ; }
219 template<
typename T ,
unsigned n >
221 static bool op(
const T * ,
const T * ) {
return true ; }
226 template<
typename T ,
unsigned n ,
unsigned i >
231 static void op( T * dst ,
const T * src )
232 { dst[i] = src[i] ; impl::Copy<T,N,i+1>::op(dst,src); }
234 static void op( T * dst ,
const T & src )
235 { dst[i] = src ; impl::Copy<T,N,i+1>::op(dst,src); }
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 ; }
245 template<
typename T ,
unsigned n ,
unsigned i >
250 static void op( T * dst ,
const T * src )
251 { dst[i] += src[i] ; impl::Sum<T,N,i+1>::op(dst,src); }
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); }
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 ; }
264 template<
typename T ,
unsigned n ,
unsigned i >
269 static void op( T * dst ,
const T * src )
270 { dst[i] *= src[i] ; impl::Prod<T,N,i+1>::op(dst,src); }
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 ; }
280 template<
typename T ,
unsigned n ,
unsigned i >
285 static void op( T * dst ,
const T * src )
286 { dst[i] |= src[i] ; impl::BitOr<T,N,i+1>::op(dst,src); }
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 ; }
296 template<
typename T ,
unsigned n ,
unsigned i >
301 static void op( T * dst ,
const T * src )
302 { dst[i] |= src[i] ; impl::BitAnd<T,N,i+1>::op(dst,src); }
305 BitAnd( T * p ) : ptr(p) {}
307 BitAnd & operator = (
const BitAnd & rhs ) { ptr = rhs.ptr ;
return *this ; }
312 template<
typename T ,
unsigned n ,
unsigned i >
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); }
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 ; }
329 template<
typename T ,
unsigned n ,
unsigned i >
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); }
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 ; }
347 template<
typename T ,
unsigned n ,
unsigned i >
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 ); }
353 template<
typename T ,
unsigned n ,
unsigned i >
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); }
359 template<
typename T ,
unsigned n ,
unsigned i >
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); }
365 template<
typename T ,
unsigned n ,
unsigned i >
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); }
372 template<
typename T ,
unsigned n ,
unsigned i >
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); }
379 template<
typename T ,
unsigned n ,
unsigned i >
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); }
386 template<
typename T ,
unsigned n ,
unsigned i >
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); }
397 template<
unsigned N ,
typename T >
399 impl::Copy<T,N>
Copy( T * dst )
400 {
return impl::Copy<T,N,0>( dst ); }
402 template<
unsigned N ,
typename T >
404 impl::Sum<T,N>
Sum( T * dst )
405 {
return impl::Sum<T,N,0>( dst ); }
407 template<
unsigned N ,
typename T >
409 impl::Prod<T,N>
Prod( T * dst )
410 {
return impl::Prod<T,N,0>( dst ); }
412 template<
unsigned N ,
typename T >
414 impl::Max<T,N>
Max( T * dst )
415 {
return impl::Max<T,N,0>( dst ); }
417 template<
unsigned N ,
typename T >
419 impl::Min<T,N>
Min( T * dst )
420 {
return impl::Min<T,N,0>( dst ); }
422 template<
unsigned N ,
typename T >
424 impl::BitOr<T,N>
BitOr( T * dst )
425 {
return impl::BitOr<T,N,0>( dst ); }
427 template<
unsigned N ,
typename T >
429 impl::BitAnd<T,N>
BitAnd( T * dst )
430 {
return impl::BitAnd<T,N,0>( dst ); }
442 template<
unsigned N ,
typename T >
444 void Copy( T * dst ,
const T * src )
445 { impl::Copy<T,N,0>::op( dst , src ); }
448 template<
unsigned N ,
typename T >
450 void Copy( T * dst ,
const T & src )
451 { impl::Copy<T,N,0>::op( dst , src ); }
454 template<
unsigned N ,
typename T >
456 void Sum( T * dst ,
const T * src )
457 { impl::Sum<T,N,0>::op( dst , src ); }
460 template<
unsigned N ,
typename T >
462 void Sum( T * dst ,
const T & a ,
const T * src )
463 { impl::Sum<T,N,0>::op( dst , a , src ); }
466 template<
unsigned N ,
typename T >
468 void Prod( T * dst ,
const T * src )
469 { impl::Prod<T,N,0>::op( dst , src ); }
472 template<
unsigned N ,
typename T >
474 void Max( T * dst ,
const T * src )
475 { impl::Max<T,N,0>::op( dst , src ); }
478 template<
unsigned N ,
typename T >
480 void Min( T * dst ,
const T * src )
481 { impl::Min<T,N,0>::op( dst , src ); }
484 template<
unsigned N ,
typename T >
486 void BitOr( T * dst ,
const T * src )
487 { impl::BitOr<T,N,0>::op( dst , src ); }
490 template<
unsigned N ,
typename T >
493 { impl::BitAnd<T,N,0>::op( dst , src ); }
498 template<
unsigned N ,
typename T >
501 {
return impl::InnerProduct<T,N,0>::op( x , y ); }
504 template<
unsigned N ,
typename T >
506 bool Equal(
const T * x ,
const T * y )
507 {
return impl::Equal<T,N,0>::op( x , y ); }
510 template<
unsigned N ,
typename T >
513 {
return impl::NotEqual<T,N,0>::op( x , y ); }
516 template<
unsigned N ,
typename T >
518 bool Less(
const T * x ,
const T * y )
519 {
return impl::Less<T,N,0>::op( x , y ); }
522 template<
unsigned N ,
typename T >
525 {
return impl::LessEqual<T,N,0>::op( x , y ); }
528 template<
unsigned N ,
typename T >
531 {
return impl::Greater<T,N,0>::op( x , y ); }
534 template<
unsigned N ,
typename T >
537 {
return impl::GreaterEqual<T,N,0>::op( x , y ); }
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]