hb_object_header_t header; bool successful; /* Allocations successful */ unsigned int population; /* Not including tombstones. */ unsigned int occupancy; /* Including tombstones. */ unsigned int mask; unsigned int prime;
item_t *items;
friend void swap (hb_hashmap_t& a, hb_hashmap_t& b)
{
if (unlikely (!a.successful || !b.successful)) return;
hb_swap (a.population, b.population);
hb_swap (a.occupancy, b.occupancy);
hb_swap (a.mask, b.mask);
hb_swap (a.prime, b.prime);
hb_swap (a.items, b.items);
} void init_shallow ()
{
successful = true;
population = occupancy = 0;
mask = 0;
prime = 0;
items = nullptr;
} void init ()
{
hb_object_init (this);
init_shallow ();
} void fini_shallow ()
{
if (likely (items)) { unsigned size = mask + 1;
for (unsigned i = 0; i < size; i++)
items[i].~item_t ();
hb_free (items);
items = nullptr;
}
population = occupancy = 0;
} void fini ()
{
hb_object_fini (this);
fini_shallow ();
}
/* Switch to new, empty, array. */
population = occupancy = 0;
mask = new_size - 1;
prime = prime_for (power);
items = new_items;
/* Insert back old items. */
if (old_items)
for (unsigned int i = 0; i < old_size; i++)
{
if (old_items[i].is_real ())
{
set_with_hash (old_items[i].key,
old_items[i].hash,
std::move (old_items[i].value));
}
old_items[i].~item_t ();
}
unsigned int bucket_for (const K &key) const
{ return bucket_for_hash (key, hb_hash (key));
}
unsigned int bucket_for_hash (const K &key, uint32_t hash) const
{
hash &= 0x3FFFFFFF; // We only store lower 30bit of hash unsigned int i = hash % prime; unsigned int step = 0; unsigned int tombstone = (unsigned) -1; while (items[i].is_used ())
{
if (items[i].hash == hash && items[i] == key) return i;
if (tombstone == (unsigned) -1 && items[i].is_tombstone ())
tombstone = i;
i = (i + ++step) & mask;
} return tombstone == (unsigned) -1 ? i : tombstone;
}
staticunsigned int prime_for (unsigned int shift)
{ /* Following comment and table copied from glib. */ /* Each table size has an associated prime modulo (the first prime *lowerthanthetablesize)usedtofindtheinitialbucket.Probing *thenworksmodulo2^n.Theprimemoduloisnecessarytogeta *gooddistributionwithpoorhashfunctions.
*/ /* Not declaring static to make all kinds of compilers happy... */ /*static*/ const unsigned int prime_mod [32] =
{ 1, /* For 1 << 0 */ 2, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, /* For 1 << 16 */ 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859, 134217689, 268435399, 536870909, 1073741789, 2147483647/* For 1 << 31 */
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.