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.
collectionconstraintevaluator.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_COLLECTIONCONSTRAINTEVALUATOR_H
8 #define IGLOO_COLLECTIONCONSTRAINTEVALUATOR_H
9 
10 #include <string>
11 #include "../../../assertionexception.h"
12 
13 namespace snowhouse
14 {
15 
16 template<typename ConstraintListType, typename ActualType>
18 {
19  static unsigned int Evaluate(const ConstraintOperator& op,
20  ConstraintListType& expression, ResultStack& result,
21  OperatorStack& operators, const ActualType& actual)
22  {
23  ConstraintOperator::EvaluateOperatorsWithLessOrEqualPrecedence(op,
24  operators, result);
25 
26  unsigned int passed_elements = 0;
27  typename ActualType::const_iterator it;
28  for(it = actual.begin(); it != actual.end(); it++)
29  {
30  if(ConstraintOperator::EvaluateElementAgainstRestOfExpression(expression,
31  *it))
32  {
33  passed_elements++;
34  }
35  }
36 
37  return passed_elements;
38  }
39 };
40 
42 {
43  static void Parse(const std::string& str, std::vector<std::string>& res)
44  {
45  size_t start = 0;
46  size_t newline = FindNewline(str, start);
47 
48  while(newline != std::string::npos)
49  {
50  StoreLine(str, start, newline, res);
51  start = MoveToNextLine(str, newline);
52  newline = FindNewline(str, start);
53  }
54 
55  if(start < str.size())
56  {
57  StoreLine(str, start, std::string::npos, res);
58  }
59  }
60 
61 private:
62  static size_t FindNewline(const std::string& str, size_t start)
63  {
64  return str.find_first_of("\r\n", start);
65  }
66 
67  static void StoreLine(const std::string& str, size_t start, size_t end,
68  std::vector<std::string>& res)
69  {
70  std::string line = str.substr(start, end - start);
71  res.push_back(line);
72  }
73 
74  static size_t MoveToNextLine(const std::string& str, size_t newline)
75  {
76  if(str.find("\r\n", newline) == newline)
77  {
78  return newline + 2;
79  }
80 
81  if(str.find("\n", newline) == newline)
82  {
83  return newline + 1;
84  }
85 
86  if(str.find("\r", newline) == newline)
87  {
88  return newline + 1;
89  }
90 
91  std::ostringstream stm;
92  stm << "This string seems to contain an invalid line ending at position "
93  << newline << ":\n" << str << std::endl;
94  throw AssertionException(stm.str());
95  }
96 };
97 
98 template<typename ConstraintListType>
99 struct CollectionConstraintEvaluator<ConstraintListType, std::string>
100 {
101  static unsigned int Evaluate(const ConstraintOperator& op,
102  ConstraintListType& expression, ResultStack& result,
103  OperatorStack& operators, const std::string& actual)
104  {
105  std::vector<std::string> lines;
106  StringLineParser::Parse(actual, lines);
107  return CollectionConstraintEvaluator<ConstraintListType, std::vector<std::string> >::Evaluate(op, expression, result, operators, lines);
108  }
109 };
110 
111 }
112 
113 #endif
Definition: assert.h:13
Definition: constraintoperator.h:26
Definition: assertionexception.h:11
Definition: collectionconstraintevaluator.h:17
Definition: collectionconstraintevaluator.h:41