lexLeo
Loading...
Searching...
No Matches
hashtable.h
1// src/foundation/data_structures/include/hashtable.h
2
3#ifndef HASHTABLE_H
4#define HASHTABLE_H
5
6#include <stddef.h>
7
8typedef struct hashtable hashtable;
9
10typedef enum {
11 HASHTABLE_KEY_TYPE_STRING, // keys are owned and strcmp is used
12 HASHTABLE_KEY_TYPE_POINTER // keys are not owned and "=" is used for key comparision
13} hashtable_key_type;
14
15typedef void (*hashtable_destroy_value_fn_t)(void *value);
16
17// hashtable_destroy_value_fn == NULL means no callback
18hashtable *hashtable_create(
19 size_t size,
20 hashtable_key_type key_type,
21 hashtable_destroy_value_fn_t destroy_value_fn );
22
23// frees values too (via callback) if callback
24void hashtable_destroy(hashtable *ht);
25
26// returns 0 if one param at least is NULL
27// otherwise:
28// returns 1 if the key is already in use, 0 otherwise
29int hashtable_key_is_in_use(hashtable *ht, const void *key);
30
31// returns NULL if the key is not found, but also if the found entry's value is NULL.
32void *hashtable_get(const hashtable *ht, const void *key);
33
34// returns 1 on error, 0 on success.
35// error if the key already exists
36int hashtable_add(hashtable *ht, const void *key, void *value);
37
38// returns 1 on error, 0 on success.
39// error if the key does not exist or ht is NULL
40int hashtable_reset_value(hashtable *ht, const void *key, void *value);
41
42// frees value via callback if callback
43int hashtable_remove(hashtable *ht, const void *key);
44
45#endif // HASHTABLE_H
Definition hashtable_internal.h:14