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-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 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 scalarPredicates_H
47 #define 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 
91  //- Names for the opType enumeration.
92  static const Enum<opType> opNames;
93 
94 
95  // Standard comparison methods
96 
97  //- Compare for equality, with specified tolerance (non-negative)
98  static unary equalOp
99  (
100  const scalar opVal,
101  const scalar tol = VSMALL
102  )
103  {
104  return [=](const scalar val)
105  {
106  return (Foam::mag(opVal - val) <= tol);
107  };
108  }
109 
110 
111  //- Compare for inequality, with specified tolerance (non-negative)
113  (
114  const scalar opVal,
115  const scalar tol = VSMALL
116  )
117  {
118  return [=](const scalar val)
119  {
120  return (Foam::mag(opVal - val) > tol);
121  };
122  }
123 
124 
125  //- Test if value is 'less' than prescribed
126  static unary lessOp(const scalar opVal)
127  {
128  return std::bind
129  (
130  std::less<scalar>(), std::placeholders::_1, opVal
131  );
132  }
133 
134 
135  //- Test if value is 'less_equal' to prescribed
136  static unary lessEqOp(const scalar opVal)
137  {
138  return std::bind
139  (
140  std::less_equal<scalar>(), std::placeholders::_1, opVal
141  );
142  }
143 
144 
145  //- Test if value is 'greater' than prescribed
146  //- Compare scalar values for inequality
147  static unary greaterOp(const scalar opVal)
148  {
149  return std::bind
150  (
151  std::greater<scalar>(), std::placeholders::_1, opVal
152  );
153  }
154 
155 
156  //- Test if value is 'greater_equal' to prescribed
157  static unary greaterEqOp(const scalar opVal)
158  {
159  return std::bind
160  (
161  std::greater_equal<scalar>(), std::placeholders::_1, opVal
162  );
163  }
164 
165 
166  // Special purpose comparison methods
167 
168  //- Predicate that always returns true
169  static unary trueOp()
170  {
171  return [](const scalar) { return true; };
172  }
173 
174  //- Predicate that always returns false
175  static unary falseOp()
176  {
177  return [](const scalar) { return false; };
178  }
179 
180 
181  // Combining operations
182 
183  //- Combine unary tests as an AND operation
184  static unary andOp(const unary& test1, const unary& test2)
185  {
186  return [=](const scalar value)
187  {
188  return (test1(value) && test2(value));
189  };
190  }
191 
192  //- Combine unary tests as an OR operation
193  static unary orOp(const unary& test1, const unary& test2)
194  {
195  return [=](const scalar value)
196  {
197  return (test1(value) || test2(value));
198  };
199  }
200 
201 
202  // Factory for standard comparison methods
203 
204  //- Standard comparison method by type
205  static unary operation
206  (
207  const opType op,
208  const scalar opVal,
209  const scalar tol = VSMALL
210  );
211 
212  //- Standard comparison method by name
213  inline static unary operation
214  (
215  const word& opName,
216  const scalar opVal,
217  const scalar tol = VSMALL
218  );
219 
220  //- Standard comparison method by (name, value)
221  inline static unary operation
222  (
223  const Tuple2<word, scalar>& op,
224  const scalar tol = VSMALL
225  );
226 
227  //- Standard comparison method by (name, value)
228  inline static unary operation
229  (
230  const std::pair<word, scalar>& op,
231  const scalar tol = VSMALL
232  );
233 
234 
235  // Constructors
236 
237  //- Inherit constructors from List of unary functions
238  using List<unary>::List;
239 
240  //- Construct from an initializer list of (opName opValue) tuples
241  explicit scalars
242  (
243  std::initializer_list<std::pair<word, scalar>> entries
244  );
245 
246  //- Copy construct from a list of (opName opValue) tuples
247  explicit scalars(const UList<Tuple2<word, scalar>>& entries);
248 
249  //- Construct from Istream, from list of (opName opValue) tuples
250  explicit scalars(Istream& is);
251 
252 
253  //- Destructor
254  ~scalars() = default;
255 
256 
257  // Member Functions
258 
259  // Search
260 
261  //- Index of the first match for the value.
262  // Any occurrences before the start pos are ignored.
263  // Linear search.
264  // \return position in list or -1 if not found.
265  label find(const scalar value, label pos = 0) const;
266 
267  //- Index of the last match for the value.
268  // Any occurrences after the end pos are ignored.
269  // Linear search.
270  // \return position in list or -1 if not found.
271  label rfind(const scalar value, label pos = -1) const;
272 
273  //- True if the value matches any in the list.
274  // Any occurrences before the start pos are ignored.
275  // Linear search.
276  // \return true if found.
277  inline bool found(const scalar value, label pos=0) const;
278 
279  //- Match any condition in the list.
280  //
281  // \return True if the value matches any condition in the list.
282  inline bool match(const scalar& value) const;
283 
284  //- Match any condition in the list.
285  //
286  // \return True if the value matches any condition in the list.
287  inline bool matchAny(const scalar& value) const;
288 
289  //- Match all conditions in the list.
290  //
291  // \return True if the value matches all conditions in the list.
292  inline bool matchAll(const scalar& value) const;
293 
294  //- Extract list indices for all matches.
295  //
296  // \return The locations (indices) in the input list where match()
297  // is true
298  inline labelList matching(const scalar& value) const;
299 
300  //- Extract list indices for all matches.
301  //
302  // \param input A list of scalar inputs to test
303  // \param invert invert the matching logic
304  // \return The locations (indices) in the input list where match()
305  // is true
306  inline labelList matching
307  (
308  const UList<scalar>& input,
309  const bool invert=false
310  ) const;
311 
312 
313  // Member Operators
314 
315  //- Identical to found(), match(), for use as a predicate.
316  inline bool operator()(const scalar& value) const;
317 };
318 
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 } // End namespace predicates
323 
324 
325 // IOstream Operators
326 
327 //- Suppressed write
329 {
330  return os;
331 }
332 
333 //- Read list of (opName opValue) tuple
334 Istream& operator>>(Istream& is, predicates::scalars& list);
335 
336 
337 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 
339 } // End namespace Foam
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 #include "scalarPredicatesI.H"
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 #endif
348 
349 // ************************************************************************* //
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< opType >
List.H
Foam::predicates::scalars::GREATER
"gt", "greater"
Definition: scalarPredicates.H:84
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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
scalarPredicatesI.H
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::predicates::scalars::match
bool match(const scalar &value) const
Match any condition in the list.
Definition: scalarPredicatesI.H:73
Foam::predicates::scalars::greaterOp
static unary greaterOp(const scalar opVal)
Definition: scalarPredicates.H:146
Foam::predicates::scalars::LESS
"lt", "less"
Definition: scalarPredicates.H:82
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::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
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::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
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::LESS_EQ
"le", "lessEq"
Definition: scalarPredicates.H:83
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
Foam::predicates::scalars::~scalars
~scalars()=default
Destructor.
Foam::predicates::scalars::andOp
static unary andOp(const unary &test1, const unary &test2)
Combine unary tests as an AND operation.
Definition: scalarPredicates.H:183
Foam::predicates::scalars::ALWAYS
Always matches.
Definition: scalarPredicates.H:86
scalar.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::predicates::scalars::lessEqOp
static unary lessEqOp(const scalar opVal)
Test if value is 'less_equal' to prescribed.
Definition: scalarPredicates.H:135
Foam::predicates::scalars::operator()
bool operator()(const scalar &value) const
Identical to found(), match(), for use as a predicate.
Definition: scalarPredicatesI.H:157
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::predicates::scalars::matchAll
bool matchAll(const scalar &value) const
Match all conditions in the list.
Definition: scalarPredicatesI.H:93
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::predicates::scalars::found
bool found(const scalar value, label pos=0) const
True if the value matches any in the list.
Definition: scalarPredicatesI.H:64
Foam::predicates::scalars::NOT_EQUAL
"neq", "notEqual"
Definition: scalarPredicates.H:81
Foam::predicates::scalars::operation
static unary operation(const opType op, const scalar opVal, const scalar tol=VSMALL)
Standard comparison method by type.
Foam::predicates::scalars::matchAny
bool matchAny(const scalar &value) const
Match any condition in the list.
Definition: scalarPredicatesI.H:79
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::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
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
bool
bool
Definition: EEqn.H:20
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
predicates.H
Foam::predicates::scalars::orOp
static unary orOp(const unary &test1, const unary &test2)
Combine unary tests as an OR operation.
Definition: scalarPredicates.H:192
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::predicates::scalars::NEVER
Never matches.
Definition: scalarPredicates.H:87
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
Foam::predicates::scalars::matching
labelList matching(const scalar &value) const
Extract list indices for all matches.
Definition: scalarPredicatesI.H:108
Foam::predicates::scalars::GREATER_EQ
"ge", "greaterEq"
Definition: scalarPredicates.H:85
Foam::predicates::scalars::EQUAL
"eq", "equal"
Definition: scalarPredicates.H:80
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
Enum.H