Loading...
Searching...
No Matches
topoBitSet.C
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-2024 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
26\*---------------------------------------------------------------------------*/
27
28#include "topoBitSet.H"
29#include "polyMesh.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34// Update stored cell numbers using map.
35// Do in two passes to prevent allocation if nothing changed.
37{
38 bitSet& labels = selected_;
39
40 {
41 const label oldId = labels.find_last();
42
43 if (oldId >= map.size())
44 {
46 << "Illegal content " << oldId << " of set:" << name()
47 << " of type " << type() << nl
48 << "Value should be between [0," << map.size() << ')'
49 << endl
50 << abort(FatalError);
51 }
52 }
53
54 // Iterate over map to see if anything changed
55
56 bool changed = false;
57
58 for (const label oldId : labels)
59 {
60 const label newId = map[oldId];
61
62 if (newId != oldId)
63 {
64 changed = true;
65 break;
66 }
67 }
68
69 if (!changed)
70 {
71 return;
72 }
73
74
75 // Relabel. Use second bitSet to prevent overlapping.
76
77 // The new length is given by the map
78 const label len = map.size();
79
80 bitSet newLabels(len);
81
82 for (const label oldId : labels)
83 {
84 const label newId = map[oldId];
85 newLabels.set(newId); // Ignores -ve indices
86 }
87
88 labels.transfer(newLabels);
89}
90
91
92void Foam::topoBitSet::check(const label maxSize)
93{
94 const bitSet& labels = selected_;
95
96 const label oldId = labels.find_last();
97
98 if (oldId >= maxSize)
99 {
101 << "Illegal content " << oldId << " of set:" << name()
102 << " of type " << type() << nl
103 << "Value should be between [0," << maxSize << ')'
104 << endl
106 }
107}
108
109
110// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111
113(
114 const polyMesh& mesh,
115 const word& setName
116)
117:
118 topoSet
119 (
120 IOobject
121 (
122 setName,
123 mesh.time().constant(),
124 mesh,
125 IOobject::NO_READ,
126 IOobject::NO_WRITE,
128 ),
129 Foam::zero{} // Empty labelHashSet (initialCapacity = 0)
130 )
131{}
132
133
135(
136 const polyMesh& mesh,
137 const word& setName,
138 const label size,
139 const bool val
140)
142 topoBitSet(mesh, setName)
143{
144 selected_.resize(size, val);
145}
146
147
149(
150 const polyMesh& mesh,
151 const word& setName,
152 const label size,
153 const bitSet& bits
154)
155:
156 topoBitSet(mesh, setName)
157{
158 selected_ = bits;
159 selected_.resize(size);
160}
161
162
164(
165 const polyMesh& mesh,
166 const word& setName,
167 const label size,
168 bitSet&& bits
169)
170:
171 topoBitSet(mesh, setName)
172{
173 selected_ = std::move(bits);
174 selected_.resize(size);
175}
176
177
178// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
180bool Foam::topoBitSet::contains(const label id) const
181{
182 return selected_.test(id);
183}
184
186bool Foam::topoBitSet::found(const label id) const
187{
188 return selected_.test(id);
189}
190
192bool Foam::topoBitSet::set(const label id)
193{
194 return selected_.set(id);
195}
196
198bool Foam::topoBitSet::unset(const label id)
199{
200 return selected_.unset(id);
201}
202
204void Foam::topoBitSet::set(const labelUList& labels)
205{
206 selected_.set(labels);
207}
208
210void Foam::topoBitSet::unset(const labelUList& labels)
211{
212 selected_.unset(labels);
213}
214
215
216void Foam::topoBitSet::invert(const label maxLen)
217{
218 selected_.resize(maxLen);
219 selected_.flip();
220}
221
222
223void Foam::topoBitSet::subset(const topoSet& set)
224{
225 // Only retain entries found in both sets
226
227 const auto* topoBitsPtr = isA<topoBitSet>(set);
228
229 if (topoBitsPtr)
230 {
231 selected_ &= topoBitsPtr->selected_;
232 }
233 else if (set.empty())
234 {
235 selected_.reset();
236 }
237 else
238 {
239 for (const label id : selected_)
240 {
241 if (!set.found(id))
242 {
243 selected_.unset(id);
244 }
245 }
246 }
247}
248
249
250void Foam::topoBitSet::subset(const labelUList& elems)
251{
252 // Only retain entries found in both sets
253 bitSet newLabels(selected_.size());
254
255 for (const label id : elems)
256 {
257 if (selected_.test(id))
258 {
259 newLabels.set(id);
260 }
261 }
262 selected_.transfer(newLabels);
263}
264
265
266void Foam::topoBitSet::addSet(const topoSet& set)
267{
268 // Add entries to the set
269 const auto* topoBitsPtr = isA<topoBitSet>(set);
270
271 if (topoBitsPtr)
272 {
273 selected_ |= topoBitsPtr->selected_;
274 }
275 else
276 {
277 for (const label id : set)
279 selected_.set(id);
280 }
281 }
282}
283
285void Foam::topoBitSet::addSet(const labelUList& elems)
286{
287 selected_.set(elems);
288}
289
290
292{
293 // Subtract entries from the set
294 const auto* topoBitsPtr = isA<topoBitSet>(set);
295
296 if (topoBitsPtr)
297 {
298 selected_ -= topoBitsPtr->selected_;
299 }
300 else
301 {
302 for (const label id : set)
304 selected_.unset(id);
305 }
306 }
307}
308
309
311{
312 selected_.unset(elems);
313}
314
315
316// ************************************************************************* //
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
@ NO_REGISTER
Do not request registration (bool: false).
@ NO_READ
Nothing to be read.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const Time & time() const noexcept
Return Time associated with the objectRegistry.
Definition IOobject.C:456
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
label find_last() const
Locate the last bit set.
Definition bitSetI.H:321
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition bitSetI.H:502
void transfer(bitSet &bitset)
Transfer the contents of the argument list into this list and annul the argument list.
Definition bitSetI.H:468
constant condensation/saturation model.
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
Base for a special purpose topoSet using labels stored as a bitSet.
Definition topoBitSet.H:50
virtual void invert(const label maxLen)
Invert contents.
Definition topoBitSet.C:209
virtual bool unset(const label id)
Unset an index.
Definition topoBitSet.C:191
virtual bool found(const label id) const
Has the given index?
Definition topoBitSet.C:179
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
Definition topoBitSet.C:243
topoBitSet(const polyMesh &mesh, const word &setName)
Construct (no-read) with empty selection.
Definition topoBitSet.C:106
virtual void subtractSet(const labelUList &elems)
Subtract given elements from the set.
Definition topoBitSet.C:303
virtual bool contains(const label id) const
Has the given index?
Definition topoBitSet.C:173
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition topoBitSet.C:29
virtual bool set(const label id)
Set an index.
Definition topoBitSet.C:185
virtual void check(const label maxSize)
Check limits on addressable range.
Definition topoBitSet.C:85
virtual void addSet(const labelUList &elems)
Add given elements to the set.
Definition topoBitSet.C:278
General set of labels of mesh quantity (points, cells, faces).
Definition topoSet.H:63
topoSet(const topoSet &)=delete
No copy construct.
virtual label maxSize(const polyMesh &mesh) const =0
Return max allowable index (+1). Not implemented.
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
Different types of constants.
Namespace for OpenFOAM.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
const Type * isA(const U &obj)
Attempt dynamic_cast to Type.
Definition typeInfo.H:87
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
UList< label > labelUList
A UList of labels.
Definition UList.H:75
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50