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 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
105 << abort(FatalError);
106 }
107}
108
109
110// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111
113(
114 const polyMesh& mesh,
115 const word& setName
116)
117:
118 topoSet
119 (
121 (
122 setName,
123 mesh.time().constant(),
124 mesh,
125 IOobject::NO_READ,
126 IOobject::NO_WRITE,
127 false
128 ),
129 0 // zero-sized (unallocated) labelHashSet
130 ),
131 selected_()
132{}
133
134
136(
137 const polyMesh& mesh,
138 const word& setName,
139 const label size,
140 const bool val
141)
142:
143 topoBitSet(mesh, setName)
144{
145 selected_.resize(size, val);
146}
147
148
150(
151 const polyMesh& mesh,
152 const word& setName,
153 const label size,
154 const bitSet& bits
155)
156:
157 topoBitSet(mesh, setName)
158{
159 selected_ = bits;
161}
162
163
165(
166 const polyMesh& mesh,
167 const word& setName,
168 const label size,
169 bitSet&& bits
170)
171:
172 topoBitSet(mesh, setName)
173{
174 selected_ = std::move(bits);
176}
177
178
179// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
180
181bool Foam::topoBitSet::found(const label id) const
182{
183 return selected_.test(id);
184}
185
186
187bool Foam::topoBitSet::set(const label id)
188{
189 return selected_.set(id);
190}
191
192
193bool Foam::topoBitSet::unset(const label id)
194{
195 return selected_.unset(id);
196}
197
198
200{
201 selected_.set(labels);
202}
203
204
206{
207 selected_.unset(labels);
208}
209
210
211void Foam::topoBitSet::invert(const label maxLen)
212{
213 selected_.resize(maxLen);
214 selected_.flip();
215}
216
217
219{
220 // Only retain entries found in both sets
221 if (isA<topoBitSet>(set))
222 {
223 selected_ &= refCast<const topoBitSet>(set).selected_;
224 }
225 else if (set.empty())
226 {
227 selected_.reset();
228 }
229 else
230 {
231 for (const label id : selected_)
232 {
233 if (!set.found(id))
234 {
235 selected_.unset(id);
236 }
237 }
238 }
239}
240
241
243{
244 // Add entries to the set
245 if (isA<topoBitSet>(set))
246 {
247 selected_ |= refCast<const topoBitSet>(set).selected_;
248 }
249 else
250 {
251 for (const label id : set)
252 {
253 selected_.set(id);
254 }
255 }
256}
257
258
260{
261 // Subtract entries from the set
262 if (isA<topoBitSet>(set))
263 {
264 selected_ -= refCast<const topoBitSet>(set).selected_;
265 }
266 else
267 {
268 for (const label id : set)
269 {
270 selected_.unset(id);
271 }
272 }
273}
274
275
276// ************************************************************************* //
bool found
void invert()
Return the matrix inverse into itself if no elem is equal to zero.
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
void resize(const label numElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:409
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
label find_last() const
Locate the last bit set.
Definition: bitSetI.H:375
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:590
void transfer(bitSet &bitset)
Definition: bitSetI.H:560
constant condensation/saturation model.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
Base for a special purpose topoSet using labels stored as a bitSet.
Definition: topoBitSet.H:54
bitSet selected_
Definition: topoBitSet.H:59
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoBitSet.C:259
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoBitSet.C:242
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoBitSet.C:218
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBitSet.C:36
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBitSet.C:92
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:67
static const triad unset
Definition: triad.H:97
bool set() const
Are all the vector set.
Definition: triadI.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53