-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc_map.c
More file actions
481 lines (397 loc) · 12.7 KB
/
c_map.c
File metadata and controls
481 lines (397 loc) · 12.7 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "c_map.h"
static node_t *c_map_create_node(node_t *node)
{
/* Setup the pointers */
node->left = node->right = node->up = NULL;
/* Set the color to black by default */
node->color = C_MAP_RED;
return NULL;
}
/*
* Perform left rotation with "node". The following happens (with respect
* to "C"):
*
* B C
* / \ / \
* A C => B D
* \ /
* D A
*
* Returns the new node pointing in the spot of the original node.
*/
static node_t *c_map_rotate_left(c_map_t obj, node_t *node)
{
node_t *r = node->right, *rl = r->left, *up = node->up;
/* Adjust */
r->up = up;
r->left = node;
node->right = rl;
node->up = r;
if (node->right)
node->right->up = node;
if (up) {
if (up->right == node)
up->right = r;
else
up->left = r;
}
if (node == obj->head)
obj->head = r;
return r;
}
/*
* Perform a right rotation with "node". The following happens (with respect
* to "C"):
*
* C B
* / \ / \
* B D => A C
* / \
* A D
*
* Return the new node pointing in the spot of the original node.
*/
static node_t *c_map_rotate_right(c_map_t obj, node_t *node)
{
node_t *l = node->left, *lr = l->right, *up = node->up;
// Adjust
l->up = up;
l->right = node;
node->left = lr;
node->up = l;
if (node->left)
node->left->up = node;
if (up) {
if (up->right == node)
up->right = l;
else
up->left = l;
}
if (node == obj->head)
obj->head = l;
return l;
}
static void c_map_l_l(c_map_t obj,
node_t *node UNUSED,
node_t *parent UNUSED,
node_t *grandparent,
node_t *uncle UNUSED)
{
/* Rotate to the right according to grandparent */
grandparent = c_map_rotate_right(obj, grandparent);
/* Swap grandparent and uncle's colors */
c_map_color_t c1 = grandparent->color, c2 = grandparent->right->color;
grandparent->color = c2;
grandparent->right->color = c1;
}
static void c_map_l_r(c_map_t obj,
node_t *node,
node_t *parent,
node_t *grandparent,
node_t *uncle)
{
/* Rotate to the left according to parent */
parent = c_map_rotate_left(obj, parent);
/* Refigure out the identity */
node = parent->left;
grandparent = parent->up;
uncle =
(grandparent->left == parent) ? grandparent->right : grandparent->left;
// Apply left-left case
c_map_l_l(obj, node, parent, grandparent, uncle);
}
static void c_map_r_r(c_map_t obj,
node_t *node UNUSED,
node_t *parent UNUSED,
node_t *grandparent,
node_t *uncle UNUSED)
{
/* Rotate to the left according to grandparent */
grandparent = c_map_rotate_left(obj, grandparent);
/* Swap grandparent and uncle's colors */
c_map_color_t c1 = grandparent->color, c2 = grandparent->left->color;
grandparent->color = c2;
grandparent->left->color = c1;
}
static void c_map_r_l(c_map_t obj,
node_t *node,
node_t *parent,
node_t *grandparent,
node_t *uncle)
{
/* Rotate to the right according to parent */
parent = c_map_rotate_right(obj, parent);
/* Refigure out the identity */
node = parent->right;
grandparent = parent->up;
uncle =
(grandparent->left == parent) ? grandparent->right : grandparent->left;
/* Apply right-right case */
c_map_r_r(obj, node, parent, grandparent, uncle);
}
static void c_map_fix_colors(c_map_t obj, node_t *node)
{
/* If root, set the color to black */
if (node == obj->head) {
node->color = C_MAP_BLACK;
return;
}
/* If node's parent is black or node is root, back out. */
if (node->up->color == C_MAP_BLACK && node->up != obj->head)
return;
/* Find out the identity */
node_t *parent = node->up, *grandparent = parent->up, *uncle;
if (!parent->up)
return;
/* Find out the uncle */
if (grandparent->left == parent)
uncle = grandparent->right;
else
uncle = grandparent->left;
if (uncle && uncle->color == C_MAP_RED) {
/* If the uncle is red, change color of parent and uncle to black */
uncle->color = C_MAP_BLACK;
parent->color = C_MAP_BLACK;
/* Change color of grandparent to red. */
grandparent->color = C_MAP_RED;
/* Call this on the grandparent */
c_map_fix_colors(obj, grandparent);
} else if (!uncle || uncle->color == C_MAP_BLACK) {
/* If the uncle is black. */
if (parent == grandparent->left && node == parent->left)
c_map_l_l(obj, node, parent, grandparent, uncle);
else if (parent == grandparent->left && node == parent->right)
c_map_l_r(obj, node, parent, grandparent, uncle);
else if (parent == grandparent->right && node == parent->left)
c_map_r_l(obj, node, parent, grandparent, uncle);
else if (parent == grandparent->right && node == parent->right)
c_map_r_r(obj, node, parent, grandparent, uncle);
}
}
/*
* Fix the red-black tree post-BST deletion. This may involve multiple
* recolors and/or rotations depending on which node was deleted, what color
* it was, and where it was in the tree at the time of deletion.
*
* These fixes occur up and down the path of the tree, and each rotation is
* guaranteed constant time. As such, there is a maximum of O(lg n) operations
* taking place during the fixup procedure.
*/
static void c_map_delete_fixup(c_map_t obj,
node_t *node,
node_t *p,
bool y_is_left,
node_t *y UNUSED)
{
node_t *w;
c_map_color_t lc, rc;
if (!node)
return;
while (node != obj->head && node->color == C_MAP_BLACK) {
if (y_is_left) { /* if left child */
w = p->right;
if (w->color == C_MAP_RED) {
w->color = C_MAP_BLACK;
p->color = C_MAP_RED;
p = c_map_rotate_left(obj, p)->left;
w = p->right;
}
lc = !w->left ? C_MAP_BLACK : w->left->color;
rc = !w->right ? C_MAP_BLACK : w->right->color;
if (lc == C_MAP_BLACK && rc == C_MAP_BLACK) {
w->color = C_MAP_RED;
node = node->up;
p = node->up;
if (p)
y_is_left = (node == p->left);
} else {
if (rc == C_MAP_BLACK) {
w->left->color = C_MAP_BLACK;
w->color = C_MAP_RED;
w = c_map_rotate_right(obj, w);
w = p->right;
}
w->color = p->color;
p->color = C_MAP_BLACK;
if (w->right)
w->right->color = C_MAP_BLACK;
p = c_map_rotate_left(obj, p);
node = obj->head;
p = NULL;
}
} else {
/* Same except flipped "left" and "right" */
w = p->left;
if (w->color == C_MAP_RED) {
w->color = C_MAP_BLACK;
p->color = C_MAP_RED;
p = c_map_rotate_right(obj, p)->right;
w = p->left;
}
lc = !w->left ? C_MAP_BLACK : w->left->color;
rc = !w->right ? C_MAP_BLACK : w->right->color;
if (lc == C_MAP_BLACK && rc == C_MAP_BLACK) {
w->color = C_MAP_RED;
node = node->up;
p = node->up;
if (p)
y_is_left = (node == p->left);
} else {
if (lc == C_MAP_BLACK) {
w->right->color = C_MAP_BLACK;
w->color = C_MAP_RED;
w = c_map_rotate_left(obj, w);
w = p->left;
}
w->color = p->color;
p->color = C_MAP_BLACK;
if (w->left)
w->left->color = C_MAP_BLACK;
p = c_map_rotate_right(obj, p);
node = obj->head;
p = NULL;
}
}
}
node->color = C_MAP_BLACK;
}
/*
* Recalculate the positions of the "least" and "most" iterators in the
* tree. This is so iterators know where the beginning and end of the tree
* resides.
*/
static void c_map_calibrate(c_map_t obj)
{
if (!obj->head) {
obj->it_least.node = obj->it_most.node = NULL;
return;
}
// Recompute it_least and it_most
obj->it_least.node = obj->it_most.node = obj->head;
while (obj->it_least.node->left)
obj->it_least.node = obj->it_least.node->left;
while (obj->it_most.node->right)
obj->it_most.node = obj->it_most.node->right;
}
/*
* Sets up a brand new, blank c_map for use. The size of the node elements
* is determined by what types are thrown in. "s1" is the size of the key
* elements in bytes, while "s2" is the size of the value elements in
* bytes.
*
* Since this is also a tree data structure, a comparison function is also
* required to be passed in. A destruct function is optional and must be
* added in through another function.
*/
c_map_t c_map_new(size_t s1, size_t s2, int (*cmp)(void *, void *))
{
c_map_t obj = malloc(sizeof(struct c_map_internal));
// Set all pointers to NULL
obj->head = NULL;
// Set up all default properties
obj->key_size = s1;
obj->element_size = s2;
obj->size = 0;
// Function pointers
obj->comparator = cmp;
obj->it_end.prev = obj->it_end.node = NULL;
obj->it_least.prev = obj->it_least.node = NULL;
obj->it_most.prev = obj->it_most.node = NULL;
obj->it_most.node = NULL;
return obj;
}
/*
* Insert a key/value pair into the c_map. The value can be blank. If so,
* it is filled with 0's, as defined in "c_map_create_node".
*/
bool c_map_insert(c_map_t obj, node_t *node, void *value)
{
/* Copy the key and value into new node and prepare it to put into tree. */
//c_map_node_t *new_node =
// c_map_create_node(&node->value, value, obj->key_size, obj->element_size);
c_map_create_node(node);
//node->rb_node = *new_node;
obj->size++;
if (!obj->head) {
/* Just insert the node in as the new head. */
obj->head = node;
obj->head->color = C_MAP_BLACK;
/* Calibrate the tree to properly assign pointers. */
c_map_calibrate(obj);
return true;
}
/* Traverse the tree until we hit the end or find a side that is NULL */
node_t *cur = obj->head;
while (1) {
int res = obj->comparator(&node->value, &cur->value);
if (res == 0) { /* If the key matches something else, don't insert */
assert(0 && "not support repetitive value");
}
if (res < 0) {
if (!cur->left) {
cur->left = node;
node->up = cur;
c_map_fix_colors(obj, node);
break;
}
cur = cur->left;
} else {
if (!cur->right) {
cur->right = node;
node->up = cur;
c_map_fix_colors(obj, node);
break;
}
cur = cur->right;
}
}
c_map_calibrate(obj);
return true;
}
node_t *c_map_first(c_map_t obj)
{
node_t *n;
n = obj->head;
if (!n)
return NULL;
while (n->left)
n = n->left;
return n;
}
node_t *c_map_next(node_t *node)
{
node_t *parent;
if (!node)
return NULL;
/*
* If we have a right-hand child, go down and then left as far
* as we can.
*/
if (node->right) {
node = node->right;
while (node->left)
node = node->left;
return node;
}
/*
* No right-hand children. Everything down and left is smaller than us,
* so any 'next' node must be in the general direction of our parent.
* Go up the tree; any time the ancestor is a right-hand child of its
* parent, keep going up. First time it's a left-hand child of its
* parent, said parent is our 'next' node.
*/
while ((parent = node->up) && node == parent->right)
node = parent;
return parent;
}
/* Free the c_map from memory and delete all nodes. */
void c_map_delete(c_map_t obj)
{
/* Free the map itself */
free(obj);
}