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-2022 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 //- Count arbitrary number of bits (of an integral type)
167 template<class UIntType>
168 inline unsigned int bit_count(UIntType x)
169 {
170  unsigned int n = 0u;
171 
172  for (; x; ++n) { x &= (x-1); }
173 
174  return n;
175 }
176 
177 
178 //- Count bits in a 32-bit value (Hamming weight method)
179 template<>
180 inline unsigned int bit_count(uint32_t x)
181 {
182  x -= (x >> 1) & 0x55555555;
183  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
184 
185  return ((((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
186 }
187 
188 
189 //- Count bits in a 64-bit value (Hamming weight method)
190 template<>
191 inline unsigned int bit_count(uint64_t x)
192 {
193  x -= (x >> 1) & 0x5555555555555555;
194  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
195 
196  return unsigned
197  ((((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56);
198 }
199 
200 
201 //- Repeat a value of the given BitWidth into the destination output type.
202 //
203 // \note when BitWidth is 1, it is better to do directly.
204 // \code
205 // (val ? ~0u : 0u)
206 // \endcode
207 template<class UIntType, unsigned BitWidth>
208 inline UIntType repeat_value(unsigned val)
209 {
210  static_assert
211  (
212  BitWidth && std::numeric_limits<UIntType>::digits >= BitWidth,
213  "BitWidth too large for target output"
214  );
215 
216  // How many fit into the target
217  const unsigned nrepeat = (std::numeric_limits<UIntType>::digits / BitWidth);
218 
219  // Max value for a single element
220  const unsigned mask = ((1u << BitWidth) - 1);
221 
222  // The first occurrence
223  UIntType fillval = ((val >= mask) ? mask : val);
224 
225  // Repeated
226  for (unsigned i = 1; i < nrepeat; ++i)
227  {
228  fillval |= (fillval << BitWidth);
229  }
230 
231  return fillval;
232 }
233 
234 
235 //- Print 0/1 bits in the (unsigned) integral type
236 template<class UIntType>
237 inline Ostream& print(Ostream& os, UIntType value, char off='0', char on='1')
238 {
239  if (os.format() == IOstreamOption::BINARY)
240  {
241  // Perhaps not the most sensible, but the only thing we currently have.
242  os << label(value);
243  }
244  else
245  {
246  // Starting from most significant bit - makes for easy reading.
247  for
248  (
249  unsigned test = (1u << (std::numeric_limits<UIntType>::digits-1));
250  test;
251  test >>= 1u
252  )
253  {
254  os << ((value & test) ? on : off);
255  }
256  }
257 
258  return os;
259 }
260 
261 
262 //- An (unsigned) integral type adapter, for output of bit values
263 template<class UIntType>
264 struct bitInfo
265 {
266  typedef UIntType value_type;
268 
269  //- Null constructible as zero
270  constexpr bitInfo() noexcept : value(0) {}
271 
272  //- Value construct
273  explicit bitInfo(UIntType val) : value(val) {}
274 
275  //- Conversion to base type
276  operator UIntType () const { return value; }
277 
278  //- Conversion to base type
279  operator UIntType& () { return value; }
280 };
281 
282 } // End namespace BitOps
283 
284 
285 /*---------------------------------------------------------------------------*\
286  Namespace BitSetOps Declaration
287 \*---------------------------------------------------------------------------*/
288 
289 namespace BitSetOps
290 {
291 
292 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
293 
294 //- Create a bitSet with length n with the specified \a on locations.
295 // The resulting bitSet is guaranteed to have \b exactly the specified length,
296 // any values or positions larger than n-1 are silently ignored.
297 //
298 // \param n the size of the output bitSet
299 // \param locations the list of positions corresponding to an \a on bit.
300 // \param on the value for on. Set as false to invert the logic.
301 //
302 // \return a bitset
304 (
305  const label n,
306  const labelHashSet& locations,
307  const bool on = true
308 );
309 
310 
311 //- Create a bitSet with length n with the specified \a on locations.
312 // The resulting bitSet is guaranteed to have \b exactly the specified length,
313 // any values or positions larger than n-1 are silently ignored.
314 //
315 // \param n the size of the output bitSet
316 // \param locations the list of positions corresponding to an \a on bit.
317 // \param on the value for on. Set as false to invert the logic.
318 //
319 // \return a bitset
321 (
322  const label n,
323  const labelUList& locations,
324  const bool on = true
325 );
327 
328 //- Create a bitSet with length n with the specified \a on locations
329 //- when the list values are equal to the select value.
330 //
331 // The resulting bitSet is guaranteed to have \b exactly the specified length,
332 // any values or positions larger than n-1 are silently ignored.
333 //
334 // \param n the size of the output bitSet
335 // \param select the value to select as 'on'
336 // \param values the values to scan for 'select'
337 // \param on the value for on. Set as false to invert the logic.
338 //
339 // \return a bitset
341 (
342  const label n,
343  const label select,
344  const labelUList& values,
345  const bool on = true
346 );
347 
348 } // End namespace BitSetOps
349 
350 
351 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 
353 
354 //- Print 0/1 bits of an (unsigned) integral type via an adapter
355 template<class UIntType>
356 inline Ostream& operator<<(Ostream& os, const BitOps::bitInfo<UIntType>& info)
357 {
358  BitOps::print(os, info.value);
359  return os;
360 }
361 
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364 
365 } // End namespace Foam
366 
367 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368 
369 #endif
370 
371 // ************************************************************************* //
UIntType repeat_value(unsigned val)
Repeat a value of the given BitWidth into the destination output type.
Definition: BitOps.H:258
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:289
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:51
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:321
scalar range
UIntType value_type
Definition: BitOps.H:320
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:326
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:322
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:99
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
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:89
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:211
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:343
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:204
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.