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 -------------------------------------------------------------------------------
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,
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;
160  selected_.resize(size);
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);
175  selected_.resize(size);
176 }
177 
178 
179 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
180 
181 bool Foam::topoBitSet::found(const label id) const
182 {
183  return selected_.test(id);
184 }
185 
186 
187 bool Foam::topoBitSet::set(const label id)
188 {
189  return selected_.set(id);
190 }
191 
192 
193 bool 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 
211 void 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 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::PackedList::resize
void resize(const label numElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:409
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::topoBitSet::set
virtual bool set(const label id)
Set an index.
Definition: topoBitSet.C:187
Foam::topoBitSet::subtractSet
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoBitSet.C:259
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoBitSet::addSet
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoBitSet.C:242
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::topoBitSet::topoBitSet
topoBitSet(const polyMesh &mesh, const word &setName)
Construct with empty selection.
Definition: topoBitSet.C:113
Foam::topoBitSet::check
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBitSet.C:92
Foam::bitSet::find_last
label find_last() const
Locate the last bit set.
Definition: bitSetI.H:374
Foam::topoBitSet::invert
virtual void invert(const label maxLen)
Invert contents.
Definition: topoBitSet.C:211
Foam::topoBitSet::unset
virtual bool unset(const label id)
Unset an index.
Definition: topoBitSet.C:193
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:63
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::topoBitSet::found
virtual bool found(const label id) const
Has the given index?
Definition: topoBitSet.C:181
Foam::topoBitSet::subset
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoBitSet.C:218
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::topoBitSet::updateLabels
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBitSet.C:36
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::topoBitSet
Base for a special purpose topoSet using labels stored as a bitSet.
Definition: topoBitSet.H:51
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::UList< label >
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::topoBitSet::selected_
bitSet selected_
Definition: topoBitSet.H:59
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Foam::IOobject::NO_READ
Definition: IOobject.H:188
topoBitSet.H