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 -------------------------------------------------------------------------------
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 #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 
92 void 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  (
120  IOobject
121  (
122  setName,
123  mesh.time().constant(),
124  mesh,
125  IOobject::NO_READ,
126  IOobject::NO_WRITE,
127  IOobject::NO_REGISTER
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 )
141 :
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);
175 }
176 
177 
178 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
180 bool Foam::topoBitSet::contains(const label id) const
181 {
182  return selected_.test(id);
183 }
184 
186 bool Foam::topoBitSet::found(const label id) const
187 {
188  return selected_.test(id);
189 }
190 
192 bool Foam::topoBitSet::set(const label id)
193 {
194  return selected_.set(id);
195 }
196 
198 bool Foam::topoBitSet::unset(const label id)
199 {
200  return selected_.unset(id);
201 }
202 
204 void Foam::topoBitSet::set(const labelUList& labels)
205 {
206  selected_.set(labels);
207 }
208 
210 void Foam::topoBitSet::unset(const labelUList& labels)
211 {
212  selected_.unset(labels);
213 }
214 
215 
216 void Foam::topoBitSet::invert(const label maxLen)
217 {
218  selected_.resize(maxLen);
219  selected_.flip();
220 }
221 
222 
223 void 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 
250 void 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 
266 void 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)
278  {
279  selected_.set(id);
280  }
281  }
282 }
283 
285 void Foam::topoBitSet::addSet(const labelUList& elems)
286 {
287  selected_.set(elems);
288 }
289 
290 
291 void Foam::topoBitSet::subtractSet(const topoSet& set)
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)
303  {
304  selected_.unset(id);
305  }
306  }
307 }
308 
309 
310 void Foam::topoBitSet::subtractSet(const labelUList& elems)
311 {
312  selected_.unset(elems);
313 }
314 
315 
316 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:498
virtual void subtractSet(const labelUList &elems)
Subtract given elements from the set.
Definition: topoBitSet.C:303
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBitSet.C:29
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
label find_last() const
Locate the last bit set.
Definition: bitSetI.H:321
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Base for a special purpose topoSet using labels stored as a bitSet.
Definition: topoBitSet.H:47
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bitSet selected_
Definition: topoBitSet.H:55
virtual bool found(const label id) const
Has the given index?
Definition: topoBitSet.C:179
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
void resize(const label numElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:455
label size() const noexcept
The number of elements in table.
Definition: HashTable.H:358
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
virtual void invert(const label maxLen)
Invert contents.
Definition: topoBitSet.C:209
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
Definition: topoBitSet.C:243
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBitSet.C:85
void resize(label newCapacity)
Rehash the hash table with new number of buckets. Currently identical to setCapacity() ...
Definition: HashTable.C:729
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
topoBitSet(const polyMesh &mesh, const word &setName)
Construct (no-read) with empty selection.
Definition: topoBitSet.C:106
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
virtual bool unset(const label id)
Unset an index.
Definition: topoBitSet.C:191
virtual bool contains(const label id) const
Has the given index?
Definition: topoBitSet.C:173
virtual bool set(const label id)
Set an index.
Definition: topoBitSet.C:185
virtual void addSet(const labelUList &elems)
Add given elements to the set.
Definition: topoBitSet.C:278
bool found
Namespace for OpenFOAM.