aboutsummaryrefslogtreecommitdiffstats
path: root/src/hashtable.h
blob: faa85912a015c47cbba8b25c8f43478e975a33d7 (plain)
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
#ifndef BDL_HASHTABLE_H
#define BDL_HASHTABLE_H

#include "darray.h"

// Minimum table capacity.
#define HT_MIN_CAP   4
#define HT_MIN_SHIFT 2

// Adjust the load factor threshold at which the table will grow on insertion.
#define HT_LOAD_THRESHOLD 0.8

typedef struct HashTablePair {
    void *key;
    void *value;
} HashTablePair;

struct HashTable;
typedef uint64_t (HashFunction)(const struct HashTable *table, void *bytes);
typedef bool (EqFunc)(void *a, void *b);

typedef struct HashTable {
    // All available key-value pairs as a dynamic array.
    HashTablePair *pairs;

    // Hash function.
    HashFunction *hash_func;

    // Equality function.
    EqFunc *eq_func;

    // This table expects the number of buckets to grow in powers of two. To
    // speedup the default hashing, we memoize the number of bits equivalent to
    // that power of 2:
    //
    //     cap := 1024 = 2 ^ 10, shift_amount := 10
    //
    uint8_t shift_amount;
} HashTable;

// Hash a byte stream using a circular shift + XOR hash function.
static inline uint64_t
_xor_shift_hash(const char *key, size_t n) {
    uint64_t hash = 0x65d9d65f6a19574f;
    char *last = (char *)key + n;
    while (key != last) {
        hash ^= (uint64_t)*key++;
        hash = (hash << 8) | (hash >> (64 - 8));
    }
    return hash;
}

// Use Fibonacci hashing to map a hash to a value in range of the table.
static inline uint64_t
_fibonacci_hash(uint64_t hash, size_t shift_amount) {
    return (hash * UINT64_C(11400714819323198485)) >> (64 - shift_amount);
}

static inline float
ht_load_factor(const HashTable *table) {
    return (float)array_size(table->pairs) / (float)array_cap(table->pairs);
}

HashTable *
ht_init(HashFunction *hash_func, EqFunc *eq_func) {
    HashTable *table = malloc(sizeof(HashTable));
    *table = (HashTable){0};
    array_init(table->pairs, HT_MIN_CAP);
    for (size_t i = 0; i < array_cap(table->pairs); i++) {
        table->pairs[i] = (HashTablePair){NULL, NULL};
    }
    table->shift_amount = HT_MIN_SHIFT;
    table->hash_func = hash_func;
    table->eq_func = eq_func;
    return table;
}

void
_ht_insert(HashTable *table, void *key, void *value) {
    size_t position = table->hash_func(table, key);
    size_t probe_position = position;

    // Verify the key in that position is free. If not, use linear probing to
    // find the next free slot.
    HashTablePair *pairs = table->pairs;
    bool update = false;
    while (true) {
        if (pairs[probe_position].key == NULL) {
            array_head(pairs)->size++;
            break;
        }
        if (table->eq_func(pairs[probe_position].key, key)) {
            update = true;
            break;
        }
        if (probe_position == array_cap(pairs) - 1) {
            probe_position = 0;
        } else {
            probe_position++;
        }
    }

    if (!update) {
        pairs[probe_position].key = key;
        pairs[probe_position].value = value;
    } else {
        pairs[probe_position].value = value;
    }
}

void
_ht_maybe_grow(HashTable *table) {
    if (ht_load_factor(table) < HT_LOAD_THRESHOLD) {
        return;
    }

    // Create a new array with 2x capacity.
    HashTablePair *old_pairs = table->pairs;
    table->pairs = NULL;
    array_init(table->pairs, array_cap(old_pairs) * 2);
    for (size_t i = 0; i < array_cap(table->pairs); i++) {
        table->pairs[i] = (HashTablePair){NULL, NULL};
    }
    table->shift_amount++;

    // Hash everything in the table for the new array capacity.
    for (size_t i = 0; i < array_cap(old_pairs); i++) {
        if (old_pairs[i].key != NULL) {
            _ht_insert(table, old_pairs[i].key, old_pairs[i].value);
        }
    }

    // Free old arrays.
    array_free(old_pairs);
}

void
ht_insert(HashTable *table, void *key, void *value) {
    _ht_maybe_grow(table);
    _ht_insert(table, key, value);
    return;
}

void *
ht_lookup(const HashTable *table, void *key) {
    size_t position = table->hash_func(table, key);
    size_t probe_position = position;

    // Verify the key in that position is the same. If not perform linear
    // probing to find it.
    HashTablePair *pairs = table->pairs;
    while (true) {
        if (pairs[probe_position].key == NULL) {
            return NULL;
        }
        if (table->eq_func(pairs[probe_position].key, key)) {
            break;
        }
        if (probe_position == array_cap(pairs) - 1) {
            probe_position = 0;
        } else {
            probe_position++;
        }
        if (probe_position == position) {
            return NULL;
        }
    }
    return pairs[probe_position].value;
}

void
ht_free(HashTable *table) {
    if (table == NULL) {
        return;
    }
    array_free(table->pairs);
    free(table);
}

#endif // BDL_HASHTABLE_H