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-2020 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, etc.
31 
32  The population count uses the Hamming weight
33  (http://en.wikipedia.org/wiki/Hamming_weight).
34 
35 Namespace
36  Foam::BitSetOps
37 
38 Description
39  Factory and other methods for bitSet.
40  Adaptor methods for other containers that are somewhat similar to
41  bitSet (eg, boolList, labelHashSet).
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef BitOps_H
46 #define BitOps_H
47 
48 #include "label.H"
49 #include "UList.H"
50 #include "HashSet.H"
51 #include "Ostream.H"
52 #include <algorithm>
53 #include <limits>
54 #include <utility>
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Forward Declarations
62 class bitSet;
63 template<class T> class List;
64 
65 /*---------------------------------------------------------------------------*\
66  Namespace BitOps Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 namespace BitOps
70 {
71 
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
73 
74 //- Count number of 'true' entries.
75 // \param val can be set to false to count the number of false values instead
76 // For compatibility with bitSet::count()
77 inline unsigned int count(const UList<bool>& bools, const bool val=true)
78 {
79  return std::count(bools.begin(), bools.end(), val);
80 }
81 
82 //- True if all entries are 'true' or if the set is empty.
83 // For compatibility with bitSet::all()
84 inline bool all(const UList<bool>& bools)
85 {
86  return std::all_of(bools.begin(), bools.end(), [](bool b){return b;});
87 }
88 
89 //- True if any entries are 'true'.
90 // For compatibility with bitSet::any()
91 inline bool any(const UList<bool>& bools)
92 {
93  return std::any_of(bools.begin(), bools.end(), [](bool b){return b;});
94 }
95 
96 //- True if no entries are 'true'.
97 // For compatibility with bitSet::none()
98 inline bool none(const UList<bool>& bools)
99 {
100  return std::none_of(bools.begin(), bools.end(), [](bool b){return b;});
101 }
102 
103 
104 //- Set the specified range 'on' in a boolList.
105 // For compatibility with bitSet::set(labelRange)
106 void set(List<bool>& bools, const labelRange& range);
107 
108 //- Set the specified range in a labelHashSet.
109 // For compatibility with bitSet::set(labelRange)
110 void set(labelHashSet& hashset, const labelRange& range);
111 
112 //- Forward to bitSet::set(labelRange)
113 void set(bitSet& bitset, const labelRange& range);
114 
115 
116 //- Unset the specified range 'on' in a boolList.
117 // For compatibility with bitSet::unset(labelRange)
118 void unset(List<bool>& bools, const labelRange& range);
119 
120 //- Unset the specified range in a labelHashSet.
121 // For compatibility with bitSet::unset(labelRange)
122 void unset(labelHashSet& hashset, const labelRange& range);
123 
124 //- Forward to bitSet::unset(labelRange)
125 void unset(bitSet& bitset, const labelRange& range);
126 
127 
128 //- Count arbitrary number of bits (of an integral type)
129 template<class UIntType>
130 inline unsigned int bit_count(UIntType x)
131 {
132  unsigned int n = 0u;
133 
134  for (; x; ++n) { x &= (x-1); }
135 
136  return n;
137 }
138 
139 
140 //- Count bits in a 32-bit value (Hamming weight method)
141 template<>
142 inline unsigned int bit_count(uint32_t x)
143 {
144  x -= (x >> 1) & 0x55555555;
145  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
146 
147  return ((((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
148 }
149 
150 
151 //- Count bits in a 64-bit value (Hamming weight method)
152 template<>
153 inline unsigned int bit_count(uint64_t x)
154 {
155  x -= (x >> 1) & 0x5555555555555555;
156  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
157 
158  return unsigned
159  ((((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56);
160 }
161 
162 
163 //- Repeat a value of the given BitWidth into the destination output type.
164 //
165 // \note when BitWidth is 1, it is better to do directly.
166 // \code
167 // (val ? ~0u : 0u)
168 // \endcode
169 template<class UIntType, unsigned BitWidth>
170 inline UIntType repeat_value(unsigned val)
171 {
172  static_assert
173  (
174  BitWidth && std::numeric_limits<UIntType>::digits >= BitWidth,
175  "BitWidth too large for target output"
176  );
177 
178  // How many fit into the target
179  const unsigned nrepeat = (std::numeric_limits<UIntType>::digits / BitWidth);
180 
181  // Max value for a single element
182  const unsigned mask = ((1u << BitWidth) - 1);
183 
184  // The first occurrence
185  UIntType fillval = ((val >= mask) ? mask : val);
186 
187  // Repeated
188  for (unsigned i = 1; i < nrepeat; ++i)
189  {
190  fillval |= (fillval << BitWidth);
191  }
192 
193  return fillval;
194 }
195 
196 
197 //- Print 0/1 bits in the (unsigned) integral type
198 template<class UIntType>
199 inline Ostream& print(Ostream& os, UIntType value, char off='0', char on='1')
200 {
201  if (os.format() == IOstream::BINARY)
202  {
203  // Perhaps not the most sensible, but the only thing we currently have.
204  os << label(value);
205  }
206  else
207  {
208  // Starting from most significant bit - makes for easy reading.
209  for
210  (
211  unsigned test = (1u << (std::numeric_limits<UIntType>::digits-1));
212  test;
213  test >>= 1u
214  )
215  {
216  os << ((value & test) ? on : off);
217  }
218  }
219 
220  return os;
221 }
222 
223 
224 //- An (unsigned) integral type adapter, for output of bit values
225 template<class UIntType>
226 struct bitInfo
227 {
228  typedef UIntType value_type;
230 
231  //- Null constructible as zero
232  constexpr bitInfo() noexcept : value(0) {}
233 
234  //- Value construct
235  explicit bitInfo(UIntType val) : value(val) {}
236 
237  //- Conversion to base type
238  operator UIntType () const { return value; }
239 
240  //- Conversion to base type
241  operator UIntType& () { return value; }
242 };
243 
244 } // End namespace BitOps
245 
246 
247 /*---------------------------------------------------------------------------*\
248  Namespace BitSetOps Declaration
249 \*---------------------------------------------------------------------------*/
250 
251 namespace BitSetOps
252 {
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 //- Create a bitSet with length n with the specified \a on locations.
257 // The resulting bitSet is guaranteed to have exactly the specified length,
258 // any values or positions larger than n-1 are silently ignored.
259 //
260 // \param n the size of the output bitSet
261 // \param locations the list of positions corresponding to an \a on bit.
262 // \param on the value for on. Set as false to invert the logic.
263 //
264 // \return a bitset
265 bitSet create
266 (
267  const label n,
268  const labelHashSet& locations,
269  const bool on = true
270 );
271 
272 
273 //- Create a bitSet with length n with the specified \a on locations.
274 // The resulting bitSet is guaranteed to have exactly the specified length,
275 // any values or positions larger than n-1 are silently ignored.
276 //
277 // \param n the size of the output bitSet
278 // \param locations the list of positions corresponding to an \a on bit.
279 // \param on the value for on. Set as false to invert the logic.
280 //
281 // \return a bitset
282 bitSet create
283 (
284  const label n,
285  const labelUList& locations,
286  const bool on = true
287 );
288 
289 
290 //- Create a bitSet with length n with the specified \a on locations
291 //- when the list values are equal to the select value.
292 //
293 // The resulting bitSet is guaranteed to have exactly the specified length,
294 // any values or positions larger than n-1 are silently ignored.
295 //
296 // \param n the size of the output bitSet
297 // \param select the value to select as 'on'
298 // \param values the values to scan for 'select'
299 // \param on the value for on. Set as false to invert the logic.
300 //
301 // \return a bitset
302 bitSet create
303 (
304  const label n,
305  const label select,
306  const labelUList& values,
307  const bool on = true
308 );
309 
310 } // End namespace BitSetOps
311 
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 
316 //- Print 0/1 bits of an (unsigned) integral type via an adapter
317 template<class UIntType>
319 {
320  BitOps::print(os, info.value);
321  return os;
322 }
323 
324 
325 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
326 
327 } // End namespace Foam
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 #endif
332 
333 // ************************************************************************* //
Foam::BitSetOps::create
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:123
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::HashSetOps::bools
List< bool > bools(const labelHashSet &locations)
Definition: HashOps.C:81
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::BitOps::bit_count
unsigned int bit_count(UIntType x)
Count arbitrary number of bits (of an integral type)
Definition: BitOps.H:130
Foam::BitOps::bitInfo::bitInfo
bitInfo(UIntType val)
Value construct.
Definition: BitOps.H:235
Foam::BitOps::bitInfo::bitInfo
constexpr bitInfo() noexcept
Null constructible as zero.
Definition: BitOps.H:232
Foam::BitOps::unset
void unset(List< bool > &bools, const labelRange &range)
Unset the specified range 'on' in a boolList.
Definition: BitOps.C:96
Foam::HashSetOps::bitset
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:72
Foam::HashSet< label, Hash< label > >
Foam::BitOps::all
bool all(const UList< bool > &bools)
True if all entries are 'true' or if the set is empty.
Definition: BitOps.H:84
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::BitOps::repeat_value
UIntType repeat_value(unsigned val)
Repeat a value of the given BitWidth into the destination output type.
Definition: BitOps.H:170
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::BitOps::any
bool any(const UList< bool > &bools)
True if any entries are 'true'.
Definition: BitOps.H:91
Foam::BitOps::bitInfo::value
value_type value
Definition: BitOps.H:229
HashSet.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::BitOps::none
bool none(const UList< bool > &bools)
True if no entries are 'true'.
Definition: BitOps.H:98
Ostream.H
Foam::BitOps::bitInfo
An (unsigned) integral type adapter, for output of bit values.
Definition: BitOps.H:226
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
UList.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
label.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::BitOps::bitInfo::value_type
UIntType value_type
Definition: BitOps.H:228
Foam::BitOps::print
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition: BitOps.H:199
Foam::UList
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:103
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:85
Foam::labelHashSet
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85