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