-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllrb.c
More file actions
347 lines (307 loc) · 8.24 KB
/
Copy pathllrb.c
File metadata and controls
347 lines (307 loc) · 8.24 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
#include "llrb.h"
#include <stddef.h>
enum {LEFT=0,RIGHT,SIDES};
enum {BLACK=0,RED,COLORS};
/* Right rotation:
h x
/ => \
x h
*/
static struct LLRBNode* rotate(struct LLRBNode *h, int side)
{
/* Let paradigm rotation be right. Code below is
* direction-agnostic, but better understood if we keep one example
* direction in mind (see also the picture above). */
int vRight = side, vLeft = !side;
struct LLRBNode* x = h->child[vLeft];
h->child[vLeft] = x->child[vRight];
x->child[vRight] = h;
x->color = h->color;
h->color = RED;
return x;
}
static void colorFlip(struct LLRBNode *h)
{
h->color = !h->color;
h->child[LEFT]->color = !h->child[LEFT]->color;
h->child[RIGHT]->color = !h->child[RIGHT]->color;
}
static int isRed(struct LLRBNode* h)
{
return h && (h->color == RED);
}
static struct LLRBNode* fixUp(struct LLRBNode *h)
{
if (isRed(h->child[RIGHT]) && !isRed(h->child[LEFT]))
h = rotate(h, LEFT);
if (isRed(h->child[LEFT]) && (isRed(h->child[LEFT]->child[LEFT])))
h = rotate(h, RIGHT);
if (isRed(h->child[LEFT]) && (isRed(h->child[RIGHT])))
colorFlip(h);
return h;
}
static struct LLRBNode* insert(struct LLRBNode* h, struct LLRBNode* x,
struct LLRBTree* t, struct LLRBNode **old,
struct LLRBNode* p, int side, int replace)
{
int cmp;
if (!h) {
x->color = RED;
x->child[LEFT] = x->child[RIGHT] = NULL;
#ifndef LLRB_NO_LIST
/* we're left child -> our right neighbour is p */
x->neigh[1-side] = p;
/* our left neighbour is p's old left neighbour */
x->neigh[side] = p->neigh[side];
/* correct neighbour pointers */
x->neigh[1-side]->neigh[side] = x;
x->neigh[side]->neigh[1-side] = x;
#else
(void)p;
#endif
return x;
}
cmp = t->compare(h,x,t);
if (cmp==0) {
*old = h;
if (replace) {
x->child[LEFT] = h->child[LEFT];
x->child[RIGHT] = h->child[RIGHT];
x->color = h->color;
#ifndef LLRB_NO_LIST
x->neigh[LEFT] = h->neigh[LEFT];
x->neigh[RIGHT] = h->neigh[RIGHT];
x->neigh[LEFT]->neigh[RIGHT] = x;
x->neigh[RIGHT]->neigh[LEFT] = x;
#endif
return x;
} else { /* keep original */
return h;
}
} else {
side = cmp<0 ? RIGHT : LEFT;
h->child[side] = insert(h->child[side], x, t, old, h, side, replace);
return fixUp(h);
}
}
static struct LLRBNode* moveRedRight(struct LLRBNode* h)
{
colorFlip(h);
if (isRed(h->child[LEFT]->child[LEFT])) {
h = rotate(h,RIGHT);
colorFlip(h);
}
return h;
}
static struct LLRBNode* moveRedLeft(struct LLRBNode* h)
{
colorFlip(h);
if (isRed(h->child[RIGHT]->child[LEFT])) {
h->child[RIGHT] = rotate(h->child[RIGHT],RIGHT);
h = rotate(h,LEFT);
colorFlip(h);
}
return h;
}
static struct LLRBNode* deleteMin(struct LLRBNode* h, struct LLRBNode** old)
{
if (!h->child[LEFT]) {
*old = h;
#ifndef LLRB_NO_LIST
h->neigh[LEFT]->neigh[RIGHT] = h->neigh[RIGHT];
h->neigh[RIGHT]->neigh[LEFT] = h->neigh[LEFT];
#endif
return NULL;
} else {
if (!isRed(h->child[LEFT])&&!isRed(h->child[LEFT]->child[LEFT])) {
h = moveRedLeft(h);
}
h->child[LEFT] = deleteMin(h->child[LEFT], old);
return fixUp(h);
}
}
static struct LLRBNode* deleteKey(struct LLRBNode* h, struct LLRBNode* key,
struct LLRBTree* t, struct LLRBNode **old)
{
if (t->compare(key, h, t) < 0) {
if (!h->child[LEFT]) {
return h;
}
/* going left */
if (!isRed(h->child[LEFT])&&!isRed(h->child[LEFT]->child[LEFT]))
h = moveRedLeft(h);
h->child[LEFT] = deleteKey(h->child[LEFT], key, t, old);
} else {
if (isRed(h->child[LEFT]))
h = rotate(h,RIGHT);
if (t->compare(key,h,t) == 0 && !h->child[RIGHT]) {
*old = h;
#ifndef LLRB_NO_LIST
h->neigh[LEFT]->neigh[RIGHT] = h->neigh[RIGHT];
h->neigh[RIGHT]->neigh[LEFT] = h->neigh[LEFT];
#endif
return NULL;
}
if (!h->child[RIGHT])
return h;
if (!isRed(h->child[RIGHT])&& !isRed(h->child[RIGHT]->child[LEFT]))
h = moveRedRight(h);
if (t->compare(key,h,t) == 0) {
struct LLRBNode* rmin;
h->child[RIGHT] = deleteMin(h->child[RIGHT], &rmin);
rmin->child[LEFT] = h->child[LEFT];
rmin->child[RIGHT] = h->child[RIGHT];
rmin->color = h->color;
#ifndef LLRB_NO_LIST
rmin->neigh[LEFT] = h->neigh[LEFT];
rmin->neigh[RIGHT] = h->neigh[RIGHT];
rmin->neigh[LEFT]->neigh[RIGHT] = rmin;
rmin->neigh[RIGHT]->neigh[LEFT] = rmin;
#endif
*old = h;
h = rmin;
} else {
h->child[RIGHT] = deleteKey(h->child[RIGHT],key,t,old);
}
}
return fixUp(h);
}
/* Public functions */
void llrb_init(struct LLRBTree* t, LLRBComparator* compare)
{
t->compare = compare;
t->root = NULL;
#ifndef LLRB_NO_LIST
t->anchor.neigh[LEFT] = t->anchor.neigh[RIGHT] = &t->anchor;
t->anchor.child[LEFT] = t->anchor.child[RIGHT] = NULL;
#endif
}
/* insert node into tree, returning old node with the same key.
* replace old node if it exists */
struct LLRBNode *llrb_insert_or_replace(struct LLRBTree *tree, struct LLRBNode* node)
{
struct LLRBNode *old = NULL;
#ifndef LLRB_NO_LIST
tree->root = insert(tree->root, node, tree, &old, &tree->anchor, LEFT, 1);
#else
tree->root = insert(tree->root, node, tree, &old, NULL, LEFT, 1);
#endif
if (tree->root)
tree->root->color = BLACK;
return old;
}
/* insert node into tree, returning old node with the same key.
* don't replace old node if it exists */
struct LLRBNode *llrb_insert_new(struct LLRBTree *tree, struct LLRBNode* node)
{
struct LLRBNode *old = NULL;
#ifndef LLRB_NO_LIST
tree->root = insert(tree->root, node, tree, &old, &tree->anchor, LEFT, 0);
#else
tree->root = insert(tree->root, node, tree, &old, NULL, LEFT, 0);
#endif
if (tree->root)
tree->root->color = BLACK;
return old;
}
/* find node equal to key under tree->compare */
struct LLRBNode *llrb_find(struct LLRBTree *tree, struct LLRBNode* key)
{
struct LLRBNode *h = tree->root;
while (h) {
int cmp = tree->compare(key,h,tree);
if (cmp == 0) {
return h;
} else if (cmp < 0) {
h = h->child[LEFT];
} else {
h = h->child[RIGHT];
}
}
return NULL;
}
/* unlink minimum (leftmost) node and return it */
struct LLRBNode* llrb_pop_min(struct LLRBTree *tree)
{
struct LLRBNode *old = NULL;
if (tree->root) {
tree->root = deleteMin(tree->root, &old);
if (tree->root)
tree->root->color = BLACK;
}
return old;
}
/* unlink node equal to key under tree->compare, return unlinked
* node or NULL if not found. */
struct LLRBNode* llrb_delete(struct LLRBTree *tree, struct LLRBNode *key)
{
struct LLRBNode *old = NULL;
if (tree->root) {
tree->root = deleteKey(tree->root, key, tree, &old);
if (tree->root) {
tree->root->color = BLACK;
}
}
return old;
}
#ifndef LLRB_NO_LIST
/* Follow neighbour pointers (to the right if isRight, to the left
* otherwise), return corresponding neighbour if it exists, NULL
* otherwise */
static struct LLRBNode* llrb_neighbour(struct LLRBTree *tree, struct LLRBNode* h, int side)
{
struct LLRBNode* x = h->neigh[side];
if (x == &tree->anchor)
return NULL;
return x;
}
struct LLRBNode* llrb_next(struct LLRBTree *tree, struct LLRBNode* h)
{
return llrb_neighbour(tree, h, 1);
}
struct LLRBNode* llrb_prev(struct LLRBTree *tree, struct LLRBNode* h)
{
return llrb_neighbour(tree, h, 0);
}
/* Minimum (leftmost) node */
struct LLRBNode* llrb_min(struct LLRBTree *tree)
{
struct LLRBNode *h = tree->anchor.neigh[RIGHT];
if (h == &tree->anchor)
return NULL;
return h;
}
/* Maximum (rightmost) node */
struct LLRBNode* llrb_max(struct LLRBTree *tree)
{
struct LLRBNode *h = tree->anchor.neigh[LEFT];
if (h == &tree->anchor)
return NULL;
return h;
}
#else /* LLRB_NO_LIST defined */
struct LLRBNode* llrb_min(struct LLRBTree *tree)
{
struct LLRBNode *h = tree->root;
if (h) {
while (h->child[LEFT])
h = h->child[LEFT];
}
return h;
}
struct LLRBNode* llrb_max(struct LLRBTree *tree)
{
struct LLRBNode *h = tree->root;
if (h) {
while (h->child[RIGHT])
h = h->child[RIGHT];
}
return h;
}
#endif
/* Comparator for pointers */
int llrb_ptrcmp(struct LLRBNode* a, struct LLRBNode* b,struct LLRBTree *tree)
{
(void)tree;
return a == b ? 0 : ((char*)a < (char*)b ? -1 : 1);
}