blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
expressionbuilder.h
1 
2 // Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef IGLOO_EXPRESSIONBUILDER_H
8 #define IGLOO_EXPRESSIONBUILDER_H
9 
10 namespace snowhouse {
11 
12  // ---- Evaluation of list of constraints
13 
14  template <typename ConstraintListType, typename ActualType>
15  inline void EvaluateConstraintList(ConstraintListType& constraint_list, ResultStack& result, OperatorStack& operators, const ActualType& actual)
16  {
17  constraint_list.m_head.Evaluate(constraint_list, result, operators, actual);
18  }
19 
20  template <typename ActualType>
21  inline void EvaluateConstraintList(Nil&, ResultStack&, OperatorStack&, const ActualType&) {}
22 
23 
24  template <typename ConstraintListType>
26  {
27  ExpressionBuilder(const ConstraintListType& list) : m_constraint_list(list)
28  {
29  }
30 
31  template <typename ExpectedType>
33  EqualTo(const ExpectedType& expected)
34  {
35  typedef ConstraintAdapter<EqualsConstraint<ExpectedType> > ConstraintAdapterType;
37 
38  ConstraintAdapterType constraint(expected);
40 
41  return BuilderType(Concatenate(m_constraint_list, node));
42  }
43 
44  template <typename ExpectedType, typename DeltaType>
46  EqualToWithDelta(const ExpectedType& expected, const DeltaType& delta)
47  {
50 
51  ConstraintAdapterType constraint(EqualsWithDeltaConstraint<ExpectedType, DeltaType>(expected, delta));
53 
54  return BuilderType(Concatenate(m_constraint_list, node));
55  }
56 
57  template <typename MatcherType>
59  Fulfilling(const MatcherType& matcher)
60  {
61  typedef ConstraintAdapter<FulfillsConstraint<MatcherType> > ConstraintAdapterType;
63 
64  ConstraintAdapterType constraint(matcher);
66 
67  return BuilderType(Concatenate(m_constraint_list, node));
68  }
69 
71  False()
72  {
73  return EqualTo<bool>(false);
74  }
75 
77  True()
78  {
79  return EqualTo<bool>(true);
80  }
81 
83  EqualTo(const char* expected)
84  {
85  return EqualTo<std::string>(std::string(expected));
86  }
87 
88  template <typename ExpectedType>
90  GreaterThan(const ExpectedType& expected)
91  {
92  typedef ConstraintAdapter<IsGreaterThanConstraint<ExpectedType> > ConstraintAdapterType;
93 
95  ConstraintAdapterType constraint(expected);
97  return BuilderType(Concatenate(m_constraint_list, node));
98  }
99 
100  template <typename ExpectedType>
102  LessThan(const ExpectedType& expected)
103  {
104  typedef ConstraintAdapter<IsLessThanConstraint<ExpectedType> > ConstraintAdapterType;
105 
107  ConstraintAdapterType constraint(expected);
109  return BuilderType(Concatenate(m_constraint_list, node));
110  }
111 
112  template <typename ExpectedType>
114  Containing(const ExpectedType& expected)
115  {
116  typedef ConstraintAdapter<ContainsConstraint<ExpectedType> > ConstraintAdapterType;
117 
119  ConstraintAdapterType constraint(expected);
121  return BuilderType(Concatenate(m_constraint_list, node));
122  }
123 
125  Containing(const char* expected)
126  {
127  return Containing<std::string>(std::string(expected));
128  }
129 
130  template <typename ExpectedType>
132  EndingWith(const ExpectedType& expected)
133  {
134  typedef ConstraintAdapter<EndsWithConstraint<ExpectedType> > ConstraintAdapterType;
136 
137  ConstraintAdapterType constraint(expected);
139  return BuilderType(Concatenate(m_constraint_list, node));
140  }
141 
143  EndingWith(const char* expected)
144  {
145  return EndingWith(std::string(expected));
146  }
147 
148  template <typename ExpectedType>
150  StartingWith(const ExpectedType& expected)
151  {
152  typedef ConstraintAdapter<StartsWithConstraint<ExpectedType> > ConstraintAdapterType;
153 
155  ConstraintAdapterType constraint(expected);
157  return BuilderType(Concatenate(m_constraint_list, node));
158  }
159 
161  StartingWith(const char* expected)
162  {
163  return StartingWith(std::string(expected));
164  }
165 
166  template <typename ExpectedType>
168  OfLength(const ExpectedType& expected)
169  {
170  typedef ConstraintAdapter<HasLengthConstraint<ExpectedType> > ConstraintAdapterType;
171 
173  ConstraintAdapterType constraint(expected);
175  return BuilderType(Concatenate(m_constraint_list, node));
176  }
177 
179  Empty()
180  {
181  typedef ConstraintAdapter<HasLengthConstraint<int> > ConstraintAdapterType;
182 
184  ConstraintAdapterType constraint(0);
186  return BuilderType(Concatenate(m_constraint_list, node));
187  }
188 
189  template <typename ExpectedType>
191  EqualToContainer(const ExpectedType& expected)
192  {
193  typedef bool (*DefaultBinaryPredivateType)(const typename ExpectedType::value_type&, const typename ExpectedType::value_type&);
195 
197  ConstraintAdapterType constraint(EqualsContainerConstraint<ExpectedType, DefaultBinaryPredivateType>(expected, constraint_internal::default_comparer<typename ExpectedType::value_type>));
199  return BuilderType(Concatenate(m_constraint_list, node));
200  }
201 
202  template <typename ExpectedType, typename BinaryPredicate>
204  EqualToContainer(const ExpectedType& expected, const BinaryPredicate predicate)
205  {
207 
209  ConstraintAdapterType constraint(EqualsContainerConstraint<ExpectedType, BinaryPredicate>(expected, predicate));
211  return BuilderType(Concatenate(m_constraint_list, node));
212  }
213 
222 
224  {
226  AllOperator op;
227  AllOperatorNode node(op, Nil());
228  return BuilderType(Concatenate(m_constraint_list, node));
229  }
230 
232  {
234  AtLeastOperator op(expected);
235  AtLeastOperatorNode node(op, Nil());
236  return BuilderType(Concatenate(m_constraint_list, node));
237  }
238 
240  {
242  ExactlyOperator op(expected);
243  ExactlyOperatorNode node(op, Nil());
244  return BuilderType(Concatenate(m_constraint_list, node));
245  }
246 
248  {
250  AtMostOperator op(expected);
251  AtMostOperatorNode node(op, Nil());
252  return BuilderType(Concatenate(m_constraint_list, node));
253  }
254 
256  {
258  NoneOperator op;
259  NoneOperatorNode node(op, Nil());
260  return BuilderType(Concatenate(m_constraint_list, node));
261  }
262 
264  {
266  AndOperator op;
267  AndOperatorNode node(op, Nil());
268  return BuilderType(Concatenate(m_constraint_list, node));
269  }
270 
272  {
274  OrOperator op;
275  OrOperatorNode node(op, Nil());
276  return BuilderType(Concatenate(m_constraint_list, node));
277  }
278 
280  {
282  NotOperator op;
283  NotOperatorNode node(op, Nil());
284  return BuilderType(Concatenate(m_constraint_list, node));
285  }
286 
287  template <typename ActualType>
288  void Evaluate(ResultStack& result, OperatorStack& operators, const ActualType& actual)
289  {
290  EvaluateConstraintList(m_constraint_list, result, operators, actual);
291  }
292 
293  ConstraintListType m_constraint_list;
294  };
295 
296  template <typename T>
297  inline void StringizeConstraintList(const T& list, std::ostringstream& stm)
298  {
299  if (stm.tellp() > 0)
300  stm << " ";
301 
302  stm << snowhouse::Stringize(list.m_head);
303  StringizeConstraintList(list.m_tail, stm);
304  }
305 
306  inline void StringizeConstraintList(const Nil&, std::ostringstream&)
307  {
308  }
309 
310  template<typename ConstraintListType>
311  struct Stringizer< ExpressionBuilder<ConstraintListType> >
312  {
313  static std::string ToString(const ExpressionBuilder<ConstraintListType>& builder)
314  {
315  std::ostringstream stm;
316  StringizeConstraintList(builder.m_constraint_list, stm);
317 
318  return stm.str();
319  }
320  };
321 }
322 
323 #endif
Definition: notoperator.h:12
Definition: assert.h:13
Definition: expressionbuilder.h:25
Definition: andoperator.h:12
Definition: constraintadapter.h:13
Definition: alloperator.h:14
Definition: constraintlist.h:17
Definition: exactlyoperator.h:12
Definition: equalswithdeltaconstraint.h:15
Definition: stringize.h:71
Definition: equalscontainerconstraint.h:21
Definition: noneoperator.h:12
Definition: atleastoperator.h:14
Definition: constraintlist.h:31
Definition: oroperator.h:12
Definition: atmostoperator.h:12