|
Sierra Toolkit
Version of the Day
|
00001 /*------------------------------------------------------------------------*/ 00002 /* Copyright 2010 Sandia Corporation. */ 00003 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */ 00004 /* license for use of this work by or on behalf of the U.S. Government. */ 00005 /* Export of this program may require a license from the */ 00006 /* United States Government. */ 00007 /*------------------------------------------------------------------------*/ 00008 00009 #ifndef stk_util_util_SimpleArrayOps_hpp 00010 #define stk_util_util_SimpleArrayOps_hpp 00011 00012 namespace stk_classic { 00013 00015 00016 // Basic operations for compile-time fixed length arrays 00017 // 00018 // Example: Sum<5>(x,y) results in x[i] += y[i] for i=0..4 00019 00020 template< unsigned n, int i=0> struct Copy ; 00021 template< unsigned n, int i=0> struct Sum ; 00022 template< unsigned n, int i=0> struct Prod ; 00023 template< unsigned n, int i=0> struct Max ; 00024 template< unsigned n, int i=0> struct Min ; 00025 template< unsigned n, int i=0> struct BitOr ; 00026 template< unsigned n, int i=0> struct BitAnd ; 00027 template< unsigned n, int i=0> struct InnerProduct ; 00028 template< unsigned n, int i=0> struct Compare ; 00029 00030 //---------------------------------------------------------------------- 00031 00032 template<int i> 00033 struct Copy<0,i> { 00034 enum { N = 0 }; 00035 Copy() {} 00036 template<typename T> inline Copy( T * const , const T * const ) {} 00037 template<typename T> inline Copy( T * const , const T ) {} 00038 }; 00039 00040 template<int i> 00041 struct Sum<0,i> { 00042 enum { N = 0 }; 00043 Sum() {} 00044 template<typename T> inline Sum( T * const , const T * const ) {} 00045 template<typename T> inline Sum( T * const , const T , const T * const ) {} 00046 }; 00047 00048 template<int i> 00049 struct Prod<0,i> { 00050 enum { N = 0 }; 00051 Prod() {} 00052 template<typename T> inline Prod( T * const , const T * const ) {} 00053 }; 00054 00055 template<int i> 00056 struct Max<0,i> { 00057 enum { N = 0 }; 00058 Max() {} 00059 template<typename T> inline Max( T * const , const T * const ) {} 00060 }; 00061 00062 template<int i> 00063 struct Min<0,i> { 00064 enum { N = 0 }; 00065 Min() {} 00066 template<typename T> inline Min( T * const , const T * const ) {} 00067 }; 00068 00069 template<int i> 00070 struct BitOr<0,i> { 00071 enum { N = 0 }; 00072 BitOr() {} 00073 template<typename T> inline BitOr( T * const , const T * const ) {} 00074 }; 00075 00076 template<int i> 00077 struct BitAnd<0,i> { 00078 enum { N = 0 }; 00079 BitAnd() {} 00080 template<typename T> inline BitAnd( T * const , const T * const ) {} 00081 }; 00082 00083 template<int i> 00084 struct InnerProduct<0,i> { 00085 enum { N = 0 }; 00086 InnerProduct() {} 00087 template<typename T> 00088 inline InnerProduct( T & value , const T * const x , const T * const y ) {} 00089 }; 00090 00091 template<int i> 00092 struct Compare<0,i> { 00093 enum { N = 0 }; 00094 Compare() {} 00095 00096 template<typename T> 00097 inline static bool equal( const T * const , const T * const ) 00098 { return true ; } 00099 00100 template<typename T> 00101 inline static bool not_equal( const T * const , const T * const ) 00102 { return false ; } 00103 00104 template<typename T> 00105 inline static bool less( const T * const , const T * const ) 00106 { return false ; } 00107 00108 template<typename T> 00109 inline static bool less_equal( const T * const , const T * const ) 00110 { return true ; } 00111 00112 template<typename T> 00113 inline static bool greater( const T * const , const T * const ) 00114 { return false ; } 00115 00116 template<typename T> 00117 inline static bool greater_equal( const T * const , const T * const ) 00118 { return true ; } 00119 }; 00120 00121 00122 template<unsigned n> 00123 struct Copy<n,-1> { 00124 enum { N = n }; 00125 Copy() {} 00126 00127 template<typename T> 00128 inline Copy( T * const dst , const T * const src ) 00129 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] = src[i]; } } 00130 00131 template<typename T> 00132 inline Copy( T * const dst , const T src ) 00133 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] = src ; } } 00134 }; 00135 00136 template<unsigned n> 00137 struct Sum<n,-1> { 00138 enum { N = n }; 00139 Sum() {} 00140 00141 template<typename T> 00142 inline Sum( T * const dst , const T * const src ) 00143 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] += src[i]; } } 00144 00145 template<typename T> 00146 inline Sum( T * const dst , const T a , const T * const src ) 00147 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] += a * src[i]; } } 00148 }; 00149 00150 template<unsigned n> 00151 struct Prod<n,-1> { 00152 enum { N = n }; 00153 Prod() {} 00154 template<typename T> 00155 inline Prod( T * const dst , const T * const src ) 00156 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] *= src[i]; } } 00157 }; 00158 00159 template<unsigned n> 00160 struct BitOr<n,-1> { 00161 enum { N = n }; 00162 BitOr() {} 00163 template<typename T> 00164 inline BitOr( T * const dst , const T * const src ) 00165 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] |= src[i]; } } 00166 }; 00167 00168 template<unsigned n> 00169 struct BitAnd<n,-1> { 00170 enum { N = n }; 00171 BitAnd() {} 00172 template<typename T> 00173 inline BitAnd( T * const dst , const T * const src ) 00174 { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] &= src[i]; } } 00175 }; 00176 00177 template<unsigned n> 00178 struct Max<n,-1> { 00179 enum { N = n }; 00180 Max() {} 00181 template<typename T> 00182 inline Max( T * const dst , const T * const src ) 00183 { 00184 for ( unsigned i = 0 ; i < N ; ++i ) 00185 { if ( dst[i] < src[i] ) { dst[i] = src[i] ; } } 00186 } 00187 }; 00188 00189 template<unsigned n> 00190 struct Min<n,-1> { 00191 enum { N = n }; 00192 Min() {} 00193 template<typename T> 00194 inline Min( T * const dst , const T * const src ) 00195 { 00196 for ( unsigned i = 0 ; i < N ; ++i ) 00197 { if ( src[i] < dst[i] ) { dst[i] = src[i] ; } } 00198 } 00199 }; 00200 00201 template<unsigned n> 00202 struct InnerProduct<n,-1> { 00203 enum { N = n }; 00204 InnerProduct() {} 00205 template<typename T> 00206 inline InnerProduct( T & value , const T * const x , const T * const y ) 00207 { for ( unsigned i = 0 ; i < N ; ++i ) { value += x[i] * y[i] ; } } 00208 }; 00209 00210 00211 template<unsigned n> 00212 struct Compare<n,-1> { 00213 enum { N = n }; 00214 Compare() {} 00215 00216 template<typename T> 00217 inline static bool equal( const T * const dst , const T * const src ) 00218 { 00219 bool result = true ; 00220 for ( unsigned i = 0 ; result && i < N ; ++i ) 00221 { result = dst[i] == src[i] ; } 00222 return result ; 00223 } 00224 00225 template<typename T> 00226 inline static bool not_equal( const T * const dst , const T * const src ) 00227 { return ! equal( dst , src ); } 00228 00229 template<typename T> 00230 inline static bool less( const T * const dst , const T * const src ) 00231 { 00232 unsigned i = 0 ; 00233 for ( ; i < N && dst[i] == src[i] ; ++i ); 00234 return i < N && dst[i] < src[i] ; 00235 } 00236 00237 template<typename T> 00238 inline static bool less_equal( const T * const dst , const T * const src ) 00239 { return ! less( src , dst ); } 00240 00241 template<typename T> 00242 inline static bool greater( const T * const dst , const T * const src ) 00243 { return less( src , dst ); } 00244 00245 template<typename T> 00246 inline static bool greater_equal( const T * const dst , const T * const src ) 00247 { return ! less( dst , src ); } 00248 }; 00249 00250 00251 template<unsigned n,int i> 00252 struct Copy { 00253 enum { N = n , I = i }; 00254 Copy() {} 00255 00256 template<typename T> 00257 inline Copy( T * const dst , const T * const src ) 00258 { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); } 00259 00260 template<typename T> 00261 inline Copy( T * const dst , const T src ) 00262 { dst[I] = src ; Copy<N-1,I+1>(dst,src); } 00263 }; 00264 00265 template<unsigned n,int i> 00266 struct Sum { 00267 enum { N = n , I = i }; 00268 Sum() {} 00269 00270 template<typename T> 00271 inline Sum( T * const dst , const T * const src ) 00272 { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); } 00273 00274 template<typename T> 00275 inline Sum( T * const dst , const T a , const T * const src ) 00276 { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); } 00277 }; 00278 00279 template<unsigned n,int i> 00280 struct Prod { 00281 enum { N = n , I = i }; 00282 Prod() {} 00283 template<typename T> 00284 inline Prod( T * const dst , const T * const src ) 00285 { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); } 00286 }; 00287 00288 template<unsigned n,int i> 00289 struct BitOr { 00290 enum { N = n , I = i }; 00291 BitOr() {} 00292 template<typename T> 00293 inline BitOr( T * const dst , const T * const src ) 00294 { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); } 00295 }; 00296 00297 template<unsigned n,int i> 00298 struct BitAnd { 00299 enum { N = n , I = i }; 00300 BitAnd() {} 00301 template<typename T> 00302 inline BitAnd( T * const dst , const T * const src ) 00303 { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); } 00304 }; 00305 00306 template<unsigned n,int i> 00307 struct Max { 00308 enum { N = n , I = i }; 00309 Max() {} 00310 template<typename T> 00311 inline Max( T * const dst , const T * const src ) 00312 { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); } 00313 }; 00314 00315 template<unsigned n,int i> 00316 struct Min { 00317 enum { N = n , I = i }; 00318 Min() {} 00319 template<typename T> 00320 inline Min( T * const dst , const T * const src ) 00321 { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); } 00322 }; 00323 00324 template<unsigned n,int i> 00325 struct InnerProduct { 00326 enum { N = n , I = i }; 00327 InnerProduct() {} 00328 template<typename T> 00329 inline InnerProduct( T & value , const T * const x , const T * const y ) 00330 { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); } 00331 }; 00332 00333 00334 template<unsigned n,int i> 00335 struct Compare { 00336 enum { N = n , I = i }; 00337 Compare() {} 00338 00339 template<typename T> 00340 inline static bool equal( const T * const dst , const T * const src ) 00341 { return dst[I] == src[I] && Compare<N-1,I+1>::equal(dst,src); } 00342 00343 template<typename T> 00344 inline static bool not_equal( const T * const dst , const T * const src ) 00345 { return dst[I] != src[I] || Compare<N-1,I+1>::not_equal(dst,src); } 00346 00347 template<typename T> 00348 inline static bool less( const T * const dst , const T * const src ) 00349 { 00350 return dst[I] != src[I] ? dst[I] < src[I] 00351 : Compare<N-1,I+1>::less(dst,src); 00352 } 00353 00354 template<typename T> 00355 inline static bool less_equal( const T * const dst , const T * const src ) 00356 { 00357 return dst[I] != src[I] ? dst[I] < src[I] 00358 : Compare<N-1,I+1>::less_equal(dst,src); 00359 } 00360 00361 template<typename T> 00362 inline static bool greater( const T * const dst , const T * const src ) 00363 { 00364 return dst[I] != src[I] ? dst[I] > src[I] 00365 : Compare<N-1,I+1>::greater(dst,src); 00366 } 00367 00368 template<typename T> 00369 inline static bool greater_equal( const T * const dst , const T * const src ) 00370 { 00371 return dst[I] != src[I] ? dst[I] > src[I] 00372 : Compare<N-1,I+1>::greater_equal(dst,src); 00373 } 00374 }; 00375 00376 //----------------------------------- 00377 00378 } // namespace stk_classic 00379 00380 #endif 00381