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-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::predicates::scalars
28
29Description
30 A list of unary predicates (tests) on scalars.
31 Includes a number of standard comparison predicates
32 (eg, "less", "greater", ...)
33
34Note
35 This class is still subject to larger changes (2018-11) as it matures.
36
37SeeAlso
38 Foam::scalarRange, Foam::scalarRanges
39
40SourceFiles
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
58namespace Foam
59{
60namespace predicates
61{
62
63/*---------------------------------------------------------------------------*\
64 Class scalars Declaration
65\*---------------------------------------------------------------------------*/
67class scalars
68:
69 public List<std::function<bool(Foam::scalar)>>
70{
71public:
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 {
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 }
137
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
207
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 match 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 //- True if the value matches any in the list.
286 // Any occurrences before the start pos are ignored.
287 // Linear search.
288 // \return true if found.
289 inline bool found(const scalar value, label pos=0) const;
290
291 //- Match any condition in the list.
292 //
293 // \return True if the value matches any condition in the list.
294 inline bool match(const scalar value) const;
295
296 //- Match any condition in the list.
297 //
298 // \return True if the value matches any condition in the list.
299 inline bool matchAny(const scalar value) const;
300
301 //- Match all conditions in the list.
302 //
303 // \return True if the value matches all conditions in the list.
304 inline bool matchAll(const scalar value) const;
305
306 //- Extract list indices for all matches.
307 //
308 // \return The locations (indices) in the input list where match()
309 // is true
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 found(), match(), for use as a predicate.
328 inline bool operator()(const scalar value) const;
329};
330
331
332// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333
334} // End namespace predicates
335
336
337// IOstream Operators
338
339//- Suppressed write
341{
342 return os;
343}
344
345//- Read list of (opName opValue) tuple
346Istream& operator>>(Istream& is, predicates::scalars& list);
347
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350
351} // End namespace Foam
352
353// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354
355#include "scalarPredicatesI.H"
356
357// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358
359#endif
360
361// ************************************************************************* //
bool found
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A list of unary predicates (tests) on scalars. Includes a number of standard comparison predicates (e...
static unary lessOp(const scalar opVal)
Test if value is 'less' than prescribed.
std::function< bool(Foam::scalar)> unary
The unary function type for testing a scalar.
static unary andOp(const unary &test1, const unary &test2)
Combine unary tests as an AND operation.
bool operator()(const scalar value) const
Identical to found(), match(), for use as a predicate.
static unary greaterEqualOp(const scalar opVal)
Test if value is 'greater_equal' to prescribed.
static unary falseOp()
Predicate that always returns false.
static unary notEqualOp(const scalar opVal, const scalar tol=VSMALL)
Compare for inequality, with specified tolerance (non-negative)
static unary lessEqualOp(const scalar opVal)
Test if value is 'less_equal' to prescribed.
static unary greaterOp(const scalar opVal)
label find(const scalar value, label pos=0) const
Index of the first match for the value.
static unary operation(const opType op, const scalar opVal, const scalar tol=VSMALL)
Standard comparison method by type.
~scalars()=default
Destructor.
void assign(std::initializer_list< std::pair< word, scalar > > entries)
opType
Enumerations for some standard comparison predicates.
@ GREATER_EQUAL
"ge", "greaterEqual", "greaterEq"
@ LESS_EQUAL
"le", "lessEqual", "lessEq"
@ NOT_EQUAL
"neq", "notEqual"
static unary equalOp(const scalar opVal, const scalar tol=VSMALL)
Compare for equality, with specified tolerance (non-negative)
static const Enum< opType > opNames
Names for the opType enumeration.
bool matchAny(const scalar value) const
Match any condition in the list.
label rfind(const scalar value, label pos=-1) const
Index of the last match for the value.
static unary trueOp()
Predicate that always returns true.
labelList matching(const scalar value) const
Extract list indices for all matches.
static unary orOp(const unary &test1, const unary &test2)
Combine unary tests as an OR operation.
bool match(const scalar value) const
Match any condition in the list.
bool matchAll(const scalar value) const
Match all conditions in the list.
A class for handling words, derived from Foam::string.
Definition: word.H:68
bool
Definition: EEqn.H:20
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Istream & operator>>(Istream &, directionInfo &)
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36