scalarPredicates.H
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 Class
27  Foam::predicates::scalars
28 
29 Description
30  A list of unary predicates (tests) on scalars.
31  Includes a number of standard comparison predicates
32  (eg, "less", "greater", ...)
33 
34 Note
35  This class is still subject to larger changes (2018-11) as it matures.
36 
37 SeeAlso
38  Foam::scalarRange, Foam::scalarRanges
39 
40 SourceFiles
41  scalarPredicates.C
42  scalarPredicatesI.H
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_scalarPredicates_H
47 #define Foam_scalarPredicates_H
48 
49 #include "scalar.H"
50 #include "predicates.H"
51 #include "Enum.H"
52 #include "List.H"
53 #include "Tuple2.H"
54 #include <functional>
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 namespace predicates
61 {
62 
63 /*---------------------------------------------------------------------------*\
64  Class scalars Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class scalars
68 :
69  public List<std::function<bool(Foam::scalar)>>
70 {
71 public:
72 
73  // Public types
74 
75  //- The unary function type for testing a scalar
76  typedef std::function<bool(Foam::scalar)> unary;
77 
78  //- Enumerations for some standard comparison predicates
79  enum opType
80  {
83  LESS,
88  NEVER,
89 
90  // OpenFOAM-v2112 and earlier:
93  };
94 
95  //- Names for the opType enumeration.
96  static const Enum<opType> opNames;
97 
98 
99  // Standard comparison methods
100 
101  //- Compare for equality, with specified tolerance (non-negative)
102  static unary equalOp
103  (
104  const scalar opVal,
105  const scalar tol = VSMALL
106  )
107  {
108  return [=](const scalar val)
109  {
110  return (Foam::mag(opVal - val) <= tol);
111  };
112  }
113 
114 
115  //- Compare for inequality, with specified tolerance (non-negative)
116  static unary notEqualOp
117  (
118  const scalar opVal,
119  const scalar tol = VSMALL
120  )
121  {
122  return [=](const scalar val)
123  {
124  return (Foam::mag(opVal - val) > tol);
125  };
126  }
127 
128 
129  //- Test if value is 'less' than prescribed
130  static unary lessOp(const scalar opVal)
131  {
132  return std::bind
133  (
134  std::less<scalar>(), std::placeholders::_1, opVal
135  );
136  }
138 
139  //- Test if value is 'less_equal' to prescribed
140  static unary lessEqualOp(const scalar opVal)
141  {
142  return std::bind
143  (
144  std::less_equal<scalar>(), std::placeholders::_1, opVal
145  );
146  }
147 
148 
149  //- Test if value is 'greater' than prescribed
150  //- Compare scalar values for inequality
151  static unary greaterOp(const scalar opVal)
152  {
153  return std::bind
154  (
155  std::greater<scalar>(), std::placeholders::_1, opVal
156  );
157  }
158 
159 
160  //- Test if value is 'greater_equal' to prescribed
161  static unary greaterEqualOp(const scalar opVal)
162  {
163  return std::bind
164  (
165  std::greater_equal<scalar>(), std::placeholders::_1, opVal
166  );
167  }
168 
169 
170  // Special purpose comparison methods
171 
172  //- Predicate that always returns true
173  static unary trueOp()
174  {
175  return [](const scalar) { return true; };
176  }
177 
178  //- Predicate that always returns false
179  static unary falseOp()
180  {
181  return [](const scalar) { return false; };
182  }
183 
184 
185  // Combining operations
186 
187  //- Combine unary tests as an AND operation
188  static unary andOp(const unary& test1, const unary& test2)
189  {
190  return [=](const scalar value)
191  {
192  return (test1(value) && test2(value));
193  };
194  }
195 
196  //- Combine unary tests as an OR operation
197  static unary orOp(const unary& test1, const unary& test2)
198  {
199  return [=](const scalar value)
200  {
201  return (test1(value) || test2(value));
202  };
203  }
204 
205 
206  // Factory for standard comparison methods
208  //- Standard comparison method by type
209  static unary operation
210  (
211  const opType op,
212  const scalar opVal,
213  const scalar tol = VSMALL
214  );
215 
216  //- Standard comparison method by name
217  inline static unary operation
218  (
219  const word& opName,
220  const scalar opVal,
221  const scalar tol = VSMALL
222  );
223 
224  //- Standard comparison method by (name, value)
225  inline static unary operation
226  (
227  const Tuple2<word, scalar>& op,
228  const scalar tol = VSMALL
229  );
230 
231  //- Standard comparison method by (name, value)
232  inline static unary operation
233  (
234  const std::pair<word, scalar>& op,
235  const scalar tol = VSMALL
236  );
237 
238 
239  // Constructors
240 
241  //- Inherit constructors from List of unary functions
242  using List<unary>::List;
243 
244  //- Construct from an initializer list of (opName opValue) tuples
245  explicit scalars
246  (
247  std::initializer_list<std::pair<word, scalar>> entries
248  );
249 
250  //- Copy construct from a list of (opName opValue) tuples
251  explicit scalars(const UList<Tuple2<word, scalar>>& entries);
252 
253  //- Construct from Istream, from list of (opName opValue) tuples
254  explicit scalars(Istream& is);
255 
256 
257  //- Destructor
258  ~scalars() = default;
259 
260 
261  // Member Functions
262 
263  //- Assign new predicates from an initializer list of
264  //- (opName opValue) tuples
265  void assign(std::initializer_list<std::pair<word, scalar>> entries);
266 
267  //- Assign new predicates from a list of (opName opValue) tuples
268  void assign(const UList<Tuple2<word, scalar>>& entries);
269 
270 
271  // Search
272 
273  //- Index of the first matching test for the value.
274  // Any occurrences before the start pos are ignored.
275  // Linear search.
276  // \return position in list or -1 if not found.
277  label find(const scalar value, label pos = 0) const;
278 
279  //- Index of the last match for the value.
280  // Any occurrences after the end pos are ignored.
281  // Linear search.
282  // \return position in list or -1 if not found.
283  label rfind(const scalar value, label pos = -1) const;
284 
285  //- Matches \em any condition in the list after the offset position.
286  // Linear search from start pos until the end of the list.
287  // Any occurrences before the start pos are ignored.
288  //
289  // \return True if the value matches any condition in the (sub)list.
290  inline bool contains(const scalar value, label pos = 0) const;
291 
292  //- Matches \em any condition in the list.
293  //
294  // \return True if the value matches any condition in the list.
295  inline bool match(const scalar value) const;
296 
297  //- Matches \em any condition in the list.
298  //
299  // \return True if the value matches any condition in the list.
300  inline bool matchAny(const scalar value) const;
301 
302  //- Matches \em all conditions in the list.
303  //
304  // \return True if the value matches all conditions in the list.
305  inline bool matchAll(const scalar value) const;
306 
307  //- Extract list indices for all matches.
308  //
309  // \return The locations (indices) of the predicates that match
310  inline labelList matching(const scalar value) const;
311 
312  //- Extract list indices for all matches.
313  //
314  // \param input A list of scalar inputs to test
315  // \param invert invert the matching logic
316  // \return The locations (indices) in the input list where match()
317  // is true
318  inline labelList matching
319  (
320  const UList<scalar>& input,
321  const bool invert=false
322  ) const;
323 
324 
325  // Member Operators
326 
327  //- Identical to contains(), match(), for use as a predicate.
328  bool operator()(const scalar value) const { return matchAny(value); }
329 
330 
331  // Housekeeping
332 
333  //- Same as contains()
334  bool found(const scalar value, label pos = 0) const
335  {
336  return this->contains(value, pos);
337  }
338 };
339 
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 } // End namespace predicates
344 
345 
346 // IOstream Operators
347 
348 //- Suppressed write
349 Ostream& operator<<(Ostream& os, const predicates::scalars& list)
350 {
351  return os;
352 }
353 
354 //- Read list of (opName opValue) tuple
355 Istream& operator>>(Istream& is, predicates::scalars& list);
356 
357 
358 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
359 
360 } // End namespace Foam
361 
362 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
363 
364 #include "scalarPredicatesI.H"
365 
366 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
367 
368 #endif
369 
370 // ************************************************************************* //
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.
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
bool match(const scalar value) const
Matches any condition in the list.
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
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
static unary notEqualOp(const scalar opVal, const scalar tol=VSMALL)
Compare for inequality, with specified tolerance (non-negative)
bool contains(const scalar value, label pos=0) const
Matches any condition in the list after the offset position.
"le", "lessEqual", "lessEq"
static unary equalOp(const scalar opVal, const scalar tol=VSMALL)
Compare for equality, with specified tolerance (non-negative)
bool matchAny(const scalar value) const
Matches any condition in the list.
void assign(std::initializer_list< std::pair< word, scalar >> entries)
Assign new predicates from an initializer list of (opName opValue) tuples.
static unary lessOp(const scalar opVal)
Test if value is &#39;less&#39; than prescribed.
bool found(const scalar value, label pos=0) const
Same as contains()
dimensionedScalar pos(const dimensionedScalar &ds)
A class for handling words, derived from Foam::string.
Definition: word.H:63
label rfind(const scalar value, label pos=-1) const
Index of the last match for the value.
Istream & operator>>(Istream &, directionInfo &)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
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
static unary orOp(const unary &test1, const unary &test2)
Combine unary tests as an OR operation.
OBJstream os(runTime.globalPath()/outputName)
labelList matching(const scalar value) const
Extract list indices for all matches.
~scalars()=default
Destructor.
static unary andOp(const unary &test1, const unary &test2)
Combine unary tests as an AND operation.
static unary greaterEqualOp(const scalar opVal)
Test if value is &#39;greater_equal&#39; to prescribed.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
static unary falseOp()
Predicate that always returns false.
bool matchAll(const scalar value) const
Matches all conditions in the list.
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.
bool operator()(const scalar value) const
Identical to contains(), match(), for use as a predicate.
std::function< bool(Foam::scalar)> unary
The unary function type for testing a scalar.
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.
opType
Enumerations for some standard comparison predicates.
"ge", "greaterEqual", "greaterEq"
Namespace for OpenFOAM.