Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
33// * * * * * * * * * * * * * * * * * BitOps * * * * * * * * * * * * * * * * //
34
35unsigned int Foam::BitOps::count(const bitSet& bitset, const bool on)
36{
37 return bitset.count(on);
38}
39
40
41// See bitSet::setMany for original implementation
42void Foam::BitOps::set(List<bool>& bools, const labelUList& locations)
43{
44 // Check the max expected value first
45 const auto max = std::max_element(locations.begin(), locations.end());
46 const label len = (max != locations.end() ? (1 + *max) : 0);
47
48 if (len > bools.size())
49 {
50 bools.resize(len, false);
51 }
52
53 for (label i : locations)
54 {
55 if (i >= 0)
56 {
57 bools[i] = true;
58 }
59 }
60}
61
62
63// See bitSet::set(labelRange) for original implementation
64void Foam::BitOps::set(List<bool>& bools, const labelRange& range)
65{
66 labelRange slice(range);
67 slice.adjust(); // No negative start, size adjusted accordingly
68
69 // Range is invalid (zero-sized or entirely negative) - noop
70 if (slice.empty())
71 {
72 return;
73 }
74
75 // Check maximum extent of the range.
76 // The end_value() method is the exclusive end-value,
77 // which corresponds to our potential new length.
78 // - resize now to avoid allocations within the loop
79
80 if (slice.end_value() >= bools.size())
81 {
82 bools.resize(slice.end_value(), false);
83 }
84
85 for (const label i : slice)
86 {
87 bools.set(i);
88 }
89}
90
91
92// See bitSet::set(labelRange) for original implementation
93void Foam::BitOps::set(labelHashSet& hashset, const labelRange& range)
94{
95 labelRange slice(range);
96 slice.adjust(); // No negative start, size adjusted accordingly
97
98 for (const label i : slice)
99 {
100 hashset.set(i);
101 }
102}
103
105void Foam::BitOps::set(bitSet& bitset, const labelRange& range)
106{
107 bitset.set(range);
108}
109
110
111void Foam::BitOps::unset(List<bool>& bools, const labelUList& locations)
112{
113 for (const label i : locations)
115 bools.unset(i);
116 }
117}
118
119
120// See bitSet::unset(labelRange) for original implementation
121void Foam::BitOps::unset(List<bool>& bools, const labelRange& range)
122{
123 for (const label i : range)
124 {
125 bools.unset(i);
126 }
127}
128
129
130void Foam::BitOps::unset(labelHashSet& hashset, const labelRange& range)
131{
132 for (const label i : range)
133 {
134 hashset.unset(i);
135 }
136}
137
139void Foam::BitOps::unset(bitSet& bitset, const labelRange& range)
140{
141 bitset.unset(range);
142}
143
144
146(
147 const label n,
148 const labelUList& locations
149)
150{
151 List<bool> bools(n, false);
153 BitOps::set(bools, locations);
154
155 return bools;
156}
157
158
160{
161 List<bool> bools;
162
163 BitOps::set(bools, locations);
164
165 return bools;
166}
167
168
169// Note: code is like ListOps findIndices() and/or bitSet toc()
171{
172 const label len = bools.size();
173
174 // Pass 1: count occurrences
175 label count = 0;
176
177 for (const bool b : bools)
178 {
179 if (b) ++count;
180 }
181
182 labelList indices(count);
183
184 // Pass 2: fill content
185 if (count)
186 {
187 const label total(count);
188 count = 0;
189
190 for (label i = 0; i < len; ++i)
191 {
192 if (bools[i])
193 {
194 indices[count] = i;
195 if (++count == total) // Terminate early
196 {
197 break;
198 }
199 }
201 }
202
203 return indices;
204}
205
210}
211
214{
215 return bitset.toc();
216}
217
220{
221 return bitset.sortedToc();
222}
223
226{
227 return hashset.sortedToc();
228}
229
230
233 return hashset.sortedToc();
234}
235
236
237// * * * * * * * * * * * * * * * * BitSetOps * * * * * * * * * * * * * * * * //
238
240(
241 const label n,
242 const labelHashSet& locations,
243 const bool on
244)
245{
246 bitSet output(n, !on);
247
248 for (const label idx : locations)
249 {
250 // Restrict the input size
251 if (idx < n)
252 {
253 output.set(idx, on);
255 }
256
257 return output;
258}
259
260
262(
263 const label n,
264 const labelUList& locations,
265 const bool on
266)
267{
268 bitSet output(n, !on);
269
270 for (const label idx : locations)
271 {
272 // Restrict the input size
273 if (idx < n)
274 {
275 output.set(idx, on);
277 }
278
279 return output;
280}
281
282
284(
285 const label n,
286 const label select,
287 const labelUList& values,
288 const bool on
289)
290{
291 bitSet output(n, !on);
292
293 // Restrict the input size
294 const label len = std::min(n, values.size());
295
296 for (label idx = 0; idx < len; ++idx)
297 {
298 if (select == values[idx])
299 {
300 output.set(idx, on);
301 }
302 }
303
304 return output;
305}
306
307
308// ************************************************************************* //
scalar range
label n
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition HashSet.H:247
bool set(const Key &key)
Same as insert (no value to overwrite).
Definition HashSet.H:237
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition HashTable.C:156
bool empty() const noexcept
True if range is empty (zero-sized).
Definition IntRange.H:198
IntType end_value() const noexcept
The value 1 beyond the end of the range.
Definition IntRangeI.H:68
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
bool set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition List.H:469
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
bool unset(const label i)
Unset the bool entry at specified position, always false for out-of-range access.
Definition UList.H:885
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
unsigned int count(const bool on=true) const
Count number of bits set.
Definition bitSetI.H:420
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition bitSetI.H:441
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition bitSetI.H:502
labelList toc() const
The indices of the on bits as a sorted labelList.
Definition bitSet.C:476
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:540
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
void adjust() noexcept
Adjust the start to avoid negative indices.
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
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:139
void unset(List< bool > &bools, const labelUList &locations)
Unset the listed locations (assign 'false').
Definition BitOps.C:104
List< label > toc(const UList< bool > &bools)
Return the (sorted) values corresponding to 'true' entries.
Definition BitOps.C:163
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to 'true' entries.
Definition BitOps.C:200
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:233
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
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition HashOps.H:164
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
List< label > labelList
A List of labels.
Definition List.H:62
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85
UList< label > labelUList
A UList of labels.
Definition UList.H:75
volScalarField & b