00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef util_SimpleArrayOps_hpp
00025 #define util_SimpleArrayOps_hpp
00026
00027 namespace phdmesh {
00028
00040 template< unsigned N , typename T > void Copy( T * , const T & );
00041 template< unsigned N , typename T > void Copy( T * , const T * );
00042 template< unsigned N , typename T > void Sum( T * , const T * );
00043 template< unsigned N , typename T > void Sum( T * , const T & , const T * );
00044 template< unsigned N , typename T > void Prod( T * , const T * );
00045 template< unsigned N , typename T > void Min( T * , const T * );
00046 template< unsigned N , typename T > void Max( T * , const T * );
00047
00048 template< unsigned N , typename T > void BitOr( T * , const T * );
00049 template< unsigned N , typename T > void BitAnd( T * , const T * );
00050
00051 template< unsigned N , typename T > T InnerProduct( const T * , const T * );
00052
00053 template< unsigned N , typename T > bool Equal( const T * , const T * );
00054 template< unsigned N , typename T > bool NotEqual( const T * , const T * );
00055 template< unsigned N , typename T > bool Less( const T * , const T * );
00056 template< unsigned N , typename T > bool LessEqual( const T * , const T * );
00057 template< unsigned N , typename T > bool Greater( const T * , const T * );
00058 template< unsigned N , typename T > bool GreaterEqual( const T * , const T * );
00059
00060 }
00061
00062
00063
00064
00065 #ifndef DOXYGEN_COMPILE
00066
00067 namespace phdmesh {
00068 namespace impl {
00069
00070 template< typename T, unsigned n , unsigned i = 0 > struct Copy ;
00071 template< typename T, unsigned n , unsigned i = 0 > struct Sum ;
00072 template< typename T, unsigned n , unsigned i = 0 > struct Prod ;
00073 template< typename T, unsigned n , unsigned i = 0 > struct Max ;
00074 template< typename T, unsigned n , unsigned i = 0 > struct Min ;
00075 template< typename T, unsigned n , unsigned i = 0 > struct BitOr ;
00076 template< typename T, unsigned n , unsigned i = 0 > struct BitAnd ;
00077
00078 template< typename T, unsigned n, unsigned i = 0 > struct InnerProduct ;
00079 template< typename T, unsigned n, unsigned i = 0 > struct Equal ;
00080 template< typename T, unsigned n, unsigned i = 0 > struct NotEqual ;
00081 template< typename T, unsigned n, unsigned i = 0 > struct Less ;
00082 template< typename T, unsigned n, unsigned i = 0 > struct LessEqual ;
00083 template< typename T, unsigned n, unsigned i = 0 > struct Greater ;
00084 template< typename T, unsigned n, unsigned i = 0 > struct GreaterEqual ;
00085
00086
00087
00088
00089 template< typename T , unsigned n >
00090 struct Copy<T,n,n> {
00091 typedef T type ;
00092 enum { N = 0 };
00093 static void op( T * , const T * ) {}
00094 static void op( T * , const T & ) {}
00095
00096 Copy() : ptr(0) {}
00097 Copy( T * p ) : ptr(p) {}
00098 Copy( const Copy & rhs ) : ptr( rhs.ptr ) {}
00099 Copy & operator = ( const Copy & rhs ) { ptr = rhs.ptr ; return *this ; }
00100
00101 T * ptr ;
00102 };
00103
00104 template< typename T , unsigned n >
00105 struct Sum<T,n,n> {
00106 typedef T type ;
00107 enum { N = 0 };
00108 static void op( T * , const T * ) {}
00109 static void op( T * , const T & , const T * ) {}
00110
00111 Sum() : ptr(0) {}
00112 Sum( T * p ) : ptr(p) {}
00113 Sum( const Sum & rhs ) : ptr( rhs.ptr ) {}
00114 Sum & operator = ( const Sum & rhs ) { ptr = rhs.ptr ; return *this ; }
00115
00116 T * ptr ;
00117 };
00118
00119 template< typename T , unsigned n >
00120 struct Prod<T,n,n> {
00121 typedef T type ;
00122 enum { N = 0 };
00123 static void op( T * , const T * ) {}
00124
00125 Prod() : ptr(0) {}
00126 Prod( T * p ) : ptr(p) {}
00127 Prod( const Prod & rhs ) : ptr( rhs.ptr ) {}
00128 Prod & operator = ( const Prod & rhs ) { ptr = rhs.ptr ; return *this ; }
00129
00130 T * ptr ;
00131 };
00132
00133 template< typename T , unsigned n >
00134 struct Max<T,n,n> {
00135 typedef T type ;
00136 enum { N = 0 };
00137 static void op( T * , const T * ) {}
00138
00139 Max() : ptr(0) {}
00140 Max( T * p ) : ptr(p) {}
00141 Max( const Max & rhs ) : ptr( rhs.ptr ) {}
00142 Max & operator = ( const Max & rhs ) { ptr = rhs.ptr ; return *this ; }
00143
00144 T * ptr ;
00145 };
00146
00147 template< typename T , unsigned n >
00148 struct Min<T,n,n> {
00149 typedef T type ;
00150 enum { N = 0 };
00151 static void op( T * , const T * ) {}
00152
00153 Min() : ptr(0) {}
00154 Min( T * p ) : ptr(p) {}
00155 Min( const Min & rhs ) : ptr( rhs.ptr ) {}
00156 Min & operator = ( const Min & rhs ) { ptr = rhs.ptr ; return *this ; }
00157
00158 T * ptr ;
00159 };
00160
00161 template< typename T , unsigned n >
00162 struct BitOr<T,n,n> {
00163 typedef T type ;
00164 enum { N = 0 };
00165 static void op( T * , const T * ) {}
00166
00167 BitOr() : ptr(0) {}
00168 BitOr( T * p ) : ptr(p) {}
00169 BitOr( const BitOr & rhs ) : ptr( rhs.ptr ) {}
00170 BitOr & operator = ( const BitOr & rhs ) { ptr = rhs.ptr ; return *this ; }
00171
00172 T * ptr ;
00173 };
00174
00175 template< typename T , unsigned n >
00176 struct BitAnd<T,n,n> {
00177 typedef T type ;
00178 enum { N = 0 };
00179 static void op( T * , const T * ) {}
00180
00181 BitAnd() : ptr(0) {}
00182 BitAnd( T * p ) : ptr(p) {}
00183 BitAnd( const BitAnd & rhs ) : ptr( rhs.ptr ) {}
00184 BitAnd & operator = ( const BitAnd & rhs ) { ptr = rhs.ptr ; return *this ; }
00185
00186 T * ptr ;
00187 };
00188
00189 template< typename T , unsigned n >
00190 struct Equal<T,n,n> {
00191 static bool op( const T * , const T * ) { return true ; }
00192 };
00193
00194 template< typename T , unsigned n >
00195 struct NotEqual<T,n,n> {
00196 static bool op( const T * , const T * ) { return false ; }
00197 };
00198
00199 template< typename T , unsigned n >
00200 struct InnerProduct<T,n,n> {
00201 static T op( const T * , const T * ) { return 0 ; }
00202 };
00203
00204 template< typename T , unsigned n >
00205 struct Less<T,n,n> {
00206 static bool op( const T * , const T * ) { return false ; }
00207 };
00208
00209 template< typename T , unsigned n >
00210 struct LessEqual<T,n,n> {
00211 static bool op( const T * , const T * ) { return true ; }
00212 };
00213
00214 template< typename T , unsigned n >
00215 struct Greater<T,n,n> {
00216 static bool op( const T * , const T * ) { return false ; }
00217 };
00218
00219 template< typename T , unsigned n >
00220 struct GreaterEqual<T,n,n> {
00221 static bool op( const T * , const T * ) { return true ; }
00222 };
00223
00224
00225
00226 template< typename T , unsigned n , unsigned i >
00227 struct Copy {
00228 typedef T type ;
00229 enum { N = n };
00230
00231 static void op( T * dst , const T * src )
00232 { dst[i] = src[i] ; impl::Copy<T,N,i+1>::op(dst,src); }
00233
00234 static void op( T * dst , const T & src )
00235 { dst[i] = src ; impl::Copy<T,N,i+1>::op(dst,src); }
00236
00237 Copy() : ptr(0) {}
00238 Copy( T * p ) : ptr(p) {}
00239 Copy( const Copy & rhs ) : ptr( rhs.ptr ) {}
00240 Copy & operator = ( const Copy & rhs ) { ptr = rhs.ptr ; return *this ; }
00241
00242 T * ptr ;
00243 };
00244
00245 template< typename T , unsigned n , unsigned i >
00246 struct Sum {
00247 typedef T type ;
00248 enum { N = n };
00249
00250 static void op( T * dst , const T * src )
00251 { dst[i] += src[i] ; impl::Sum<T,N,i+1>::op(dst,src); }
00252
00253 static void op( T * dst , const T & a , const T * src )
00254 { dst[i] += a * src[i] ; impl::Sum<T,N,i+1>::op(dst,a,src); }
00255
00256 Sum() : ptr(0) {}
00257 Sum( T * p ) : ptr(p) {}
00258 Sum( const Sum & rhs ) : ptr( rhs.ptr ) {}
00259 Sum & operator = ( const Sum & rhs ) { ptr = rhs.ptr ; return *this ; }
00260
00261 T * ptr ;
00262 };
00263
00264 template< typename T , unsigned n , unsigned i >
00265 struct Prod {
00266 typedef T type ;
00267 enum { N = n };
00268
00269 static void op( T * dst , const T * src )
00270 { dst[i] *= src[i] ; impl::Prod<T,N,i+1>::op(dst,src); }
00271
00272 Prod() : ptr(0) {}
00273 Prod( T * p ) : ptr(p) {}
00274 Prod( const Prod & rhs ) : ptr( rhs.ptr ) {}
00275 Prod & operator = ( const Prod & rhs ) { ptr = rhs.ptr ; return *this ; }
00276
00277 T * ptr ;
00278 };
00279
00280 template< typename T , unsigned n , unsigned i >
00281 struct BitOr {
00282 typedef T type ;
00283 enum { N = n };
00284
00285 static void op( T * dst , const T * src )
00286 { dst[i] |= src[i] ; impl::BitOr<T,N,i+1>::op(dst,src); }
00287
00288 BitOr() : ptr(0) {}
00289 BitOr( T * p ) : ptr(p) {}
00290 BitOr( const BitOr & rhs ) : ptr( rhs.ptr ) {}
00291 BitOr & operator = ( const BitOr & rhs ) { ptr = rhs.ptr ; return *this ; }
00292
00293 T * ptr ;
00294 };
00295
00296 template< typename T , unsigned n , unsigned i >
00297 struct BitAnd {
00298 typedef T type ;
00299 enum { N = n };
00300
00301 static void op( T * dst , const T * src )
00302 { dst[i] |= src[i] ; impl::BitAnd<T,N,i+1>::op(dst,src); }
00303
00304 BitAnd() : ptr(0) {}
00305 BitAnd( T * p ) : ptr(p) {}
00306 BitAnd( const BitAnd & rhs ) : ptr( rhs.ptr ) {}
00307 BitAnd & operator = ( const BitAnd & rhs ) { ptr = rhs.ptr ; return *this ; }
00308
00309 T * ptr ;
00310 };
00311
00312 template< typename T , unsigned n , unsigned i >
00313 struct Max {
00314 typedef T type ;
00315 enum { N = n };
00316
00317 static void op( T * dst , const T * src )
00318 { if ( dst[i] < src[i] ) { dst[i] = src[i] ; }
00319 impl::Max<T,N,i+1>::op(dst,src); }
00320
00321 Max() : ptr(0) {}
00322 Max( T * p ) : ptr(p) {}
00323 Max( const Max & rhs ) : ptr( rhs.ptr ) {}
00324 Max & operator = ( const Max & rhs ) { ptr = rhs.ptr ; return *this ; }
00325
00326 T * ptr ;
00327 };
00328
00329 template< typename T , unsigned n , unsigned i >
00330 struct Min {
00331 typedef T type ;
00332 enum { N = n };
00333
00334 static void op( T * dst , const T * src )
00335 { if ( src[i] < dst[i] ) { dst[i] = src[i] ; }
00336 impl::Min<T,N,i+1>::op(dst,src); }
00337
00338 Min() : ptr(0) {}
00339 Min( T * p ) : ptr(p) {}
00340 Min( const Min & rhs ) : ptr( rhs.ptr ) {}
00341 Min & operator = ( const Min & rhs ) { ptr = rhs.ptr ; return *this ; }
00342
00343 T * ptr ;
00344 };
00345
00346
00347 template< typename T , unsigned n , unsigned i >
00348 struct InnerProduct {
00349 static T op( const T * x , const T * y )
00350 { return x[i] * y[i] + impl::InnerProduct<T,n,i+1>::op( x , y ); }
00351 };
00352
00353 template< typename T , unsigned n , unsigned i >
00354 struct Equal {
00355 static bool op( const T * x , const T * y )
00356 { return x[i] == y[i] && impl::Equal<T,n,i+1>::op(x,y); }
00357 };
00358
00359 template< typename T , unsigned n , unsigned i >
00360 struct NotEqual {
00361 static bool op( const T * x , const T * y )
00362 { return x[i] != y[i] || impl::NotEqual<T,n,i+1>::op(x,y); }
00363 };
00364
00365 template< typename T , unsigned n , unsigned i >
00366 struct Less {
00367 static bool op( const T * const lhs , const T * const rhs )
00368 { return lhs[i] != rhs[i] ? lhs[i] < rhs[i]
00369 : impl::Less<T,n,i+1>::op(lhs,rhs); }
00370 };
00371
00372 template< typename T , unsigned n , unsigned i >
00373 struct LessEqual {
00374 static bool op( const T * const lhs , const T * const rhs )
00375 { return lhs[i] != rhs[i] ? lhs[i] < rhs[i]
00376 : impl::LessEqual<T,n,i+1>::op(lhs,rhs); }
00377 };
00378
00379 template< typename T , unsigned n , unsigned i >
00380 struct Greater {
00381 static bool op( const T * const lhs , const T * const rhs )
00382 { return lhs[i] != rhs[i] ? lhs[i] > rhs[i]
00383 : impl::Greater<T,n,i+1>::op(lhs,rhs); }
00384 };
00385
00386 template< typename T , unsigned n , unsigned i >
00387 struct GreaterEqual {
00388 static bool op( const T * const lhs , const T * const rhs )
00389 { return lhs[i] != rhs[i] ? lhs[i] > rhs[i]
00390 : impl::GreaterEqual<T,n,i+1>::op(lhs,rhs); }
00391 };
00392
00393 }
00394
00395
00396
00397 template< unsigned N , typename T >
00398 inline
00399 impl::Copy<T,N> Copy( T * dst )
00400 { return impl::Copy<T,N,0>( dst ); }
00401
00402 template< unsigned N , typename T >
00403 inline
00404 impl::Sum<T,N> Sum( T * dst )
00405 { return impl::Sum<T,N,0>( dst ); }
00406
00407 template< unsigned N , typename T >
00408 inline
00409 impl::Prod<T,N> Prod( T * dst )
00410 { return impl::Prod<T,N,0>( dst ); }
00411
00412 template< unsigned N , typename T >
00413 inline
00414 impl::Max<T,N> Max( T * dst )
00415 { return impl::Max<T,N,0>( dst ); }
00416
00417 template< unsigned N , typename T >
00418 inline
00419 impl::Min<T,N> Min( T * dst )
00420 { return impl::Min<T,N,0>( dst ); }
00421
00422 template< unsigned N , typename T >
00423 inline
00424 impl::BitOr<T,N> BitOr( T * dst )
00425 { return impl::BitOr<T,N,0>( dst ); }
00426
00427 template< unsigned N , typename T >
00428 inline
00429 impl::BitAnd<T,N> BitAnd( T * dst )
00430 { return impl::BitAnd<T,N,0>( dst ); }
00431
00432 }
00433
00434 #endif
00435
00436
00437
00438
00439 namespace phdmesh {
00440
00442 template< unsigned N , typename T >
00443 inline
00444 void Copy( T * dst , const T * src )
00445 { impl::Copy<T,N,0>::op( dst , src ); }
00446
00448 template< unsigned N , typename T >
00449 inline
00450 void Copy( T * dst , const T & src )
00451 { impl::Copy<T,N,0>::op( dst , src ); }
00452
00454 template< unsigned N , typename T >
00455 inline
00456 void Sum( T * dst , const T * src )
00457 { impl::Sum<T,N,0>::op( dst , src ); }
00458
00460 template< unsigned N , typename T >
00461 inline
00462 void Sum( T * dst , const T & a , const T * src )
00463 { impl::Sum<T,N,0>::op( dst , a , src ); }
00464
00466 template< unsigned N , typename T >
00467 inline
00468 void Prod( T * dst , const T * src )
00469 { impl::Prod<T,N,0>::op( dst , src ); }
00470
00472 template< unsigned N , typename T >
00473 inline
00474 void Max( T * dst , const T * src )
00475 { impl::Max<T,N,0>::op( dst , src ); }
00476
00478 template< unsigned N , typename T >
00479 inline
00480 void Min( T * dst , const T * src )
00481 { impl::Min<T,N,0>::op( dst , src ); }
00482
00484 template< unsigned N , typename T >
00485 inline
00486 void BitOr( T * dst , const T * src )
00487 { impl::BitOr<T,N,0>::op( dst , src ); }
00488
00490 template< unsigned N , typename T >
00491 inline
00492 void BitAnd( T * dst , const T * src )
00493 { impl::BitAnd<T,N,0>::op( dst , src ); }
00494
00495
00496
00498 template< unsigned N , typename T >
00499 inline
00500 T InnerProduct( const T * x , const T * y )
00501 { return impl::InnerProduct<T,N,0>::op( x , y ); }
00502
00504 template< unsigned N , typename T >
00505 inline
00506 bool Equal( const T * x , const T * y )
00507 { return impl::Equal<T,N,0>::op( x , y ); }
00508
00510 template< unsigned N , typename T >
00511 inline
00512 bool NotEqual( const T * x , const T * y )
00513 { return impl::NotEqual<T,N,0>::op( x , y ); }
00514
00516 template< unsigned N , typename T >
00517 inline
00518 bool Less( const T * x , const T * y )
00519 { return impl::Less<T,N,0>::op( x , y ); }
00520
00522 template< unsigned N , typename T >
00523 inline
00524 bool LessEqual( const T * x , const T * y )
00525 { return impl::LessEqual<T,N,0>::op( x , y ); }
00526
00528 template< unsigned N , typename T >
00529 inline
00530 bool Greater( const T * x , const T * y )
00531 { return impl::Greater<T,N,0>::op( x , y ); }
00532
00534 template< unsigned N , typename T >
00535 inline
00536 bool GreaterEqual( const T * x , const T * y )
00537 { return impl::GreaterEqual<T,N,0>::op( x , y ); }
00538
00539 }
00540
00543
00544
00545 #endif
00546