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  label(0) // zero-sized (unallocated) labelHashSet
139  ),
140  selected_()
141 {}
142 
143 
145 (
146  const polyMesh& mesh,
147  const word& setName,
148  const label size,
149  const bool val
150 )
151 :
152  topoBoolSet(mesh, setName)
153 {
154  selected_.resize(size, val);
155 }
156 
157 
159 (
160  const polyMesh& mesh,
161  const word& setName,
162  const label size,
163  const boolList& bools
164 )
165 :
166  topoBoolSet(mesh, setName)
167 {
168  selected_ = bools;
169  selected_.resize(size);
170 }
171 
172 
174 (
175  const polyMesh& mesh,
176  const word& setName,
177  const label size,
178  boolList&& bools
179 )
180 :
181  topoBoolSet(mesh, setName)
182 {
183  selected_ = std::move(bools);
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
190 bool Foam::topoBoolSet::found(const label id) const
191 {
192  return selected_.test(id);
193 }
194 
196 bool Foam::topoBoolSet::set(const label id)
197 {
198  return selected_.set(id);
199 }
200 
202 bool Foam::topoBoolSet::unset(const label id)
203 {
204  return selected_.unset(id);
205 }
206 
207 
208 void Foam::topoBoolSet::set(const labelUList& labels)
209 {
210  for (const label id : labels)
211  {
212  selected_[id] = true;
213  }
214 }
215 
216 
217 void Foam::topoBoolSet::unset(const labelUList& labels)
218 {
219  for (const label id : labels)
220  {
221  selected_.unset(id);
222  }
223 }
224 
225 
226 void Foam::topoBoolSet::invert(const label maxLen)
227 {
228  selected_.resize(maxLen);
229  for (bool& b : selected_)
230  {
231  b = !b;
232  }
233 }
234 
235 
236 void Foam::topoBoolSet::subset(const topoSet& set)
237 {
238  // Only retain entries found in both sets
239  if (set.empty())
240  {
241  selected_ = false;
242  }
243  else
244  {
245  forAll(selected_, i)
246  {
247  selected_[i] = (selected_[i] && set.found(i));
248  }
249  }
250 }
251 
252 
253 void Foam::topoBoolSet::addSet(const topoSet& set)
254 {
255  // Add entries to the set
256  for (const label id : set)
257  {
258  selected_[id] = true;
259  }
260 }
261 
262 
263 void Foam::topoBoolSet::subtractSet(const topoSet& set)
264 {
265  // Subtract entries from the set
266  for (const label id : set)
267  {
268  selected_.unset(id);
269  }
270 }
271 
272 
273 // ************************************************************************* //
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:195
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
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:598
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:489
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:219
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:342
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:189
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
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual bool found(const label id) const
Has the given index?
Definition: topoBoolSet.C:183
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoBoolSet.C:256
void resize(label newCapacity)
Rehash the hash table with new number of buckets. Currently identical to setCapacity() ...
Definition: HashTable.C:705
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 subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoBoolSet.C:229
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:769
topoBoolSet(const polyMesh &mesh, const word &setName)
Construct with empty selection.
Definition: topoBoolSet.C:115
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBoolSet.C:29
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
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
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoBoolSet.C:246