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

◆ rehash_inline_no_resize()

template<typename Key, typename T, typename Hash = Hash<Key>, typename KeyEqual = EqualTo<Key>, int INLINED_COUNT = FASTLED_HASHMAP_INLINED_COUNT>
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_NULL_DEREFERENCE void fl::HashMap< Key, T, Hash, KeyEqual, INLINED_COUNT >::rehash_inline_no_resize ( )
inlineprivate

Definition at line 612 of file hash_map.h.

612 {
613 // filter out tombstones and compact
614 fl::size cap = _buckets.size();
615 fl::size pos = 0;
616 // compact live elements to the left.
617 for (fl::size i = 0; i < cap; ++i) {
618 if (is_occupied(i)) {
619 if (pos != i) {
621 mark_empty(i);
623 }
624 ++pos;
625 } else if (is_deleted(i)) {
626 mark_empty(i);
627 }
628 }
629
630 fl::bitset<1024> occupied; // Preallocate a bitset of size 1024
631 // swap the components, this will happen at most N times,
632 // use the occupied bitset to track which entries are occupied
633 // in the array rather than just copied in.
635 for (fl::size i = 0; i < _size; ++i) {
636 const bool already_finished = occupied.test(i);
637 if (already_finished) {
638 continue;
639 }
640 auto &e = _buckets[i];
641 // FASTLED_ASSERT(e.state == EntryState::Occupied,
642 // "HashMap::rehash_inline_no_resize: invalid
643 // state");
644
646 if (idx == npos()) {
647 // no more space
649 false, "HashMap::rehash_inline_no_resize: invalid index at "
650 << idx << " which is " << npos());
651 return;
652 }
653 // if idx < pos then we are moving the entry to a new location
655 "HashMap::rehash_inline_no_resize: invalid tmp");
656 if (idx >= _size) {
657 // directly copy it now
658 _buckets[idx] = e;
659 continue;
660 }
661 tmp = e;
662 occupied.set(idx);
663 _buckets[idx] = *tmp.ptr();
664 while (!tmp.empty()) {
665 // we have to find a place for temp.
666 // find new position for tmp.
667 auto key = tmp.ptr()->key;
670 if (new_idx == npos()) {
671 // no more space
673 false,
674 "HashMap::rehash_inline_no_resize: invalid index at "
675 << new_idx << " which is " << npos());
676 return;
677 }
678 occupied.set(new_idx);
679 if (new_idx < _size) {
680 // we have to swap the entry at new_idx with tmp
682 _buckets[new_idx] = *tmp.ptr();
683 tmp = tmp2;
684 } else {
685 // we can just move tmp to new_idx
686 _buckets[new_idx] = *tmp.ptr();
687 tmp.reset();
688 }
689 }
691 occupied.test(i),
692 "HashMap::rehash_inline_no_resize: invalid occupied at " << i);
694 tmp.empty(), "HashMap::rehash_inline_no_resize: invalid tmp at " << i);
695 }
696 // Reset tombstones count since we've cleared all deleted entries
697 _tombstones = 0;
698 }
fl::size _size
Definition hash_map.h:702
static fl::size npos()
Definition hash_map.h:408
void mark_empty(fl::size idx)
Definition hash_map.h:431
fl::size find_unoccupied_index_using_bitset(const Key &key, const fl::bitset< 1024 > &occupied_set) const
Definition hash_map.h:547
bool is_deleted(fl::size idx) const
Definition hash_map.h:415
bool is_occupied(fl::size idx) const
Definition hash_map.h:413
bool empty() const
Definition hash_map.h:402
fl::size _tombstones
Definition hash_map.h:703
FL_DISABLE_WARNING_POP fl::vector_inlined< Entry, INLINED_COUNT > _buckets
Definition hash_map.h:701
void mark_occupied(fl::size idx)
Definition hash_map.h:421