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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Papo IRC Server
* Copyright (C) 2021 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "hash_table.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITAL_CAPACITY 16
#define FNV_OFFSET 14695981039346656037UL
#define FNV_PRIME 1099511628211UL
static uint64_t hash_key(const char *key);
static const char *hash_table_set_entry(hash_table_entry_t *entries,
size_t capacity,
const char *key,
void *value,
size_t *plength);
static bool hash_table_expand(hash_table_t *ht);
hash_table_t*
hash_table_new(void)
{
hash_table_t* ht = malloc(sizeof(hash_table_t));
if (ht == NULL) {
fprintf(stderr, "could not create hash_table: out of memory\n");
return NULL;
}
ht->capacity = INITAL_CAPACITY;
ht->length = 0;
ht->entries = calloc(ht->capacity, sizeof(hash_table_entry_t));
if (ht->entries == NULL) {
fprintf(stderr, "could not create hash_entry: out of memory\n");
free(ht);
return NULL;
}
return ht;
}
void
hash_table_destroy(hash_table_t *ht)
{
for (size_t i = 0; i < ht->capacity; ++i) {
if (ht->entries[i].key != NULL) {
free((void*) ht->entries[i].key);
}
}
free(ht->entries);
free(ht);
}
void
hash_table_insert(hash_table_t *ht, const char *key, void *value)
{
assert(value != NULL);
if (ht->length >= ht->capacity / 2) {
if (!hash_table_expand(ht)) {
return;
}
}
hash_table_set_entry(ht->entries, ht->capacity, key, value, &ht->length);
}
void*
hash_table_get(hash_table_t *ht,
const char *key)
{
uint64_t hash = hash_key(key);
size_t index = (size_t)(hash & (uint64_t)(ht->capacity - 1));
while (ht->entries[index].key != NULL) {
if (strcmp(key, ht->entries[index].key) == 0) {
return ht->entries[index].value;
}
index++;
if (index >= ht->capacity) {
index = 0;
}
}
return NULL;
}
bool
hash_table_remove(hash_table_t *ht,
const char *key)
{
uint64_t hash = hash_key(key);
size_t index = (size_t)(hash & (uint64_t)(ht->capacity - 1));
while (ht->entries[index].key != NULL) {
if (strcmp(key, ht->entries[index].key) == 0) {
free(ht->entries[index].key);
ht->entries[index] = (hash_table_entry_t) { NULL, NULL };
ht->length--;
return true;
}
index++;
if (index >= ht->capacity) {
index = 0;
}
}
return false;
}
size_t
hash_table_length(hash_table_t *ht)
{
return ht->length;
}
static uint64_t
hash_key(const char *key)
{
uint64_t hash = FNV_OFFSET;
for (const char* p = key; *p; ++p) {
hash ^= (uint64_t) (unsigned char) (*p);
hash *= FNV_PRIME;
}
return hash;
}
static const char*
hash_table_set_entry(hash_table_entry_t *entries,
size_t capacity,
const char *key,
void *value,
size_t *plength)
{
uint64_t hash = hash_key(key);
size_t index = (size_t)(hash & (uint64_t)(capacity - 1));
while (entries[index].key != NULL) {
if (strcmp(key, entries[index].key) == 0) {
entries[index].value = value;
return entries[index].key;
}
index++;
if (index >= capacity) {
index = 0;
}
}
if (plength != NULL) {
key = strdup(key);
if (key == NULL) {
return NULL;
}
(*plength)++;
}
entries[index].key = (char*)key;
entries[index].value = value;
return key;
}
static bool
hash_table_expand(hash_table_t *ht)
{
size_t new_capacity = ht->capacity * 2;
if (new_capacity < ht->capacity) {
return false;
}
hash_table_entry_t* new_entries = calloc(new_capacity, sizeof(hash_table_entry_t));
if (new_entries == NULL) {
return false;
}
for (size_t i = 0; i < ht->capacity; ++i) {
hash_table_entry_t entry = ht->entries[i];
if (entry.key != NULL) {
hash_table_set_entry(
new_entries,
new_capacity,
entry.key,
entry.value,
NULL
);
}
}
free(ht->entries);
ht->entries = new_entries;
ht->capacity = new_capacity;
return true;
}
hash_table_iterator_t hash_table_get_iterator(hash_table_t *ht) {
hash_table_iterator_t it;
it._table = ht;
it._index = 0;
return it;
}
bool hash_table_iterator_next(hash_table_iterator_t *it) {
hash_table_t* ht = it->_table;
while (it->_index < ht->capacity) {
size_t i = it->_index;
it->_index++;
if (ht->entries[i].key != NULL) {
hash_table_entry_t entry = ht->entries[i];
it->key = entry.key;
it->value = entry.value;
return true;
}
}
return false;
}
|