Visual Computing Library
Loading...
Searching...
No Matches
bit_set.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_BIT_SET_H
24#define VCL_SPACE_CORE_BIT_SET_H
25
26#include "bit_set/bit_proxy.h"
27
28#include <vclib/concepts/types.h>
29#include <vclib/io/serialization.h>
30
31#include <stdexcept>
32#include <string>
33
34namespace vcl {
35
51template<std::integral T>
52class BitSet
53{
54 T mBits = static_cast<T>(0);
55
56public:
60 static constexpr std::size_t SIZE = sizeof(T) * 8;
61
65 BitSet() = default;
66
77 template<NonBoolIntegralOrEnum I>
78 BitSet(std::initializer_list<I> l)
79 {
80 for (const auto& i : l) {
81 if constexpr (std::is_enum_v<I>)
82 at(toUnderlying(i)) = true;
83 else
84 at(i) = true;
85 }
86 }
87
97 template<typename B>
98 BitSet(std::initializer_list<B> l) requires std::same_as<bool, B>
99 {
100 if (l.size() > SIZE)
101 throw std::invalid_argument(
102 "BitSet: list size is greater than the number of bits of the "
103 "BitSet");
104
105 for (uint i = 0; const auto& b : l)
106 at(i++) = b;
107 }
108
113 constexpr std::size_t size() const { return SIZE; }
114
120 bool operator[](uint i) const
121 {
122 assert(i < SIZE);
123 return mBits & (1 << i);
124 }
125
132 {
133 assert(i < SIZE);
134 return BitProxy(mBits, i);
135 }
136
143 bool at(uint i) const
144 {
145 if (i < SIZE)
146 return mBits & (1 << i);
147 else
148 throw std::out_of_range(std::to_string(i) + " out of range.");
149 }
150
159 {
160 if (i < SIZE)
161 return BitProxy(mBits, i);
162 else
163 throw std::out_of_range(std::to_string(i) + " out of range.");
164 }
165
170 bool all() const { return mBits == ~static_cast<T>(0); }
171
176 bool any() const { return !none(); }
177
182 bool none() const { return mBits == static_cast<T>(0); }
183
189 {
190 mBits = ~static_cast<T>(0);
191 return *this;
192 }
193
201 {
202 at(i) = b;
203 return *this;
204 }
205
211 {
212 mBits = 0;
213 return *this;
214 }
215
222 {
223 at(i) = false;
224 return *this;
225 }
226
232 {
233 mBits = ~mBits;
234 return *this;
235 }
236
243 {
244 at(i) = !at(i);
245 return *this;
246 }
247
252 T underlying() const { return mBits; }
253
263 {
264 return BitSet<T>(mBits & other.mBits);
265 }
266
276 {
277 return BitSet<T>(mBits | other.mBits);
278 }
279
289 {
290 return BitSet<T>(mBits ^ other.mBits);
291 }
292
299 BitSet<T> operator~() const { return BitSet<T>(~mBits); }
300
309 {
310 mBits &= other.mBits;
311 return *this;
312 }
313
322 {
323 mBits |= other.mBits;
324 return *this;
325 }
326
335 {
336 mBits ^= other.mBits;
337 return *this;
338 }
339
344 void serialize(std::ostream& os) const { vcl::serialize(os, mBits); }
345
350 void deserialize(std::istream& is) { vcl::deserialize(is, mBits); }
351
352 auto operator<=>(const BitSet<T>&) const = default;
353
355 template<typename U>
356 friend std::ostream& operator<<(std::ostream& os, const BitSet<U>& bs);
357
358private:
359 // constructor to initialize the BitSet with a given integral value
360 BitSet(T bits) : mBits(bits) {}
361};
362
369template<typename T>
370std::ostream& operator<<(std::ostream& os, const BitSet<T>& bs)
371{
372 os << "BitSet<" << typeid(T).name() << ">(";
373 for (uint i = 0; i < bs.size(); i++)
374 os << bs[i];
375 os << ")";
376 return os;
377}
378
379/* Specialization Aliases */
380
387
394
401
408
409} // namespace vcl
410
411#endif // VCL_SPACE_CORE_BIT_SET_H
The BitProxy class allows to access to a bool reference from a bit saved in a mask,...
Definition bit_proxy.h:43
The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size.
Definition bit_set.h:53
bool all() const
Returns true if all the bits of the BitSet are set to true.
Definition bit_set.h:170
BitSet< T > set()
Sets all the bits to true.
Definition bit_set.h:188
BitSet< T > reset(uint i)
Sets the bit at position i to false.
Definition bit_set.h:221
bool any() const
Returns true if any of the bits of the BitSet are set to true.
Definition bit_set.h:176
BitSet< T > flip()
Flips all the bits of the BitSet.
Definition bit_set.h:231
BitSet< T > operator^(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise XOR between this BitSet and another BitSet.
Definition bit_set.h:288
constexpr std::size_t size() const
Returns the number of bits of the BitSet.
Definition bit_set.h:113
BitSet< T > operator~() const
Returns a BitSet that is the result of the bitwise NOT of this BitSet.
Definition bit_set.h:299
BitProxy< T > operator[](uint i)
Returns a reference of the i-th bit value of the BitSet.
Definition bit_set.h:131
bool operator[](uint i) const
Returns the i-th bit value of the BitSet.
Definition bit_set.h:120
BitSet< T > operator&(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise AND between this BitSet and another BitSet.
Definition bit_set.h:262
static constexpr std::size_t SIZE
The number of the bits of the BitSet.
Definition bit_set.h:60
BitSet< T > & operator|=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise OR between this BitSet and another BitSet.
Definition bit_set.h:321
BitSet< T > & operator&=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise AND between this BitSet and another BitSet.
Definition bit_set.h:308
BitSet< T > operator|(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise OR between this BitSet and another BitSet.
Definition bit_set.h:275
BitSet< T > set(bool b, uint i)
Sets the bit at position i to b.
Definition bit_set.h:200
void serialize(std::ostream &os) const
Serializes the BitSet to the given output stream.
Definition bit_set.h:344
BitSet(std::initializer_list< I > l)
Constructor from list of integral (or enum) values that represent the indices of the true bits,...
Definition bit_set.h:78
bool at(uint i) const
Returns the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:143
BitSet< T > reset()
Sets all the bits to false.
Definition bit_set.h:210
BitSet< T > flip(uint i)
Flips the bit at position i.
Definition bit_set.h:242
bool none() const
Returns true if none of the bits of the BitSet is set to true.
Definition bit_set.h:182
void deserialize(std::istream &is)
Deserializes the BitSet from the given input stream.
Definition bit_set.h:350
T underlying() const
Returns the underlying integral value of the BitSet.
Definition bit_set.h:252
BitProxy< T > at(uint i)
Returns a reference of the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:158
BitSet< T > & operator^=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise XOR between this BitSet and another BitSet.
Definition bit_set.h:334
BitSet(std::initializer_list< B > l)
Constructor from a list of boolean values, allowing braces initialization.
Definition bit_set.h:98
BitSet()=default
Empty constructor. All the Bits of the BitSet are set to false.
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43