QDP++
Functors.h
Go to the documentation of this file.
1// -*- C++ -*-
2// ACL:license
3// ----------------------------------------------------------------------
4// This software and ancillary information (herein called "SOFTWARE")
5// called PETE (Portable Expression Template Engine) is
6// made available under the terms described here. The SOFTWARE has been
7// approved for release with associated LA-CC Number LA-CC-99-5.
8//
9// Unless otherwise indicated, this SOFTWARE has been authored by an
10// employee or employees of the University of California, operator of the
11// Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
12// the U.S. Department of Energy. The U.S. Government has rights to use,
13// reproduce, and distribute this SOFTWARE. The public may copy, distribute,
14// prepare derivative works and publicly display this SOFTWARE without
15// charge, provided that this Notice and any statement of authorship are
16// reproduced on all copies. Neither the Government nor the University
17// makes any warranty, express or implied, or assumes any liability or
18// responsibility for the use of this SOFTWARE.
19//
20// If SOFTWARE is modified to produce derivative works, such modified
21// SOFTWARE should be clearly marked, so as not to confuse it with the
22// version available from LANL.
23//
24// For more information about PETE, send e-mail to pete@acl.lanl.gov,
25// or visit the PETE web page at http://www.acl.lanl.gov/pete/.
26// ----------------------------------------------------------------------
27// ACL:license
28
29#ifndef PETE_PETE_FUNCTORS_H
30#define PETE_PETE_FUNCTORS_H
31
33//
34// WARNING: THIS FILE IS FOR INTERNAL PETE USE. DON'T INCLUDE IT YOURSELF
35//
37
38//-----------------------------------------------------------------------------
39// Include files.
40//-----------------------------------------------------------------------------
41
42// #include <iterator>
43
44//-----------------------------------------------------------------------------
45//
46// CLASS NAME
47// LeafFunctor<LeafType, LeafTag>
48//
49// DESCRIPTION
50// LeafType is the type of something at the leaf of the expression tree.
51// LeafTag specifies the operation being applied.
52//
53// LeafFunctors are used by ForEach to apply operations to the leaves of the
54// expression tree. Typical functors are evaluators, counters, etc.
55// Users define functors for use with ForEach by specializing
56// the struct LeafFunctor<LeafType, LeafTag> for the user defined Functor and
57// any Leaf types that are necessary.
58//
59// This isn't a functor in the conventional sense since it isn't invoked
60// operator() on a LeafFunctor object. Instead, the static apply member
61// function is called without an object. In a lot of ways, this is better
62// and more flexible than a regular functor.
63//
64// LeafFunctor specializations must define the following:
65//
66// typedef ... Type_t;
67// - the return type of the functor.
68// static Type_t apply(const LeafType &l, const LeafTag &f) {}
69// - evaluates the functor on leaf l.
70//
71//-----------------------------------------------------------------------------
72
73template<class LeafType, class LeafTag>
75{ };
76
77
78//-----------------------------------------------------------------------------
79//
80// CLASS NAMES
81// EvalLeaf1-7, LeafFunctor<Scalar<T>, EvalLeaf1-7 >
82//
83// DESCRIPTION
84// EvalLeaf1 through EvalLeaf7 are used to evaluate leaves using 1 to 7
85// integer indices. We supply the tags and scalar versions here. Users must
86// supply specializations for their own containers.
87//
88//-----------------------------------------------------------------------------
89
90// 1D
91
93{
94 int i1_m;
95 inline EvalLeaf1(int i1) : i1_m(i1) { }
96 inline int val1() const { return i1_m; }
97};
98
99template<class T>
101{
102 typedef T Type_t;
103 inline static
104 const Type_t &apply(const Scalar<T> &s, const EvalLeaf1 &)
105 {
106 return s.value();
107 }
108};
109
110// 2D
111
113{
114 int i1_m, i2_m;
115 inline EvalLeaf2(int i1, int i2) : i1_m(i1), i2_m(i2) { }
116 inline int val1() const { return i1_m; }
117 inline int val2() const { return i2_m; }
118};
119
120template<class T>
122{
123 typedef T Type_t;
124 inline static
125 const Type_t &apply(const Scalar<T> &s, const EvalLeaf2 &)
126 {
127 return s.value();
128 }
129};
130
131// 3D
132
134{
136 inline EvalLeaf3(int i1, int i2, int i3)
137 : i1_m(i1), i2_m(i2), i3_m(i3) { }
138 inline int val1() const { return i1_m; }
139 inline int val2() const { return i2_m; }
140 inline int val3() const { return i3_m; }
141};
142
143template<class T>
145{
146 typedef T Type_t;
147 inline static
148 const Type_t &apply(const Scalar<T> &s, const EvalLeaf3 &)
149 {
150 return s.value();
151 }
152};
153
154// 4D
155
157{
159 inline EvalLeaf4(int i1, int i2, int i3, int i4)
160 : i1_m(i1), i2_m(i2), i3_m(i3), i4_m(i4) { }
161 inline int val1() const { return i1_m; }
162 inline int val2() const { return i2_m; }
163 inline int val3() const { return i3_m; }
164 inline int val4() const { return i4_m; }
165};
166
167template<class T>
169{
170 typedef T Type_t;
171 inline static
172 const Type_t &apply(const Scalar<T> &s, const EvalLeaf4 &)
173 {
174 return s.value();
175 }
176};
177
178// 5D
179
181{
183 inline EvalLeaf5(int i1, int i2, int i3, int i4, int i5)
184 : i1_m(i1), i2_m(i2), i3_m(i3), i4_m(i4), i5_m(i5) { }
185 inline int val1() const { return i1_m; }
186 inline int val2() const { return i2_m; }
187 inline int val3() const { return i3_m; }
188 inline int val4() const { return i4_m; }
189 inline int val5() const { return i5_m; }
190};
191
192template<class T>
194{
195 typedef T Type_t;
196 inline static
197 const Type_t &apply(const Scalar<T> &s, const EvalLeaf5 &)
198 {
199 return s.value();
200 }
201};
202
203// 6D
204
206{
208 inline EvalLeaf6(int i1, int i2, int i3, int i4, int i5, int i6)
209 : i1_m(i1), i2_m(i2), i3_m(i3), i4_m(i4), i5_m(i5), i6_m(i6) { }
210 inline int val1() const { return i1_m; }
211 inline int val2() const { return i2_m; }
212 inline int val3() const { return i3_m; }
213 inline int val4() const { return i4_m; }
214 inline int val5() const { return i5_m; }
215 inline int val6() const { return i6_m; }
216};
217
218template<class T>
220{
221 typedef T Type_t;
222 inline static
223 const Type_t &apply(const Scalar<T> &s, const EvalLeaf6 &)
224 {
225 return s.value();
226 }
227};
228
229// 7D
230
232{
234 inline EvalLeaf7(int i1, int i2, int i3, int i4, int i5, int i6,
235 int i7)
236 : i1_m(i1), i2_m(i2), i3_m(i3), i4_m(i4), i5_m(i5), i6_m(i6), i7_m(i7) { }
237 inline int val1() const { return i1_m; }
238 inline int val2() const { return i2_m; }
239 inline int val3() const { return i3_m; }
240 inline int val4() const { return i4_m; }
241 inline int val5() const { return i5_m; }
242 inline int val6() const { return i6_m; }
243 inline int val7() const { return i7_m; }
244};
245
246template<class T>
248{
249 typedef T Type_t;
250 inline static
251 const Type_t &apply(const Scalar<T> &s, const EvalLeaf7 &)
252 {
253 return s.value();
254 }
255};
256
257
258//-----------------------------------------------------------------------------
259//
260// CLASS NAME
261// IncrementLeaf, LeafFunctor<{T, Scalar<T>}, IncrementLeaf >
262//
263// DESCRIPTION
264// A leaf-tag and functor used to increment an iterator for scalars and
265// leaves made up of STL iterators.
266//
267//-----------------------------------------------------------------------------
268
270{ };
271
272template<class T>
274{
275 typedef int Type_t;
276 inline static
277 Type_t apply(const T &cl, const IncrementLeaf &)
278 {
279 T &l = const_cast<T &>(cl);
280 ++l;
281 return 0;
282 }
283};
284
285#if defined(__MWERKS__)
286
287// Workaround for screwy CWPro 4.1 bug.
288
289template <class T>
290struct LeafFunctor<const T*, IncrementLeaf>
291{
292 typedef int Type_t;
293 inline static
294 Type_t apply(const T* & const ci, const IncrementLeaf &)
295 {
296 T* &i = const_cast<T* &>(ci);
297 ++i;
298 return 0;
299 }
300};
301
302#endif
303
304template<class T>
306{
307 typedef int Type_t;
308 inline static
310 {
311 return 0;
312 }
313};
314
315
316//-----------------------------------------------------------------------------
317//
318// CLASS NAME
319// DecrementLeaf, LeafFunctor<{T, Scalar<T>}, DecrementLeaf >
320//
321// DESCRIPTION
322// A leaf-tag and functor used to decrement an iterator for scalars and
323// leaves made up of STL iterators.
324//
325//-----------------------------------------------------------------------------
326
328{ };
329
330template<class T>
332{
333 typedef int Type_t;
334 inline static
335 Type_t apply(const T &cl, const DecrementLeaf &)
336 {
337 T &l = const_cast<T &>(cl);
338 --l;
339 return 0;
340 }
341};
342
343#if defined(__MWERKS__)
344// Workaround for screwy CWPro 4.1 bug.
345template <class T>
346struct LeafFunctor<const T*, DecrementLeaf>
347{
348 typedef int Type_t;
349 inline static
350 Type_t apply(const T* & const ci, const IncrementLeaf &)
351 {
352 T* &i = const_cast<T* &>(ci);
353 --i;
354 return 0;
355 }
356};
357#endif
358
359template<class T>
361{
362 typedef int Type_t;
363 inline static
365 {
366 return 0;
367 }
368};
369
370//-----------------------------------------------------------------------------
371//
372// CLASS NAME
373// DereferenceLeaf, LeafFunctor<{T, Scalar<T>}, DereferenceLeaf >
374//
375// DESCRIPTION
376// A leaf-tag and functor used to dereference an iterator for scalars and
377// leaves made up of STL iterators.
378//
379//-----------------------------------------------------------------------------
380
382{ };
383
384#if 0
385template<class ForwardIterator>
386struct LeafFunctor<ForwardIterator, DereferenceLeaf>
387{
388 typedef typename std::iterator_traits<ForwardIterator>::value_type Type_t;
389 inline static
390 Type_t apply(const ForwardIterator &i, const DereferenceLeaf &)
391 {
392 return *i;
393 }
394};
395#endif
396
397#if defined(__MWERKS__)
398// Workaround for screwy CWPro 4.1 bug.
399template <class T>
400struct LeafFunctor<const T*, DereferenceLeaf>
401{
402 typedef T Type_t;
403 inline static
404 Type_t apply(const T *i, const DereferenceLeaf &)
405 {
406 return *i;
407 }
408};
409#endif
410
411template<class T>
413{
414 typedef T Type_t;
415 inline static
416 const Type_t &apply(const Scalar<T> &s, const DereferenceLeaf &)
417 {
418 return s.value();
419 }
420};
421
422
423#endif // PETE_PETE_FUNCTORS_H
424
425// ACL:rcsinfo
426// ----------------------------------------------------------------------
427// $RCSfile: Functors.h,v $ $Author: edwards $
428// $Revision: 1.1 $ $Date: 2002-09-12 18:22:16 $
429// ----------------------------------------------------------------------
430// ACL:rcsinfo
const T & value() const
Definition Scalar.h:94
int i1_m
Definition Functors.h:94
int val1() const
Definition Functors.h:96
EvalLeaf1(int i1)
Definition Functors.h:95
EvalLeaf2(int i1, int i2)
Definition Functors.h:115
int val1() const
Definition Functors.h:116
int val2() const
Definition Functors.h:117
int val3() const
Definition Functors.h:140
int val2() const
Definition Functors.h:139
int val1() const
Definition Functors.h:138
EvalLeaf3(int i1, int i2, int i3)
Definition Functors.h:136
int val3() const
Definition Functors.h:163
EvalLeaf4(int i1, int i2, int i3, int i4)
Definition Functors.h:159
int val2() const
Definition Functors.h:162
int val1() const
Definition Functors.h:161
int val4() const
Definition Functors.h:164
int val3() const
Definition Functors.h:187
int val4() const
Definition Functors.h:188
int val1() const
Definition Functors.h:185
int val2() const
Definition Functors.h:186
EvalLeaf5(int i1, int i2, int i3, int i4, int i5)
Definition Functors.h:183
int val5() const
Definition Functors.h:189
int val6() const
Definition Functors.h:215
int val2() const
Definition Functors.h:211
int val5() const
Definition Functors.h:214
int val1() const
Definition Functors.h:210
int val4() const
Definition Functors.h:213
int val3() const
Definition Functors.h:212
EvalLeaf6(int i1, int i2, int i3, int i4, int i5, int i6)
Definition Functors.h:208
int val1() const
Definition Functors.h:237
int val7() const
Definition Functors.h:243
int val5() const
Definition Functors.h:241
int val6() const
Definition Functors.h:242
int val4() const
Definition Functors.h:240
EvalLeaf7(int i1, int i2, int i3, int i4, int i5, int i6, int i7)
Definition Functors.h:234
int val2() const
Definition Functors.h:238
int val3() const
Definition Functors.h:239
static Type_t apply(const Scalar< T > &, const DecrementLeaf &)
Definition Functors.h:364
static const Type_t & apply(const Scalar< T > &s, const DereferenceLeaf &)
Definition Functors.h:416
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf1 &)
Definition Functors.h:104
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf2 &)
Definition Functors.h:125
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf3 &)
Definition Functors.h:148
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf4 &)
Definition Functors.h:172
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf5 &)
Definition Functors.h:197
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf6 &)
Definition Functors.h:223
static const Type_t & apply(const Scalar< T > &s, const EvalLeaf7 &)
Definition Functors.h:251
static Type_t apply(const Scalar< T > &, const IncrementLeaf &)
Definition Functors.h:309
static Type_t apply(const T &cl, const DecrementLeaf &)
Definition Functors.h:335
static Type_t apply(const T &cl, const IncrementLeaf &)
Definition Functors.h:277