BitSet< S, E, size_ > Class Template Reference

Manage a set of bit values using enumeration. More...

#include <BitSet.h>

Classes

class  BitRef
 

Public Types

using IfHelperType = void(BitSet::*)() const
 

Public Member Functions

constexpr BitSet ()=default
 Construct empty set. More...
 
template<typename S2 >
constexpr BitSet (const BitSet< S2, E > &bitset)
 Copy constructor. More...
 
constexpr BitSet (S value)
 Construct from a raw set of bits. More...
 
constexpr BitSet (E e)
 Construct set containing a single value. More...
 
bool operator== (const BitSet &other) const
 Compare this set with another for equality. More...
 
bool operator!= (const BitSet &other) const
 Compare this set with another for inequality. More...
 
constexpr BitSet operator~ () const
 Obtain a set containing all elements not in this one. More...
 
BitSetflip ()
 Flip all bits in the set. More...
 
BitSetflip (E e)
 Flip state of the given bit. More...
 
size_t count () const
 Get the number of elements in the set, i.e. bits set to 1. More...
 
BitSetoperator+= (const BitSet &rhs)
 Union: Add elements to set. More...
 
BitSetoperator-= (const BitSet &rhs)
 Remove elements from set. More...
 
BitSetoperator&= (const BitSet &rhs)
 Intersection: Leave only elements common to both sets. More...
 
BitSetoperator|= (const BitSet &rhs)
 Union: Add elements to set. More...
 
BitSetoperator^= (const BitSet &rhs)
 XOR - toggle state of bits using another set. More...
 
bool test (E e) const
 Test to see if given element is in the set. More...
 
bool operator[] (E e) const
 Read-only [] operator. More...
 
BitRef operator[] (E e)
 Read/write [] operator. More...
 
bool any () const
 Determine if set contains any values. More...
 
bool any (const BitSet &other) const
 Determine if set contains any values from another set i.e. intersection != []. More...
 
bool all () const
 Test if set contains all possible values. More...
 
bool none () const
 Test if set is empty. More...
 
BitSetset ()
 Add all possible values to the bit set. More...
 
BitSetset (E e, bool state=true)
 Set the state of the given bit (i.e. add to or remove from the set) More...
 
BitSetreset ()
 Remove all values from the set. More...
 
BitSetreset (E e)
 Clear the state of the given bit (i.e. remove it from the set) More...
 
bool operator== (E e) const
 Determine if set consists of only the one given element. More...
 
constexpr operator S () const
 Allow casts from the native storage type to get a numeric result for this set. More...
 
constexpr S value () const
 Get stored bits for this bitset. More...
 
 operator IfHelperType () const
 
size_t printTo (Print &p, const String &separator=", ") const
 Class template to print the contents of a BitSet to a String. More...
 

Static Public Member Functions

static constexpr size_t size ()
 Get the number of possible elements in the set. More...
 
static constexpr BitSet domain ()
 Get the set of all possible values. More...
 
static constexpr S bitVal (E e)
 Get the bitmask corresponding to a given value. More...
 

Detailed Description

template<typename S, typename E, size_t size_ = sizeof(S) * 8>
class BitSet< S, E, size_ >

Manage a set of bit values using enumeration.

API is similar to a simplified std::bitset, but with added +/- operators.

Template Parameters
SStorage type (e.g. uint32_t). This determines how much space to use, and must be an unsigned integer. It is safe to use this class in structures, where it will occupy exactly the required space.
EElement type e.g. enum class. You can use any enum or unsigned integer for the elements. These must have ordinal sequence starting at 0.
size_Number of possible values in the set. Defaults to maximum for given storage type. Number of possible values in the set. This must be at least 1, and cannot be more than the given Storage type may contain. For example, a :cpp:type:uint8_t may contain up to 8 values.
Note
It is important to specify size correctly when using enumerated values. In the FruitBasket example, we use a uint8_t storage type so can have up to 8 possible values. However, the Fruit enum contains only 7 values. The set operations will therefore be restricted to ensure that the unused bit is never set.

Member Typedef Documentation

◆ IfHelperType

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
using BitSet< S, E, size_ >::IfHelperType = void (BitSet::*)() const

Constructor & Destructor Documentation

◆ BitSet() [1/4]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr BitSet< S, E, size_ >::BitSet ( )
constexprdefault

Construct empty set.

◆ BitSet() [2/4]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
template<typename S2 >
constexpr BitSet< S, E, size_ >::BitSet ( const BitSet< S2, E > &  bitset)
inlineconstexpr

Copy constructor.

Parameters
bitsetThe set to copy

◆ BitSet() [3/4]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr BitSet< S, E, size_ >::BitSet ( value)
inlineconstexpr

Construct from a raw set of bits.

Parameters
valueIntegral type whose bits will be interpreted as set{E}

◆ BitSet() [4/4]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr BitSet< S, E, size_ >::BitSet ( e)
inlineconstexpr

Construct set containing a single value.

Parameters
eValue to place in our new BitSet object

Member Function Documentation

◆ all()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::all ( ) const
inline

Test if set contains all possible values.

◆ any() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::any ( ) const
inline

Determine if set contains any values.

◆ any() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::any ( const BitSet< S, E, size_ > &  other) const
inline

Determine if set contains any values from another set i.e. intersection != [].

◆ bitVal()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
static constexpr S BitSet< S, E, size_ >::bitVal ( e)
inlinestaticconstexpr

Get the bitmask corresponding to a given value.

◆ count()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
size_t BitSet< S, E, size_ >::count ( ) const
inline

Get the number of elements in the set, i.e. bits set to 1.

◆ domain()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
static constexpr BitSet BitSet< S, E, size_ >::domain ( )
inlinestaticconstexpr

Get the set of all possible values.

◆ flip() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::flip ( )
inline

Flip all bits in the set.

◆ flip() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::flip ( e)
inline

Flip state of the given bit.

◆ none()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::none ( ) const
inline

Test if set is empty.

◆ operator IfHelperType()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet< S, E, size_ >::operator IfHelperType ( ) const
inline

◆ operator S()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr BitSet< S, E, size_ >::operator S ( ) const
inlineexplicitconstexpr

Allow casts from the native storage type to get a numeric result for this set.

◆ operator!=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::operator!= ( const BitSet< S, E, size_ > &  other) const
inline

Compare this set with another for inequality.

◆ operator&=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::operator&= ( const BitSet< S, E, size_ > &  rhs)
inline

Intersection: Leave only elements common to both sets.

◆ operator+=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::operator+= ( const BitSet< S, E, size_ > &  rhs)
inline

Union: Add elements to set.

◆ operator-=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::operator-= ( const BitSet< S, E, size_ > &  rhs)
inline

Remove elements from set.

◆ operator==() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::operator== ( const BitSet< S, E, size_ > &  other) const
inline

Compare this set with another for equality.

◆ operator==() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::operator== ( e) const
inline

Determine if set consists of only the one given element.

◆ operator[]() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitRef BitSet< S, E, size_ >::operator[] ( e)
inline

Read/write [] operator.

Parameters
eElement to read or write
Return values
BitRefTemporary object used to do the read or write

This returns a temporary BitRef object to support assignment operations such as set[x] = value

◆ operator[]() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::operator[] ( e) const
inline

Read-only [] operator.

Parameters
eElement to test for
Return values
booltrue if given element is in the set

◆ operator^=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::operator^= ( const BitSet< S, E, size_ > &  rhs)
inline

XOR - toggle state of bits using another set.

◆ operator|=()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::operator|= ( const BitSet< S, E, size_ > &  rhs)
inline

Union: Add elements to set.

◆ operator~()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr BitSet BitSet< S, E, size_ >::operator~ ( ) const
inlineconstexpr

Obtain a set containing all elements not in this one.

◆ printTo()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
size_t BitSet< S, E, size_ >::printTo ( Print p,
const String separator = ", " 
) const
inline

Class template to print the contents of a BitSet to a String.

Note
Requires an implementation of toString(E)

◆ reset() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::reset ( )
inline

Remove all values from the set.

◆ reset() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::reset ( e)
inline

Clear the state of the given bit (i.e. remove it from the set)

◆ set() [1/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::set ( )
inline

Add all possible values to the bit set.

◆ set() [2/2]

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
BitSet& BitSet< S, E, size_ >::set ( e,
bool  state = true 
)
inline

Set the state of the given bit (i.e. add to or remove from the set)

Parameters
eElement to change
statetrue to add the element, false to remove it

◆ size()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
static constexpr size_t BitSet< S, E, size_ >::size ( )
inlinestaticconstexpr

Get the number of possible elements in the set.

◆ test()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
bool BitSet< S, E, size_ >::test ( e) const
inline

Test to see if given element is in the set.

◆ value()

template<typename S , typename E , size_t size_ = sizeof(S) * 8>
constexpr S BitSet< S, E, size_ >::value ( ) const
inlineconstexpr

Get stored bits for this bitset.


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