Shards_SimpleArrayOps.hpp

00001 /*------------------------------------------------------------------------*/
00002 /*                  shards : Shared Discretization Tools                  */
00003 /*                Copyright (2008) Sandia Corporation                     */
00004 /*                                                                        */
00005 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00006 /*  license for use of this work by or on behalf of the U.S. Government.  */
00007 /*                                                                        */
00008 /*  This library is free software; you can redistribute it and/or modify  */
00009 /*  it under the terms of the GNU Lesser General Public License as        */
00010 /*  published by the Free Software Foundation; either version 2.1 of the  */
00011 /*  License, or (at your option) any later version.                       */
00012 /*                                                                        */
00013 /*  This library is distributed in the hope that it will be useful,       */
00014 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
00015 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     */
00016 /*  Lesser General Public License for more details.                       */
00017 /*                                                                        */
00018 /*  You should have received a copy of the GNU Lesser General Public      */
00019 /*  License along with this library; if not, write to the Free Software   */
00020 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307   */
00021 /*  USA                                                                   */
00022 /*------------------------------------------------------------------------*/
00023 
00024 #ifndef Shards_SimpleArrayOps_hpp
00025 #define Shards_SimpleArrayOps_hpp
00026 
00027 namespace shards {
00028 
00042 template< unsigned n , unsigned i = 0 >
00043 struct Copy {
00044   enum { N = n , I = i };
00045 
00047   template<typename T>
00048   inline Copy( T * const dst , const T * const src )
00049     { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); }
00050 
00052   template<typename T>
00053   inline Copy( T * const dst , const T src )
00054     { dst[I] = src ; Copy<N-1,I+1>(dst,src); }
00055 
00056   Copy() {}
00057 };
00058 
00062 template< unsigned n , unsigned i = 0 >
00063 struct Sum {
00064   enum { N = n , I = i };
00065 
00067   template<typename T>
00068   inline Sum( T * const dst , const T * const src )
00069     { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); }
00070 
00072   template<typename T>
00073   inline Sum( T * const dst , const T a , const T * const src )
00074     { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); }
00075 
00076   Sum() {}
00077 };
00078 
00082 template< unsigned n , unsigned i = 0 >
00083 struct Prod {
00084   enum { N = n , I = i };
00085 
00087   template<typename T>
00088   inline Prod( T * const dst , const T * const src )
00089     { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); }
00090   Prod() {}
00091 };
00092 
00096 template< unsigned n , unsigned i = 0 >
00097 struct BitOr {
00098   enum { N = n , I = i };
00099 
00101   template<typename T>
00102   inline BitOr( T * const dst , const T * const src )
00103     { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); }
00104 
00105   BitOr() {}
00106 };
00107 
00111 template< unsigned n , unsigned i = 0 >
00112 struct BitAnd {
00113   enum { N = n , I = i };
00114 
00116   template<typename T>
00117   inline BitAnd( T * const dst , const T * const src )
00118     { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); }
00119 
00120   BitAnd() {}
00121 };
00122 
00126 template< unsigned n , unsigned i = 0 >
00127 struct Max {
00128   enum { N = n , I = i };
00129 
00131   template<typename T>
00132   inline Max( T * const dst , const T * const src )
00133     { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); }
00134 
00135   Max() {}
00136 };
00137 
00141 template< unsigned n , unsigned i = 0 >
00142 struct Min {
00143   enum { N = n , I = i };
00144 
00146   template<typename T>
00147   inline Min( T * const dst , const T * const src )
00148     { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); }
00149 
00150   Min() {}
00151 };
00152 
00156 template< unsigned n , unsigned i = 0 >
00157 struct InnerProduct {
00158   enum { N = n , I = i };
00159 
00161   template<typename T>
00162   inline InnerProduct( T & value , const T * const x , const T * const y )
00163     { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); }
00164 
00165   InnerProduct() {}
00166 };
00167 
00171 template< unsigned n , unsigned i = 0 >
00172 struct Compare {
00173   enum { N = n , I = i };
00174 
00176   template<typename T>
00177   inline static bool equal( const T * const x , const T * const y )
00178     { return x[I] == y[I] && Compare<N-1,I+1>::equal(x,y); }
00179 
00181   template<typename T>
00182   inline static bool not_equal( const T * const x , const T * const y )
00183     { return x[I] != y[I] || Compare<N-1,I+1>::not_equal(x,y); }
00184 
00186   template<typename T>
00187   inline static bool less( const T * const x , const T * const y )
00188     {
00189       return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less(x,y);
00190     }
00191 
00193   template<typename T>
00194   inline static bool less_equal( const T * const x , const T * const y )
00195     {
00196       return x[I] != y[I] ? x[I] < y[I] : Compare<N-1,I+1>::less_equal(x,y);
00197     }
00198 
00200   template<typename T>
00201   inline static bool greater( const T * const x , const T * const y )
00202     {
00203       return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater(x,y);
00204     }
00205 
00207   template<typename T>
00208   inline static bool greater_equal( const T * const x , const T * const y )
00209     {
00210       return x[I] != y[I] ? x[I] > y[I] : Compare<N-1,I+1>::greater_equal(x,y);
00211     }
00212 
00213   Compare() {}
00214 };
00215 
00218 //----------------------------------------------------------------------
00219 
00220 #ifndef DOXYGEN_COMPILE
00221 
00222 template<unsigned i>
00223 struct Copy<0,i> {
00224   enum { N = 0 };
00225   Copy() {}
00226   template<typename T> inline Copy( T * const , const T * const ) {}
00227   template<typename T> inline Copy( T * const , const T ) {}
00228 };
00229 
00230 template<unsigned i>
00231 struct Sum<0,i> {
00232   enum { N = 0 };
00233   Sum() {}
00234   template<typename T> inline Sum( T * const , const T * const ) {}
00235   template<typename T> inline Sum( T * const , const T , const T * const ) {}
00236 };
00237 
00238 template<unsigned i>
00239 struct Prod<0,i> {
00240   enum { N = 0 };
00241   Prod() {}
00242   template<typename T> inline Prod( T * const , const T * const ) {}
00243 };
00244 
00245 template<unsigned i>
00246 struct Max<0,i> {
00247   enum { N = 0 };
00248   Max() {}
00249   template<typename T> inline Max( T * const , const T * const ) {}
00250 };
00251 
00252 template<unsigned i>
00253 struct Min<0,i> {
00254   enum { N = 0 };
00255   Min() {}
00256   template<typename T> inline Min( T * const , const T * const ) {}
00257 };
00258 
00259 template<unsigned i>
00260 struct BitOr<0,i> {
00261   enum { N = 0 };
00262   BitOr() {}
00263   template<typename T> inline BitOr( T * const , const T * const ) {}
00264 };
00265 
00266 template<unsigned i>
00267 struct BitAnd<0,i> {
00268   enum { N = 0 };
00269   BitAnd() {}
00270   template<typename T> inline BitAnd( T * const , const T * const ) {}
00271 };
00272 
00273 template<unsigned i>
00274 struct InnerProduct<0,i> {
00275   enum { N = 0 };
00276   InnerProduct() {}
00277   template<typename T>
00278   inline InnerProduct( T & , const T * const , const T * const ) {}
00279 };
00280 
00281 template<unsigned i>
00282 struct Compare<0,i> {
00283   enum { N = 0 };
00284   Compare() {}
00285 
00286   template<typename T>
00287   inline static bool equal( const T * const , const T * const )
00288     { return true ; }
00289 
00290   template<typename T>
00291   inline static bool not_equal( const T * const , const T * const )
00292     { return false ; }
00293 
00294   template<typename T>
00295   inline static bool less( const T * const , const T * const )
00296     { return false ; }
00297 
00298   template<typename T>
00299   inline static bool less_equal( const T * const , const T * const )
00300     { return true ; }
00301 
00302   template<typename T>
00303   inline static bool greater( const T * const , const T * const )
00304     { return false ; }
00305 
00306   template<typename T>
00307   inline static bool greater_equal( const T * const , const T * const )
00308     { return true ; }
00309 };
00310 
00311 
00312 #endif /* DOXYGEN_COMPILE */
00313 
00314 } // namespace shards
00315 
00316 #endif
00317 
Generated on Wed Mar 31 15:02:54 2010 for shards by  doxygen 1.6.3