QDP++
Combiners.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_COMBINERS_H
30#define PETE_PETE_COMBINERS_H
31
33//
34// WARNING: THIS FILE IS FOR INTERNAL PETE USE. DON'T INCLUDE IT YOURSELF
35//
37
38//-----------------------------------------------------------------------------
39//
40// CLASS NAMES
41// Combine1<A,Op,Tag>
42// Combine2<A,B,Op,Tag>
43// Combine3<A,B,C,Op,Tag>
44//
45// DESCRIPTION
46// A,B, and C are the types of the combined arguments
47// Op is the type of the operator from the expression tree
48// Tag is the tag type which selects the type of combiner operation
49//
50// Users create their own combiner operations by specializing these
51// three structs for a particular user defined tag. A typical example
52// might be an operation that compares the domains of all the leaves in
53// an expression.
54//
55// Combine1 requires the user to define:
56// typedef ... Type_t;
57// - the return type of the combination
58// static Type_t combine(const A &a, const Op &op, const Tag &t) {}
59// - a function that combines a
60//
61// Combine2 requires the user to define:
62// typedef ... Type_t;
63// - the return type of the combination
64// static Type_t combine(const A &a, const B &b, const Op &op,
65// const Tag &t) {}
66// - a function that combines a and b
67//
68// Combine3 requires the user to define:
69// typedef ... Type_t;
70// - the return type of the combination
71// static Type_t combine(const A &a, const B &b, const C &c,
72// const const Op &op, const Tag &t) {}
73// - a function that combines a and b and c
74//
75// The default actions for combiners are:
76//
77// Combine1 - returns the arguments
78// Combine2 - no default action... the user must define this at least
79// Combine3 - uses Combine2 to combine a and b and combine that with c
80//
81//-----------------------------------------------------------------------------
82
83template<class A, class Op, class Tag>
85{
86 typedef A Type_t;
87 inline static
88 Type_t combine(const A &a, const Op &, const Tag &) { return a; }
89};
90
91template<class A, class B, class Op, class Tag>
93{
94 // no default action. It's an error to not specialize this struct.
95};
96
97template<class A,class B,class C,class Op,class Tag>
99{
102 inline static
103 Type_t combine(const A& a,const B& b,const C& c,const Op& op,const Tag& t)
104 {
105 return
107 Op, Tag>::combine(Combine2<A, B, Op, Tag>::combine(a,b,op,t),c,op,t);
108 }
109};
110
111//-----------------------------------------------------------------------------
112//
113// FUNCTION NAME
114// peteCombine()
115//
116// DESCRIPTION
117// These functions provide a simple user interface to PETE's CombineN
118// functors. For example:
119//
120// peteCombine(a, b, op, tag);
121//
122// is a synonym for:
123//
124// Combine2<A, B, Op, Tag>::combine(a, b, op, tag);
125//
126//-----------------------------------------------------------------------------
127
128template<class A, class Op, class Tag>
129inline typename Combine1<A, Op, Tag>::Type_t
130peteCombine(const A &a, const Op &op, const Tag &t)
131{
132 return Combine1<A, Op, Tag>::combine(a, op, t);
133}
134
135template<class A, class B, class Op, class Tag>
137peteCombine(const A &a, const B &b, const Op &op, const Tag &t)
138{
139 return Combine2<A, B, Op, Tag>::combine(a, b, op, t);
140}
141
142template<class A, class B, class C, class Op, class Tag>
144peteCombine(const A &a, const B &b, const C &c, const Op &op, const Tag &t)
145{
146 return Combine3<A, B, C, Op, Tag>::combine(a, b, c, op, t);
147}
148
149//-----------------------------------------------------------------------------
150//
151// CLASS NAME
152// TreeCombine
153//
154// DESCRIPTION
155// This combiner is provided so that the user can write a functor
156// that modifies the leaves of an expression and use
157// ForEach<Expr,UserFunctor,TreeCombine> to get the entire expression
158// back with modified leaves.
159//
160// This type of operation is useful when expressions are being
161// passed around. For example, you might subset and expression and pass
162// that result to another function. The type of expression tree that you
163// are passing has subsetted nodes, and could be a different type from the
164// original expression.
165//
166//-----------------------------------------------------------------------------
167
172
173template<class A, class Op>
174struct Combine1<A, Op, TreeCombine >
175{
177 inline static
178 Type_t combine(const A &a, const Op &op, const TreeCombine &t)
179 {
180 return Type_t(op, a);
181 }
182};
183
184template<class A, class B, class Op>
185struct Combine2<A, B, Op, TreeCombine >
186{
188 inline static
189 Type_t combine(const A &a, const B &b, const Op &op,
190 const TreeCombine &t)
191 {
192 return Type_t(op, a, b);
193 }
194};
195
196template<class A, class B, class C, class Op>
197struct Combine3<A, B, C, Op, TreeCombine >
198{
200 inline static
201 Type_t combine(const A &a, const B &b, const C &c, const Op &op,
202 const TreeCombine &t)
203 {
204 return Type_t(op, a, b, c);
205 }
206};
207
208
209//-----------------------------------------------------------------------------
210//
211// CLASS NAME
212// OpCombine
213//
214// DESCRIPTION
215// A combiner that uses the operations in the expression tree.
216//
217//-----------------------------------------------------------------------------
218
223
224template<class A,class Op>
225struct Combine1<A, Op, OpCombine>
226{
228 inline static
229 Type_t combine(A a, Op op, OpCombine) { return op(a); }
230};
231
232template<class A,class B,class Op>
233struct Combine2<A, B, Op, OpCombine>
234{
236 inline static
237 Type_t combine(A a, B b, Op op, OpCombine)
238 {
239 return op(a, b);
240 }
241};
242
243template<class A,class B,class C,class Op>
244struct Combine3<A, B, C, Op, OpCombine>
245{
247 inline static
248 Type_t combine(A a, B b, C c, Op op, OpCombine)
249 {
250 return op(a, b, c);
251 }
252};
253
254
255//-----------------------------------------------------------------------------
256//
257// CLASS NAME
258// AndCombine
259//
260// DESCRIPTION
261// A handy combiner for extracting bool queries from expressions.
262//
263//-----------------------------------------------------------------------------
264
269
270template<class Op>
271struct Combine2<bool, bool, Op, AndCombine>
272{
273 typedef bool Type_t;
274 inline static
275 Type_t combine(bool a, bool b, Op, AndCombine)
276 {
277 return (a && b);
278 }
279};
280
281
282//-----------------------------------------------------------------------------
283//
284// CLASS NAME
285// OrCombine
286//
287// DESCRIPTION
288// A handy combiner for extracting bool queries from expressions.
289//
290//-----------------------------------------------------------------------------
291
296
297template<class Op>
298struct Combine2<bool, bool, Op, OrCombine>
299{
300 typedef bool Type_t;
301 inline static
302 Type_t combine(bool a, bool b, Op, OrCombine)
303 {
304 return (a || b);
305 }
306};
307
308
309//-----------------------------------------------------------------------------
310//
311// CLASS NAME
312// NullCombine
313//
314// DESCRIPTION
315// This combiner doesn't do anything. Used when the combination isn't meant
316// return anything since combiners cannot return void.
317//
318//-----------------------------------------------------------------------------
319
324
325template<class Op>
326struct Combine2<int, int, Op, NullCombine>
327{
328 typedef int Type_t;
329 inline static
331 {
332 return 0;
333 }
334};
335
336
337//-----------------------------------------------------------------------------
338//
339// CLASS NAME
340// SumCombine
341//
342// DESCRIPTION
343// This combiner can be used to count things in expressions.
344//
345//-----------------------------------------------------------------------------
346
351
352template<class Op>
353struct Combine2<int, int, Op, SumCombine>
354{
355 typedef int Type_t;
356 inline static
357 Type_t combine(int a, int b, Op, SumCombine)
358 {
359 return a + b;
360 }
361};
362
363
364#endif // PETE_PETE_COMBINERS_H
365
366// ACL:rcsinfo
367// ----------------------------------------------------------------------
368// $RCSfile: Combiners.h,v $ $Author: edwards $
369// $Revision: 1.1 $ $Date: 2002-09-12 18:22:16 $
370// ----------------------------------------------------------------------
371// ACL:rcsinfo
Combine1< A, Op, Tag >::Type_t peteCombine(const A &a, const Op &op, const Tag &t)
Definition Combiners.h:130
#define PETE_EMPTY_CONSTRUCTORS(CLASS)
Definition PETE.h:58
OLattice< PScalar< PColorMatrix< RComplexFloat, 3 > > > C
Promote< T1, T2 >::Type_t Type_t
static Type_t combine(A a, Op op, OpCombine)
Definition Combiners.h:229
UnaryReturn< A, Op >::Type_t Type_t
Definition Combiners.h:227
static Type_t combine(const A &a, const Op &op, const TreeCombine &t)
Definition Combiners.h:178
UnaryNode< Op, A > Type_t
Definition Combiners.h:176
static Type_t combine(const A &a, const Op &, const Tag &)
Definition Combiners.h:88
BinaryReturn< A, B, Op >::Type_t Type_t
Definition Combiners.h:235
static Type_t combine(A a, B b, Op op, OpCombine)
Definition Combiners.h:237
BinaryNode< Op, A, B > Type_t
Definition Combiners.h:187
static Type_t combine(const A &a, const B &b, const Op &op, const TreeCombine &t)
Definition Combiners.h:189
static Type_t combine(bool a, bool b, Op, AndCombine)
Definition Combiners.h:275
static Type_t combine(bool a, bool b, Op, OrCombine)
Definition Combiners.h:302
static Type_t combine(int, int, Op, NullCombine)
Definition Combiners.h:330
static Type_t combine(int a, int b, Op, SumCombine)
Definition Combiners.h:357
TrinaryReturn< A, B, C, Op >::Type_t Type_t
Definition Combiners.h:246
static Type_t combine(A a, B b, C c, Op op, OpCombine)
Definition Combiners.h:248
TrinaryNode< Op, A, B, C > Type_t
Definition Combiners.h:199
static Type_t combine(const A &a, const B &b, const C &c, const Op &op, const TreeCombine &t)
Definition Combiners.h:201
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
Combine2< A, B, Op, Tag >::Type_t Type1_t
Definition Combiners.h:100
BinaryReturn< T2, T3, Op >::Type_t Type_t