From 0aba2c97db60e074323f7f86a73e1b1c16d32197 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 14 Oct 2023 13:23:03 -0400 Subject: [PATCH] hashtable: SDL_IterateHashTable might as well provide both key and value. And SDL_IterateHashTableKey is only necessary for stackable hashtables, since non-stackable ones can either iterate each unique key/value pair with SDL_IterateHashTable, or get a specific key/value pair by using SDL_FindInHashTable. --- src/SDL_hashtable.c | 10 ++++++---- src/SDL_hashtable.h | 7 +++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/SDL_hashtable.c b/src/SDL_hashtable.c index a5fc0f09e6..e2714d1321 100644 --- a/src/SDL_hashtable.c +++ b/src/SDL_hashtable.c @@ -161,9 +161,9 @@ SDL_bool SDL_RemoveFromHashTable(SDL_HashTable *table, const void *key) return SDL_FALSE; } -SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void *key, const void **_value, void **iter) +SDL_bool SDL_IterateHashTableKey(const SDL_HashTable *table, const void *key, const void **_value, void **iter) { - SDL_HashItem *item = iter ? ((SDL_HashItem *) *iter)->next : table->table[calc_hash(table, key)]; + SDL_HashItem *item = *iter ? ((SDL_HashItem *) *iter)->next : table->table[calc_hash(table, key)]; while (item != NULL) { if (table->keymatch(key, item->key, table->data)) { @@ -180,7 +180,7 @@ SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void *key, const return SDL_FALSE; } -SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key, void **iter) +SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void **_key, const void **_value, void **iter) { SDL_HashItem *item = (SDL_HashItem *) *iter; Uint32 idx = 0; @@ -189,7 +189,7 @@ SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key, const SDL_HashItem *orig = item; item = item->next; if (item == NULL) { - idx = calc_hash(table, orig->key) + 1; + idx = calc_hash(table, orig->key) + 1; // !!! FIXME: we probably shouldn't rehash each time. } } @@ -204,7 +204,9 @@ SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key, } *_key = item->key; + *_value = item->value; *iter = item; + return SDL_TRUE; } diff --git a/src/SDL_hashtable.h b/src/SDL_hashtable.h index eb8d7ba3ca..44a1788724 100644 --- a/src/SDL_hashtable.h +++ b/src/SDL_hashtable.h @@ -42,8 +42,11 @@ SDL_bool SDL_RemoveFromHashTable(SDL_HashTable *table, const void *key); SDL_bool SDL_FindInHashTable(const SDL_HashTable *table, const void *key, const void **_value); SDL_bool SDL_HashTableEmpty(SDL_HashTable *table); -SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void *key, const void **_value, void **iter); -SDL_bool SDL_IterateHashTableKeys(const SDL_HashTable *table, const void **_key, void **iter); +// iterate all values for a specific key. This only makes sense if the hash is stackable. If not-stackable, just use SDL_FindInHashTable(). +SDL_bool SDL_IterateHashTableKey(const SDL_HashTable *table, const void *key, const void **_value, void **iter); + +// iterate all key/value pairs in a hash (stackable hashes can have duplicate keys with multiple values). +SDL_bool SDL_IterateHashTable(const SDL_HashTable *table, const void **_key, const void **_value, void **iter); Uint32 SDL_HashString(const void *key, void *unused); SDL_bool SDL_KeyMatchString(const void *a, const void *b, void *unused);