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-2023 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_EQUAL, "le" },
48  { opType::LESS_EQUAL, "lessEqual" },
49  { opType::LESS_EQUAL, "lessEq" },
50  { opType::GREATER, "gt" },
51  { opType::GREATER, "greater" },
52  { opType::GREATER_EQUAL, "ge" },
53  { opType::GREATER_EQUAL, "greaterEqual" },
54  { opType::GREATER_EQUAL, "greaterEq" },
55 });
56 
57 
58 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
59 
60 std::function<bool(Foam::scalar)> Foam::predicates::scalars::operation
61 (
62  const enum predicates::scalars::opType op,
63  const Foam::scalar opVal,
64  const Foam::scalar tol
65 )
66 {
67  switch (op)
68  {
69  case opType::EQUAL:
70  return equalOp(opVal, tol);
71  break;
72  case opType::NOT_EQUAL:
73  return notEqualOp(opVal, tol);
74  break;
75  case opType::LESS:
76  return lessOp(opVal);
77  break;
78  case opType::LESS_EQUAL:
79  return lessEqualOp(opVal);
80  break;
81  case opType::GREATER:
82  return greaterOp(opVal);
83  break;
84  case opType::GREATER_EQUAL:
85  return greaterEqualOp(opVal);
86  break;
87  case opType::ALWAYS:
88  return trueOp();
89  break;
90  default:
91  break;
92  }
93 
94  return falseOp();
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
99 
100 namespace Foam
101 {
102  // Check for bad/unknown operations
103  template<class Container, class Get0>
104  static bool hasBadEntries
105  (
106  const Container& entries,
107  const Get0& get0
108  )
109  {
110  for (const auto& entry : entries)
111  {
112  if (!Foam::predicates::scalars::opNames.contains(get0(entry)))
113  {
114  return true;
115  }
116  }
117 
118  return false;
119  }
120 
121 
122  // Print bad/unknown operations
123  template<class Error, class Container, class Get0, class Get1>
124  static Error& printBadEntries
125  (
126  Error& err,
127  const Container& entries,
128  const Get0& get0,
129  const Get1& get1
130  )
131  {
132  labelHashSet badIndices;
133 
134  label idx = 0;
135 
136  for (const auto& entry : entries)
137  {
138  if (!Foam::predicates::scalars::opNames.contains(get0(entry)))
139  {
140  badIndices.insert(idx);
141  }
142  ++idx;
143  }
144 
145  err
146  << "Entries with unknown operations:" << nl
147  << idx << nl
148  << '(' << nl;
149 
150  idx = 0;
151  for (const auto& entry : entries)
152  {
153  const bool bad = badIndices.contains(idx);
154  ++idx;
155 
156  if (bad)
157  {
158  err << ">>> ";
159  }
160  else
161  {
162  err << " ";
163  }
164  err << '(' << get0(entry) << ' ' << get1(entry) << ')';
165 
166  if (bad)
167  {
168  err << " <<<";
169  }
170  err << nl;
171  }
172 
173  err << ')' << nl;
174 
175  return err;
176  }
178 } // End namespace Foam
179 
180 
181 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
182 
184 (
185  std::initializer_list<std::pair<word, scalar>> entries
186 )
187 {
188  assign(entries);
189 }
190 
191 
193 (
194  const UList<Tuple2<word, scalar>>& entries
195 )
196 {
197  assign(entries);
198 }
199 
200 
202 {
203  is >> *this;
204 }
205 
206 
207 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
208 
210 (
211  std::initializer_list<std::pair<word, scalar>> entries
212 )
213 {
214  typedef std::pair<word, scalar> tuple_type;
215 
216  // Access
217  const auto get0 = [](const tuple_type& entry) { return entry.first; };
218  const auto get1 = [](const tuple_type& entry) { return entry.second; };
219 
220  // Check for bad/unknown operations
221  if (hasBadEntries(entries, get0))
222  {
223  printBadEntries(FatalErrorInFunction, entries, get0, get1)
224  << exit(FatalError);
225  }
226 
227  // Appears to be good, fill the list
228  this->resize_nocopy(entries.size());
229  auto iter = this->begin();
230 
231  for (const tuple_type& entry : entries)
232  {
234  ++iter;
235  }
236 }
237 
238 
240 (
241  const UList<Tuple2<word, scalar>>& entries
242 )
243 {
244  typedef Tuple2<word, scalar> tuple_type;
245 
246  // Access
247  const auto get0 = [](const tuple_type& entry) { return entry.first(); };
248  const auto get1 = [](const tuple_type& entry) { return entry.second(); };
249 
250  // Check for bad/unknown operations
251  if (hasBadEntries(entries, get0))
252  {
253  printBadEntries(FatalErrorInFunction, entries, get0, get1)
254  << exit(FatalError);
255  }
256 
257  // Appears to be good, fill the list
258  this->resize_nocopy(entries.size());
259  auto iter = this->begin();
260 
261  for (const tuple_type& entry : entries)
262  {
264  ++iter;
265  }
266 }
267 
268 
270 (
271  const scalar value,
272  label pos
273 ) const
274 {
275  const label len = this->size();
276 
277  if (pos >= 0 && len)
278  {
279  // auto iter = this->cbegin();
280 
281  while (pos < len)
282  {
283  if ((*this)[pos](value))
284  {
285  return pos;
286  }
287 
288  ++pos;
289  }
290  }
291 
292  return -1;
293 }
294 
295 
297 (
298  const scalar value,
299  label pos
300 ) const
301 {
302  // pos == -1 has same meaning as std::string::npos - search from end
303  if (pos < 0 || pos >= this->size())
304  {
305  pos = this->size()-1;
306  }
307 
308  while (pos >= 0)
309  {
310  if ((*this)[pos](value))
311  {
312  return pos;
313  }
314 
315  --pos;
316  }
318  return -1;
319 }
320 
321 
322 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
323 
325 {
326  // Read tuples
327  List<Tuple2<word, scalar>> entries(is);
328 
329  list.assign(entries);
330 
331  return is;
332 }
333 
334 
335 // ************************************************************************* //
static unary lessEqualOp(const scalar opVal)
Test if value is &#39;less_equal&#39; to prescribed.
static unary greaterOp(const scalar opVal)
Test if value is &#39;greater&#39; than prescribed Compare scalar values for inequality.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
static bool hasBadEntries(const Container &entries, const Get0 &get0)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
label find(const scalar value, label pos=0) const
Index of the first matching test for the value.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static unary notEqualOp(const scalar opVal, const scalar tol=VSMALL)
Compare for inequality, with specified tolerance (non-negative)
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:232
static unary equalOp(const scalar opVal, const scalar tol=VSMALL)
Compare for equality, with specified tolerance (non-negative)
void assign(std::initializer_list< std::pair< word, scalar >> entries)
Assign new predicates from an initializer list of (opName opValue) tuples.
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85
static unary lessOp(const scalar opVal)
Test if value is &#39;less&#39; than prescribed.
dimensionedScalar pos(const dimensionedScalar &ds)
label rfind(const scalar value, label pos=-1) const
Index of the last match for the value.
static Error & printBadEntries(Error &err, const Container &entries, const Get0 &get0, const Get1 &get1)
Istream & operator>>(Istream &, directionInfo &)
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:105
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:28
static unary greaterEqualOp(const scalar opVal)
Test if value is &#39;greater_equal&#39; to prescribed.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
static unary falseOp()
Predicate that always returns false.
scalars(std::initializer_list< std::pair< word, scalar >> entries)
Construct from an initializer list of (opName opValue) tuples.
static unary trueOp()
Predicate that always returns true.
static unary operation(const opType op, const scalar opVal, const scalar tol=VSMALL)
Standard comparison method by type.
A list of unary predicates (tests) on scalars. Includes a number of standard comparison predicates (e...
static const Enum< opType > opNames
Names for the opType enumeration.
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
opType
Enumerations for some standard comparison predicates.
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63