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
inline

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 322 of file bitset_dynamic.h.

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

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

+ Here is the call graph for this function: