-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.h
65 lines (54 loc) · 1.61 KB
/
hash_table.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
64
65
#include <stdint.h>
/* HT_BASE_SIZE defines the minimal size of the hash_table
* from which it will grow. The size will never fall below
* this value when shrinking */
#define HT_BASE_SIZE 16
/* Resizing parameters, double or half hash_table size when
* the ratio count/size*100 HT_MIN_FILL or exceeds HT_MAX_FILL
* in percent. */
#define HT_MIN_FILL 10
#define HT_MAX_FILL 70
/* HT_INTERLINK if defined, the items will be linked according to
* their insertion order via next_insert and prev_insert members,
* which makes iterating the items straightforward. */
#define HT_INTERLINK 1
typedef struct ht_item ht_item;
struct ht_item {
unsigned long hash;
char* key;
union {
void *val;
uint64_t u64;
int64_t s64;
double d;
} v;
ht_item* next;
#if HT_INTERLINK
ht_item* next_insert, *prev_insert;
#endif
};
typedef struct ht_type ht_type;
struct ht_type {
void *(*valdup)(const void *obj);
void (*valfree)(void *obj);
};
typedef struct ht_hash_table ht_hash_table;
struct ht_hash_table {
ht_type *type;
unsigned long size;
unsigned long count;
ht_item** items;
#if HT_INTERLINK
ht_item *oldest, *newest;
#endif
};
/* API */
ht_hash_table* ht_new(ht_type *type);
void ht_del_hash_table(ht_hash_table* ht);
ht_item* ht_search_raw(ht_hash_table *ht, const char *key);
ht_item* ht_insert_raw(ht_hash_table *ht, const char* key);
void* ht_search(ht_hash_table* ht, const char* key);
int ht_insert(ht_hash_table* ht, const char* key, const void* value);
int ht_delete(ht_hash_table* h, const char* key);
/* Debugging */
void ht_print(ht_hash_table *ht);