topoBoolSet.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 "topoBoolSet.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  boolList& labels = selected_;
39 
40  // Iterate over map to see if anything changed
41  // Must iterate over ALL elements, to properly trap bounds errors
42 
43  bool changed = false;
44 
45  forAll(labels, oldId)
46  {
47  if (!labels.test(oldId))
48  {
49  continue;
50  }
51 
52  if (oldId >= map.size())
53  {
55  << "Illegal content " << oldId << " of set:" << name()
56  << " of type " << type() << nl
57  << "Value should be between [0," << map.size() << ')'
58  << endl
59  << abort(FatalError);
60  }
61 
62  const label newId = map[oldId];
63 
64  if (newId != oldId)
65  {
66  changed = true;
67  #ifdef FULLDEBUG
68  continue; // Check all elements in FULLDEBUG mode
69  #endif
70  break;
71  }
72  }
73 
74  if (!changed)
75  {
76  return;
77  }
78 
79 
80  // Relabel. Use second boolList to prevent overlapping.
81 
82  // The new length is given by the map
83  const label len = map.size();
84 
85  boolList newLabels(len, false);
86 
87  forAll(labels, oldId)
88  {
89  const label newId = map[oldId];
90 
91  if (newId >= 0)
92  {
93  newLabels.set(newId); // Ignores -ve indices
94  }
95  }
96 
97  labels.transfer(newLabels);
98 }
99 
100 
101 void Foam::topoBoolSet::check(const label maxSize)
102 {
103  const boolList& labels = selected_;
104 
105  const label oldId = labels.rfind(true);
106 
107  if (oldId >= maxSize)
108  {
110  << "Illegal content " << oldId << " of set:" << name()
111  << " of type " << type() << nl
112  << "Value should be between [0," << maxSize << ')'
113  << endl
114  << abort(FatalError);
115  }
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120 
122 (
123  const polyMesh& mesh,
124  const word& setName
125 )
126 :
127  topoSet
128  (
129  IOobject
130  (
131  setName,
132  mesh.time().constant(),
133  mesh,
134  IOobject::NO_READ,
135  IOobject::NO_WRITE,
136  IOobject::NO_REGISTER
137  ),
138  Foam::zero{} // Empty labelHashSet (initialCapacity = 0)
139  )
140 {}
141 
142 
144 (
145  const polyMesh& mesh,
146  const word& setName,
147  const label size,
148  const bool val
149 )
150 :
151  topoBoolSet(mesh, setName)
152 {
153  selected_.resize(size, val);
154 }
155 
156 
158 (
159  const polyMesh& mesh,
160  const word& setName,
161  const label size,
162  const boolList& bools
163 )
164 :
165  topoBoolSet(mesh, setName)
166 {
167  selected_ = bools;
168  selected_.resize(size);
169 }
170 
171 
173 (
174  const polyMesh& mesh,
175  const word& setName,
176  const label size,
177  boolList&& bools
178 )
179 :
180  topoBoolSet(mesh, setName)
181 {
182  selected_ = std::move(bools);
184 }
185 
186 
187 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189 bool Foam::topoBoolSet::contains(const label id) const
190 {
191  return selected_.test(id);
192 }
193 
195 bool Foam::topoBoolSet::found(const label id) const
196 {
197  return selected_.test(id);
198 }
199 
201 bool Foam::topoBoolSet::set(const label id)
202 {
203  return selected_.set(id);
204 }
205 
207 bool Foam::topoBoolSet::unset(const label id)
208 {
209  return selected_.unset(id);
210 }
211 
212 
213 void Foam::topoBoolSet::set(const labelUList& labels)
214 {
215  for (const label id : labels)
216  {
217  selected_[id] = true;
218  }
219 }
220 
221 
222 void Foam::topoBoolSet::unset(const labelUList& labels)
223 {
224  for (const label id : labels)
225  {
226  selected_.unset(id);
227  }
228 }
229 
230 
231 void Foam::topoBoolSet::invert(const label maxLen)
232 {
233  selected_.resize(maxLen);
234  for (bool& b : selected_)
235  {
236  b = !b;
237  }
238 }
239 
240 
241 void Foam::topoBoolSet::subset(const topoSet& set)
242 {
243  // Only retain entries found in both sets
244  if (set.empty())
245  {
246  selected_ = false;
247  }
248  else
249  {
250  forAll(selected_, i)
251  {
252  selected_[i] = (selected_[i] && set.found(i));
253  }
254  }
255 }
256 
257 
258 void Foam::topoBoolSet::subset(const labelUList& set)
259 {
260  // Only retain entries found in both sets
261  if (set.empty())
262  {
263  selected_ = false;
264  }
265  else
266  {
267  const boolList oldSelected(selected_);
268  selected_ = false;
269  for (const label id : set)
270  {
271  selected_[id] = oldSelected[id];
272  }
273  }
274 }
275 
276 
277 void Foam::topoBoolSet::addSet(const topoSet& set)
278 {
279  // Add entries to the set
280  for (const label id : set)
281  {
282  selected_[id] = true;
283  }
284 }
285 
286 
287 void Foam::topoBoolSet::addSet(const labelUList& set)
288 {
289  // Add entries to the set
290  for (const label id : set)
291  {
292  selected_[id] = true;
293  }
294 }
295 
296 
297 void Foam::topoBoolSet::subtractSet(const topoSet& set)
298 {
299  // Subtract entries from the set
300  for (const label id : set)
301  {
302  selected_.unset(id);
303  }
304 }
305 
306 
308 {
309  // Subtract entries from the set
310  for (const label id : set)
311  {
312  selected_.unset(id);
313  }
314 }
315 
316 
317 // ************************************************************************* //
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
Definition: topoBoolSet.C:251
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
virtual bool unset(const label id)
Unset an index.
Definition: topoBoolSet.C:200
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
virtual bool contains(const label id) const
Has the given index?
Definition: topoBoolSet.C:182
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#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
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:493
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual void invert(const label maxLen)
Invert contents.
Definition: topoBoolSet.C:224
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
boolList selected_
Definition: topoBoolSet.H:55
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
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 bool set(const label id)
Set an index.
Definition: topoBoolSet.C:194
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Base for a special purpose topoSet using labels stored as a boolList.
Definition: topoBoolSet.H:47
A class for handling words, derived from Foam::string.
Definition: word.H:63
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:212
virtual void addSet(const labelUList &elems)
Add given elements to the set.
Definition: topoBoolSet.C:280
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual bool found(const label id) const
Has the given index?
Definition: topoBoolSet.C:188
void resize(label newCapacity)
Rehash the hash table with new number of buckets. Currently identical to setCapacity() ...
Definition: HashTable.C:729
List< bool > bools(const labelHashSet &locations)
Transform the on locations to a boolList, with true for each non-negative location and false for all ...
Definition: HashOps.C:72
virtual void subtractSet(const labelUList &elems)
Subtract given elements from the set.
Definition: topoBoolSet.C:300
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type test(const label i) const
Test bool value at specified position, always false for out-of-range access.
Definition: UList.H:778
topoBoolSet(const polyMesh &mesh, const word &setName)
Construct (no-read) with empty selection.
Definition: topoBoolSet.C:115
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBoolSet.C:29
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBoolSet.C:94
List< bool > boolList
A List of bools.
Definition: List.H:60
Namespace for OpenFOAM.