ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
esphome::FiniteSetMask< ValueType, BitPolicy > Class Template Reference

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}
 

Detailed Description

template<typename ValueType, typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
class esphome::FiniteSetMask< ValueType, BitPolicy >

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:

  • using mask_t = <uint8_t|uint16_t|uint32_t> // Bitmask storage type
  • static constexpr int MAX_BITS // Maximum number of bits
  • static constexpr unsigned to_bit(ValueType) // Convert value to bit position
  • static constexpr ValueType from_bit(unsigned) // Convert bit position to value

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:

  • Policy-based design allows custom bit mappings without template specialization
  • Iterator converts bit positions to actual values during traversal
  • All operations are constexpr-compatible for compile-time initialization
  • Drop-in replacement for std::set<ValueType> with simpler API

Definition at line 56 of file finite_set_mask.h.

Member Typedef Documentation

◆ bitmask_t

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
using esphome::FiniteSetMask< ValueType, BitPolicy >::bitmask_t = typename BitPolicy::mask_t

Definition at line 58 of file finite_set_mask.h.

Constructor & Destructor Documentation

◆ FiniteSetMask() [1/2]

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
esphome::FiniteSetMask< ValueType, BitPolicy >::FiniteSetMask ( )
constexprdefault

◆ FiniteSetMask() [2/2]

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
esphome::FiniteSetMask< ValueType, BitPolicy >::FiniteSetMask ( std::initializer_list< ValueType > values)
inlineconstexpr

Construct from initializer list: {VALUE1, VALUE2, ...}.

Definition at line 63 of file finite_set_mask.h.

Member Function Documentation

◆ begin()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
Iterator esphome::FiniteSetMask< ValueType, BitPolicy >::begin ( ) const
inlineconstexpr

Definition at line 139 of file finite_set_mask.h.

◆ clear()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
void esphome::FiniteSetMask< ValueType, BitPolicy >::clear ( )
inlineconstexpr

Clear all values from the set.

Definition at line 83 of file finite_set_mask.h.

◆ count()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
size_t esphome::FiniteSetMask< ValueType, BitPolicy >::count ( ValueType value) const
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.

◆ empty()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
bool esphome::FiniteSetMask< ValueType, BitPolicy >::empty ( ) const
inlineconstexpr

Check if the set is empty.

Definition at line 105 of file finite_set_mask.h.

◆ end()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
Iterator esphome::FiniteSetMask< ValueType, BitPolicy >::end ( ) const
inlineconstexpr

Definition at line 140 of file finite_set_mask.h.

◆ erase()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
void esphome::FiniteSetMask< ValueType, BitPolicy >::erase ( ValueType value)
inlineconstexpr

Remove a value from the set (std::set compatibility)

Definition at line 80 of file finite_set_mask.h.

◆ find_next_set_bit()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
static constexpr int esphome::FiniteSetMask< ValueType, BitPolicy >::find_next_set_bit ( bitmask_t mask,
int start_bit )
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.

◆ first_value_from_mask()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
static constexpr ValueType esphome::FiniteSetMask< ValueType, BitPolicy >::first_value_from_mask ( bitmask_t mask)
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.

◆ get_mask()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
bitmask_t esphome::FiniteSetMask< ValueType, BitPolicy >::get_mask ( ) const
inlineconstexpr

Get the raw bitmask value for optimized operations.

Definition at line 143 of file finite_set_mask.h.

◆ insert() [1/2]

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
void esphome::FiniteSetMask< ValueType, BitPolicy >::insert ( std::initializer_list< ValueType > values)
inlineconstexpr

Add multiple values from initializer list.

Definition at line 73 of file finite_set_mask.h.

◆ insert() [2/2]

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
void esphome::FiniteSetMask< ValueType, BitPolicy >::insert ( ValueType value)
inlineconstexpr

Add a single value to the set (std::set compatibility)

Definition at line 70 of file finite_set_mask.h.

◆ mask_contains()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
static constexpr bool esphome::FiniteSetMask< ValueType, BitPolicy >::mask_contains ( bitmask_t mask,
ValueType value )
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.

◆ size()

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
size_t esphome::FiniteSetMask< ValueType, BitPolicy >::size ( ) const
inlineconstexpr

Count the number of values in the set.

Definition at line 92 of file finite_set_mask.h.

Field Documentation

◆ mask_

template<typename ValueType , typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
bitmask_t esphome::FiniteSetMask< ValueType, BitPolicy >::mask_ {0}
protected

Definition at line 168 of file finite_set_mask.h.


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