SDL_hashtable.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef SDL_hashtable_h_
  19. #define SDL_hashtable_h_
  20. // this is not (currently) a public API. But maybe it should be!
  21. struct SDL_HashTable;
  22. typedef struct SDL_HashTable SDL_HashTable;
  23. typedef Uint32 (*SDL_HashTable_HashFn)(const void *key, void *data);
  24. typedef bool (*SDL_HashTable_KeyMatchFn)(const void *a, const void *b, void *data);
  25. typedef void (*SDL_HashTable_NukeFn)(const void *key, const void *value, void *data);
  26. extern SDL_HashTable *SDL_CreateHashTable(void *data,
  27. Uint32 num_buckets,
  28. SDL_HashTable_HashFn hashfn,
  29. SDL_HashTable_KeyMatchFn keymatchfn,
  30. SDL_HashTable_NukeFn nukefn,
  31. bool threadsafe,
  32. bool stackable);
  33. // This function is thread-safe if the hashtable was created with threadsafe = true
  34. extern void SDL_EmptyHashTable(SDL_HashTable *table);
  35. // This function is not thread-safe.
  36. extern void SDL_DestroyHashTable(SDL_HashTable *table);
  37. // This function is thread-safe if the hashtable was created with threadsafe = true
  38. extern bool SDL_InsertIntoHashTable(SDL_HashTable *table, const void *key, const void *value);
  39. // This function is thread-safe if the hashtable was created with threadsafe = true
  40. extern bool SDL_RemoveFromHashTable(SDL_HashTable *table, const void *key);
  41. // This function is thread-safe if the hashtable was created with threadsafe = true
  42. extern bool SDL_FindInHashTable(const SDL_HashTable *table, const void *key, const void **_value);
  43. // This function is thread-safe if the hashtable was created with threadsafe = true
  44. extern bool SDL_HashTableEmpty(SDL_HashTable *table);
  45. // iterate all values for a specific key. This only makes sense if the hash is stackable. If not-stackable, just use SDL_FindInHashTable().
  46. // This function is not thread-safe, you should use external locking if you use this function
  47. extern bool SDL_IterateHashTableKey(const SDL_HashTable *table, const void *key, const void **_value, void **iter);
  48. // iterate all key/value pairs in a hash (stackable hashes can have duplicate keys with multiple values).
  49. // This function is not thread-safe, you should use external locking if you use this function
  50. extern bool SDL_IterateHashTable(const SDL_HashTable *table, const void **_key, const void **_value, void **iter);
  51. extern Uint32 SDL_HashPointer(const void *key, void *unused);
  52. extern bool SDL_KeyMatchPointer(const void *a, const void *b, void *unused);
  53. extern Uint32 SDL_HashString(const void *key, void *unused);
  54. extern bool SDL_KeyMatchString(const void *a, const void *b, void *unused);
  55. extern Uint32 SDL_HashID(const void *key, void *unused);
  56. extern bool SDL_KeyMatchID(const void *a, const void *b, void *unused);
  57. extern void SDL_NukeFreeKey(const void *key, const void *value, void *unused);
  58. extern void SDL_NukeFreeValue(const void *key, const void *value, void *unused);
  59. #endif // SDL_hashtable_h_