shards Version of the Day
Shards_SimpleArrayOps.hpp
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 //
00005 //                Shards : Shared Discretization Tools
00006 //                 Copyright 2008 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Carter Edwards (hcedwar@sandia.gov),
00039 //                    Pavel Bochev (pbboche@sandia.gov), or
00040 //                    Denis Ridzal (dridzal@sandia.gov).
00041 //
00042 // ************************************************************************
00043 //@HEADER
00044 */
00045 
00046 #ifndef Shards_SimpleArrayOps_hpp
00047 #define Shards_SimpleArrayOps_hpp
00048 
00049 namespace shards {
00050 
00064 template< unsigned n , unsigned i = 0 >
00065 struct Copy {
00066   enum { N = n , I = i };
00067 
00069   template<typename T>
00070   inline Copy( T * const dst , const T * const src )
00071     { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); }
00072 
00074   template<typename T>
00075   inline Copy( T * const dst , const T src )
00076     { dst[I] = src ; Copy<N-1,I+1>(dst,src); }
00077 
00078   Copy() {}
00079 };
00080 
00084 template< unsigned n , unsigned i = 0 >
00085 struct Sum {
00086   enum { N = n , I = i };
00087 
00089   template<typename T>
00090   inline Sum( T * const dst , const T * const src )
00091     { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); }
00092 
00094   template<typename T>
00095   inline Sum( T * const dst , const T a , const T * const src )
00096     { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); }
00097 
00098   Sum() {}
00099 };
00100 
00104 template< unsigned n , unsigned i = 0 >
00105 struct Prod {
00106   enum { N = n , I = i };
00107 
00109   template<typename T>
00110   inline Prod( T * const dst , const T * const src )
00111     { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); }
00112   Prod() {}
00113 };
00114 
00118 template< unsigned n , unsigned i = 0 >
00119 struct BitOr {
00120   enum { N = n , I = i };
00121 
00123   template<typename T>
00124   inline BitOr( T * const dst , const T * const src )
00125     { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); }
00126 
00127   BitOr() {}
00128 };
00129 
00133 template< unsigned n , unsigned i = 0 >
00134 struct BitAnd {
00135   enum { N = n , I = i };
00136 
00138   template<typename T>
00139   inline BitAnd( T * const dst , const T * const src )
00140     { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); }
00141 
00142   BitAnd() {}
00143 };
00144 
00148 template< unsigned n , unsigned i = 0 >
00149 struct Max {
00150   enum { N = n , I = i };
00151 
00153   template<typename T>
00154   inline Max( T * const dst , const T * const src )
00155     { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); }
00156 
00157   Max() {}
00158 };
00159 
00163 template< unsigned n , unsigned i = 0 >
00164 struct Min {
00165   enum { N = n , I = i };
00166 
00168   template<typename T>
00169   inline Min( T * const dst , const T * const src )
00170     { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); }
00171 
00172   Min() {}
00173 };
00174 
00178 template< unsigned n , unsigned i = 0 >
00179 struct InnerProduct {
00180   enum { N = n , I = i };
00181 
00183   template<typename T>
00184   inline InnerProduct( T & value , const T * const x , const T * const y )
00185     { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); }
00186 
00187   InnerProduct() {}
00188 };
00189 
00193 template< unsigned n , unsigned i = 0 >
00194 struct Compare {
00195   enum { N = n , I = i };
00196 
00198   template<typename T>
00199   inline static bool equal( const T * const x , const T * const y )
00200     { return x[I] == y[I] && Compare<N-1,I+1>::equal(x,y); }
00201 
00203   template<typename T>
00204   inline static bool not_equal( const T * const x , const T * const y )
00205     { return x[I] != y[I] || Compare<N-1,I+1>::not_equal(x,y); }
00206 
00208   template<typename T>
00209   inline static bool less( const T * const x , const T * const y )
00210     {
00211       return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less(x,y);
00212     }
00213 
00215   template<typename T>
00216   inline static bool less_equal( const T * const x , const T * const y )
00217     {
00218       return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less_equal(x,y);
00219     }
00220 
00222   template<typename T>
00223   inline static bool greater( const T * const x , const T * const y )
00224     {
00225       return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater(x,y);
00226     }
00227 
00229   template<typename T>
00230   inline static bool greater_equal( const T * const x , const T * const y )
00231     {
00232       return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater_equal(x,y);
00233     }
00234 
00235   Compare() {}
00236 };
00237 
00240 //----------------------------------------------------------------------
00241 
00242 #ifndef DOXYGEN_COMPILE
00243 
00244 template<unsigned i>
00245 struct Copy<0,i> {
00246   enum { N = 0 };
00247   Copy() {}
00248   template<typename T> inline Copy( T * const , const T * const ) {}
00249   template<typename T> inline Copy( T * const , const T ) {}
00250 };
00251 
00252 template<unsigned i>
00253 struct Sum<0,i> {
00254   enum { N = 0 };
00255   Sum() {}
00256   template<typename T> inline Sum( T * const , const T * const ) {}
00257   template<typename T> inline Sum( T * const , const T , const T * const ) {}
00258 };
00259 
00260 template<unsigned i>
00261 struct Prod<0,i> {
00262   enum { N = 0 };
00263   Prod() {}
00264   template<typename T> inline Prod( T * const , const T * const ) {}
00265 };
00266 
00267 template<unsigned i>
00268 struct Max<0,i> {
00269   enum { N = 0 };
00270   Max() {}
00271   template<typename T> inline Max( T * const , const T * const ) {}
00272 };
00273 
00274 template<unsigned i>
00275 struct Min<0,i> {
00276   enum { N = 0 };
00277   Min() {}
00278   template<typename T> inline Min( T * const , const T * const ) {}
00279 };
00280 
00281 template<unsigned i>
00282 struct BitOr<0,i> {
00283   enum { N = 0 };
00284   BitOr() {}
00285   template<typename T> inline BitOr( T * const , const T * const ) {}
00286 };
00287 
00288 template<unsigned i>
00289 struct BitAnd<0,i> {
00290   enum { N = 0 };
00291   BitAnd() {}
00292   template<typename T> inline BitAnd( T * const , const T * const ) {}
00293 };
00294 
00295 template<unsigned i>
00296 struct InnerProduct<0,i> {
00297   enum { N = 0 };
00298   InnerProduct() {}
00299   template<typename T>
00300   inline InnerProduct( T & , const T * const , const T * const ) {}
00301 };
00302 
00303 template<unsigned i>
00304 struct Compare<0,i> {
00305   enum { N = 0 };
00306   Compare() {}
00307 
00308   template<typename T>
00309   inline static bool equal( const T * const , const T * const )
00310     { return true ; }
00311 
00312   template<typename T>
00313   inline static bool not_equal( const T * const , const T * const )
00314     { return false ; }
00315 
00316   template<typename T>
00317   inline static bool less( const T * const , const T * const )
00318     { return false ; }
00319 
00320   template<typename T>
00321   inline static bool less_equal( const T * const , const T * const )
00322     { return true ; }
00323 
00324   template<typename T>
00325   inline static bool greater( const T * const , const T * const )
00326     { return false ; }
00327 
00328   template<typename T>
00329   inline static bool greater_equal( const T * const , const T * const )
00330     { return true ; }
00331 };
00332 
00333 
00334 #endif /* DOXYGEN_COMPILE */
00335 
00336 } // namespace shards
00337 
00338 #endif
00339 
 All Classes Functions Variables Typedefs Enumerator