|
ESPHome 2025.12.0-dev
|
Generic bitmask for storing a finite set of discrete values efficiently. More...
#include <finite_set_mask.h>
Data Structures | |
| class | Iterator |
| Iterator support for range-based for loops and API encoding Iterates over set bits and converts bit positions to values Optimization: removes bits from mask as we iterate. More... | |
Public Types | |
| using | bitmask_t = typename BitPolicy::mask_t |
Public Member Functions | |
| constexpr | FiniteSetMask ()=default |
| constexpr | FiniteSetMask (std::initializer_list< ValueType > values) |
| Construct from initializer list: {VALUE1, VALUE2, ...}. | |
| constexpr void | insert (ValueType value) |
| Add a single value to the set (std::set compatibility) | |
| constexpr void | insert (std::initializer_list< ValueType > values) |
| Add multiple values from initializer list. | |
| constexpr void | erase (ValueType value) |
| Remove a value from the set (std::set compatibility) | |
| constexpr void | clear () |
| Clear all values from the set. | |
| constexpr size_t | count (ValueType value) const |
| Check if the set contains a specific value (std::set compatibility) Returns 1 if present, 0 if not (same as std::set for unique elements) | |
| constexpr size_t | size () const |
| Count the number of values in the set. | |
| constexpr bool | empty () const |
| Check if the set is empty. | |
| constexpr Iterator | begin () const |
| constexpr Iterator | end () const |
| constexpr bitmask_t | get_mask () const |
| Get the raw bitmask value for optimized operations. | |
Static Public Member Functions | |
| static constexpr bool | mask_contains (bitmask_t mask, ValueType value) |
| Check if a specific value is present in a raw bitmask Useful for checking intersection results without creating temporary objects. | |
| static constexpr ValueType | first_value_from_mask (bitmask_t mask) |
| Get the first value from a raw bitmask Used for optimizing intersection logic (e.g., "pick first suitable mode") | |
| static constexpr int | find_next_set_bit (bitmask_t mask, int start_bit) |
| Find the next set bit in a bitmask starting from a given position Returns the bit position, or MAX_BITS if no more bits are set. | |
Protected Attributes | |
| bitmask_t | mask_ {0} |
Generic bitmask for storing a finite set of discrete values efficiently.
Replaces std::set<ValueType> to eliminate red-black tree overhead (~586 bytes per instantiation).
Template parameters: ValueType: The type to store (typically enum, but can be any discrete bounded type) BitPolicy: Policy class defining bit mapping and mask type (defaults to DefaultBitPolicy)
BitPolicy requirements:
Example usage (1:1 mapping - climate enums): // For contiguous enums starting at 0, use DefaultBitPolicy using ClimateModeMask = FiniteSetMask<ClimateMode, DefaultBitPolicy<ClimateMode, CLIMATE_MODE_AUTO + 1>>; ClimateModeMask modes({CLIMATE_MODE_HEAT, CLIMATE_MODE_COOL}); if (modes.count(CLIMATE_MODE_HEAT)) { ... } for (auto mode : modes) { ... }
Example usage (custom mapping - ColorMode): // For custom mappings, define a custom BitPolicy // See esphome/components/light/color_mode.h for complete example
Design notes:
Definition at line 56 of file finite_set_mask.h.
| using esphome::FiniteSetMask< ValueType, BitPolicy >::bitmask_t = typename BitPolicy::mask_t |
Definition at line 58 of file finite_set_mask.h.
|
constexprdefault |
|
inlineconstexpr |
Construct from initializer list: {VALUE1, VALUE2, ...}.
Definition at line 63 of file finite_set_mask.h.
|
inlineconstexpr |
Definition at line 139 of file finite_set_mask.h.
|
inlineconstexpr |
Clear all values from the set.
Definition at line 83 of file finite_set_mask.h.
|
inlineconstexpr |
Check if the set contains a specific value (std::set compatibility) Returns 1 if present, 0 if not (same as std::set for unique elements)
Definition at line 87 of file finite_set_mask.h.
|
inlineconstexpr |
Check if the set is empty.
Definition at line 105 of file finite_set_mask.h.
|
inlineconstexpr |
Definition at line 140 of file finite_set_mask.h.
|
inlineconstexpr |
Remove a value from the set (std::set compatibility)
Definition at line 80 of file finite_set_mask.h.
|
inlinestaticconstexpr |
Find the next set bit in a bitmask starting from a given position Returns the bit position, or MAX_BITS if no more bits are set.
Definition at line 159 of file finite_set_mask.h.
|
inlinestaticconstexpr |
Get the first value from a raw bitmask Used for optimizing intersection logic (e.g., "pick first suitable mode")
Definition at line 153 of file finite_set_mask.h.
|
inlineconstexpr |
Get the raw bitmask value for optimized operations.
Definition at line 143 of file finite_set_mask.h.
|
inlineconstexpr |
Add multiple values from initializer list.
Definition at line 73 of file finite_set_mask.h.
|
inlineconstexpr |
Add a single value to the set (std::set compatibility)
Definition at line 70 of file finite_set_mask.h.
|
inlinestaticconstexpr |
Check if a specific value is present in a raw bitmask Useful for checking intersection results without creating temporary objects.
Definition at line 147 of file finite_set_mask.h.
|
inlineconstexpr |
Count the number of values in the set.
Definition at line 92 of file finite_set_mask.h.
|
protected |
Definition at line 168 of file finite_set_mask.h.