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... | |
BitSet & | flip () |
Flip all bits in the set. More... | |
BitSet & | flip (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... | |
BitSet & | operator+= (const BitSet &rhs) |
Union: Add elements to set. More... | |
BitSet & | operator-= (const BitSet &rhs) |
Remove elements from set. More... | |
BitSet & | operator&= (const BitSet &rhs) |
Intersection: Leave only elements common to both sets. More... | |
BitSet & | operator|= (const BitSet &rhs) |
Union: Add elements to set. More... | |
BitSet & | operator^= (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... | |
BitSet & | set () |
Add all possible values to the bit set. More... | |
BitSet & | set (E e, bool state=true) |
Set the state of the given bit (i.e. add to or remove from the set) More... | |
BitSet & | reset () |
Remove all values from the set. More... | |
BitSet & | reset (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
-
S Storage 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. E Element 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 auint8_t
storage type so can have up to 8 possible values. However, theFruit
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
using BitSet< S, E, size_ >::IfHelperType = void (BitSet::*)() const |
Constructor & Destructor Documentation
◆ BitSet() [1/4]
|
constexprdefault |
Construct empty set.
◆ BitSet() [2/4]
|
inlineconstexpr |
Copy constructor.
- Parameters
-
bitset The set to copy
◆ BitSet() [3/4]
|
inlineconstexpr |
Construct from a raw set of bits.
- Parameters
-
value Integral type whose bits will be interpreted as set{E}
◆ BitSet() [4/4]
|
inlineconstexpr |
Construct set containing a single value.
- Parameters
-
e Value to place in our new BitSet object
Member Function Documentation
◆ all()
|
inline |
Test if set contains all possible values.
◆ any() [1/2]
|
inline |
Determine if set contains any values.
◆ any() [2/2]
|
inline |
Determine if set contains any values from another set i.e. intersection != [].
◆ bitVal()
|
inlinestaticconstexpr |
Get the bitmask corresponding to a given value.
◆ count()
|
inline |
Get the number of elements in the set, i.e. bits set to 1.
◆ domain()
|
inlinestaticconstexpr |
Get the set of all possible values.
◆ flip() [1/2]
|
inline |
Flip all bits in the set.
◆ flip() [2/2]
|
inline |
Flip state of the given bit.
◆ none()
|
inline |
Test if set is empty.
◆ operator IfHelperType()
|
inline |
◆ operator S()
|
inlineexplicitconstexpr |
Allow casts from the native storage type to get a numeric result for this set.
◆ operator!=()
|
inline |
Compare this set with another for inequality.
◆ operator&=()
|
inline |
Intersection: Leave only elements common to both sets.
◆ operator+=()
|
inline |
Union: Add elements to set.
◆ operator-=()
|
inline |
Remove elements from set.
◆ operator==() [1/2]
|
inline |
Compare this set with another for equality.
◆ operator==() [2/2]
|
inline |
Determine if set consists of only the one given element.
◆ operator[]() [1/2]
◆ operator[]() [2/2]
|
inline |
Read-only [] operator.
- Parameters
-
e Element to test for
- Return values
-
bool true if given element is in the set
◆ operator^=()
|
inline |
XOR - toggle state of bits using another set.
◆ operator|=()
|
inline |
Union: Add elements to set.
◆ operator~()
|
inlineconstexpr |
Obtain a set containing all elements not in this one.
◆ printTo()
|
inline |
Class template to print the contents of a BitSet to a String.
- Note
- Requires an implementation of
toString(E)
◆ reset() [1/2]
|
inline |
Remove all values from the set.
◆ reset() [2/2]
|
inline |
Clear the state of the given bit (i.e. remove it from the set)
◆ set() [1/2]
|
inline |
Add all possible values to the bit set.
◆ set() [2/2]
|
inline |
Set the state of the given bit (i.e. add to or remove from the set)
- Parameters
-
e Element to change state true to add the element, false to remove it
◆ size()
|
inlinestaticconstexpr |
Get the number of possible elements in the set.
◆ test()
|
inline |
Test to see if given element is in the set.
◆ value()
|
inlineconstexpr |
Get stored bits for this bitset.
The documentation for this class was generated from the following file: