Skip to content

Commit

Permalink
update SDL_hashtable.c from SDL3
Browse files Browse the repository at this point in the history
Fixes: #408
  • Loading branch information
sezero committed Oct 15, 2024
1 parent 45faf5a commit aab7cc5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/SDL_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SDL_HashTable *SDL_CreateHashTable(void *data,
return table;
}

static SDL_INLINE Uint32 calc_hash(const SDL_HashTable *restrict table, const void *key)
static SDL_INLINE Uint32 calc_hash(const SDL_HashTable *table, const void *key)
{
const Uint32 BitMixer = 0x9E3779B1u;
return table->hash(key, table->data) * BitMixer;
Expand All @@ -112,7 +112,7 @@ static SDL_INLINE Uint32 get_probe_length(Uint32 zero_idx, Uint32 actual_idx, Ui
return actual_idx - zero_idx;
}

static SDL_HashItem *find_item(const SDL_HashTable *restrict ht, const void *key, Uint32 hash, Uint32 *restrict i, Uint32 *restrict probe_len)
static SDL_HashItem *find_item(const SDL_HashTable *ht, const void *key, Uint32 hash, Uint32 *i, Uint32 *probe_len)
{
Uint32 hash_mask = ht->hash_mask;
Uint32 max_probe_len = ht->max_probe_len;
Expand Down Expand Up @@ -146,14 +146,14 @@ static SDL_HashItem *find_item(const SDL_HashTable *restrict ht, const void *key
}
}

static SDL_HashItem *find_first_item(const SDL_HashTable *restrict ht, const void *key, Uint32 hash)
static SDL_HashItem *find_first_item(const SDL_HashTable *ht, const void *key, Uint32 hash)
{
Uint32 i = hash & ht->hash_mask;
Uint32 probe_len = 0;
return find_item(ht, key, hash, &i, &probe_len);
}

static SDL_HashItem *insert_item(SDL_HashItem *restrict item_to_insert, SDL_HashItem *restrict table, Uint32 hash_mask, Uint32 *max_probe_len_ptr)
static SDL_HashItem *insert_item(SDL_HashItem *item_to_insert, SDL_HashItem *table, Uint32 hash_mask, Uint32 *max_probe_len_ptr)
{
Uint32 idx = item_to_insert->hash & hash_mask;
SDL_HashItem temp_item, *target = NULL;
Expand Down Expand Up @@ -213,7 +213,7 @@ static SDL_HashItem *insert_item(SDL_HashItem *restrict item_to_insert, SDL_Hash
return target;
}

static void delete_item(SDL_HashTable *restrict ht, SDL_HashItem *item)
static void delete_item(SDL_HashTable *ht, SDL_HashItem *item)
{
Uint32 hash_mask = ht->hash_mask;
SDL_HashItem *table = ht->table;
Expand Down Expand Up @@ -241,7 +241,7 @@ static void delete_item(SDL_HashTable *restrict ht, SDL_HashItem *item)
}
}

static bool resize(SDL_HashTable *restrict ht, Uint32 new_size)
static bool resize(SDL_HashTable *ht, Uint32 new_size)
{
SDL_HashItem *old_table = ht->table;
Uint32 old_size = ht->hash_mask + 1;
Expand All @@ -267,7 +267,7 @@ static bool resize(SDL_HashTable *restrict ht, Uint32 new_size)
return true;
}

static bool maybe_resize(SDL_HashTable *restrict ht)
static bool maybe_resize(SDL_HashTable *ht)
{
Uint32 capacity = ht->hash_mask + 1;

Expand All @@ -276,7 +276,7 @@ static bool maybe_resize(SDL_HashTable *restrict ht)
}

Uint32 max_load_factor = 217; // range: 0-255; 217 is roughly 85%
Uint32 resize_threshold = (max_load_factor * (Uint64)capacity) >> 8;
Uint32 resize_threshold = (Uint32)((max_load_factor * (Uint64)capacity) >> 8);

if (ht->num_occupied_slots > resize_threshold) {
return resize(ht, capacity * 2);
Expand All @@ -285,7 +285,7 @@ static bool maybe_resize(SDL_HashTable *restrict ht)
return true;
}

bool SDL_InsertIntoHashTable(SDL_HashTable *restrict table, const void *key, const void *value)
bool SDL_InsertIntoHashTable(SDL_HashTable *table, const void *key, const void *value)
{
SDL_HashItem *item;
Uint32 hash;
Expand All @@ -307,6 +307,7 @@ bool SDL_InsertIntoHashTable(SDL_HashTable *restrict table, const void *key, con
new_item.value = value;
new_item.hash = hash;
new_item.live = true;
new_item.probe_len = 0;

table->num_occupied_slots++;

Expand Down Expand Up @@ -455,7 +456,7 @@ bool SDL_HashTableEmpty(SDL_HashTable *table)
return !(table && table->num_occupied_slots);
}

static void nuke_all(SDL_HashTable *restrict table)
static void nuke_all(SDL_HashTable *table)
{
void *data = table->data;
SDL_HashItem *end = table->table + (table->hash_mask + 1);
Expand All @@ -468,7 +469,7 @@ static void nuke_all(SDL_HashTable *restrict table)
}
}

void SDL_EmptyHashTable(SDL_HashTable *restrict table)
void SDL_EmptyHashTable(SDL_HashTable *table)
{
if (table) {
if (table->nuke) {
Expand Down

0 comments on commit aab7cc5

Please sign in to comment.