aboutsummaryrefslogtreecommitdiff
path: root/HashTable.c
blob: 0fed138b287c4fd8b5a97aeddfb77b907bda79ce (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "HashTable.h"
#include "Types.h"
#include <stdio.h>
#include <string.h>

/** Hashing function **/

// Fowler/Noll/Vo 32-bit hash implementation
// http://www.isthe.com/chongo/tech/comp/fnv/index.html
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash

#define FNV_OFFSET_BASIS 2166136261
#define FNV_PRIME        16777619
typedef u32 fnv_int;

static fnv_int fnv1_hash(const char* str) {
    fnv_int hash = FNV_OFFSET_BASIS;
    while (*str != '\0') {
        hash *= FNV_PRIME;
        hash ^= *(str++);
    }
    return hash;
}

/** Metadata **/

struct HashTableMeta {
    MemoryManager *mm;
    usize size;
    usize count;
};

static inline struct HashTableMeta*
H_metadata(HashTable ht) {
    return ht - sizeof(struct HashTableMeta);
}

/** Helpers **/

static inline const char**
H_keys(HashTable ht) {
    return ht;
}

static inline void**
H_vals(HashTable ht) {
    return ht + H_metadata(ht)->size * sizeof(const char*);
}

static inline Result
H_find_hash(HashTable ht, const char *key, fnv_int *hash) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    struct HashTableMeta *htm = H_metadata(ht);

    *hash = fnv1_hash(key) % htm->size;
    fnv_int hash_orig = *hash;

    do {
        if (H_keys(ht)[*hash] == NULL)
            return R_NotFound;

        if (0 == strcmp(H_keys(ht)[*hash], key))
            return R_Ok;

        *hash = (*hash + 1) % htm->size;
    } while (*hash != hash_orig);

    return R_NotFound;
}

/* Create */

HashTable
H_new(MemoryManager *mm, usize size) {
    if (NULL == mm)
        return NULL;

    struct HashTableMeta htm = {
        .mm = mm,
        .size = size,
        .count = 0,
    };

    void* start = NULL;
    if (R_Ok != mm->alloc(*mm, &start, sizeof(htm) + size * sizeof(const char*) + size * sizeof(void*)) ||
        NULL == start)
    {
        return NULL;
    }

    memcpy(start, &htm, sizeof(htm));
    start += sizeof(htm);
    memset(start, 0, size * sizeof(const char*) + size * sizeof(void*));

    return start;
}

/* Free */

Result
H_free(HashTable* ht) {
    if (NULL == ht || NULL == *ht)
        return R_NullArgument;

    struct HashTableMeta *htm = H_metadata(*ht);

    for (usize i = 0; i < htm->size; ++i) {
        if (NULL != H_keys(*ht)[i])
            RETURN_NOTOK(htm->mm->free(*htm->mm, (void**)(H_keys(*ht) + i)));
    }

    *ht = htm;
    return htm->mm->free(*htm->mm, ht);
}

Result
H_freeat(HashTable ht, const char* key) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    fnv_int hash = 0;
    RETURN_NOTOK(H_find_hash(ht, key, &hash));

    struct HashTableMeta *htm = H_metadata(ht);

    RETURN_NOTOK(htm->mm->free(*htm->mm, H_vals(ht) + hash));
    RETURN_NOTOK(htm->mm->free(*htm->mm, (void**)(H_keys(ht) + hash)));
    --htm->count;

    return R_Ok;
}

Result
H_freevals(HashTable* ht) {
    if (NULL == ht || NULL == *ht)
        return R_NullArgument;

    struct HashTableMeta *htm = H_metadata(*ht);

    for (usize i = 0; i < htm->size; ++i) {
        htm->mm->free(*htm->mm, (void**)(H_vals(*ht) + i));
    }

    return R_Ok;
}

/* Modify */

Result
H_insert(HashTable ht, const char *key, void *value) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    struct HashTableMeta *htm = H_metadata(ht);

    if (htm->count >= htm->size)
        return R_OutOfMemory;

    fnv_int hash = fnv1_hash(key) % htm->size;
    while (NULL != H_keys(ht)[hash]) {
        hash = (hash + 1) % htm->size;
    }

    H_keys(ht)[hash] = M_strdup(*htm->mm, key);
    H_vals(ht)[hash] = value;
    ++htm->count;

    return R_Ok;
}

Result
H_insertstr(HashTable ht, const char *key, const char *value) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    MemoryManager *mm = H_metadata(ht)->mm;

    char* copy = M_strdup(*mm, value);
    Result r = H_insert(ht, key, copy);

    if (R_Ok != r) {
        mm->free(*mm, (void**)&copy);
    }
    return r;
}

Result
H_inserti(HashTable ht, const char *key, int value) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    MemoryManager *mm = H_metadata(ht)->mm;

    int* copy = NULL;
    RETURN_NOTOK(
        mm->alloc(*mm, (void**)&copy, sizeof(int)));
    memcpy(copy, &value, sizeof(int));

    Result r = H_insert(ht, key, copy);

    if (R_Ok != r) {
        mm->free(*mm, (void**)&copy);
    }

    return r;
}

Result
H_insertf(HashTable ht, const char *key, double value) {
    if (NULL == ht || NULL == key)
        return R_NullArgument;

    MemoryManager *mm = H_metadata(ht)->mm;

    float* copy = NULL;
    RETURN_NOTOK(
        mm->alloc(*mm, (void**)&copy, sizeof(double)));
    memcpy(copy, &value, sizeof(double));

    Result r = H_insert(ht, key, copy);

    if (R_Ok != r) {
        mm->free(*mm, (void**)&copy);
    }
    return r;
}

/* Get */

usize
H_size(HashTable ht) {
    if (NULL == ht)
        return 0;
    return H_metadata(ht)->size;
}

usize
H_count(HashTable ht) {
    if (NULL == ht)
        return 0;
    return H_metadata(ht)->count;
}

void*
H_get(HashTable ht, const char* key) {
    if (NULL == ht || NULL == key)
        return NULL;

    struct HashTableMeta *htm = H_metadata(ht);

    fnv_int hash = 0;
    if (R_Ok != H_find_hash(ht, key, &hash))
        return NULL;

    return H_vals(ht)[hash];
}

inline char*
H_getstr(HashTable ht, const char* key) {
    return H_get(ht, key);
}

inline int*
H_geti(HashTable ht, const char* key) {
    return H_get(ht, key);
}

inline double*
H_getf(HashTable ht, const char* key) {
    return H_get(ht, key);
}