scalarPredicates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "scalarPredicates.H"
29 #include "HashSet.H"
30 #include "FlatOutput.H"
31 #include "Tuple2.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const Foam::Enum
36 <
38 >
40 ({
41  { opType::EQUAL, "eq" },
42  { opType::EQUAL, "equal" },
43  { opType::NOT_EQUAL, "neq" },
44  { opType::NOT_EQUAL, "notEqual" },
45  { opType::LESS, "lt" },
46  { opType::LESS, "less" },
47  { opType::LESS_EQ, "le" },
48  { opType::LESS_EQ, "lessEq" },
49  { opType::GREATER, "gt" },
50  { opType::GREATER, "greater" },
51  { opType::GREATER_EQ, "ge" },
52  { opType::GREATER_EQ, "greaterEq" },
53 });
54 
55 
56 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
57 
58 std::function<bool(Foam::scalar)> Foam::predicates::scalars::operation
59 (
60  const enum predicates::scalars::opType op,
61  const Foam::scalar opVal,
62  const Foam::scalar tol
63 )
64 {
65  switch (op)
66  {
67  case opType::EQUAL:
68  return equalOp(opVal, tol);
69  break;
70  case opType::NOT_EQUAL:
71  return notEqualOp(opVal, tol);
72  break;
73  case opType::LESS:
74  return lessOp(opVal);
75  break;
76  case opType::LESS_EQ:
77  return lessEqOp(opVal);
78  break;
79  case opType::GREATER:
80  return greaterOp(opVal);
81  break;
82  case opType::GREATER_EQ:
83  return greaterEqOp(opVal);
84  break;
85  case opType::ALWAYS:
86  return trueOp();
87  break;
88  default:
89  break;
90  }
91 
92  return falseOp();
93 }
94 
95 
96 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
97 
98 namespace Foam
99 {
100  // Check for bad/unknown operations
101  template<class Container, class Get0>
102  static bool hasBadEntries
103  (
104  const Container& entries,
105  const Get0& get0
106  )
107  {
108  for (const auto& entry : entries)
109  {
111  {
112  return true;
113  }
114  }
115 
116  return false;
117  }
118 
119 
120  // Print bad/unknown operations
121  template<class Error, class Container, class Get0, class Get1>
122  static Error& printBadEntries
123  (
124  Error& err,
125  const Container& entries,
126  const Get0& get0,
127  const Get1& get1
128  )
129  {
130  labelHashSet badIndices;
131 
132  label idx = 0;
133 
134  for (const auto& entry : entries)
135  {
137  {
138  badIndices.insert(idx);
139  }
140  ++idx;
141  }
142 
143  err
144  << "Entries with unknown operations:" << nl
145  << idx << nl
146  << '(' << nl;
147 
148  idx = 0;
149  for (const auto& entry : entries)
150  {
151  const bool bad = badIndices.found(idx);
152  ++idx;
153 
154  if (bad)
155  {
156  err << ">>> ";
157  }
158  else
159  {
160  err << " ";
161  }
162  err << '(' << get0(entry) << ' ' << get1(entry) << ')';
163 
164  if (bad)
165  {
166  err << " <<<";
167  }
168  err << nl;
169  }
170 
171  err << ')' << nl;
172 
173  return err;
174  }
175 
176 } // End namespace Foam
177 
178 
179 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
180 
182 (
183  std::initializer_list<std::pair<word, scalar>> entries
184 )
185 :
186  List<unary>(entries.size())
187 {
188  // Access
189  const auto get0 =
190  [](const std::pair<word,scalar>& entry) { return entry.first; };
191  const auto get1 =
192  [](const std::pair<word,scalar>& entry) { return entry.second; };
193 
194  // Check for bad/unknown operations
195  if (hasBadEntries(entries, get0))
196  {
197  printBadEntries(FatalErrorInFunction, entries, get0, get1)
198  << exit(FatalError);
199  }
200 
201  // Appears to be good
202  auto& list = *this;
203  label idx = 0;
204  for (const auto& entry : entries)
205  {
207  ++idx;
208  }
209 }
210 
211 
213 (
214  const UList<Tuple2<word, scalar>>& entries
215 )
216 :
217  List<unary>(entries.size())
218 {
219  // Access
220  const auto get0 =
221  [](const Tuple2<word,scalar>& entry) { return entry.first(); };
222  const auto get1 =
223  [](const Tuple2<word,scalar>& entry) { return entry.second(); };
224 
225  // Check for bad/unknown operations
226  if (hasBadEntries(entries, get0))
227  {
228  printBadEntries(FatalErrorInFunction, entries, get0, get1)
229  << exit(FatalError);
230  }
231 
232  // Appears to be good
233  auto& list = *this;
234  label idx = 0;
235  for (const auto& entry : entries)
236  {
238  ++idx;
239  }
240 }
241 
242 
244 :
245  List<unary>()
246 {
247  is >> *this;
248 }
249 
250 
251 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
252 
254 (
255  const scalar value,
256  label pos
257 ) const
258 {
259  const label len = this->size();
260 
261  if (pos >= 0 && len)
262  {
263  // auto iter = this->cbegin();
264 
265  while (pos < len)
266  {
267  if ((*this)[pos](value))
268  {
269  return pos;
270  }
271 
272  ++pos;
273  }
274  }
275 
276  return -1;
277 }
278 
279 
281 (
282  const scalar value,
283  label pos
284 ) const
285 {
286  // pos == -1 has same meaning as std::string::npos - search from end
287  if (pos < 0 || pos >= this->size())
288  {
289  pos = this->size()-1;
290  }
291 
292  while (pos >= 0)
293  {
294  if ((*this)[pos](value))
295  {
296  return pos;
297  }
298 
299  --pos;
300  }
301 
302  return -1;
303 }
304 
305 
306 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
307 
309 {
310  // Read tuples
311  List<Tuple2<word, scalar>> entries(is);
312 
313  // Access
314  const auto get0 =
315  [](const Tuple2<word,scalar>& entry) { return entry.first(); };
316  const auto get1 =
317  [](const Tuple2<word,scalar>& entry) { return entry.second(); };
318 
319 
320  // Check for bad/unknown operations
321  if (hasBadEntries(entries, get0))
322  {
323  printBadEntries(FatalIOErrorInFunction(is), entries, get0, get1)
324  << exit(FatalIOError);
325  }
326 
327  // Appears to be good
328  list.resize(entries.size());
329 
330  label idx = 0;
331  for (const Tuple2<word, scalar>& entry : entries)
332  {
334  ++idx;
335  }
336 
337  return is;
338 }
339 
340 
341 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::predicates::scalars::scalars
scalars(std::initializer_list< std::pair< word, scalar >> entries)
Construct from an initializer list of (opName opValue) tuples.
Definition: scalarPredicates.C:182
Foam::predicates::scalars::greaterEqOp
static unary greaterEqOp(const scalar opVal)
Test if value is 'greater_equal' to prescribed.
Definition: scalarPredicates.H:156
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::predicates::scalars::opNames
static const Enum< opType > opNames
Names for the opType enumeration.
Definition: scalarPredicates.H:91
Foam::predicates::scalars::opType
opType
Enumerations for some standard comparison predicates.
Definition: scalarPredicates.H:78
Tuple2.H
Foam::predicates::scalars
A list of unary predicates (tests) on scalars. Includes a number of standard comparison predicates (e...
Definition: scalarPredicates.H:66
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::hasBadEntries
static bool hasBadEntries(const Container &entries, const Get0 &get0)
Definition: scalarPredicates.C:103
Foam::HashSet< label, Hash< label > >
Foam::predicates::scalars::greaterOp
static unary greaterOp(const scalar opVal)
Definition: scalarPredicates.H:146
Foam::predicates::scalars::find
label find(const scalar value, label pos=0) const
Index of the first match for the value.
Definition: scalarPredicates.C:254
Foam::predicates::scalars::rfind
label rfind(const scalar value, label pos=-1) const
Index of the last match for the value.
Definition: scalarPredicates.C:281
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::predicates::scalars::falseOp
static unary falseOp()
Predicate that always returns false.
Definition: scalarPredicates.H:174
Foam::predicates::scalars::notEqualOp
static unary notEqualOp(const scalar opVal, const scalar tol=VSMALL)
Compare for inequality, with specified tolerance (non-negative)
Definition: scalarPredicates.H:112
Foam::predicates::scalars::trueOp
static unary trueOp()
Predicate that always returns true.
Definition: scalarPredicates.H:168
HashSet.H
Foam::FatalError
error FatalError
Foam::predicates::scalars::lessEqOp
static unary lessEqOp(const scalar opVal)
Test if value is 'less_equal' to prescribed.
Definition: scalarPredicates.H:135
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::predicates::scalars::equalOp
static unary equalOp(const scalar opVal, const scalar tol=VSMALL)
Compare for equality, with specified tolerance (non-negative)
Definition: scalarPredicates.H:98
Foam::predicates::scalars::lessOp
static unary lessOp(const scalar opVal)
Test if value is 'less' than prescribed.
Definition: scalarPredicates.H:125
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::predicates::scalars::operation
static unary operation(const opType op, const scalar opVal, const scalar tol=VSMALL)
Standard comparison method by type.
found
bool found
Definition: TABSMDCalcMethod2.H:32
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::printBadEntries
static Error & printBadEntries(Error &err, const Container &entries, const Get0 &get0, const Get1 &get1)
Definition: scalarPredicates.C:123
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
bool
bool
Definition: EEqn.H:20
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
scalarPredicates.H
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
FlatOutput.H
Foam::predicates::scalars::unary
std::function< bool(Foam::scalar)> unary
The unary function type for testing a scalar.
Definition: scalarPredicates.H:75
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177