FastLED 3.9.15
Loading...
Searching...
No Matches

◆ find_first()

fl::i32 fl::bitset_dynamic::find_first ( bool test_value,
fl::u32 offset = 0 ) const
inlinenoexcept

Finds the first bit that matches the test value.

Returns the index of the first matching bit, or -1 if none found.

Parameters
test_valueThe value to search for (true or false)
offsetStarting position to search from (default: 0)

Definition at line 315 of file bitset_dynamic.h.

315 {
316 // If offset is beyond our size, no match possible
317 if (offset >= _size) {
318 return -1;
319 }
320
321 // Calculate which block to start from
322 fl::u32 start_block = offset / bits_per_block;
323 fl::u32 start_bit = offset % bits_per_block;
324
325 for (fl::u32 block_idx = start_block; block_idx < _block_count; ++block_idx) {
326 block_type current_block = _blocks[block_idx];
327
328 // For the last block, we need to mask out unused bits
329 if (block_idx == _block_count - 1 && _size % bits_per_block != 0) {
330 const fl::u32 valid_bits = _size % bits_per_block;
331 block_type mask = (valid_bits == bits_per_block)
332 ? static_cast<block_type>(~block_type(0))
333 : static_cast<block_type>(((block_type(1) << valid_bits) - 1));
334 current_block &= mask;
335 }
336
337 // If looking for false bits, invert the block
338 if (!test_value) {
339 current_block = ~current_block;
340 }
341
342 // For the first block, mask out bits before the offset
343 if (block_idx == start_block && start_bit > 0) {
344 current_block &= ~static_cast<block_type>(((block_type(1) << start_bit) - 1));
345 }
346
347 // If there are any matching bits in this block
348 if (current_block != 0) {
349 // Find the first set bit
350 fl::u32 bit_pos = static_cast<fl::u32>(__builtin_ctz(current_block));
351 fl::u32 absolute_pos = block_idx * bits_per_block + bit_pos;
352
353 // Make sure we haven't gone past the end of the bitset
354 if (absolute_pos < _size) {
355 return static_cast<fl::i32>(absolute_pos);
356 }
357 }
358 }
359
360 return -1; // No matching bit found
361 }
block_type * _blocks
static constexpr fl::u32 bits_per_block
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)

References _block_count, _blocks, _size, bits_per_block, and offset().

+ Here is the call graph for this function: