BitOps.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-2023 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 "BitOps.H"
29 #include "bitSet.H"
30 #include "HashSet.H"
31 #include "List.H"
32 #include "labelRange.H"
33 
34 // * * * * * * * * * * * * * * * * * BitOps * * * * * * * * * * * * * * * * //
35 
36 // See bitSet::setMany for original implementation
37 void Foam::BitOps::set(List<bool>& bools, const labelUList& locations)
38 {
39  // Check the max expected value first
40  const auto max = std::max_element(locations.begin(), locations.end());
41  const label len = (max != locations.end() ? (1 + *max) : 0);
42 
43  if (len > bools.size())
44  {
45  bools.resize(len, false);
46  }
47 
48  for (label i : locations)
49  {
50  if (i >= 0)
51  {
52  bools[i] = true;
53  }
54  }
55 }
56 
57 
58 // See bitSet::set(labelRange) for original implementation
59 void Foam::BitOps::set(List<bool>& bools, const labelRange& range)
60 {
61  labelRange slice(range);
62  slice.adjust(); // No negative start, size adjusted accordingly
63 
64  // Range is invalid (zero-sized or entirely negative) - noop
65  if (slice.empty())
66  {
67  return;
68  }
69 
70  // Check maximum extent of the range.
71  // The end_value() method is the exclusive end-value,
72  // which corresponds to our potential new length.
73  // - resize now to avoid allocations within the loop
74 
75  if (slice.end_value() >= bools.size())
76  {
77  bools.resize(slice.end_value(), false);
78  }
79 
80  for (const label i : slice)
81  {
82  bools.set(i);
83  }
84 }
85 
86 
87 // See bitSet::set(labelRange) for original implementation
88 void Foam::BitOps::set(labelHashSet& hashset, const labelRange& range)
89 {
90  labelRange slice(range);
91  slice.adjust(); // No negative start, size adjusted accordingly
92 
93  for (const label i : slice)
94  {
95  hashset.set(i);
96  }
97 }
98 
99 
101 {
102  bitset.set(range);
103 }
104 
105 
106 void Foam::BitOps::unset(List<bool>& bools, const labelUList& locations)
107 {
108  for (const label i : locations)
109  {
110  bools.unset(i);
111  }
112 }
113 
114 
115 // See bitSet::unset(labelRange) for original implementation
116 void Foam::BitOps::unset(List<bool>& bools, const labelRange& range)
117 {
118  for (const label i : range)
119  {
120  bools.unset(i);
121  }
122 }
123 
124 
125 void Foam::BitOps::unset(labelHashSet& hashset, const labelRange& range)
126 {
127  for (const label i : range)
128  {
129  hashset.unset(i);
130  }
131 }
132 
133 
135 {
136  bitset.unset(range);
137 }
138 
139 
141 (
142  const label n,
143  const labelUList& locations
144 )
145 {
146  List<bool> bools(n, false);
148  BitOps::set(bools, locations);
149 
150  return bools;
151 }
152 
153 
155 {
157 
158  BitOps::set(bools, locations);
159 
160  return bools;
161 }
162 
163 
164 // Note: code is like ListOps findIndices() and/or bitSet toc()
166 {
167  const label len = bools.size();
168 
169  // Pass 1: count occurrences
170  label count = 0;
171 
172  for (const bool b : bools)
173  {
174  if (b) ++count;
175  }
176 
177  labelList indices(count);
178 
179  // Pass 2: fill content
180  if (count)
181  {
182  const label total(count);
183  count = 0;
184 
185  for (label i = 0; i < len; ++i)
186  {
187  if (bools[i])
188  {
189  indices[count] = i;
190  if (++count == total) // Terminate early
191  {
192  break;
193  }
194  }
195  }
196  }
197 
198  return indices;
199 }
200 
203 {
204  return BitOps::toc(bools);
205 }
206 
209 {
210  return bitset.toc();
211 }
212 
215 {
216  return bitset.sortedToc();
217 }
218 
221 {
222  return hashset.sortedToc();
223 }
224 
225 
227 {
228  return hashset.sortedToc();
229 }
230 
231 
232 // * * * * * * * * * * * * * * * * BitSetOps * * * * * * * * * * * * * * * * //
233 
235 (
236  const label n,
237  const labelHashSet& locations,
238  const bool on
239 )
240 {
241  bitSet output(n, !on);
242 
243  for (const label idx : locations)
244  {
245  // Restrict the input size
246  if (idx < n)
247  {
248  output.set(idx, on);
249  }
250  }
251 
252  return output;
253 }
254 
255 
257 (
258  const label n,
259  const labelUList& locations,
260  const bool on
261 )
262 {
263  bitSet output(n, !on);
264 
265  for (const label idx : locations)
266  {
267  // Restrict the input size
268  if (idx < n)
269  {
270  output.set(idx, on);
271  }
272  }
273 
274  return output;
275 }
276 
277 
279 (
280  const label n,
281  const label select,
282  const labelUList& values,
283  const bool on
284 )
285 {
286  bitSet output(n, !on);
287 
288  // Restrict the input size
289  const label len = std::min(n, values.size());
290 
291  for (label idx = 0; idx < len; ++idx)
292  {
293  if (select == values[idx])
294  {
295  output.set(idx, on);
296  }
297  }
298 
299  return output;
300 }
301 
302 
303 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type unset(const label i)
Unset the bool entry at specified position, always false for out-of-range access. ...
Definition: UList.H:796
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:504
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
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
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition: bitSetI.H:447
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign &#39;true&#39;).
Definition: BitOps.C:30
scalar range
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
void unset(List< bool > &bools, const labelUList &locations)
Unset the listed locations (assign &#39;false&#39;).
Definition: BitOps.C:99
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
bitSet & unset(const bitSet &other)
Unset (subtract) the bits specified in the other bitset, which is a set difference corresponds to the...
Definition: bitSetI.H:542
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
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
labelList toc() const
The indices of the on bits as a sorted labelList.
Definition: bitSet.C:467
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:139
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
label n
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
List< label > labelList
A List of labels.
Definition: List.H:62
bitSet create(const label n, const labelHashSet &locations, const bool on=true)
Create a bitSet with length n with the specified on locations.
Definition: BitOps.C:228
List< label > toc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:158
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:63