aboutsummaryrefslogtreecommitdiff
path: root/src/std_gc/hash_map.c
blob: 3f12f359df001af8b38760039decc3b9b804b178 (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
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
#include "../../include/std_gc/hash_map.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <gc.h>

void tsl_hash_map_init(TslHashMap *self) {
    self->buckets_data = NULL;
    self->buckets_size = 0;
    self->buckets_capacity = 0;
}

/* TODO: Remove if (data) and if (output) */
static int tsl_hash_map_append_bucket(TslHashMapNode **head_node, uint64_t hash, const TslValue *key, TslValue *value, TslValue **output) {
    TslHashMapNode *next_node;

    if(output)
        *output = NULL;

    next_node = GC_MALLOC(sizeof(TslHashMapNode));
    if(!next_node) {
        fprintf(stderr, "Error: hash map insert failed. Reason: out of memory\n");
        return 0;
    }

    next_node->hash = hash;
    next_node->key = *key;
    if(value)
        next_node->value = *value;

    if(output)
        *output = &next_node->value;

    if(*head_node) {
        next_node->next = (*head_node)->next;
        (*head_node)->next = next_node;
    } else {
        next_node->next = NULL;
        *head_node = next_node;
    }

    return 1;
}

static size_t tsl_hash_map_get_index(TslHashMap *self, uint64_t hash) {
    assert((self->buckets_capacity >> 3) > 0);
    /* >>3 = 8 = sizeof(TslHashMapNode*) */
    return hash & ((self->buckets_capacity >> 3) - 1);
}

static void tsl_hash_map_reorder_nodes(TslHashMap *self, size_t old_capacity) {
    TslHashMapNode **bucket = (TslHashMapNode**)self->buckets_data;
    TslHashMapNode **bucket_end = (TslHashMapNode**)((char*)self->buckets_data + old_capacity);
    size_t bucket_index = 0;
    while(bucket != bucket_end) {
        TslHashMapNode *node = *bucket;
        TslHashMapNode *prev_node = node; /* Set to node for optimization reason, where prev_node->next = node->next; which becomes no-op */
        int all_nodes_moved = 1;

        while(node) {
            size_t index = tsl_hash_map_get_index(self, node->hash);
            if(index != bucket_index) {
                TslHashMapNode **new_bucket = (TslHashMapNode**)self->buckets_data + index;
                prev_node->next = node->next;
                if(*new_bucket) {
                    node->next = (*new_bucket)->next;
                    (*new_bucket)->next = node;
                } else {
                    node->next = NULL;
                    *new_bucket = node;
                }
            } else {
                all_nodes_moved = 0;
            }

            prev_node = node;
            node = node->next;
        }

        if(all_nodes_moved)
            *bucket = NULL;

        ++bucket_index;
        ++bucket;
    }
}

static int tsl_hash_map_ensure_bucket_capacity_for_one_new_item(TslHashMap *self) {
    void *new_ptr;
    size_t new_capacity = self->buckets_capacity;
    size_t new_size = self->buckets_size + sizeof(TslHashMapNode*);
    size_t old_capacity = self->buckets_capacity;
    self->buckets_size = new_size;
    if(new_size <= self->buckets_capacity)
        return 1;

    if(new_capacity == 0)
        new_capacity = sizeof(TslHashMapNode*) << 3;

    while(new_capacity < new_size) {
        new_capacity <<= 1;
    }

    new_ptr = GC_REALLOC(self->buckets_data, new_capacity);
    if(!new_ptr) {
        fprintf(stderr, "Error: hash map realloc failed. Reason: out of memory\n");
        return 0;
    }

    self->buckets_data = new_ptr;
    self->buckets_capacity = new_capacity;
    {
        /* TODO: Remove this. This is not needed since GC_REALLOC sets the data to 0 */
        TslHashMapNode **bucket = (TslHashMapNode**)((char*)self->buckets_data + old_capacity);
        TslHashMapNode **bucket_end = (TslHashMapNode**)((char*)self->buckets_data + new_capacity);
        while(bucket != bucket_end) {
            *bucket = NULL;
            ++bucket;
        }
    }

    tsl_hash_map_reorder_nodes(self, old_capacity);
    return 1;
}

int tsl_hash_map_insert(TslHashMap *self, const TslValue *key, TslValue *value) {
    uint64_t hash = tsl_value_hash(key);
    size_t index;
    TslHashMapNode **bucket;
    assert(!tsl_hash_map_get(self, key));
    if(!tsl_hash_map_ensure_bucket_capacity_for_one_new_item(self))
        return 0;

    index = tsl_hash_map_get_index(self, hash);
    bucket = (TslHashMapNode**)self->buckets_data + index;
    return tsl_hash_map_append_bucket(bucket, hash, key, value, NULL);
}

TslValue* tsl_hash_map_get(TslHashMap *self, const TslValue *key) {
    uint64_t hash;
    size_t index;
    TslHashMapNode *node;
    if(self->buckets_capacity == 0)
        return NULL;

    hash = tsl_value_hash(key);
    index = tsl_hash_map_get_index(self, hash);
    node = *((TslHashMapNode**)self->buckets_data + index);
    while(node) {
        if(hash == node->hash && tsl_value_equals(key, &node->key))
            return &node->value;
        node = node->next;
    }

    return NULL;
}

TslValue* tsl_hash_map_get_or_create(TslHashMap *self, const TslValue *key) {
    uint64_t hash;
    size_t index;
    TslValue *value;
    TslHashMapNode **bucket;

    hash = tsl_value_hash(key);
    if(self->buckets_capacity > 0) {
        size_t index = tsl_hash_map_get_index(self, hash);
        TslHashMapNode **bucket = (TslHashMapNode**)self->buckets_data + index;
        TslHashMapNode *node = *bucket;
        while(node) {
            if(hash == node->hash && tsl_value_equals(key, &node->key))
                return &node->value;
            node = node->next;
        }
    }

    if(!tsl_hash_map_ensure_bucket_capacity_for_one_new_item(self))
        return NULL;

    index = tsl_hash_map_get_index(self, hash);
    bucket = (TslHashMapNode**)self->buckets_data + index;

    if(!tsl_hash_map_append_bucket(bucket, hash, key, NULL, &value))
        return NULL;
    
    return value;
}

void tsl_hash_map_create_iterator(TslHashMap *self, TslHashMapIterator *iterator) {
    iterator->hash_map = self;
    iterator->index = -1;
    iterator->node = NULL;
}

int tsl_hash_map_iterator_next(TslHashMapIterator *self) {
    if(self->node) {
        TslHashMapNode *new_node = self->node->next;
        if(new_node) {
            self->node = new_node;
            return 1;
        }
    }

    ++self->index;
    while(self->index < self->hash_map->buckets_size) {
        TslHashMapNode **bucket = (TslHashMapNode**)self->hash_map->buckets_data + self->index;
        if(*bucket) {
            self->node = *bucket;
            return 1;
        }
        ++self->index;
    }

    return 0;
}