-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.c
233 lines (201 loc) · 6.73 KB
/
dict.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
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
/* Ken Sheedlo
* kmdata Data Structures Library
* dict implementation based on hash tables. */
#include "dict.h"
void dict_init(dict_t *dict, size_t size, int32_t (*hash)(const void *),
int32_t (*eq)(const void *, const void *), int32_t options){
size_t length = MAX(size, DICTHT_MINIMUM_SIZE);
dict->table = calloc(length, sizeof(bucket_t *));
if(dict->table == NULL){
fprintf(stderr, "%s\n", "Memory allocation failure.");
return;
}
dict->hash = hash;
dict->eq = eq;
dict->length = length;
dict->load = 0;
dict->flags = options;
}
void *dict_get(dict_t *dict, void *key){
int32_t hash = dict->hash(key);
int32_t index = MOD(hash, dict->length);
bucket_t *bucket = *(dict->table + index);
if(bucket == NULL)
return NULL;
while(!(dict->eq(key, bucket->key))){
if(bucket->next == NULL)
return NULL;
bucket = bucket->next;
}
return bucket->data;
}
int32_t dict_add(dict_t *dict, void *key, void *data, bucket_t *result){
/* Check the size of the dict. Do we need to grow? */
if(((double)(dict->load + 1)) / dict->length > DICTHT_MAX_LOAD
&& !(dict->flags & DICTHT_NO_GROW)){
_dict_resize(dict, (size_t)(dict->length * DICTHT_GROW_FACTOR));
}
int32_t hash = dict->hash(key);
int32_t index = MOD(hash, dict->length);
bucket_t *new_bucket = malloc(sizeof(bucket_t));
if(!new_bucket){
fprintf(stderr, "%s\n", "Memory allocation failure.");
result->key = NULL;
return -1;
}
new_bucket->data = data;
new_bucket->key = key;
new_bucket->next = NULL;
bucket_t **target = dict->table + index;
if(*target == NULL){
*target = new_bucket;
}else{
bucket_t *bucket = *target;
bucket_t *last_bucket = NULL;
do{
last_bucket = bucket;
if(dict->eq(key, bucket->key)){
/* Scan down the buckets and replace the old one if the keys are
* actually equal. */
result->key = bucket->key;
result->data = bucket->data;
bucket->key = key;
bucket->data = data;
free(new_bucket);
return hash;
}
bucket = bucket->next;
}while(bucket != NULL);
last_bucket->next = new_bucket;
}
dict->load = dict->load + 1;
result->key = key;
result->data = data;
return hash;
}
void *dict_remove(dict_t *dict, void *key, bucket_t *result){
/* Check the size. Should we be using a smaller table? */
if((((double)(dict->load - 1)) / dict->length) < DICTHT_MIN_LOAD
&& !(dict->flags & DICTHT_NO_SHRINK) && (dict->length > DICTHT_MINIMUM_SIZE)){
size_t new_size = MAX((size_t)(dict->length / DICTHT_GROW_FACTOR),
DICTHT_MINIMUM_SIZE);
_dict_resize(dict, new_size);
}
/* Assume no match is found initially */
result->key = NULL;
result->data = NULL;
int32_t hash = dict->hash(key);
int32_t index = MOD(hash, dict->length);
bucket_t *bucket = *(dict->table + index);
if(bucket == NULL){
return NULL; /* no such entry in the table */
}
bucket_t *last_bucket = NULL;
do{
if(dict->eq(key, bucket->key)){
dict->load = dict->load - 1;
void *ret = bucket->data;
result->key = bucket->key;
result->data = ret;
if(last_bucket == NULL){
*(dict->table + index) = bucket->next;
}else{
last_bucket->next = bucket->next;
}
free(bucket);
return ret;
}
last_bucket = bucket;
}while((bucket = bucket->next) != NULL);
return NULL; /*no match found*/
}
void dict_clear(dict_t *dict, int32_t options){
int32_t free_keys = options & DICTHT_FREE_KEYS;
int32_t free_values = options & DICTHT_FREE_VALUES;
for(int i = 0; i<dict->length; i++){
if(dict->table[i] != NULL){
bucket_t *bucket = dict->table[i];
bucket_t *next_bucket;
do{
next_bucket = bucket->next;
if(free_keys){
free(bucket->key);
}
if(free_values){
free(bucket->data);
}
free(bucket);
}while((bucket = next_bucket) != NULL);
}
}
free(dict->table);
dict->load = 0;
dict->length = 0;
dict->hash = NULL;
dict->eq = NULL;
}
void dict_print(FILE *output, dict_t *dict, void (*disp_key)(FILE *, const void *),
void (*disp_value)(FILE *, const void *)){
fprintf(output, "{ ");
int32_t count = 0;
for(int i = 0; i<dict->length; i++){
if(dict->table[i] != NULL){
bucket_t *bucket = dict->table[i];
do{
disp_key(output, bucket->key);
fprintf(output, ": ");
disp_value(output, bucket->data);
if(++count < dict->load){
fprintf(output, ", ");
}
}while((bucket = bucket->next) != NULL);
}
}
fprintf(output, "} ");
}
void _dict_resize(dict_t *dict, size_t new_size){
bucket_t **new_table = calloc(new_size, sizeof(bucket_t *));
if(!new_table){
fprintf(stderr, "%s\n", "Memory allocation failure.");
return;
}
/* Go through the old table and rehash the entries into the new one */
for(int i = 0; i < dict->length; i++){
if(dict->table[i] != NULL){
bucket_t *bucket = dict->table[i];
bucket_t *next_bucket;
do{
/* This is fast and screws up the old table to no end, so do it
* right */
int32_t hash = dict->hash(bucket->key);
int32_t new_index = MOD(hash, new_size);
next_bucket = bucket->next;
bucket->next = new_table[new_index];
new_table[new_index] = bucket;
}while((bucket = next_bucket) != NULL);
}
}
free(dict->table);
dict->table = new_table;
dict->length = new_size;
}
void dict_key_set(list_t *rop, dict_t *dict){
bucket_t *bucket;
for(int i = 0; i < dict->length; i++){
if((bucket = dict->table[i]) != NULL){
do{
list_addlast(rop, bucket->key);
}while((bucket = bucket->next) != NULL);
}
}
}
void dict_value_set(list_t *rop, dict_t *dict){
bucket_t *bucket;
for(int i = 0; i < dict->length; i++){
if((bucket = dict->table[i]) != NULL){
do{
list_addlast(rop, bucket->data);
}while((bucket = bucket->next) != NULL);
}
}
}