-
Notifications
You must be signed in to change notification settings - Fork 3
/
rsht.h
63 lines (48 loc) · 1.47 KB
/
rsht.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* rsht.h
*
* rsht - really simple hash table
* Copyright (c) 2018 Robert Stolarz
*
* rsht is available free of charge under the terms of the MIT
* License. You are free to redistribute and/or modify it under those
* terms. It is distributed in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY. See the LICENSE file for more details.
*/
#ifndef RSHT_H
#define RSHT_H
#ifndef RSHT_NO_INCLUDES
#include <stdbool.h>
#include <stddef.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct rsht_entry {
char *key;
void *val;
} rsht_entry;
typedef struct rsht_ht {
size_t capacity;
size_t num_slots_used;
rsht_entry *items;
size_t num_buckets;
size_t *buckets;
} rsht_ht;
// returns NULL if we were unable to allocate space for buckets or entries
rsht_ht *rsht_create(rsht_ht *ht, const size_t num_buckets, const size_t initial_capacity);
rsht_entry *rsht_get(const rsht_ht *ht, const char *key);
// returns false if we needed to increase capacity but failed to do so
bool rsht_put(rsht_ht *ht, char *key, void *val, void **old_val_ref);
void rsht_destroy(rsht_ht *ht);
// should return whether to continue iterating or not
typedef bool (*rsht_callback)(rsht_entry *, void *);
/*
* the userdata argument will be passed as the second parameter to fn
* this routine will return the number of items iterated over (exclusive of an item that fails)
*/
size_t rsht_foreach(rsht_ht *ht, rsht_callback fn, void *userdata);
#ifdef __cplusplus
}
#endif
#endif