23#ifndef VCL_SPACE_CORE_BIT_SET_H
24#define VCL_SPACE_CORE_BIT_SET_H
26#include "bit_set/bit_proxy.h"
28#include <vclib/base.h>
50template<std::
integral T>
53 T mBits =
static_cast<T
>(0);
64 static constexpr std::size_t
SIZE =
sizeof(T) * 8;
81 template<NonBoolIntegralOrEnum I>
84 for (
const auto&
i :
l) {
85 if constexpr (std::is_enum_v<I>)
86 at(toUnderlying(
i)) =
true;
102 BitSet(std::initializer_list<B>
l)
requires std::same_as<bool, B>
105 throw std::invalid_argument(
106 "BitSet: list size is greater than the number of bits of the "
109 for (
uint i = 0;
const auto&
b :
l)
117 constexpr std::size_t
size()
const {
return SIZE; }
127 return mBits & (1 <<
i);
150 return mBits & (1 <<
i);
152 throw std::out_of_range(std::to_string(
i) +
" out of range.");
167 throw std::out_of_range(std::to_string(
i) +
" out of range.");
186 bool none()
const {
return mBits ==
static_cast<T
>(0); }
314 mBits &=
other.mBits;
327 mBits |=
other.mBits;
340 mBits ^=
other.mBits;
354 void deserialize(std::istream& is) { vcl::deserialize(is, mBits); }
374std::ostream& operator<<(std::ostream& os,
const BitSet<T>& bs)
376 os <<
"BitSet<" <<
typeid(T).name() <<
">(";
377 for (uint i = 0; i < bs.size(); i++)
427 std::remove_cvref_t<T>,
428 BitSet<typename RemoveRef<T>::UnderlyingType>>;
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:52
bool all() const
Returns true if all the bits of the BitSet are set to true.
Definition bit_set.h:174
BitSet< T > set()
Sets all the bits to true.
Definition bit_set.h:192
BitSet< T > reset(uint i)
Sets the bit at position i to false.
Definition bit_set.h:225
bool any() const
Returns true if any of the bits of the BitSet are set to true.
Definition bit_set.h:180
BitSet< T > flip()
Flips all the bits of the BitSet.
Definition bit_set.h:235
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:292
constexpr std::size_t size() const
Returns the number of bits of the BitSet.
Definition bit_set.h:117
BitSet< T > operator~() const
Returns a BitSet that is the result of the bitwise NOT of this BitSet.
Definition bit_set.h:303
BitProxy< T > operator[](uint i)
Returns a reference of the i-th bit value of the BitSet.
Definition bit_set.h:135
bool operator[](uint i) const
Returns the i-th bit value of the BitSet.
Definition bit_set.h:124
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:266
static constexpr std::size_t SIZE
The number of the bits of the BitSet.
Definition bit_set.h:64
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:325
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:312
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:279
BitSet< T > set(bool b, uint i)
Sets the bit at position i to b.
Definition bit_set.h:204
void serialize(std::ostream &os) const
Serializes the BitSet to the given output stream.
Definition bit_set.h:348
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:82
bool at(uint i) const
Returns the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:147
BitSet< T > reset()
Sets all the bits to false.
Definition bit_set.h:214
BitSet< T > flip(uint i)
Flips the bit at position i.
Definition bit_set.h:246
T UnderlyingType
The type of the underlying integral value used to store the bits.
Definition bit_set.h:59
bool none() const
Returns true if none of the bits of the BitSet is set to true.
Definition bit_set.h:186
void deserialize(std::istream &is)
Deserializes the BitSet from the given input stream.
Definition bit_set.h:354
T underlying() const
Returns the underlying integral value of the BitSet.
Definition bit_set.h:256
BitProxy< T > at(uint i)
Returns a reference of the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:162
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:338
BitSet(std::initializer_list< B > l)
Constructor from a list of boolean values, allowing braces initialization.
Definition bit_set.h:102
BitSet()=default
Empty constructor. All the Bits of the BitSet are set to false.
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
A concept representing a BitSet.
Definition bit_set.h:426