Sierra Toolkit  Version of the Day
SimpleArrayOps.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef stk_util_util_SimpleArrayOps_hpp
10 #define stk_util_util_SimpleArrayOps_hpp
11 
12 namespace stk_classic {
13 
15 
16 // Basic operations for compile-time fixed length arrays
17 //
18 // Example: Sum<5>(x,y) results in x[i] += y[i] for i=0..4
19 
20 template< unsigned n, int i=0> struct Copy ;
21 template< unsigned n, int i=0> struct Sum ;
22 template< unsigned n, int i=0> struct Prod ;
23 template< unsigned n, int i=0> struct Max ;
24 template< unsigned n, int i=0> struct Min ;
25 template< unsigned n, int i=0> struct BitOr ;
26 template< unsigned n, int i=0> struct BitAnd ;
27 template< unsigned n, int i=0> struct InnerProduct ;
28 template< unsigned n, int i=0> struct Compare ;
29 
30 //----------------------------------------------------------------------
31 
32 template<int i>
33 struct Copy<0,i> {
34  enum { N = 0 };
35  Copy() {}
36  template<typename T> inline Copy( T * const , const T * const ) {}
37  template<typename T> inline Copy( T * const , const T ) {}
38 };
39 
40 template<int i>
41 struct Sum<0,i> {
42  enum { N = 0 };
43  Sum() {}
44  template<typename T> inline Sum( T * const , const T * const ) {}
45  template<typename T> inline Sum( T * const , const T , const T * const ) {}
46 };
47 
48 template<int i>
49 struct Prod<0,i> {
50  enum { N = 0 };
51  Prod() {}
52  template<typename T> inline Prod( T * const , const T * const ) {}
53 };
54 
55 template<int i>
56 struct Max<0,i> {
57  enum { N = 0 };
58  Max() {}
59  template<typename T> inline Max( T * const , const T * const ) {}
60 };
61 
62 template<int i>
63 struct Min<0,i> {
64  enum { N = 0 };
65  Min() {}
66  template<typename T> inline Min( T * const , const T * const ) {}
67 };
68 
69 template<int i>
70 struct BitOr<0,i> {
71  enum { N = 0 };
72  BitOr() {}
73  template<typename T> inline BitOr( T * const , const T * const ) {}
74 };
75 
76 template<int i>
77 struct BitAnd<0,i> {
78  enum { N = 0 };
79  BitAnd() {}
80  template<typename T> inline BitAnd( T * const , const T * const ) {}
81 };
82 
83 template<int i>
84 struct InnerProduct<0,i> {
85  enum { N = 0 };
86  InnerProduct() {}
87  template<typename T>
88  inline InnerProduct( T & value , const T * const x , const T * const y ) {}
89 };
90 
91 template<int i>
92 struct Compare<0,i> {
93  enum { N = 0 };
94  Compare() {}
95 
96  template<typename T>
97  inline static bool equal( const T * const , const T * const )
98  { return true ; }
99 
100  template<typename T>
101  inline static bool not_equal( const T * const , const T * const )
102  { return false ; }
103 
104  template<typename T>
105  inline static bool less( const T * const , const T * const )
106  { return false ; }
107 
108  template<typename T>
109  inline static bool less_equal( const T * const , const T * const )
110  { return true ; }
111 
112  template<typename T>
113  inline static bool greater( const T * const , const T * const )
114  { return false ; }
115 
116  template<typename T>
117  inline static bool greater_equal( const T * const , const T * const )
118  { return true ; }
119 };
120 
121 
122 template<unsigned n>
123 struct Copy<n,-1> {
124  enum { N = n };
125  Copy() {}
126 
127  template<typename T>
128  inline Copy( T * const dst , const T * const src )
129  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] = src[i]; } }
130 
131  template<typename T>
132  inline Copy( T * const dst , const T src )
133  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] = src ; } }
134 };
135 
136 template<unsigned n>
137 struct Sum<n,-1> {
138  enum { N = n };
139  Sum() {}
140 
141  template<typename T>
142  inline Sum( T * const dst , const T * const src )
143  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] += src[i]; } }
144 
145  template<typename T>
146  inline Sum( T * const dst , const T a , const T * const src )
147  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] += a * src[i]; } }
148 };
149 
150 template<unsigned n>
151 struct Prod<n,-1> {
152  enum { N = n };
153  Prod() {}
154  template<typename T>
155  inline Prod( T * const dst , const T * const src )
156  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] *= src[i]; } }
157 };
158 
159 template<unsigned n>
160 struct BitOr<n,-1> {
161  enum { N = n };
162  BitOr() {}
163  template<typename T>
164  inline BitOr( T * const dst , const T * const src )
165  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] |= src[i]; } }
166 };
167 
168 template<unsigned n>
169 struct BitAnd<n,-1> {
170  enum { N = n };
171  BitAnd() {}
172  template<typename T>
173  inline BitAnd( T * const dst , const T * const src )
174  { for ( unsigned i = 0 ; i < N ; ++i ) { dst[i] &= src[i]; } }
175 };
176 
177 template<unsigned n>
178 struct Max<n,-1> {
179  enum { N = n };
180  Max() {}
181  template<typename T>
182  inline Max( T * const dst , const T * const src )
183  {
184  for ( unsigned i = 0 ; i < N ; ++i )
185  { if ( dst[i] < src[i] ) { dst[i] = src[i] ; } }
186  }
187 };
188 
189 template<unsigned n>
190 struct Min<n,-1> {
191  enum { N = n };
192  Min() {}
193  template<typename T>
194  inline Min( T * const dst , const T * const src )
195  {
196  for ( unsigned i = 0 ; i < N ; ++i )
197  { if ( src[i] < dst[i] ) { dst[i] = src[i] ; } }
198  }
199 };
200 
201 template<unsigned n>
202 struct InnerProduct<n,-1> {
203  enum { N = n };
204  InnerProduct() {}
205  template<typename T>
206  inline InnerProduct( T & value , const T * const x , const T * const y )
207  { for ( unsigned i = 0 ; i < N ; ++i ) { value += x[i] * y[i] ; } }
208 };
209 
210 
211 template<unsigned n>
212 struct Compare<n,-1> {
213  enum { N = n };
214  Compare() {}
215 
216  template<typename T>
217  inline static bool equal( const T * const dst , const T * const src )
218  {
219  bool result = true ;
220  for ( unsigned i = 0 ; result && i < N ; ++i )
221  { result = dst[i] == src[i] ; }
222  return result ;
223  }
224 
225  template<typename T>
226  inline static bool not_equal( const T * const dst , const T * const src )
227  { return ! equal( dst , src ); }
228 
229  template<typename T>
230  inline static bool less( const T * const dst , const T * const src )
231  {
232  unsigned i = 0 ;
233  for ( ; i < N && dst[i] == src[i] ; ++i );
234  return i < N && dst[i] < src[i] ;
235  }
236 
237  template<typename T>
238  inline static bool less_equal( const T * const dst , const T * const src )
239  { return ! less( src , dst ); }
240 
241  template<typename T>
242  inline static bool greater( const T * const dst , const T * const src )
243  { return less( src , dst ); }
244 
245  template<typename T>
246  inline static bool greater_equal( const T * const dst , const T * const src )
247  { return ! less( dst , src ); }
248 };
249 
250 
251 template<unsigned n,int i>
252 struct Copy {
253  enum { N = n , I = i };
254  Copy() {}
255 
256  template<typename T>
257  inline Copy( T * const dst , const T * const src )
258  { dst[I] = src[I] ; Copy<N-1,I+1>(dst,src); }
259 
260  template<typename T>
261  inline Copy( T * const dst , const T src )
262  { dst[I] = src ; Copy<N-1,I+1>(dst,src); }
263 };
264 
265 template<unsigned n,int i>
266 struct Sum {
267  enum { N = n , I = i };
268  Sum() {}
269 
270  template<typename T>
271  inline Sum( T * const dst , const T * const src )
272  { dst[I] += src[I] ; Sum<N-1,I+1>(dst,src); }
273 
274  template<typename T>
275  inline Sum( T * const dst , const T a , const T * const src )
276  { dst[I] += a * src[I] ; Sum<N-1,I+1>(dst,a,src); }
277 };
278 
279 template<unsigned n,int i>
280 struct Prod {
281  enum { N = n , I = i };
282  Prod() {}
283  template<typename T>
284  inline Prod( T * const dst , const T * const src )
285  { dst[I] *= src[I] ; Prod<N-1,I+1>(dst,src); }
286 };
287 
288 template<unsigned n,int i>
289 struct BitOr {
290  enum { N = n , I = i };
291  BitOr() {}
292  template<typename T>
293  inline BitOr( T * const dst , const T * const src )
294  { dst[I] |= src[I] ; BitOr<N-1,I+1>(dst,src); }
295 };
296 
297 template<unsigned n,int i>
298 struct BitAnd {
299  enum { N = n , I = i };
300  BitAnd() {}
301  template<typename T>
302  inline BitAnd( T * const dst , const T * const src )
303  { dst[I] &= src[I] ; BitAnd<N-1,I+1>(dst,src); }
304 };
305 
306 template<unsigned n,int i>
307 struct Max {
308  enum { N = n , I = i };
309  Max() {}
310  template<typename T>
311  inline Max( T * const dst , const T * const src )
312  { if ( dst[I] < src[I] ) { dst[I] = src[I] ; } Max<N-1,I+1>(dst,src); }
313 };
314 
315 template<unsigned n,int i>
316 struct Min {
317  enum { N = n , I = i };
318  Min() {}
319  template<typename T>
320  inline Min( T * const dst , const T * const src )
321  { if ( src[I] < dst[I] ) { dst[I] = src[I] ; } Min<N-1,I+1>(dst,src); }
322 };
323 
324 template<unsigned n,int i>
325 struct InnerProduct {
326  enum { N = n , I = i };
327  InnerProduct() {}
328  template<typename T>
329  inline InnerProduct( T & value , const T * const x , const T * const y )
330  { value += x[I] * y[I] ; InnerProduct<N-1,I+1>( value , x , y ); }
331 };
332 
333 
334 template<unsigned n,int i>
335 struct Compare {
336  enum { N = n , I = i };
337  Compare() {}
338 
339  template<typename T>
340  inline static bool equal( const T * const dst , const T * const src )
341  { return dst[I] == src[I] && Compare<N-1,I+1>::equal(dst,src); }
342 
343  template<typename T>
344  inline static bool not_equal( const T * const dst , const T * const src )
345  { return dst[I] != src[I] || Compare<N-1,I+1>::not_equal(dst,src); }
346 
347  template<typename T>
348  inline static bool less( const T * const dst , const T * const src )
349  {
350  return dst[I] != src[I] ? dst[I] < src[I]
351  : Compare<N-1,I+1>::less(dst,src);
352  }
353 
354  template<typename T>
355  inline static bool less_equal( const T * const dst , const T * const src )
356  {
357  return dst[I] != src[I] ? dst[I] < src[I]
358  : Compare<N-1,I+1>::less_equal(dst,src);
359  }
360 
361  template<typename T>
362  inline static bool greater( const T * const dst , const T * const src )
363  {
364  return dst[I] != src[I] ? dst[I] > src[I]
365  : Compare<N-1,I+1>::greater(dst,src);
366  }
367 
368  template<typename T>
369  inline static bool greater_equal( const T * const dst , const T * const src )
370  {
371  return dst[I] != src[I] ? dst[I] > src[I]
372  : Compare<N-1,I+1>::greater_equal(dst,src);
373  }
374 };
375 
376 //-----------------------------------
377 
378 } // namespace stk_classic
379 
380 #endif
381 
Sierra Toolkit.
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)