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

◆ find_slot()

template<typename Key, typename T, typename Hash = Hash<Key>, typename KeyEqual = EqualTo<Key>, int INLINED_COUNT = FASTLED_HASHMAP_INLINED_COUNT>
pair< size_t, bool > fl::HashMap< Key, T, Hash, KeyEqual, INLINED_COUNT >::find_slot ( const Key & key) const
inlineprivate

Definition at line 333 of file hash_map.h.

333 {
334 const size_t cap = _buckets.size();
335 const size_t mask = cap - 1;
336 const size_t h = _hash(key) & mask;
337 size_t first_tomb = npos;
338
339 if (cap <= 8) {
340 // linear probing
341 for (size_t i = 0; i < cap; ++i) {
342 const size_t idx = (h + i) & mask;
343
344 if (is_empty(idx))
345 return {first_tomb != npos ? first_tomb : idx, true};
346 if (is_deleted(idx)) {
347 if (first_tomb == npos)
348 first_tomb = idx;
349 } else if (is_occupied(idx) && _equal(_buckets[idx].key, key)) {
350 return {idx, false};
351 }
352 }
353 } else {
354 // quadratic probing up to 8 tries
355 size_t i = 0;
356 for (; i < 8; ++i) {
357 const size_t idx = (h + i + i * i) & mask;
358
359 if (is_empty(idx))
360 return {first_tomb != npos ? first_tomb : idx, true};
361 if (is_deleted(idx)) {
362 if (first_tomb == npos)
363 first_tomb = idx;
364 } else if (is_occupied(idx) && _equal(_buckets[idx].key, key)) {
365 return {idx, false};
366 }
367 }
368 // fallback to linear for the rest
369 for (; i < cap; ++i) {
370 const size_t idx = (h + i) & mask;
371
372 if (is_empty(idx))
373 return {first_tomb != npos ? first_tomb : idx, true};
374 if (is_deleted(idx)) {
375 if (first_tomb == npos)
376 first_tomb = idx;
377 } else if (is_occupied(idx) && _equal(_buckets[idx].key, key)) {
378 return {idx, false};
379 }
380 }
381 }
382
383 return {npos, false};
384 }
static constexpr size_t npos
Definition hash_map.h:291
bool is_empty(size_t idx) const
Definition hash_map.h:298
KeyEqual _equal
Definition hash_map.h:583
bool is_occupied(size_t idx) const
Definition hash_map.h:294
fl::vector_inlined< Entry, INLINED_COUNT > _buckets
Definition hash_map.h:576
bool is_deleted(size_t idx) const
Definition hash_map.h:296

Referenced by fl::HashMap< Key, T, Hash, KeyEqual >::insert(), and fl::HashMap< Key, T, Hash, KeyEqual >::operator[]().

+ Here is the caller graph for this function: