BitOps.H
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 Namespace
27  Foam::BitOps
28 
29 Description
30  Various bit-wise operations and adaptor methods for containers
31  that are somewhat similar to bitSet (eg, boolList, labelHashSet).
32 
33  The population count uses the Hamming weight
34  (http://en.wikipedia.org/wiki/Hamming_weight).
35 
36 Namespace
37  Foam::BitSetOps
38 
39 Description
40  Factory and other methods for bitSet.
41  Adaptor methods for other containers that are somewhat similar to
42  bitSet (eg, boolList, labelHashSet).
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_BitOps_H
47 #define Foam_BitOps_H
48 
49 #include "UList.H"
50 #include "HashSet.H"
51 #include "Ostream.H"
52 #include <limits>
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward Declarations
60 class bitSet;
61 template<class T> class List;
62 
63 /*---------------------------------------------------------------------------*\
64  Namespace BitOps Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 namespace BitOps
68 {
69 
70 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 
72 //- Count number of 'true' entries.
73 // \param val can be set to false to count the number of false values instead
74 // For compatibility with bitSet::count()
75 inline unsigned int count(const UList<bool>& bools, const bool val=true)
76 {
77  return std::count(bools.begin(), bools.end(), val);
78 }
79 
80 //- True if all entries are 'true' or if the set is empty.
81 // For compatibility with bitSet::all()
82 inline bool all(const UList<bool>& bools)
83 {
84  return std::all_of(bools.begin(), bools.end(), identityOp());
85 }
86 
87 //- True if any entries are 'true'.
88 // For compatibility with bitSet::any()
89 inline bool any(const UList<bool>& bools)
90 {
91  return std::any_of(bools.begin(), bools.end(), identityOp());
92 }
93 
94 //- True if no entries are 'true'.
95 // For compatibility with bitSet::none()
96 inline bool none(const UList<bool>& bools)
97 {
98  return std::none_of(bools.begin(), bools.end(), identityOp());
99 }
100 
101 
102 //- Set the listed locations (assign 'true').
103 // Does auto-vivify for non-existent entries.
104 //
105 // For compatibility with bitSet::set(labelUList)
106 void set(List<bool>& bools, const labelUList& locations);
107 
108 //- Set the specified range 'on' in a boolList.
109 // For compatibility with bitSet::set(labelRange)
110 void set(List<bool>& bools, const labelRange& range);
111 
112 //- Set the specified range in a labelHashSet.
113 // For compatibility with bitSet::set(labelRange)
114 void set(labelHashSet& hashset, const labelRange& range);
115 
116 //- Forward to bitSet::set(labelRange)
117 void set(bitSet& bitset, const labelRange& range);
118 
119 
120 //- Unset the listed locations (assign 'false').
121 // No auto-vivify non-existent entries.
122 //
123 // For compatibility with bitSet::set(labelUList)
124 void unset(List<bool>& bools, const labelUList& locations);
125 
126 //- Unset the specified range 'on' in a boolList.
127 // For compatibility with bitSet::unset(labelRange)
128 void unset(List<bool>& bools, const labelRange& range);
129 
130 //- Unset the specified range in a labelHashSet.
131 // For compatibility with bitSet::unset(labelRange)
132 void unset(labelHashSet& hashset, const labelRange& range);
133 
134 //- Forward to bitSet::unset(labelRange)
135 void unset(bitSet& bitset, const labelRange& range);
136 
137 
138 //- Construct a selection list of bools (all false) with the given pre-size,
139 //- subsequently add specified locations as true,
140 //- auto-vivify entries if needed.
141 // Similar to bitSet construction from locations
142 //
143 // \return a List of bools
144 List<bool> select(const label n, const labelUList& locations);
145 
146 //- Construct an auto-sized selection list of bools (all false),
147 //- and populate the specified locations as true.
148 // Similar to bitSet construction from locations
149 //
150 // \return a List of bools
151 List<bool> select(const labelUList& locations);
152 
153 //- Return the (sorted) values corresponding to 'true' entries.
154 // Similar to bitSet::toc()
155 //
156 // \return a List of labels
158 
159 //- Return the (sorted) values corresponding to 'true' entries.
160 // Similar to bitSet::sortedToc() and labelHashSet::sortedToc()
161 //
162 // \return a List of labels
164 
165 
166 //- Forward to bitSet::toc(), the sorted values of the 'on' entries.
167 //
168 // \return a List of labels
169 List<label> toc(const bitSet& bitset);
170 
171 //- Forward to bitSet::sortedToc(), the sorted values of the 'on' entries.
172 //
173 // \return a List of labels
175 
176 
177 //- Forward to labelHashSet::sortedToc(), the sorted values of the 'on' entries.
178 //
179 // \return a List of labels
180 List<label> toc(const labelHashSet& hashset);
181 
182 //- Forward to labelHashSet::sortedToc(), the sorted values of the 'on' entries.
183 //
184 // \return a List of labels
185 List<label> sortedToc(const labelHashSet& hashset);
186 
187 
188 //- Count arbitrary number of bits (of an integral type)
189 template<class UIntType>
190 inline unsigned int bit_count(UIntType x)
191 {
192  unsigned int n = 0u;
193 
194  for (; x; ++n) { x &= (x-1); }
195 
196  return n;
197 }
198 
199 
200 //- Count bits in a 32-bit value (Hamming weight method)
201 template<>
202 inline unsigned int bit_count(uint32_t x)
203 {
204  x -= (x >> 1) & 0x55555555;
205  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
206 
207  return ((((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
208 }
209 
210 
211 //- Count bits in a 64-bit value (Hamming weight method)
212 template<>
213 inline unsigned int bit_count(uint64_t x)
214 {
215  x -= (x >> 1) & 0x5555555555555555;
216  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
217 
218  return unsigned
219  ((((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56);
220 }
221 
222 
223 //- Repeat a value of the given BitWidth into the destination output type.
224 //
225 // \note when BitWidth is 1, it is better to do directly.
226 // \code
227 // (val ? ~0u : 0u)
228 // \endcode
229 template<class UIntType, unsigned BitWidth>
230 inline UIntType repeat_value(unsigned val)
231 {
232  static_assert
233  (
234  BitWidth && std::numeric_limits<UIntType>::digits >= BitWidth,
235  "BitWidth too large for target output"
236  );
237 
238  // How many fit into the target
239  const unsigned nrepeat = (std::numeric_limits<UIntType>::digits / BitWidth);
240 
241  // Max value for a single element
242  const unsigned mask = ((1u << BitWidth) - 1);
243 
244  // The first occurrence
245  UIntType fillval = ((val >= mask) ? mask : val);
246 
247  // Repeated
248  for (unsigned i = 1; i < nrepeat; ++i)
249  {
250  fillval |= (fillval << BitWidth);
251  }
252 
253  return fillval;
254 }
255 
256 
257 //- Print 0/1 bits in the (unsigned) integral type
258 template<class UIntType>
259 inline Ostream& print(Ostream& os, UIntType value, char off='0', char on='1')
260 {
261  if (os.format() == IOstreamOption::BINARY)
262  {
263  // Perhaps not the most sensible, but the only thing we currently have.
264  os << label(value);
265  }
266  else
267  {
268  // Starting from most significant bit - makes for easy reading.
269  for
270  (
271  unsigned test = (1u << (std::numeric_limits<UIntType>::digits-1));
272  test;
273  test >>= 1u
274  )
275  {
276  os << ((value & test) ? on : off);
277  }
278  }
279 
280  return os;
281 }
282 
283 
284 //- An (unsigned) integral type adapter, for output of bit values
285 template<class UIntType>
286 struct bitInfo
287 {
288  typedef UIntType value_type;
290 
291  //- Null constructible as zero
292  constexpr bitInfo() noexcept : value(0) {}
293 
294  //- Value construct
295  explicit bitInfo(UIntType val) : value(val) {}
296 
297  //- Conversion to base type
298  operator UIntType () const { return value; }
299 
300  //- Conversion to base type
301  operator UIntType& () { return value; }
302 };
303 
304 } // End namespace BitOps
305 
306 
307 /*---------------------------------------------------------------------------*\
308  Namespace BitSetOps Declaration
309 \*---------------------------------------------------------------------------*/
310 
311 namespace BitSetOps
312 {
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 
316 //- Create a bitSet with length n with the specified \a on locations.
317 // The resulting bitSet is guaranteed to have \b exactly the specified length,
318 // any values or positions larger than n-1 are silently ignored.
319 //
320 // \param n the size of the output bitSet
321 // \param locations the list of positions corresponding to an \a on bit.
322 // \param on the value for on. Set as false to invert the logic.
323 //
324 // \return a bitset
326 (
327  const label n,
328  const labelHashSet& locations,
329  const bool on = true
330 );
331 
332 
333 //- Create a bitSet with length n with the specified \a on locations.
334 // The resulting bitSet is guaranteed to have \b exactly the specified length,
335 // any values or positions larger than n-1 are silently ignored.
336 //
337 // \param n the size of the output bitSet
338 // \param locations the list of positions corresponding to an \a on bit.
339 // \param on the value for on. Set as false to invert the logic.
340 //
341 // \return a bitset
343 (
344  const label n,
345  const labelUList& locations,
346  const bool on = true
347 );
348 
349 
350 //- Create a bitSet with length n with the specified \a on locations
351 //- when the list values are equal to the select value.
352 //
353 // The resulting bitSet is guaranteed to have \b exactly the specified length,
354 // any values or positions larger than n-1 are silently ignored.
355 //
356 // \param n the size of the output bitSet
357 // \param select the value to select as 'on'
358 // \param values the values to scan for 'select'
359 // \param on the value for on. Set as false to invert the logic.
360 //
361 // \return a bitset
363 (
364  const label n,
365  const label select,
366  const labelUList& values,
367  const bool on = true
368 );
369 
370 } // End namespace BitSetOps
371 
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
376 //- Print 0/1 bits of an (unsigned) integral type via an adapter
377 template<class UIntType>
378 inline Ostream& operator<<(Ostream& os, const BitOps::bitInfo<UIntType>& info)
379 {
380  BitOps::print(os, info.value);
381  return os;
382 }
383 
384 
385 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
386 
387 } // End namespace Foam
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 #endif
392 
393 // ************************************************************************* //
UIntType repeat_value(unsigned val)
Repeat a value of the given BitWidth into the destination output type.
Definition: BitOps.H:292
bool any(const UList< bool > &bools)
True if any entries are &#39;true&#39;.
Definition: BitOps.H:93
bool none(const UList< bool > &bools)
True if no entries are &#39;true&#39;.
Definition: BitOps.H:103
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition: BitOps.H:323
List< bool > select(const labelUList &locations)
Construct an auto-sized selection list of bools (all false), and populate the specified locations as ...
Definition: BitOps.C:147
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
value_type value
Definition: BitOps.H:355
scalar range
UIntType value_type
Definition: BitOps.H:354
void unset(List< bool > &bools, const labelUList &locations)
Unset the listed locations (assign &#39;false&#39;).
Definition: BitOps.C:99
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
constexpr bitInfo() noexcept
Null constructible as zero.
Definition: BitOps.H:360
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized...
Definition: stdFoam.H:90
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
OBJstream os(runTime.globalPath()/outputName)
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
unsigned int bit_count(UIntType x)
Count arbitrary number of bits (of an integral type)
Definition: BitOps.H:245
bool all(const UList< bool > &bools)
True if all entries are &#39;true&#39; or if the set is empty.
Definition: BitOps.H:83
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
label n
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
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
Namespace for OpenFOAM.