-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashtable.h
More file actions
44 lines (32 loc) · 1.25 KB
/
Copy pathhashtable.h
File metadata and controls
44 lines (32 loc) · 1.25 KB
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
// Blizzard Hashtable (MIT License)
// Author: korialuo (https://github.com/korialuo/hashtable)
// Create: 2018/10/18
#ifndef __hashtable_h__
#define __hashtable_h__
#include <stdint.h>
#include <stddef.h>
typedef void *(*fn_alloc)(size_t size);
typedef void (*fn_free)(void *ptr);
typedef void (*cb_del)(void *ptr);
// global init
void hashtable_global_init(fn_alloc al, fn_free fr);
// create a hashtable
// suggested size: 1361, 2729, 5471, 10949, 21911, 87719, 175447, 350899 ...
struct hashtable *hashtable_create(int32_t size);
// put an object into hashtable
// return -1 for failed, >= 0 for index.
int32_t hashtable_put(struct hashtable *ht, const char *key, void *val);
// get an object by key
void *hashtable_get(struct hashtable *ht, const char *key);
// get an object by index
void *hashtable_getidx(struct hashtable *ht, int32_t idx);
// remove an object by key
// return the object if exists, or return NULL.
void *hashtable_del(struct hashtable *ht, const char *key);
// get length of the hashtable
// return -1 for error.
int32_t hashtable_len(struct hashtable *ht);
// destroy a hashtable
// del callback for release every object in the hashtable. NULL for no need.
void hashtable_release(struct hashtable *ht, cb_del delfn);
#endif // __hashtable_h__