QDP++
ForEach.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_FOREACH_H
30#define PETE_PETE_FOREACH_H
31
33//
34// WARNING: THIS FILE IS FOR INTERNAL PETE USE. DON'T INCLUDE IT YOURSELF
35//
37
38//-----------------------------------------------------------------------------
39//
40// CLASS NAME
41// ForEach<Expr, FTag, CTag>
42// forEach(Expr& e, FTag& f, CTag& c)
43//
44// Expr is the type of the expression tree.
45// FTag is the type of the leaf tag.
46// CTag is the type of the combiner tag.
47//
48// ForEach<Expr,FTag,CTag>::apply(Expr &e,FTag& f,CTag& c) is a function
49// that traverses the expression tree defined by e, applies the functor f
50// to each leaf and combines the results together using the combiner c.
51// The type of object returned is given by:
52// typename ForEach<Expr,FTag,CTag>::Type_t
53// the function forEachTag(Expr& e,FTag& f,CTag& c) duplicates the action
54// of ForEach::apply and is provided for convenience. (You don't have to
55// type the template arguments.)
56//
57// This generic ForEach functor differs from the original
58// PETE forEach in 3 ways:
59// 1) The user should not specialize ForEach.
60// Specializing TagFunctor and TagCombiner should take care of
61// the behaviour at leaves and nodes and the user knows nothing
62// of the tree.
63// 2) It's a functor, so it contains generic type computations.
64// The typedef Type_t gives you the type of the result for any
65// user defined type and method for combining that type.
66// 3) It handles generic combination. Previously the user had the
67// choice of using the operators in the tree to produce the expression
68// return type or defining a combiner with a uniform return type.
69//
70//-----------------------------------------------------------------------------
71
72// Default behaviour assumes you're at a leaf.
73
74template<class Expr, class FTag, class CTag>
75struct ForEach
76{
78 inline static
79 Type_t apply(const Expr &expr, const FTag &f, const CTag &)
80 {
81 return LeafFunctor<Expr, FTag>::apply(expr, f);
82 }
83};
84
85template<class Expr, class FTag, class CTag>
87forEach(const Expr &e, const FTag &f, const CTag &c)
88{
90}
91
92template<class Op, class A, class FTag, class CTag>
93struct ForEach<UnaryNode<Op, A>, FTag, CTag>
94{
97 inline static
98 Type_t apply(const UnaryNode<Op, A> &expr, const FTag &f,
99 const CTag &c)
100 {
103 expr.operation(), c);
104 }
105};
106
107template<class Op, class A, class B, class FTag, class CTag>
108struct ForEach<BinaryNode<Op, A, B>, FTag, CTag >
109{
113 inline static
114 Type_t apply(const BinaryNode<Op, A, B> &expr, const FTag &f,
115 const CTag &c)
116 {
120 expr.operation(), c);
121 }
122};
123
124template<class Op, class A, class B, class C, class FTag, class CTag>
125struct ForEach<TrinaryNode<Op, A, B, C>, FTag, CTag >
126{
132 inline static
133 Type_t apply(const TrinaryNode<Op, A, B, C> &expr, const FTag &f,
134 const CTag &c)
135 {
140 expr.operation(), c);
141 }
142};
143
144#ifndef PETE_USER_DEFINED_EXPRESSION
145
146template<class T> class Expression;
147
148template<class T, class FTag, class CTag>
149struct ForEach<Expression<T>, FTag, CTag>
150{
152 inline static
153 Type_t apply(const Expression<T> &expr, const FTag &f,
154 const CTag &c)
155 {
156 return ForEach<T, FTag, CTag>::apply(expr.expression(), f, c);
157 }
158};
159
160#endif // !PETE_USER_DEFINED_EXPRESSION
161
162template<class T> struct Reference;
163
164template<class T, class FTag, class CTag>
165struct ForEach<Reference<T>, FTag, CTag>
166{
168 inline static
169 Type_t apply(const Reference<T> &ref, const FTag &f,
170 const CTag &c)
171 {
172 return ForEach<T, FTag, CTag>::apply(ref.reference(), f, c);
173 }
174};
175
176#endif // PETE_PETE_FOREACH_H
177
178// ACL:rcsinfo
179// ----------------------------------------------------------------------
180// $RCSfile: ForEach.h,v $ $Author: edwards $
181// $Revision: 1.1 $ $Date: 2002-09-12 18:22:16 $
182// ----------------------------------------------------------------------
183// ACL:rcsinfo
ForEach< Expr, FTag, CTag >::Type_t forEach(const Expr &e, const FTag &f, const CTag &c)
Definition ForEach.h:87
DeReference< Left >::Return_t left() const
Definition TreeNodes.h:247
DeReference< Right >::Return_t right() const
Definition TreeNodes.h:251
const Op & operation() const
Definition TreeNodes.h:243
const Expression_t & expression() const
Definition CreateLeaf.h:62
DeReference< Left >::Return_t left() const
Definition TreeNodes.h:361
DeReference< Right >::Return_t right() const
Definition TreeNodes.h:365
DeReference< Middle >::Return_t middle() const
Definition TreeNodes.h:369
const Op & operation() const
Definition TreeNodes.h:357
DeReference< Child >::Return_t child() const
Definition TreeNodes.h:151
const Op & operation() const
Definition TreeNodes.h:147
OLattice< PScalar< PColorMatrix< RComplexFloat, 3 > > > C
static Type_t combine(const A &a, const Op &, const Tag &)
Definition Combiners.h:88
static Type_t combine(const A &a, const B &b, const C &c, const Op &op, const Tag &t)
Definition Combiners.h:103
Combine2< Type1_t, C, Op, Tag >::Type_t Type_t
Definition Combiners.h:101
ForEach< B, FTag, CTag >::Type_t TypeB_t
Definition ForEach.h:111
ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition ForEach.h:110
Combine2< TypeA_t, TypeB_t, Op, CTag >::Type_t Type_t
Definition ForEach.h:112
static Type_t apply(const BinaryNode< Op, A, B > &expr, const FTag &f, const CTag &c)
Definition ForEach.h:114
static Type_t apply(const Expression< T > &expr, const FTag &f, const CTag &c)
Definition ForEach.h:153
ForEach< T, FTag, CTag >::Type_t Type_t
Definition ForEach.h:151
ForEach< T, FTag, CTag >::Type_t Type_t
Definition ForEach.h:167
static Type_t apply(const Reference< T > &ref, const FTag &f, const CTag &c)
Definition ForEach.h:169
Combine3< TypeA_t, TypeB_t, TypeC_t, Op, CTag >::Type_t Type_t
Definition ForEach.h:131
ForEach< C, FTag, CTag >::Type_t TypeC_t
Definition ForEach.h:129
static Type_t apply(const TrinaryNode< Op, A, B, C > &expr, const FTag &f, const CTag &c)
Definition ForEach.h:133
ForEach< B, FTag, CTag >::Type_t TypeB_t
Definition ForEach.h:128
ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition ForEach.h:127
Combine1< TypeA_t, Op, CTag >::Type_t Type_t
Definition ForEach.h:96
ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition ForEach.h:95
static Type_t apply(const UnaryNode< Op, A > &expr, const FTag &f, const CTag &c)
Definition ForEach.h:98
LeafFunctor< Expr, FTag >::Type_t Type_t
Definition ForEach.h:77
static Type_t apply(const Expr &expr, const FTag &f, const CTag &)
Definition ForEach.h:79
const T & reference() const
Definition TreeNodes.h:81