-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.c
459 lines (402 loc) · 10.3 KB
/
Index.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Index module, see Index.h for description.
*
* This is 1st or 2nd year Computer Science stuff. As a professional
* programmer I've been spoiled by Python, C++ STL, and other
* containers, so I haven't written code like this since ~1992.
* Writing this evokes memories of my compsci professors drawing nodes
* on the whiteboard...
*/
#include "Index.h"
#include "Mem.h"
#include "String.h"
#include <string.h> /* memset */
#include <inttypes.h> /* PRIu32 */
static Index_node * Index_node_new(uint32_t key, String *stringKey, void * voidKey, void *value)
{
Index_node *node = Mem_malloc(sizeof(*node));
*node = (Index_node) {
.left = 0L,
.right = 0L,
.stringKey = String_dup(stringKey),
.voidKey = voidKey,
.value = value,
.key = key,
};
return node;
}
static void Index_node_free(Index_node ** ppnode)
{
Index_node * node = *ppnode;
if (node->stringKey) {
String_free(&(node->stringKey));
}
Mem_free(node);
*ppnode = 0;
}
Index *Index_new(void)
{
Index *idx = Mem_calloc(1, sizeof(*idx));
return idx;
}
#include <stdio.h>
#include <unistd.h>
static uint32_t key_from_bytes(char * bytes, int len)
{
/* Generate a 32-bit hash key from a set of bytes. Fast is good.
Wide distribution over key space is good. Want each string bit
to perturb all key bits. */
uint32_t key = 0x90a4ae8c; /* Random number from a Python session the day I wrote this. */
char *p = bytes;
int i;
//printf("%d bytes[", len);
for (i=0; i < len; i++) {
uint8_t b = p[i];
//printf(" %02x", b);
uint8_t rotate = b & 0x1f; /* Rotate up to 5 bits. */
key = (key << rotate) | (key >> (32 - rotate));
key ^= ((b << 23) | (b << 14) | (b << 6) | (b << 0));
/* key ^= ((b << 24) | (b << 16) | (b << 8) | (b << 0)); */
}
//printf(" ], key=%" PRIu32 "\n", key);
return key;
}
static inline uint32_t key_from_string(String *stringKey)
{
//printf("stringKey=%s, ", s(stringKey));
return key_from_bytes(s(stringKey), String_len(stringKey));
}
static inline uint32_t key_from_ptr(void * voidKey)
{
return key_from_bytes((char*)voidKey, sizeof(voidKey));
}
static inline void key_from_either(String * stringKey, void * voidKey,
uint32_t * key,
int * err)
{
*key = 0; /* keep compiler happy */
if (stringKey) {
*key = key_from_string(stringKey);
*err = INDEX_NO_ERROR;
}
else if (voidKey) {
*key = key_from_ptr(voidKey);
*err = INDEX_NO_ERROR;
}
else {
fprintf(stderr, "%s: neither stringKey nor voidKey provided!\n", __func__);
*err = INDEX_NO_KEY;
}
}
static void Index_analyze_2(Index_node *p, int depth, int *maxDepth,
int *leftNodes, int *rightNodes)
{
if (depth > *maxDepth) {
*maxDepth = depth;
}
if (p->left) {
*leftNodes += 1;
Index_analyze_2(p->left, depth+1, maxDepth, leftNodes, rightNodes);
}
if (p->right) {
*rightNodes += 1;
Index_analyze_2(p->right, depth+1, maxDepth, leftNodes, rightNodes);
}
}
void Index_analyze(Index *idx)
{
Index_node *p = idx->_nodes;
int maxDepth = 0;
int leftNodes = 0;
int rightNodes = 0;
if (p) {
Index_analyze_2(p, 0, &maxDepth, &leftNodes, &rightNodes);
}
printf("max depth=%d, %d left nodes, %d right nodes, %d total nodes\n",
maxDepth, leftNodes, rightNodes, leftNodes + rightNodes + 1);
}
void Index_walk_2(Index_node *node, void (*callback)(Index_node *node))
{
if (node->left) {
Index_walk_2(node->left, callback);
}
if (node->right) {
Index_walk_2(node->right, callback);
}
if (callback) {
callback(node);
}
}
void Index_walk(Index *idx, void (*callback)(Index_node *node))
{
if (idx->_nodes) {
Index_walk_2(idx->_nodes, callback);
}
}
static void clear_nodes(Index_node *p)
{
if (p->left) {
clear_nodes(p->left);
}
if (p->right) {
clear_nodes(p->right);
}
if (p->stringKey) {
String_free(&p->stringKey);
}
memset(p, 0, sizeof(*p));
Mem_free(p);
}
void Index_clear(Index **_idx)
{
Index *idx = *_idx;
Index_node *p = idx->_nodes;
clear_nodes(p);
Mem_free(idx);
*_idx = NULL;
}
static void insert_node(Index_node * node, uint32_t key, Index_node * new_node)
{
/* I broke this out of Index_op() to keep it cleaner. */
while (node) {
Index_node *parent = node;
if (key < node->key) {
node = node->left;
if (!node) {
parent->left = new_node;
return;
}
}
else if (key > node->key) {
node = node->right;
if (!node) {
parent->right = new_node;
return;
}
}
else if (key == node->key) {
/* FIXME: Test for actual key collisiion */
{
/* Hash collision but different keys, continue down the left side. */
node = node->left;
if (!node) {
parent->left = new_node;
return;
}
}
}
}
}
enum {
INDEX_OP_ADD,
INDEX_OP_FIND,
};
/*
* 2014-Jun-03: New experiments.
* Single function to walk the tree and set up for add or find, with
* flags for delete.
*/
static void _Index_op(Index *idx,
String *stringKey, void * voidKey,
void *new_value, void ** existing_value,
int op,
int del,
int * err)
{
uint32_t key;
if (!idx) {
fprintf(stderr, "%s: idx not set!\n", __func__);
*err = INDEX_NO_IDX;
return;
}
key_from_either(stringKey, voidKey, &key, err);
if (*err == INDEX_NO_KEY) {
return;
}
Index_node *node = idx->_nodes;
Index_node **pnode = &(idx->_nodes);
while (node) {
if (key < node->key) {
pnode = &(node->left);
node = node->left;
}
else if (key > node->key) {
pnode = &(node->right);
node = node->right;
}
else if (key == node->key) {
/* Duplicate hash keys are allowed. After all, 1 in 4 billion isn't
that rare if working on GHz CPUs with sets of thousands or millions
of nodes. So, verify source key. */
if ( (stringKey && String_cmp(stringKey, node->stringKey) == 0)
|| (voidKey && voidKey == node->voidKey) ) {
/* Matching node found. */
break;
}
else {
/* Keep searching down left tree... */
pnode = &(node->left);
node = node->left;
}
}
}
/*
* If "node" is set, then above loop has found existing node with matching key.
* Either way, "pnode" points at the location where a new node would go, or the
* location to update if a node is to be removed.
*/
if (op == INDEX_OP_FIND) {
if (!node) {
*err = INDEX_KEY_NOT_FOUND;
}
else {
if (!existing_value) {
*err = INDEX_NO_DEST;
return;
}
*existing_value = node->value;
if (del) {
/* Delete this node. 4 cases to handle. */
if (node->left && node->right) {
/* Node has both subtrees. Move left subtree under right. */
if (node->key & 1) {
insert_node(node->right, node->left->key, node->left);
*pnode = node->right;
}
else {
insert_node(node->left, node->right->key, node->right);
*pnode = node->left;
}
}
else if (node->left) {
/* Only left subtree. */
*pnode = node->left;
}
else if (node->right) {
/* Only right subtree. */
*pnode = node->right;
}
else {
/* No subtrees. */
*pnode = NULL;
}
Index_node_free(&node);
idx->count -= 1;
}
}
}
else if (op == INDEX_OP_ADD) {
if (node) {
if (del) {
if (existing_value) {
*existing_value = node->value;
}
node->value = new_value;
*err = INDEX_NO_ERROR;
}
else {
*err = INDEX_DUPLICATE_KEY;
}
}
else {
/* create new node, assign to pnode */
*pnode = Index_node_new(key, stringKey, voidKey, new_value);
*err = INDEX_NO_ERROR;
idx->count += 1;
}
}
}
/* public API functions: */
void Index_add_string(Index * idx, String * stringKey, void * value, int * err)
{
_Index_op(idx,
stringKey, NULL,
value, NULL,
INDEX_OP_ADD,
0,
err);
}
void Index_replace_string(Index * idx, String * stringKey, void * value, void **oldvalue, int * err)
{
_Index_op(idx,
stringKey, NULL,
value, oldvalue,
INDEX_OP_ADD,
1,
err);
}
void Index_add_ptrkey(Index * idx, void * voidKey, void * value, int * err)
{
_Index_op(idx,
NULL, voidKey,
value, NULL,
INDEX_OP_ADD,
0,
err);
}
void * Index_find_string(Index * idx, String * stringKey, int * err)
{
void *oldvalue = NULL;
_Index_op(idx,
stringKey, NULL,
NULL, &oldvalue,
INDEX_OP_FIND,
0,
err);
return oldvalue;
}
void * Index_find_ptrkey(Index * idx, void * voidKey, int * err)
{
void *oldvalue = NULL;
_Index_op(idx,
NULL, voidKey,
NULL, &oldvalue,
INDEX_OP_FIND,
0,
err);
return oldvalue;
}
void Index_del_string(Index * idx, String * stringKey, int * err)
{
/* Find and delete, ignore return value. */
_Index_op(idx,
stringKey, NULL,
NULL, NULL,
INDEX_OP_FIND,
1,
err);
}
void Index_del_ptrkey(Index * idx, void * voidKey, int * err)
{
/* Find and delete, ignore return value. */
_Index_op(idx,
NULL, voidKey,
NULL, NULL,
INDEX_OP_FIND,
1,
err);
}
void * Index_pull_string(Index * idx, String * stringKey, int * err)
{
/* Find and delete, returning value. */
void *oldvalue = NULL;
_Index_op(idx,
stringKey, NULL,
NULL, &oldvalue,
INDEX_OP_FIND,
1,
err);
return oldvalue;
}
void * Index_pull_ptrkey(Index * idx, void * voidKey, int * err)
{
/* Find and delete, returning value. */
void *oldvalue = NULL;
_Index_op(idx,
NULL, voidKey,
NULL, &oldvalue,
INDEX_OP_FIND,
1,
err);
return oldvalue;
}