Visual Computing Library
Loading...
Searching...
No Matches
vcl::BitSet< T > Class Template Reference

The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size. More...

#include <vclib/space/core/bit_set.h>

Public Member Functions

 BitSet ()=default
 Empty constructor. All the Bits of the BitSet are set to false.
 
template<NonBoolIntegralOrEnum I>
 BitSet (std::initializer_list< I > l)
 Constructor from list of integral (or enum) values that represent the indices of the true bits, allowing braces initialization.
 
template<typename B >
requires std::same_as<bool, B>
 BitSet (std::initializer_list< B > l)
 Constructor from a list of boolean values, allowing braces initialization.
 
constexpr std::size_t size () const
 Returns the number of bits of the BitSet.
 
bool operator[] (uint i) const
 Returns the i-th bit value of the BitSet.
 
BitProxy< T > operator[] (uint i)
 Returns a reference of the i-th bit value of the BitSet.
 
bool at (uint i) const
 Returns the i-th bit value of the BitSet, with bounds checking.
 
BitProxy< T > at (uint i)
 Returns a reference of the i-th bit value of the BitSet, with bounds checking.
 
bool all () const
 Returns true if all the bits of the BitSet are set to true.
 
bool any () const
 Returns true if any of the bits of the BitSet are set to true.
 
bool none () const
 Returns true if none of the bits of the BitSet is set to true.
 
BitSet< T > set ()
 Sets all the bits to true.
 
BitSet< T > set (bool b, uint i)
 Sets the bit at position i to b.
 
BitSet< T > reset ()
 Sets all the bits to false.
 
BitSet< T > reset (uint i)
 Sets the bit at position i to false.
 
BitSet< T > flip ()
 Flips all the bits of the BitSet.
 
BitSet< T > flip (uint i)
 Flips the bit at position i.
 
underlying () const
 Returns the underlying integral value of the BitSet.
 
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.
 
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.
 
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.
 
BitSet< T > operator~ () const
 Returns a BitSet that is the result of the bitwise NOT of this BitSet.
 
BitSet< T > & operator&= (const BitSet< T > &other)
 Compound assignment operator that performs the bitwise AND between this BitSet and another BitSet.
 
BitSet< T > & operator|= (const BitSet< T > &other)
 Compound assignment operator that performs the bitwise OR between this BitSet and another BitSet.
 
BitSet< T > & operator^= (const BitSet< T > &other)
 Compound assignment operator that performs the bitwise XOR between this BitSet and another BitSet.
 
void serialize (std::ostream &os) const
 Serializes the BitSet to the given output stream.
 
void deserialize (std::istream &is)
 Deserializes the BitSet from the given input stream.
 
auto operator<=> (const BitSet< T > &) const =default
 

Static Public Attributes

static constexpr std::size_t SIZE = sizeof(T) * 8
 The number of the bits of the BitSet.
 

Private Member Functions

 BitSet (T bits)
 

Private Attributes

mBits = static_cast<T>(0)
 

Friends

template<typename U >
std::ostream & operator<< (std::ostream &os, const BitSet< U > &bs)
 

Detailed Description

template<std::integral T>
class vcl::BitSet< T >

The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size.

This class is necessary w.r.t. the std::bitset because the realization of the std::bitset class is implementation defined, and the standard does not constraint the size of the bitset itself.

This class guarantess that the sizeof(BitSet<T>) is equal to sizeof(T).

Template Parameters
TThe type to use as a storage for the bits. It must satisfy the std::integral concept.

Constructor & Destructor Documentation

◆ BitSet() [1/2]

template<std::integral T>
template<NonBoolIntegralOrEnum I>
vcl::BitSet< T >::BitSet ( std::initializer_list< I >  l)
inline

Constructor from list of integral (or enum) values that represent the indices of the true bits, allowing braces initialization.

Creates a BitSet by setting to true the bits at the indices specified in the list. All the other bits are set to false. Each value of the list must be less than the number of bits of the BitSet.

Parameters
[in]lthe list of bit positions to set to true.

◆ BitSet() [2/2]

template<std::integral T>
template<typename B >
requires std::same_as<bool, B>
vcl::BitSet< T >::BitSet ( std::initializer_list< B >  l)
inline

Constructor from a list of boolean values, allowing braces initialization.

Constructs the bitset from the list of boolean values. The size of the list must be less or equal to the number of bits of the BitSet.

Parameters
[in]lthe list of boolean values to assign to the BitSet.

Member Function Documentation

◆ all()

template<std::integral T>
bool vcl::BitSet< T >::all ( ) const
inline

Returns true if all the bits of the BitSet are set to true.

Returns
true if all the bits of the BitSet are set to true.

◆ any()

template<std::integral T>
bool vcl::BitSet< T >::any ( ) const
inline

Returns true if any of the bits of the BitSet are set to true.

Returns
true if any of the bits of the BitSet are set to true.

◆ at() [1/2]

template<std::integral T>
BitProxy< T > vcl::BitSet< T >::at ( uint  i)
inline

Returns a reference of the i-th bit value of the BitSet, with bounds checking.

Parameters
[in]ithe position of the bit
Exceptions
std::out_of_rangeif i is >= BitSet<T>::SIZE
Returns
a reference of the boolean value of the bit

◆ at() [2/2]

template<std::integral T>
bool vcl::BitSet< T >::at ( uint  i) const
inline

Returns the i-th bit value of the BitSet, with bounds checking.

Parameters
[in]ithe position of the bit
Exceptions
std::out_of_rangeif i is >= BitSet<T>::SIZE
Returns
the boolean value of the bit

◆ deserialize()

template<std::integral T>
void vcl::BitSet< T >::deserialize ( std::istream &  is)
inline

Deserializes the BitSet from the given input stream.

Parameters
[in]isThe input stream.

◆ flip() [1/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::flip ( )
inline

Flips all the bits of the BitSet.

Returns
*this

◆ flip() [2/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::flip ( uint  i)
inline

Flips the bit at position i.

Parameters
[in]ithe position of the bit
Returns
*this

◆ none()

template<std::integral T>
bool vcl::BitSet< T >::none ( ) const
inline

Returns true if none of the bits of the BitSet is set to true.

Returns
true if none of the bits of the BitSet is set to true.

◆ operator&()

template<std::integral T>
BitSet< T > vcl::BitSet< T >::operator& ( const BitSet< T > &  other) const
inline

Returns a BitSet that is the result of the bitwise AND between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
the result of the bitwise AND between this BitSet and another BitSet.

◆ operator&=()

template<std::integral T>
BitSet< T > & vcl::BitSet< T >::operator&= ( const BitSet< T > &  other)
inline

Compound assignment operator that performs the bitwise AND between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
*this

◆ operator[]() [1/2]

template<std::integral T>
BitProxy< T > vcl::BitSet< T >::operator[] ( uint  i)
inline

Returns a reference of the i-th bit value of the BitSet.

Parameters
[in]ithe position of the bit
Returns
a reference of the boolean value of the bit

◆ operator[]() [2/2]

template<std::integral T>
bool vcl::BitSet< T >::operator[] ( uint  i) const
inline

Returns the i-th bit value of the BitSet.

Parameters
[in]ithe position of the bit
Returns
the boolean value of the bit

◆ operator^()

template<std::integral T>
BitSet< T > vcl::BitSet< T >::operator^ ( const BitSet< T > &  other) const
inline

Returns a BitSet that is the result of the bitwise XOR between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
the result of the bitwise XOR between this BitSet and another BitSet.

◆ operator^=()

template<std::integral T>
BitSet< T > & vcl::BitSet< T >::operator^= ( const BitSet< T > &  other)
inline

Compound assignment operator that performs the bitwise XOR between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
*this

◆ operator|()

template<std::integral T>
BitSet< T > vcl::BitSet< T >::operator| ( const BitSet< T > &  other) const
inline

Returns a BitSet that is the result of the bitwise OR between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
the result of the bitwise OR between this BitSet and another BitSet.

◆ operator|=()

template<std::integral T>
BitSet< T > & vcl::BitSet< T >::operator|= ( const BitSet< T > &  other)
inline

Compound assignment operator that performs the bitwise OR between this BitSet and another BitSet.

Parameters
[in]otherthe other BitSet
Returns
*this

◆ operator~()

template<std::integral T>
BitSet< T > vcl::BitSet< T >::operator~ ( ) const
inline

Returns a BitSet that is the result of the bitwise NOT of this BitSet.

Returns
the result of the bitwise NOT of this BitSet.

◆ reset() [1/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::reset ( )
inline

Sets all the bits to false.

Returns
*this

◆ reset() [2/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::reset ( uint  i)
inline

Sets the bit at position i to false.

Parameters
[in]ithe position of the bit
Returns
*this

◆ serialize()

template<std::integral T>
void vcl::BitSet< T >::serialize ( std::ostream &  os) const
inline

Serializes the BitSet to the given output stream.

Parameters
[in]osThe output stream.

◆ set() [1/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::set ( )
inline

Sets all the bits to true.

Returns
*this

◆ set() [2/2]

template<std::integral T>
BitSet< T > vcl::BitSet< T >::set ( bool  b,
uint  i 
)
inline

Sets the bit at position i to b.

Parameters
[in]bthe boolena value to assign to the bit
[in]ithe position of the bit
Returns
*this

◆ size()

template<std::integral T>
constexpr std::size_t vcl::BitSet< T >::size ( ) const
inlineconstexpr

Returns the number of bits of the BitSet.

Returns
the number of bits of the BitSet.

◆ underlying()

template<std::integral T>
T vcl::BitSet< T >::underlying ( ) const
inline

Returns the underlying integral value of the BitSet.

Returns
the underlying integral value of the BitSet

The documentation for this class was generated from the following file: