-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
150 lines (120 loc) · 3.39 KB
/
hashtable.c
File metadata and controls
150 lines (120 loc) · 3.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
#define LOAD_FACTOR 0.75f
#define INITIAL_SIZE 10
static void *safe_calloc(size_t nmemb, size_t size) {
void *mem = calloc(nmemb, size);
if (!mem) {
fprintf(stderr, "out of memory!\n");
exit(1);
}
return mem;
}
void *safe_malloc(size_t size) {
void *mem = malloc(size);
if (!mem && size) {
fprintf(stderr, "out of memory!\n");
exit(1);
}
return mem;
}
void *safe_realloc(void *ptr, size_t nmemb, size_t size) {
void *mem = realloc(ptr, nmemb * size);
if (!mem && nmemb) {
fprintf(stderr, "out of memory!\n");
exit(1);
}
return mem;
}
hashtable_t *hash_init() {
hashtable_t *tbl = safe_malloc(sizeof(hashtable_t));
tbl->capacity = INITIAL_SIZE;
tbl->size = 0;
tbl->entries = safe_calloc(tbl->capacity, sizeof(entry_t));
return tbl;
}
// djb2
static size_t hash(const char *key, size_t capacity, size_t i) {
size_t hash = 5381;
int c;
while (c = *key++) {
hash = ((hash << 5) + hash) + c;
}
return (hash + i) % capacity;
}
static void insert_entry(entry_t *entries, entry_t *entry, size_t capacity) {
size_t i = 0;
do {
size_t j = hash(entry->key, capacity, i);
entry_t *cur_entry = (entries + j);
if (!cur_entry->key || !(*(cur_entry->key))) {
free(cur_entry->key);
cur_entry->key = entry->key;
cur_entry->value = entry->value;
break;
} else {
i++;
}
} while (i < capacity);
}
static void rehash(hashtable_t *tbl) {
size_t capacity = 2 * tbl->capacity;
entry_t *entries = safe_calloc(capacity, sizeof(entry_t));
for (size_t i = 0; i < tbl->capacity; i++) {
entry_t *entry = (tbl->entries + i);
if (entry->key) {
insert_entry(entries, entry, capacity);
}
}
free(tbl->entries);
tbl->entries = entries;
tbl->capacity = capacity;
}
void hash_insert(hashtable_t *tbl, const char *key, size_t key_size,
void *value) {
if ((tbl->size / (float) tbl->capacity) > LOAD_FACTOR) {
rehash(tbl);
}
entry_t entry;
entry.key = safe_malloc((key_size + 1) * sizeof(char));
strncpy(entry.key, key, key_size);
entry.key[key_size] = '\0';
entry.value = value;
insert_entry(tbl->entries, &entry, tbl->capacity);
tbl->size++;
}
static entry_t *_hash_search(const hashtable_t *tbl, const char *key) {
size_t i = 0;
do {
size_t j = hash(key, tbl->capacity, i);
entry_t *entry = (tbl->entries + j);
if (entry->key && !strcmp(entry->key, key)) {
return entry;
}
i++;
} while (i < tbl->capacity);
return NULL;
}
void *hash_search(const hashtable_t *tbl, const char *key) {
return _hash_search(tbl, key)->value;
}
void *hash_remove(hashtable_t *tbl, const char *key) {
entry_t *entry = _hash_search(tbl, key);
entry->key = safe_realloc(entry->key, 1, sizeof(char));
entry->key = '\0';
void *tmp = entry->value;
entry->value = NULL;
return tmp;
}
void hash_destroy(hashtable_t *tbl) {
for (size_t i = 0; i < tbl->capacity; i++) {
entry_t *entry = (tbl->entries + i);
free(entry->key);
free(entry->value);
}
free(tbl->entries);
free(tbl);
}