-
Notifications
You must be signed in to change notification settings - Fork 3
/
rb.h
395 lines (377 loc) · 29.5 KB
/
rb.h
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
/******************************************************************************
*
* Copyright (C) 2002 Jason Evans <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice(s), this list of conditions and the following disclaimer
* unmodified other than the allowable addition of one or more
* copyright notices.
* 2. Redistributions in binary form must reproduce the above copyright
* notice(s), this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*
* cpp macro implementation of red-black trees. Red-black trees are difficult
* to explain without lots of diagrams, so little attempt is made to document
* this code. However, an excellent discussion can be found in the following
* book, which was used as the reference for writing this implementation:
*
* Introduction to Algorithms
* Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest
* MIT Press (1990)
* ISBN 0-07-013143-0
*
* Some macros use a comparison function pointer, which is expected to have the
* following prototype:
*
* int (compare *)(<a_type> *a_a, <a_type> *a_b);
*
* Interpretation of comparision function return values:
*
* -1 : a_a < a_b
* 0 : a_a == a_b
* 1 : a_a > a_b
*
* Some of the macros expand out to be quite a bit of code, so if they are
* called in a program in more than a couple of places for a particular type, it
* is probably a good idea to create functions that wrap the macros to keep code
* size down.
*
******************************************************************************/
/* Node structure. */
#define rb_node(a_type) \
struct { \
a_type *rbn_par; \
a_type *rbn_left; \
a_type *rbn_right; \
boolean_t rbn_red; \
}
#define rb_node_new(a_tree, a_node, a_field) \
do { \
(a_node)->a_field.rbn_par = &(a_tree)->rbt_nil; \
(a_node)->a_field.rbn_left = &(a_tree)->rbt_nil; \
(a_node)->a_field.rbn_right = &(a_tree)->rbt_nil; \
(a_node)->a_field.rbn_red = FALSE; \
} while (0)
/* Root structure. */
#define rb_tree(a_type) \
struct { \
a_type *rbt_root; \
a_type rbt_nil; \
}
#define rb_tree_new(a_tree, a_field) \
do { \
(a_tree)->rbt_root = &((a_tree)->rbt_nil); \
rb_node_new(a_tree, &(a_tree)->rbt_nil, a_field); \
} while (0)
#define rb_tree_nil(a_tree) (&(a_tree)->rbt_nil)
/* Operations. */
#define rb_root(a_tree) (a_tree)->rbt_root
#define rb_p_first(a_tree, a_root, a_field, r_node) \
do { \
for ((r_node) = (a_root); (r_node)->a_field.rbn_left != &(a_tree)->rbt_nil; \
(r_node) = (r_node)->a_field.rbn_left) { } \
} while (0)
#define rb_p_last(a_tree, a_root, a_field, r_node) \
do { \
for ((r_node) = (a_root); (r_node)->a_field.rbn_right != &(a_tree)->rbt_nil; \
(r_node) = (r_node)->a_field.rbn_right) { } \
} while (0)
#define rb_first(a_tree, a_field, r_node) rb_p_first(a_tree, rb_root(a_tree), a_field, r_node)
#define rb_last(a_tree, a_field, r_node) rb_p_last(a_tree, rb_root(a_tree), a_field, r_node)
#define rb_next(a_tree, a_node, a_type, a_field, r_node) \
do { \
if ((a_node)->a_field.rbn_right != &(a_tree)->rbt_nil) { \
rb_p_first(a_tree, (a_node)->a_field.rbn_right, a_field, r_node); \
} else { \
a_type *t = (a_node); \
(r_node) = (a_node)->a_field.rbn_par; \
while ((r_node) != &(a_tree)->rbt_nil && t == (r_node)->a_field.rbn_right) { \
t = (r_node); \
(r_node) = (r_node)->a_field.rbn_par; \
} \
} \
} while (0)
#define rb_prev(a_tree, a_node, a_type, a_field, r_node) \
do { \
if ((a_node)->a_field.rbn_left != &(a_tree)->rbt_nil) { \
rb_p_last(a_tree, (a_node)->a_field.rbn_left, a_field, r_node); \
} else { \
a_type *t = (a_node); \
(r_node) = (a_node)->a_field.rbn_par; \
while ((r_node) != &(a_tree)->rbt_nil && t == (r_node)->a_field.rbn_left) { \
t = (r_node); \
(r_node) = (r_node)->a_field.rbn_par; \
} \
} \
} while (0)
/* a_key is always the first argument to a_comp. */
#define rb_search(a_tree, a_key, a_comp, a_field, r_node) \
do { \
int t; \
(r_node) = (a_tree)->rbt_root; \
while ((r_node) != &(a_tree)->rbt_nil && (t = (a_comp)((a_key), (r_node))) != 0) { \
if (t == -1) { \
(r_node) = (r_node)->a_field.rbn_left; \
} else { \
(r_node) = (r_node)->a_field.rbn_right; \
} \
} \
} while (0)
/* Find a match if it exists. Otherwise, find the next greater node, if one
* exists. */
#define rb_nsearch(a_tree, a_key, a_comp, a_type, a_field, r_node) \
do { \
int t; \
(r_node) = (a_tree)->rbt_root; \
while ((r_node) != &(a_tree)->rbt_nil && (t = (a_comp)((a_key), (r_node))) != 0) { \
if (t == -1) { \
if ((r_node)->a_field.rbn_left == &(a_tree)->rbt_nil) { \
break; \
} \
(r_node) = (r_node)->a_field.rbn_left; \
} else { \
if ((r_node)->a_field.rbn_right == &(a_tree)->rbt_nil) { \
a_type *n = (r_node); \
(r_node) = (r_node)->a_field.rbn_par; \
while ((r_node) != &(a_tree)->rbt_nil && n == (r_node)->a_field.rbn_right) { \
n = (r_node); \
(r_node) = (r_node)->a_field.rbn_par; \
} \
break; \
} \
(r_node) = (r_node)->a_field.rbn_right; \
} \
} \
} while (0)
#define rb_p_left_rotate(a_tree, a_node, a_type, a_field) \
do { \
a_type *t = (a_node)->a_field.rbn_right; \
(a_node)->a_field.rbn_right = t->a_field.rbn_left; \
if (t->a_field.rbn_left != &(a_tree)->rbt_nil) { \
t->a_field.rbn_left->a_field.rbn_par = (a_node); \
} \
t->a_field.rbn_par = (a_node)->a_field.rbn_par; \
if ((a_node)->a_field.rbn_par == &(a_tree)->rbt_nil) { \
(a_tree)->rbt_root = t; \
} else if ((a_node) == (a_node)->a_field.rbn_par->a_field.rbn_left) { \
(a_node)->a_field.rbn_par->a_field.rbn_left = t; \
} else { \
(a_node)->a_field.rbn_par->a_field.rbn_right = t; \
} \
t->a_field.rbn_left = (a_node); \
(a_node)->a_field.rbn_par = t; \
} while (0)
#define rb_p_right_rotate(a_tree, a_node, a_type, a_field) \
do { \
a_type *t = (a_node)->a_field.rbn_left; \
(a_node)->a_field.rbn_left = t->a_field.rbn_right; \
if (t->a_field.rbn_right != &(a_tree)->rbt_nil) { \
t->a_field.rbn_right->a_field.rbn_par = (a_node); \
} \
t->a_field.rbn_par = (a_node)->a_field.rbn_par; \
if ((a_node)->a_field.rbn_par == &(a_tree)->rbt_nil) { \
(a_tree)->rbt_root = t; \
} else if ((a_node) == (a_node)->a_field.rbn_par->a_field.rbn_right) { \
(a_node)->a_field.rbn_par->a_field.rbn_right = t; \
} else { \
(a_node)->a_field.rbn_par->a_field.rbn_left = t; \
} \
t->a_field.rbn_right = (a_node); \
(a_node)->a_field.rbn_par = t; \
} while (0)
/* a_node is always the first argument to a_comp. */
#define rb_insert(a_tree, a_node, a_comp, a_type, a_field) \
do { \
/* Insert. */ \
a_type *x = &(a_tree)->rbt_nil; \
a_type *y = (a_tree)->rbt_root; \
int c = 0; \
while (y != &(a_tree)->rbt_nil) { \
x = y; \
c = (a_comp)((a_node), y); \
if (c == -1) { \
y = y->a_field.rbn_left; \
} else { \
y = y->a_field.rbn_right; \
} \
} \
(a_node)->a_field.rbn_par = x; \
if (x == &(a_tree)->rbt_nil) { \
(a_tree)->rbt_root = (a_node); \
} else if (c == -1) { \
x->a_field.rbn_left = (a_node); \
} else { \
x->a_field.rbn_right = (a_node); \
} \
/* Fix up. */ \
x = (a_node); \
x->a_field.rbn_red = TRUE; \
while (x != (a_tree)->rbt_root && x->a_field.rbn_par->a_field.rbn_red) { \
y = x->a_field.rbn_par; \
if (y == y->a_field.rbn_par->a_field.rbn_left) { \
y = y->a_field.rbn_par->a_field.rbn_right; \
if (y->a_field.rbn_red) { \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
y->a_field.rbn_red = FALSE; \
x->a_field.rbn_par->a_field.rbn_par->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par->a_field.rbn_par; \
} else { \
if (x == x->a_field.rbn_par->a_field.rbn_right) { \
x = x->a_field.rbn_par; \
rb_p_left_rotate(a_tree, x, a_type, a_field); \
} \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
x->a_field.rbn_par->a_field.rbn_par->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par->a_field.rbn_par; \
rb_p_right_rotate(a_tree, x, a_type, a_field); \
} \
} else { \
y = y->a_field.rbn_par->a_field.rbn_left; \
if (y->a_field.rbn_red) { \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
y->a_field.rbn_red = FALSE; \
x->a_field.rbn_par->a_field.rbn_par->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par->a_field.rbn_par; \
} else { \
if (x == x->a_field.rbn_par->a_field.rbn_left) { \
x = x->a_field.rbn_par; \
rb_p_right_rotate(a_tree, x, a_type, a_field); \
} \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
x->a_field.rbn_par->a_field.rbn_par->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par->a_field.rbn_par; \
rb_p_left_rotate(a_tree, x, a_type, a_field); \
} \
} \
} \
(a_tree)->rbt_root->a_field.rbn_red = FALSE; \
} while (0)
#define rb_remove(a_tree, a_node, a_type, a_field) \
do { \
boolean_t fixup; \
a_type *x, *y; \
if ((a_node)->a_field.rbn_left == &(a_tree)->rbt_nil \
|| (a_node)->a_field.rbn_right == &(a_tree)->rbt_nil) { \
y = (a_node); \
} else { \
rb_next(a_tree, a_node, a_type, a_field, y); \
} \
if (y->a_field.rbn_left != &(a_tree)->rbt_nil) { \
x = y->a_field.rbn_left; \
} else { \
x = y->a_field.rbn_right; \
} \
x->a_field.rbn_par = y->a_field.rbn_par; \
if (y->a_field.rbn_par == &(a_tree)->rbt_nil) { \
(a_tree)->rbt_root = x; \
} else if (y == y->a_field.rbn_par->a_field.rbn_left) { \
y->a_field.rbn_par->a_field.rbn_left = x; \
} else { \
y->a_field.rbn_par->a_field.rbn_right = x; \
} \
if (y->a_field.rbn_red == FALSE) { \
fixup = TRUE; \
} else { \
fixup = FALSE; \
} \
if (y != (a_node)) { \
/* Splice y into a_node's location. */ \
y->a_field.rbn_par = (a_node)->a_field.rbn_par; \
y->a_field.rbn_left = (a_node)->a_field.rbn_left; \
y->a_field.rbn_right = (a_node)->a_field.rbn_right; \
y->a_field.rbn_red = (a_node)->a_field.rbn_red; \
if (y->a_field.rbn_par != &(a_tree)->rbt_nil) { \
if (y->a_field.rbn_par->a_field.rbn_left == (a_node)) { \
y->a_field.rbn_par->a_field.rbn_left = y; \
} else { \
y->a_field.rbn_par->a_field.rbn_right = y; \
} \
} else { \
(a_tree)->rbt_root = y; \
} \
y->a_field.rbn_right->a_field.rbn_par = y; \
y->a_field.rbn_left->a_field.rbn_par = y; \
} \
rb_node_new(a_tree, a_node, a_field); \
if (fixup) { \
/* Fix up. */ \
a_type *v, *w; \
while (x != (a_tree)->rbt_root && x->a_field.rbn_red == FALSE) { \
if (x == x->a_field.rbn_par->a_field.rbn_left) { \
w = x->a_field.rbn_par->a_field.rbn_right; \
if (w->a_field.rbn_red) { \
w->a_field.rbn_red = FALSE; \
v = x->a_field.rbn_par; \
v->a_field.rbn_red = TRUE; \
rb_p_left_rotate(a_tree, v, a_type, a_field); \
w = x->a_field.rbn_par->a_field.rbn_right; \
} \
if (w->a_field.rbn_left->a_field.rbn_red == FALSE \
&& w->a_field.rbn_right->a_field.rbn_red == FALSE) { \
w->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par; \
} else { \
if (w->a_field.rbn_right->a_field.rbn_red == FALSE) { \
w->a_field.rbn_left->a_field.rbn_red = FALSE; \
w->a_field.rbn_red = TRUE; \
rb_p_right_rotate(a_tree, w, a_type, a_field); \
w = x->a_field.rbn_par->a_field.rbn_right; \
} \
w->a_field.rbn_red = x->a_field.rbn_par->a_field.rbn_red; \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
w->a_field.rbn_right->a_field.rbn_red = FALSE; \
v = x->a_field.rbn_par; \
rb_p_left_rotate(a_tree, v, a_type, a_field); \
break; \
} \
} else { \
w = x->a_field.rbn_par->a_field.rbn_left; \
if (w->a_field.rbn_red) { \
w->a_field.rbn_red = FALSE; \
v = x->a_field.rbn_par; \
v->a_field.rbn_red = TRUE; \
rb_p_right_rotate(a_tree, v, a_type, a_field); \
w = x->a_field.rbn_par->a_field.rbn_left; \
} \
if (w->a_field.rbn_right->a_field.rbn_red == FALSE \
&& w->a_field.rbn_left->a_field.rbn_red == FALSE) { \
w->a_field.rbn_red = TRUE; \
x = x->a_field.rbn_par; \
} else { \
if (w->a_field.rbn_left->a_field.rbn_red == FALSE) { \
w->a_field.rbn_right->a_field.rbn_red = FALSE; \
w->a_field.rbn_red = TRUE; \
rb_p_left_rotate(a_tree, w, a_type, a_field); \
w = x->a_field.rbn_par->a_field.rbn_left; \
} \
w->a_field.rbn_red = x->a_field.rbn_par->a_field.rbn_red; \
x->a_field.rbn_par->a_field.rbn_red = FALSE; \
w->a_field.rbn_left->a_field.rbn_red = FALSE; \
v = x->a_field.rbn_par; \
rb_p_right_rotate(a_tree, v, a_type, a_field); \
break; \
} \
} \
} \
x->a_field.rbn_red = FALSE; \
} \
} while (0)