Newer
Older
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "scalarPredicates.H"
#include "HashSet.H"
#include "FlatOutput.H"
#include "Tuple2.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::Enum
<
Foam::predicates::scalars::opType
>
Foam::predicates::scalars::opNames
({
{ opType::EQUAL, "eq" },
{ opType::EQUAL, "equal" },
{ opType::NOT_EQUAL, "neq" },
{ opType::NOT_EQUAL, "notEqual" },
{ opType::LESS, "lt" },
{ opType::LESS, "less" },
{ opType::LESS_EQ, "le" },
{ opType::LESS_EQ, "lessEq" },
{ opType::GREATER, "gt" },
{ opType::GREATER, "greater" },
{ opType::GREATER_EQ, "ge" },
{ opType::GREATER_EQ, "greaterEq" },
});
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
std::function<bool(Foam::scalar)> Foam::predicates::scalars::operation
(
const enum predicates::scalars::opType op,
const Foam::scalar opVal,
const Foam::scalar tol
)
{
switch (op)
{
case opType::EQUAL:
return equalOp(opVal, tol);
break;
case opType::NOT_EQUAL:
return notEqualOp(opVal, tol);
break;
case opType::LESS:
return lessOp(opVal);
break;
case opType::LESS_EQ:
return lessEqOp(opVal);
break;
case opType::GREATER:
return greaterOp(opVal);
break;
case opType::GREATER_EQ:
return greaterEqOp(opVal);
break;
case opType::ALWAYS:
return trueOp();
break;
default:
break;
}
return falseOp();
}
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Check for bad/unknown operations
template<class Container, class Get0>
static bool hasBadEntries
(
const Container& entries,
const Get0& get0
)
{
for (const auto& entry : entries)
{
if (!Foam::predicates::scalars::opNames.found(get0(entry)))
{
return true;
}
}
return false;
}
// Print bad/unknown operations
template<class Error, class Container, class Get0, class Get1>
static Error& printBadEntries
(
Error& err,
const Container& entries,
const Get0& get0,
const Get1& get1
)
{
labelHashSet badIndices;
label idx = 0;
for (const auto& entry : entries)
{
if (!Foam::predicates::scalars::opNames.found(get0(entry)))
{
badIndices.insert(idx);
}
++idx;
}
err
<< "Entries with unknown operations:" << nl
<< idx << nl
<< '(' << nl;
idx = 0;
for (const auto& entry : entries)
{
const bool bad = badIndices.found(idx);
++idx;
if (bad)
{
err << ">>> ";
}
else
{
err << " ";
}
err << '(' << get0(entry) << ' ' << get1(entry) << ')';
if (bad)
{
err << " <<<";
}
err << nl;
}
err << ')' << nl;
return err;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::predicates::scalars::scalars
(
std::initializer_list<std::pair<word, scalar>> entries
)
:
List<unary>(entries.size())
{
// Access
const auto get0 =
[](const std::pair<word,scalar>& entry) { return entry.first; };
const auto get1 =
[](const std::pair<word,scalar>& entry) { return entry.second; };
// Check for bad/unknown operations
if (hasBadEntries(entries, get0))
{
printBadEntries(FatalErrorInFunction, entries, get0, get1)
<< exit(FatalError);
}
// Appears to be good
auto& list = *this;
label idx = 0;
for (const auto& entry : entries)
{
list[idx] = predicates::scalars::operation(entry);
++idx;
}
}
Foam::predicates::scalars::scalars
(
const UList<Tuple2<word, scalar>>& entries
)
:
List<unary>(entries.size())
{
// Access
const auto get0 =
[](const Tuple2<word,scalar>& entry) { return entry.first(); };
const auto get1 =
[](const Tuple2<word,scalar>& entry) { return entry.second(); };
// Check for bad/unknown operations
if (hasBadEntries(entries, get0))
{
printBadEntries(FatalErrorInFunction, entries, get0, get1)
<< exit(FatalError);
}
// Appears to be good
auto& list = *this;
label idx = 0;
for (const auto& entry : entries)
{
list[idx] = predicates::scalars::operation(entry);
++idx;
}
}
Foam::predicates::scalars::scalars(Istream& is)
:
List<unary>()
{
is >> *this;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::label Foam::predicates::scalars::find
(
const scalar value,
label pos
) const
{
const label len = this->size();
{
// auto iter = this->cbegin();
}
}
return -1;
}
Foam::label Foam::predicates::scalars::rfind
(
const scalar value,
label pos
) const
{
// pos == -1 has same meaning as std::string::npos - search from end
if (pos < 0 || pos >= this->size())
{
pos = this->size()-1;
}
while (pos >= 0)
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
}
return -1;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, Foam::predicates::scalars& list)
{
// Read tuples
List<Tuple2<word, scalar>> entries(is);
// Access
const auto get0 =
[](const Tuple2<word,scalar>& entry) { return entry.first(); };
const auto get1 =
[](const Tuple2<word,scalar>& entry) { return entry.second(); };
// Check for bad/unknown operations
if (hasBadEntries(entries, get0))
{
printBadEntries(FatalIOErrorInFunction(is), entries, get0, get1)
<< exit(FatalIOError);
}
// Appears to be good
list.resize(entries.size());
label idx = 0;
for (const Tuple2<word, scalar>& entry : entries)
{
list[idx] = predicates::scalars::operation(entry);
++idx;
}
return is;
}
// ************************************************************************* //