scalarPredicatesI.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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
29 
31 (
32  const word& opName,
33  const scalar opVal,
34  const scalar tol
35 )
36 {
37  return operation(opNames[opName], opVal, tol);
38 }
39 
40 
42 (
43  const Tuple2<word, scalar>& op,
44  const scalar tol
45 )
46 {
47  return operation(opNames[op.first()], op.second(), tol);
48 }
49 
50 
52 (
53  const std::pair<word, scalar>& op,
54  const scalar tol
55 )
56 {
57  return operation(opNames[op.first], op.second, tol);
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 
64 (
65  const scalar value,
66  label pos
67 ) const
68 {
69  return (this->find(value, pos) >= 0);
70 }
71 
72 
73 inline bool Foam::predicates::scalars::match(const scalar value) const
74 {
75  return matchAny(value);
76 }
77 
78 
79 inline bool Foam::predicates::scalars::matchAny(const scalar value) const
80 {
81  for (const unary& pred : *this)
82  {
83  if (pred(value))
84  {
85  return true;
86  }
87  }
88 
89  return false;
90 }
91 
92 
93 inline bool Foam::predicates::scalars::matchAll(const scalar value) const
94 {
95  for (const unary& pred : *this)
96  {
97  if (!pred(value))
98  {
99  return false;
100  }
101  }
102 
103  return (!this->empty());
104 }
105 
106 
108 (
109  const scalar value
110 ) const
111 {
112  labelList indices(this->size());
113 
114  label i = 0, count = 0;
115  for (const unary& pred : *this)
116  {
117  if (pred(value))
118  {
119  indices[count] = i;
120  ++count;
121  }
122  ++i;
123  }
124  indices.resize(count);
125 
126  return indices;
127 }
128 
129 
131 (
132  const UList<scalar>& input,
133  const bool invert
134 ) const
135 {
136  const label len = input.size();
137 
138  labelList indices(len);
139 
140  label count = 0;
141  for (label i=0; i < len; ++i)
142  {
143  if (matchAny(input[i]) ? !invert : invert)
144  {
145  indices[count] = i;
146  ++count;
147  }
148  }
149  indices.resize(count);
150 
151  return indices;
152 }
153 
154 
155 // ************************************************************************* //
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
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
bool contains(const scalar value, label pos=0) const
Matches any condition in the list after the offset position.
bool matchAny(const scalar value) const
Matches any condition in the list.
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
A class for handling words, derived from Foam::string.
Definition: word.H:63
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
labelList matching(const scalar value) const
Extract list indices for all matches.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
bool matchAll(const scalar value) const
Matches all conditions in the list.
const T2 & second() const noexcept
Access the second element.
Definition: Tuple2.H:142
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.
static const Enum< opType > opNames
Names for the opType enumeration.
const T1 & first() const noexcept
Access the first element.
Definition: Tuple2.H:132