-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.c
171 lines (159 loc) · 4.08 KB
/
table.c
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
#include <string.h>
#include <malloc.h>
#include <xxhash.h>
#include "table.h"
TOMLTable TOMLTable_with_size(int count)
{
int size = offsetof(TOMLTable_Header, items) +
count * sizeof(TOMLTable_Bucket);
TOMLTable_Header *hdr = malloc(size);
memset(hdr, '\0', size);
TOMLTable hmap = NULL;
if (hdr != NULL)
{
hmap = &(hdr->items[0]);
hdr->size = count;
}
return hmap;
}
#define TOMLTable_header(m) \
((TOMLTable_Header *)((void *)(m) - \
offsetof(TOMLTable_Header, items)))
static int TOMLTable_expand(TOMLTable *hmap_p)
{
int status = 0;
TOMLTable_Header *hdr = TOMLTable_header(*hmap_p);
int new_size = (hdr->size == 0 ? 2 : hdr->size) * 2;
TOMLTable new_map = TOMLTable_with_size(new_size);
for (TOMLTable_Bucket *offset = *hmap_p, *end = &((*hmap_p)[hdr->size]);
offset < end; ++offset)
{
if (offset->key != NULL)
{
status = TOMLTable_insert(&new_map, offset->key, &(offset->value));
if (status != 0)
{
TOMLTable_cleanup(new_map);
return status;
}
}
}
TOMLTable_cleanup(*hmap_p);
*hmap_p = new_map;
return status;
}
static TOMLTable_Bucket *get_bucket(TOMLTable hmap, String key,
uint32_t *hash_p)
{
int size = TOMLTable_size(hmap);
if (size == 0)
{
return NULL;
}
TOMLTable_Bucket *end = &(hmap[size]);
uint32_t hash = XXH32(key, String_len(key), 0);
size_t index = hash % size;
TOMLTable_Bucket *offset = &(hmap[index]);
if (hash_p != NULL)
{
*hash_p = hash;
}
for (; offset < end; ++offset)
{
if (!offset->hash ||
(offset->hash == hash && strcmp(key, offset->key) == 0))
{
return offset;
}
}
return NULL;
}
TOMLValue const *TOMLTable_get(TOMLTable hmap, String key)
{
TOMLTable_Bucket *bucket = get_bucket(hmap, key, NULL);
return bucket == NULL || !bucket->value.kind ? NULL : &(bucket->value);
}
TOMLValue *TOMLTable_put_extra(TOMLTable *hmap_p, String key, int store)
{
TOMLTable_Bucket *bucket = NULL;
TOMLValue *val_p = NULL;
uint32_t hash;
TOMLTable_Header *hdr;
do {
// TODO: Prevent infinite loop
bucket = get_bucket(*hmap_p, key, &hash);
hdr = TOMLTable_header(*hmap_p);
} while ((bucket == NULL || (hdr->count * 2) > hdr->size) &&
TOMLTable_expand(hmap_p) == 0);
if (bucket != NULL && (hdr->count * 2) <= hdr->size)
{
if (bucket->hash == 0 && store == 1)
{
bucket->hash = hash;
bucket->key = key;
++(hdr->count);
bucket->value.float_ = 0;
bucket->value.kind = 0;
}
val_p = &(bucket->value);
}
return val_p;
}
int TOMLTable_insert(TOMLTable *hmap_p, String key,
TOMLValue const *val_ps)
{
TOMLValue *val_pd = TOMLTable_put_extra(hmap_p, key, 1);
if (val_pd != NULL)
{
*val_pd = *val_ps;
return 0;
} else
{
return -1;
}
}
int TOMLTable_has_key(TOMLTable hmap, String key)
{
uint32_t hash;
TOMLTable_Bucket *bucket = get_bucket(hmap, key, &hash);
return bucket != NULL && bucket->hash == hash &&
strcmp(key, bucket->key) == 0;
}
int TOMLTable_pop(TOMLTable hmap, String key, TOMLValue *val_p)
{
int status = 0;
uint32_t hash;
TOMLTable_Bucket *bucket = get_bucket(hmap, key, &hash);
if (bucket != NULL)
{
if (val_p != NULL)
{
memcpy(val_p, &(bucket->value), sizeof(TOMLValue));
}
TOMLTable_Bucket *c = bucket + 1;
for (TOMLTable_Bucket *end = &(hmap[TOMLTable_header(hmap)->size]);
c < end && c->hash && c->hash == hash; ++c);
memcpy(bucket, bucket + 1, (int)((void *)c - (void *)bucket));
memset(bucket - 1, '\0', sizeof(*bucket));
} else
{
status = -1;
}
return status;
}
void TOMLTable_destroy(TOMLTable table)
{
int const size = TOMLTable_header(table)->size;
int const used = TOMLTable_header(table)->count;
for (int i = 0, destroyed = 0; i < size && destroyed < used; ++i)
{
TOMLTable_Bucket *entry = &(table[i]);
if (entry->key != NULL)
{
++destroyed;
String_cleanup(entry->key);
TOMLValue_destroy(&(entry->value));
}
}
TOMLTable_cleanup(table);
}