QMCPACK
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 
32 namespace qmcplusplus
33 {
34 ///////////////////////////////////////////////////////////////////////////////
35 //
36 // WARNING: THIS FILE IS FOR INTERNAL PETE USE. DON'T INCLUDE IT YOURSELF
37 //
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 //-----------------------------------------------------------------------------
41 //
42 // CLASS NAME
43 // ForEach<Expr, FTag, CTag>
44 // forEach(Expr& e, FTag& f, CTag& c)
45 //
46 // Expr is the type of the expression tree.
47 // FTag is the type of the leaf tag.
48 // CTag is the type of the combiner tag.
49 //
50 // ForEach<Expr,FTag,CTag>::apply(Expr &e,FTag& f,CTag& c) is a function
51 // that traverses the expression tree defined by e, applies the functor f
52 // to each leaf and combines the results together using the combiner c.
53 // The type of object returned is given by:
54 // typename ForEach<Expr,FTag,CTag>::Type_t
55 // the function forEachTag(Expr& e,FTag& f,CTag& c) duplicates the action
56 // of ForEach::apply and is provided for convenience. (You don't have to
57 // type the template arguments.)
58 //
59 // This generic ForEach functor differs from the original
60 // PETE forEach in 3 ways:
61 // 1) The user should not specialize ForEach.
62 // Specializing TagFunctor and TagCombiner should take care of
63 // the behaviour at leaves and nodes and the user knows nothing
64 // of the tree.
65 // 2) It's a functor, so it contains generic type computations.
66 // The typedef Type_t gives you the type of the result for any
67 // user defined type and method for combining that type.
68 // 3) It handles generic combination. Previously the user had the
69 // choice of using the operators in the tree to produce the expression
70 // return type or defining a combiner with a uniform return type.
71 //
72 //-----------------------------------------------------------------------------
73 
74 // Default behaviour assumes you're at a leaf.
75 
76 template<class Expr, class FTag, class CTag>
77 struct ForEach
78 {
80  inline static Type_t apply(const Expr& expr, const FTag& f, const CTag&)
81  {
82  return LeafFunctor<Expr, FTag>::apply(expr, f);
83  }
84 };
85 
86 template<class Expr, class FTag, class CTag>
87 inline typename ForEach<Expr, FTag, CTag>::Type_t forEach(const Expr& e, const FTag& f, const CTag& c)
88 {
89  return ForEach<Expr, FTag, CTag>::apply(e, f, c);
90 }
91 
92 template<class Op, class A, class FTag, class CTag>
93 struct ForEach<UnaryNode<Op, A>, FTag, CTag>
94 {
97  inline static Type_t apply(const UnaryNode<Op, A>& expr, const FTag& f, const CTag& c)
98  {
100  }
101 };
102 
103 template<class Op, class A, class B, class FTag, class CTag>
104 struct ForEach<BinaryNode<Op, A, B>, FTag, CTag>
105 {
109  inline static Type_t apply(const BinaryNode<Op, A, B>& expr, const FTag& f, const CTag& c)
110  {
112  ForEach<B, FTag, CTag>::apply(expr.right(), f, c),
113  expr.operation(), c);
114  }
115 };
116 
117 template<class Op, class A, class B, class C, class FTag, class CTag>
118 struct ForEach<TrinaryNode<Op, A, B, C>, FTag, CTag>
119 {
124  inline static Type_t apply(const TrinaryNode<Op, A, B, C>& expr, const FTag& f, const CTag& c)
125  {
128  ForEach<C, FTag, CTag>::apply(expr.right(), f, c),
129  expr.operation(), c);
130  }
131 };
132 
133 #ifndef PETE_USER_DEFINED_EXPRESSION
134 
135 template<class T>
136 class Expression;
137 
138 template<class T, class FTag, class CTag>
139 struct ForEach<Expression<T>, FTag, CTag>
140 {
142  inline static Type_t apply(const Expression<T>& expr, const FTag& f, const CTag& c)
143  {
144  return ForEach<T, FTag, CTag>::apply(expr.expression(), f, c);
145  }
146 };
147 
148 #endif // !PETE_USER_DEFINED_EXPRESSION
149 
150 template<class T>
151 struct Reference;
152 
153 template<class T, class FTag, class CTag>
154 struct ForEach<Reference<T>, FTag, CTag>
155 {
157  inline static Type_t apply(const Reference<T>& ref, const FTag& f, const CTag& c)
158  {
159  return ForEach<T, FTag, CTag>::apply(ref.reference(), f, c);
160  }
161 };
162 
163 } // namespace qmcplusplus
164 
165 #endif // PETE_PETE_FOREACH_H
166 
167 // ACL:rcsinfo
DeReference< Right >::Return_t right() const
Definition: TreeNodes.h:213
typename ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition: ForEach.h:106
typename Combine1< TypeA_t, Op, CTag >::Type_t Type_t
Definition: ForEach.h:96
static Type_t apply(const Expr &expr, const FTag &f, const CTag &)
Definition: ForEach.h:80
typename ForEach< T, FTag, CTag >::Type_t Type_t
Definition: ForEach.h:141
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
typename ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition: ForEach.h:120
typename LeafFunctor< Expr, FTag >::Type_t Type_t
Definition: ForEach.h:79
ForEach< Expr, FTag, CTag >::Type_t forEach(const Expr &e, const FTag &f, const CTag &c)
Definition: ForEach.h:87
const Op & operation() const
Definition: TreeNodes.h:296
const Op & operation() const
Definition: TreeNodes.h:133
DeReference< Left >::Return_t left() const
Definition: TreeNodes.h:298
static Type_t combine(const A &a, const B &b, const C &c, const Op &op, const Tag &t)
Definition: Combiners.h:103
static Type_t apply(const Reference< T > &ref, const FTag &f, const CTag &c)
Definition: ForEach.h:157
typename ForEach< C, FTag, CTag >::Type_t TypeC_t
Definition: ForEach.h:122
typename ForEach< T, FTag, CTag >::Type_t Type_t
Definition: ForEach.h:156
typename ForEach< A, FTag, CTag >::Type_t TypeA_t
Definition: ForEach.h:95
DeReference< Left >::Return_t left() const
Definition: TreeNodes.h:211
static Type_t apply(const Expression< T > &expr, const FTag &f, const CTag &c)
Definition: ForEach.h:142
const T & reference() const
Definition: TreeNodes.h:73
static Type_t apply(const BinaryNode< Op, A, B > &expr, const FTag &f, const CTag &c)
Definition: ForEach.h:109
const Op & operation() const
Definition: TreeNodes.h:209
typename ForEach< B, FTag, CTag >::Type_t TypeB_t
Definition: ForEach.h:121
DeReference< Child >::Return_t child() const
Definition: TreeNodes.h:135
typename Combine2< TypeA_t, TypeB_t, Op, CTag >::Type_t Type_t
Definition: ForEach.h:108
const Expression_t & expression() const
Definition: CreateLeaf.h:64
static Type_t apply(const TrinaryNode< Op, A, B, C > &expr, const FTag &f, const CTag &c)
Definition: ForEach.h:124
typename Combine3< TypeA_t, TypeB_t, TypeC_t, Op, CTag >::Type_t Type_t
Definition: ForEach.h:123
typename ForEach< B, FTag, CTag >::Type_t TypeB_t
Definition: ForEach.h:107
DeReference< Middle >::Return_t middle() const
Definition: TreeNodes.h:302
double B(double x, int k, int i, const std::vector< double > &t)
typename Combine2< Type1_t, C, Op, Tag >::Type_t Type_t
Definition: Combiners.h:102
static Type_t apply(const UnaryNode< Op, A > &expr, const FTag &f, const CTag &c)
Definition: ForEach.h:97
DeReference< Right >::Return_t right() const
Definition: TreeNodes.h:300
static Type_t combine(const A &a, const Op &, const Tag &)
Definition: Combiners.h:89