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-2022 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#include "labelRange.H"
33#include <algorithm>
34
35// * * * * * * * * * * * * * * * * * BitOps * * * * * * * * * * * * * * * * //
36
37// See bitSet::setMany for original implementation
38void Foam::BitOps::set(List<bool>& bools, const labelUList& locations)
39{
40 // Check the max expected value first
41 const auto max = std::max_element(locations.begin(), locations.end());
42 const label len = (max != locations.end() ? (1 + *max) : 0);
43
44 if (len > bools.size())
45 {
46 bools.resize(len, false);
47 }
48
49 for (label i : locations)
50 {
51 if (i >= 0)
52 {
53 bools[i] = true;
54 }
55 }
56}
57
58
59// See bitSet::set(labelRange) for original implementation
61{
62 labelRange slice(range);
63 slice.adjust(); // No negative start, size adjusted accordingly
64
65 // Range is invalid (zero-sized or entirely negative) - noop
66 if (slice.empty())
67 {
68 return;
69 }
70
71 // Check maximum extent of the range.
72 // The after() method is the exclusive end-value,
73 // which corresponds to our potential new length.
74 // - resize now to avoid allocations within the loop
75
76 if (slice.after() >= bools.size())
77 {
78 bools.resize(slice.after(), false);
79 }
80
81 for (const label i : slice)
82 {
83 bools.set(i);
84 }
85}
86
87
88// See bitSet::set(labelRange) for original implementation
90{
91 labelRange slice(range);
92 slice.adjust(); // No negative start, size adjusted accordingly
93
94 for (const label i : slice)
95 {
96 hashset.set(i);
97 }
98}
99
100
102{
103 bitset.set(range);
104}
105
106
107void Foam::BitOps::unset(List<bool>& bools, const labelUList& locations)
108{
109 for (const label i : locations)
110 {
111 bools.unset(i);
112 }
113}
114
115
116// See bitSet::unset(labelRange) for original implementation
118{
119 for (const label i : range)
120 {
121 bools.unset(i);
122 }
123}
124
125
127{
128 for (const label i : range)
129 {
130 hashset.unset(i);
131 }
132}
133
134
136{
137 bitset.unset(range);
138}
139
140
142(
143 const label n,
144 const labelUList& locations
145)
146{
147 List<bool> bools(n, false);
148
149 BitOps::set(bools, locations);
150
151 return bools;
152}
153
154
156{
157 List<bool> bools;
158
159 BitOps::set(bools, locations);
160
161 return bools;
162}
163
164
165// Note: code is like ListOps findIndices() and/or bitSet toc()
167{
168 const label len = bools.size();
169
170 // Pass 1: count occurrences
171 label count = 0;
172
173 for (const bool b : bools)
174 {
175 if (b) ++count;
176 }
177
178 labelList indices(count);
179
180 // Pass 2: fill content
181 if (count)
182 {
183 const label total(count);
184 count = 0;
185
186 for (label i = 0; i < len; ++i)
187 {
188 if (bools[i])
189 {
190 indices[count] = i;
191 if (++count == total) // Terminate early
192 {
193 break;
194 }
195 }
196 }
197 }
198
199 return indices;
200}
201
202
204{
205 return BitOps::toc(bools);
206}
207
208
209// * * * * * * * * * * * * * * * * BitSetOps * * * * * * * * * * * * * * * * //
210
212(
213 const label n,
214 const labelHashSet& locations,
215 const bool on
216)
217{
218 bitSet output(n, !on);
219
220 for (const label idx : locations)
221 {
222 // Restrict the input size
223 if (idx < n)
224 {
225 output.set(idx, on);
226 }
227 }
228
229 return output;
230}
231
232
234(
235 const label n,
236 const labelUList& locations,
237 const bool on
238)
239{
240 bitSet output(n, !on);
241
242 for (const label idx : locations)
243 {
244 // Restrict the input size
245 if (idx < n)
246 {
247 output.set(idx, on);
248 }
249 }
250
251 return output;
252}
253
254
256(
257 const label n,
258 const label select,
259 const labelUList& values,
260 const bool on
261)
262{
263 bitSet output(n, !on);
264
265 // Restrict the input size
266 const label len = std::min(n, values.size());
267
268 for (label idx = 0; idx < len; ++idx)
269 {
270 if (select == values[idx])
271 {
272 output.set(idx, on);
273 }
274 }
275
276 return output;
277}
278
279
280// ************************************************************************* //
scalar range
label n
Y[inertIndex] max(0.0)
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:204
bool set(const Key &key)
Same as insert (no value to overwrite)
Definition: HashSet.H:197
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRangeI.H:436
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
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:330
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type unset(const label i)
Definition: UList.H:539
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:590
bitSet & unset(const bitSet &other)
Definition: bitSetI.H:628
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:81
label after() const noexcept
The value after the last element in the range.
Definition: labelRangeI.H:95
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition: BitOps.C:38
List< bool > select(const label n, const labelUList &locations)
Definition: BitOps.C:142
void unset(List< bool > &bools, const labelUList &locations)
Unset the listed locations (assign 'false').
Definition: BitOps.C:107
List< label > toc(const UList< bool > &bools)
Return the (sorted) values corresponding to 'true' entries.
Definition: BitOps.C:166
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:78
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to 'true' entries.
Definition: BitOps.C:203
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:212
volScalarField & b
Definition: createFields.H:27