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/concepts/types.h>
29#include <vclib/io/serialization.h>
51template<std::
integral T>
54 T mBits =
static_cast<T
>(0);
60 static constexpr std::size_t
SIZE =
sizeof(T) * 8;
77 template<NonBoolIntegralOrEnum I>
80 for (
const auto&
i :
l) {
81 if constexpr (std::is_enum_v<I>)
82 at(toUnderlying(
i)) =
true;
98 BitSet(std::initializer_list<B>
l)
requires std::same_as<bool, B>
101 throw std::invalid_argument(
102 "BitSet: list size is greater than the number of bits of the "
105 for (
uint i = 0;
const auto&
b :
l)
113 constexpr std::size_t
size()
const {
return SIZE; }
123 return mBits & (1 <<
i);
146 return mBits & (1 <<
i);
148 throw std::out_of_range(std::to_string(
i) +
" out of range.");
163 throw std::out_of_range(std::to_string(
i) +
" out of range.");
182 bool none()
const {
return mBits ==
static_cast<T
>(0); }
310 mBits &=
other.mBits;
323 mBits |=
other.mBits;
336 mBits ^=
other.mBits;
350 void deserialize(std::istream& is) { vcl::deserialize(is, mBits); }
370std::ostream& operator<<(std::ostream& os,
const BitSet<T>& bs)
372 os <<
"BitSet<" <<
typeid(T).name() <<
">(";
373 for (uint i = 0; i < bs.size(); i++)
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