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

◆ operator[]()

template<typename Key, typename T, typename Hash = Hash<Key>, typename KeyEqual = EqualTo<Key>, int INLINED_COUNT = FASTLED_HASHMAP_INLINED_COUNT>
T & fl::HashMap< Key, T, Hash, KeyEqual, INLINED_COUNT >::operator[] ( const Key & key)
inline

Definition at line 355 of file hash_map.h.

355 {
357 bool is_new;
358
360 idx = p.first;
361 is_new = p.second;
362
363 // Check if find_slot failed to find a valid slot (HashMap is full)
364 if (idx == npos()) {
365 // Need to resize to make room
366 if (needs_rehash()) {
367 // if half the buckets are tombstones, rehash inline to prevent
368 // memory growth. Otherwise, double the size.
369 if (_tombstones >= _buckets.size() / 2) {
371 } else {
372 rehash(_buckets.size() * 2);
373 }
374 } else {
375 // Force a rehash with double size if needs_rehash() doesn't detect the issue
376 rehash(_buckets.size() * 2);
377 }
378
379 // Try find_slot again after resize
380 p = find_slot(key);
381 idx = p.first;
382 is_new = p.second;
383
384 // If still npos() after resize, something is seriously wrong
385 if (idx == npos()) {
386 // This should never happen after a successful resize
387 static T default_value{};
388 return default_value;
389 }
390 }
391
392 if (is_new) {
393 _buckets[idx].key = key;
394 _buckets[idx].value = T{};
396 ++_size;
397 }
398 return _buckets[idx].value;
399 }
fl::size _size
Definition hash_map.h:702
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_NULL_DEREFERENCE void rehash_inline_no_resize()
Definition hash_map.h:612
static fl::size npos()
Definition hash_map.h:408
pair< fl::size, bool > find_slot(const Key &key) const
Definition hash_map.h:452
void rehash(fl::size new_cap)
Definition hash_map.h:587
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
bool needs_rehash() const
Definition hash_map.h:229