[map] Change bucket_for_hash() to item_for_hash()

This commit is contained in:
Behdad Esfahbod 2022-11-18 16:31:27 -07:00
parent d012f9a9b3
commit f1d716871d
1 changed files with 13 additions and 13 deletions

View File

@ -322,23 +322,23 @@ struct hb_hashmap_t
{ {
if (unlikely (!successful)) return false; if (unlikely (!successful)) return false;
if (unlikely ((occupancy + occupancy / 2) >= mask && !resize ())) return false; if (unlikely ((occupancy + occupancy / 2) >= mask && !resize ())) return false;
unsigned int i = bucket_for_hash (key, hash); item_t &item = item_for_hash (key, hash);
if (is_delete && items[i].key != key) if (is_delete && item.key != key)
return true; /* Trying to delete non-existent key. */ return true; /* Trying to delete non-existent key. */
if (items[i].is_used ()) if (item.is_used ())
{ {
occupancy--; occupancy--;
if (!items[i].is_tombstone ()) if (!item.is_tombstone ())
population--; population--;
} }
items[i].key = key; item.key = key;
items[i].value = std::forward<VV> (value); item.value = std::forward<VV> (value);
items[i].hash = hash; item.hash = hash;
items[i].set_used (true); item.set_used (true);
items[i].set_tombstone (is_delete); item.set_tombstone (is_delete);
occupancy++; occupancy++;
if (!is_delete) if (!is_delete)
@ -349,10 +349,10 @@ struct hb_hashmap_t
item_t& item_for (const K &key) const item_t& item_for (const K &key) const
{ {
return items[bucket_for_hash (key, hb_hash (key))]; return item_for_hash (key, hb_hash (key));
} }
unsigned int bucket_for_hash (const K &key, uint32_t hash) const item_t& item_for_hash (const K &key, uint32_t hash) const
{ {
hash &= 0x3FFFFFFF; // We only store lower 30bit of hash hash &= 0x3FFFFFFF; // We only store lower 30bit of hash
unsigned int i = hash % prime; unsigned int i = hash % prime;
@ -361,12 +361,12 @@ struct hb_hashmap_t
while (items[i].is_used ()) while (items[i].is_used ())
{ {
if (items[i].hash == hash && items[i] == key) if (items[i].hash == hash && items[i] == key)
return i; return items[i];
if (tombstone == (unsigned) -1 && items[i].is_tombstone ()) if (tombstone == (unsigned) -1 && items[i].is_tombstone ())
tombstone = i; tombstone = i;
i = (i + ++step) & mask; i = (i + ++step) & mask;
} }
return tombstone == (unsigned) -1 ? i : tombstone; return items[tombstone == (unsigned) -1 ? i : tombstone];
} }
static unsigned int prime_for (unsigned int shift) static unsigned int prime_for (unsigned int shift)