Blob


1 /* $OpenBSD: tree.h,v 1.30 2020/10/10 18:03:41 otto Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
27 #ifndef _SYS_TREE_H_
28 #define _SYS_TREE_H_
30 #include <stddef.h>
32 /*
33 * Local modifications:
34 * - dropped __unused
35 * - __inline -> inline
36 */
38 /*
39 * This file defines data structures for different types of trees:
40 * splay trees and red-black trees.
41 *
42 * A splay tree is a self-organizing data structure. Every operation
43 * on the tree causes a splay to happen. The splay moves the requested
44 * node to the root of the tree and partly rebalances it.
45 *
46 * This has the benefit that request locality causes faster lookups as
47 * the requested nodes move to the top of the tree. On the other hand,
48 * every lookup causes memory writes.
49 *
50 * The Balance Theorem bounds the total access time for m operations
51 * and n inserts on an initially empty tree as O((m + n)lg n). The
52 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
53 *
54 * A red-black tree is a binary search tree with the node color as an
55 * extra attribute. It fulfills a set of conditions:
56 * - every search path from the root to a leaf consists of the
57 * same number of black nodes,
58 * - each red node (except for the root) has a black parent,
59 * - each leaf node is black.
60 *
61 * Every operation on a red-black tree is bounded as O(lg n).
62 * The maximum height of a red-black tree is 2lg (n+1).
63 */
65 #define SPLAY_HEAD(name, type) \
66 struct name { \
67 struct type *sph_root; /* root of the tree */ \
68 }
70 #define SPLAY_INITIALIZER(root) \
71 { NULL }
73 #define SPLAY_INIT(root) do { \
74 (root)->sph_root = NULL; \
75 } while (0)
77 #define SPLAY_ENTRY(type) \
78 struct { \
79 struct type *spe_left; /* left element */ \
80 struct type *spe_right; /* right element */ \
81 }
83 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
84 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
85 #define SPLAY_ROOT(head) (head)->sph_root
86 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
88 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
89 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
90 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
91 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
92 (head)->sph_root = tmp; \
93 } while (0)
95 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
96 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
97 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
98 (head)->sph_root = tmp; \
99 } while (0)
101 #define SPLAY_LINKLEFT(head, tmp, field) do { \
102 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
103 tmp = (head)->sph_root; \
104 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
105 } while (0)
107 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
108 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
109 tmp = (head)->sph_root; \
110 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
111 } while (0)
113 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
114 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
115 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
116 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
117 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
118 } while (0)
120 /* Generates prototypes and inline functions */
122 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
123 void name##_SPLAY(struct name *, struct type *); \
124 void name##_SPLAY_MINMAX(struct name *, int); \
125 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
126 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
128 /* Finds the node with the same key as elm */ \
129 static inline struct type * \
130 name##_SPLAY_FIND(struct name *head, struct type *elm) \
131 { \
132 if (SPLAY_EMPTY(head)) \
133 return(NULL); \
134 name##_SPLAY(head, elm); \
135 if ((cmp)(elm, (head)->sph_root) == 0) \
136 return (head->sph_root); \
137 return (NULL); \
138 } \
140 static inline struct type * \
141 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
142 { \
143 name##_SPLAY(head, elm); \
144 if (SPLAY_RIGHT(elm, field) != NULL) { \
145 elm = SPLAY_RIGHT(elm, field); \
146 while (SPLAY_LEFT(elm, field) != NULL) { \
147 elm = SPLAY_LEFT(elm, field); \
148 } \
149 } else \
150 elm = NULL; \
151 return (elm); \
152 } \
154 static inline struct type * \
155 name##_SPLAY_MIN_MAX(struct name *head, int val) \
156 { \
157 name##_SPLAY_MINMAX(head, val); \
158 return (SPLAY_ROOT(head)); \
161 /* Main splay operation.
162 * Moves node close to the key of elm to top
163 */
164 #define SPLAY_GENERATE(name, type, field, cmp) \
165 struct type * \
166 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
167 { \
168 if (SPLAY_EMPTY(head)) { \
169 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
170 } else { \
171 int __comp; \
172 name##_SPLAY(head, elm); \
173 __comp = (cmp)(elm, (head)->sph_root); \
174 if(__comp < 0) { \
175 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
176 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
177 SPLAY_LEFT((head)->sph_root, field) = NULL; \
178 } else if (__comp > 0) { \
179 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
180 SPLAY_LEFT(elm, field) = (head)->sph_root; \
181 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
182 } else \
183 return ((head)->sph_root); \
184 } \
185 (head)->sph_root = (elm); \
186 return (NULL); \
187 } \
189 struct type * \
190 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
191 { \
192 struct type *__tmp; \
193 if (SPLAY_EMPTY(head)) \
194 return (NULL); \
195 name##_SPLAY(head, elm); \
196 if ((cmp)(elm, (head)->sph_root) == 0) { \
197 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
198 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
199 } else { \
200 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
201 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
202 name##_SPLAY(head, elm); \
203 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
204 } \
205 return (elm); \
206 } \
207 return (NULL); \
208 } \
210 void \
211 name##_SPLAY(struct name *head, struct type *elm) \
212 { \
213 struct type __node, *__left, *__right, *__tmp; \
214 int __comp; \
216 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
217 __left = __right = &__node; \
219 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
220 if (__comp < 0) { \
221 __tmp = SPLAY_LEFT((head)->sph_root, field); \
222 if (__tmp == NULL) \
223 break; \
224 if ((cmp)(elm, __tmp) < 0){ \
225 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
226 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
227 break; \
228 } \
229 SPLAY_LINKLEFT(head, __right, field); \
230 } else if (__comp > 0) { \
231 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
232 if (__tmp == NULL) \
233 break; \
234 if ((cmp)(elm, __tmp) > 0){ \
235 SPLAY_ROTATE_LEFT(head, __tmp, field); \
236 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
237 break; \
238 } \
239 SPLAY_LINKRIGHT(head, __left, field); \
240 } \
241 } \
242 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
243 } \
245 /* Splay with either the minimum or the maximum element \
246 * Used to find minimum or maximum element in tree. \
247 */ \
248 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
249 { \
250 struct type __node, *__left, *__right, *__tmp; \
252 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
253 __left = __right = &__node; \
255 while (1) { \
256 if (__comp < 0) { \
257 __tmp = SPLAY_LEFT((head)->sph_root, field); \
258 if (__tmp == NULL) \
259 break; \
260 if (__comp < 0){ \
261 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
262 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
263 break; \
264 } \
265 SPLAY_LINKLEFT(head, __right, field); \
266 } else if (__comp > 0) { \
267 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
268 if (__tmp == NULL) \
269 break; \
270 if (__comp > 0) { \
271 SPLAY_ROTATE_LEFT(head, __tmp, field); \
272 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
273 break; \
274 } \
275 SPLAY_LINKRIGHT(head, __left, field); \
276 } \
277 } \
278 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
281 #define SPLAY_NEGINF -1
282 #define SPLAY_INF 1
284 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
285 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
286 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
287 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
288 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
289 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
290 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
291 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
293 #define SPLAY_FOREACH(x, name, head) \
294 for ((x) = SPLAY_MIN(name, head); \
295 (x) != NULL; \
296 (x) = SPLAY_NEXT(name, head, x))
298 /* Macros that define a red-black tree */
299 #define RB_HEAD(name, type) \
300 struct name { \
301 struct type *rbh_root; /* root of the tree */ \
304 #define RB_INITIALIZER(root) \
305 { NULL }
307 #define RB_INIT(root) do { \
308 (root)->rbh_root = NULL; \
309 } while (0)
311 #define RB_BLACK 0
312 #define RB_RED 1
313 #define RB_ENTRY(type) \
314 struct { \
315 struct type *rbe_left; /* left element */ \
316 struct type *rbe_right; /* right element */ \
317 struct type *rbe_parent; /* parent element */ \
318 int rbe_color; /* node color */ \
321 #define RB_LEFT(elm, field) (elm)->field.rbe_left
322 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
323 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
324 #define RB_COLOR(elm, field) (elm)->field.rbe_color
325 #define RB_ROOT(head) (head)->rbh_root
326 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
328 #define RB_SET(elm, parent, field) do { \
329 RB_PARENT(elm, field) = parent; \
330 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
331 RB_COLOR(elm, field) = RB_RED; \
332 } while (0)
334 #define RB_SET_BLACKRED(black, red, field) do { \
335 RB_COLOR(black, field) = RB_BLACK; \
336 RB_COLOR(red, field) = RB_RED; \
337 } while (0)
339 #ifndef RB_AUGMENT
340 #define RB_AUGMENT(x) do {} while (0)
341 #endif
343 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
344 (tmp) = RB_RIGHT(elm, field); \
345 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
346 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
347 } \
348 RB_AUGMENT(elm); \
349 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
350 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
351 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
352 else \
353 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
354 } else \
355 (head)->rbh_root = (tmp); \
356 RB_LEFT(tmp, field) = (elm); \
357 RB_PARENT(elm, field) = (tmp); \
358 RB_AUGMENT(tmp); \
359 if ((RB_PARENT(tmp, field))) \
360 RB_AUGMENT(RB_PARENT(tmp, field)); \
361 } while (0)
363 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
364 (tmp) = RB_LEFT(elm, field); \
365 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
366 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
367 } \
368 RB_AUGMENT(elm); \
369 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
370 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
371 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
372 else \
373 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
374 } else \
375 (head)->rbh_root = (tmp); \
376 RB_RIGHT(tmp, field) = (elm); \
377 RB_PARENT(elm, field) = (tmp); \
378 RB_AUGMENT(tmp); \
379 if ((RB_PARENT(tmp, field))) \
380 RB_AUGMENT(RB_PARENT(tmp, field)); \
381 } while (0)
383 /* Generates prototypes and inline functions */
384 #define RB_PROTOTYPE(name, type, field, cmp) \
385 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
386 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
387 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
388 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
389 attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
390 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
391 attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
392 attr struct type *name##_RB_INSERT(struct name *, struct type *); \
393 attr struct type *name##_RB_FIND(struct name *, struct type *); \
394 attr struct type *name##_RB_NFIND(struct name *, struct type *); \
395 attr struct type *name##_RB_NEXT(struct type *); \
396 attr struct type *name##_RB_PREV(struct type *); \
397 attr struct type *name##_RB_MINMAX(struct name *, int); \
400 /* Main rb operation.
401 * Moves node close to the key of elm to top
402 */
403 #define RB_GENERATE(name, type, field, cmp) \
404 RB_GENERATE_INTERNAL(name, type, field, cmp,)
405 #define RB_GENERATE_STATIC(name, type, field, cmp) \
406 RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
407 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
408 attr void \
409 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
410 { \
411 struct type *parent, *gparent, *tmp; \
412 while ((parent = RB_PARENT(elm, field)) && \
413 RB_COLOR(parent, field) == RB_RED) { \
414 gparent = RB_PARENT(parent, field); \
415 if (parent == RB_LEFT(gparent, field)) { \
416 tmp = RB_RIGHT(gparent, field); \
417 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
418 RB_COLOR(tmp, field) = RB_BLACK; \
419 RB_SET_BLACKRED(parent, gparent, field);\
420 elm = gparent; \
421 continue; \
422 } \
423 if (RB_RIGHT(parent, field) == elm) { \
424 RB_ROTATE_LEFT(head, parent, tmp, field);\
425 tmp = parent; \
426 parent = elm; \
427 elm = tmp; \
428 } \
429 RB_SET_BLACKRED(parent, gparent, field); \
430 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
431 } else { \
432 tmp = RB_LEFT(gparent, field); \
433 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
434 RB_COLOR(tmp, field) = RB_BLACK; \
435 RB_SET_BLACKRED(parent, gparent, field);\
436 elm = gparent; \
437 continue; \
438 } \
439 if (RB_LEFT(parent, field) == elm) { \
440 RB_ROTATE_RIGHT(head, parent, tmp, field);\
441 tmp = parent; \
442 parent = elm; \
443 elm = tmp; \
444 } \
445 RB_SET_BLACKRED(parent, gparent, field); \
446 RB_ROTATE_LEFT(head, gparent, tmp, field); \
447 } \
448 } \
449 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
450 } \
452 attr void \
453 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
454 { \
455 struct type *tmp; \
456 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
457 elm != RB_ROOT(head)) { \
458 if (RB_LEFT(parent, field) == elm) { \
459 tmp = RB_RIGHT(parent, field); \
460 if (RB_COLOR(tmp, field) == RB_RED) { \
461 RB_SET_BLACKRED(tmp, parent, field); \
462 RB_ROTATE_LEFT(head, parent, tmp, field);\
463 tmp = RB_RIGHT(parent, field); \
464 } \
465 if ((RB_LEFT(tmp, field) == NULL || \
466 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
467 (RB_RIGHT(tmp, field) == NULL || \
468 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
469 RB_COLOR(tmp, field) = RB_RED; \
470 elm = parent; \
471 parent = RB_PARENT(elm, field); \
472 } else { \
473 if (RB_RIGHT(tmp, field) == NULL || \
474 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
475 struct type *oleft; \
476 if ((oleft = RB_LEFT(tmp, field)))\
477 RB_COLOR(oleft, field) = RB_BLACK;\
478 RB_COLOR(tmp, field) = RB_RED; \
479 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
480 tmp = RB_RIGHT(parent, field); \
481 } \
482 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
483 RB_COLOR(parent, field) = RB_BLACK; \
484 if (RB_RIGHT(tmp, field)) \
485 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
486 RB_ROTATE_LEFT(head, parent, tmp, field);\
487 elm = RB_ROOT(head); \
488 break; \
489 } \
490 } else { \
491 tmp = RB_LEFT(parent, field); \
492 if (RB_COLOR(tmp, field) == RB_RED) { \
493 RB_SET_BLACKRED(tmp, parent, field); \
494 RB_ROTATE_RIGHT(head, parent, tmp, field);\
495 tmp = RB_LEFT(parent, field); \
496 } \
497 if ((RB_LEFT(tmp, field) == NULL || \
498 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
499 (RB_RIGHT(tmp, field) == NULL || \
500 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
501 RB_COLOR(tmp, field) = RB_RED; \
502 elm = parent; \
503 parent = RB_PARENT(elm, field); \
504 } else { \
505 if (RB_LEFT(tmp, field) == NULL || \
506 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
507 struct type *oright; \
508 if ((oright = RB_RIGHT(tmp, field)))\
509 RB_COLOR(oright, field) = RB_BLACK;\
510 RB_COLOR(tmp, field) = RB_RED; \
511 RB_ROTATE_LEFT(head, tmp, oright, field);\
512 tmp = RB_LEFT(parent, field); \
513 } \
514 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
515 RB_COLOR(parent, field) = RB_BLACK; \
516 if (RB_LEFT(tmp, field)) \
517 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
518 RB_ROTATE_RIGHT(head, parent, tmp, field);\
519 elm = RB_ROOT(head); \
520 break; \
521 } \
522 } \
523 } \
524 if (elm) \
525 RB_COLOR(elm, field) = RB_BLACK; \
526 } \
528 attr struct type * \
529 name##_RB_REMOVE(struct name *head, struct type *elm) \
530 { \
531 struct type *child, *parent, *old = elm; \
532 int color; \
533 if (RB_LEFT(elm, field) == NULL) \
534 child = RB_RIGHT(elm, field); \
535 else if (RB_RIGHT(elm, field) == NULL) \
536 child = RB_LEFT(elm, field); \
537 else { \
538 struct type *left; \
539 elm = RB_RIGHT(elm, field); \
540 while ((left = RB_LEFT(elm, field))) \
541 elm = left; \
542 child = RB_RIGHT(elm, field); \
543 parent = RB_PARENT(elm, field); \
544 color = RB_COLOR(elm, field); \
545 if (child) \
546 RB_PARENT(child, field) = parent; \
547 if (parent) { \
548 if (RB_LEFT(parent, field) == elm) \
549 RB_LEFT(parent, field) = child; \
550 else \
551 RB_RIGHT(parent, field) = child; \
552 RB_AUGMENT(parent); \
553 } else \
554 RB_ROOT(head) = child; \
555 if (RB_PARENT(elm, field) == old) \
556 parent = elm; \
557 (elm)->field = (old)->field; \
558 if (RB_PARENT(old, field)) { \
559 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
560 RB_LEFT(RB_PARENT(old, field), field) = elm;\
561 else \
562 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
563 RB_AUGMENT(RB_PARENT(old, field)); \
564 } else \
565 RB_ROOT(head) = elm; \
566 RB_PARENT(RB_LEFT(old, field), field) = elm; \
567 if (RB_RIGHT(old, field)) \
568 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
569 if (parent) { \
570 left = parent; \
571 do { \
572 RB_AUGMENT(left); \
573 } while ((left = RB_PARENT(left, field))); \
574 } \
575 goto color; \
576 } \
577 parent = RB_PARENT(elm, field); \
578 color = RB_COLOR(elm, field); \
579 if (child) \
580 RB_PARENT(child, field) = parent; \
581 if (parent) { \
582 if (RB_LEFT(parent, field) == elm) \
583 RB_LEFT(parent, field) = child; \
584 else \
585 RB_RIGHT(parent, field) = child; \
586 RB_AUGMENT(parent); \
587 } else \
588 RB_ROOT(head) = child; \
589 color: \
590 if (color == RB_BLACK) \
591 name##_RB_REMOVE_COLOR(head, parent, child); \
592 return (old); \
593 } \
595 /* Inserts a node into the RB tree */ \
596 attr struct type * \
597 name##_RB_INSERT(struct name *head, struct type *elm) \
598 { \
599 struct type *tmp; \
600 struct type *parent = NULL; \
601 int comp = 0; \
602 tmp = RB_ROOT(head); \
603 while (tmp) { \
604 parent = tmp; \
605 comp = (cmp)(elm, parent); \
606 if (comp < 0) \
607 tmp = RB_LEFT(tmp, field); \
608 else if (comp > 0) \
609 tmp = RB_RIGHT(tmp, field); \
610 else \
611 return (tmp); \
612 } \
613 RB_SET(elm, parent, field); \
614 if (parent != NULL) { \
615 if (comp < 0) \
616 RB_LEFT(parent, field) = elm; \
617 else \
618 RB_RIGHT(parent, field) = elm; \
619 RB_AUGMENT(parent); \
620 } else \
621 RB_ROOT(head) = elm; \
622 name##_RB_INSERT_COLOR(head, elm); \
623 return (NULL); \
624 } \
626 /* Finds the node with the same key as elm */ \
627 attr struct type * \
628 name##_RB_FIND(struct name *head, struct type *elm) \
629 { \
630 struct type *tmp = RB_ROOT(head); \
631 int comp; \
632 while (tmp) { \
633 comp = cmp(elm, tmp); \
634 if (comp < 0) \
635 tmp = RB_LEFT(tmp, field); \
636 else if (comp > 0) \
637 tmp = RB_RIGHT(tmp, field); \
638 else \
639 return (tmp); \
640 } \
641 return (NULL); \
642 } \
644 /* Finds the first node greater than or equal to the search key */ \
645 attr struct type * \
646 name##_RB_NFIND(struct name *head, struct type *elm) \
647 { \
648 struct type *tmp = RB_ROOT(head); \
649 struct type *res = NULL; \
650 int comp; \
651 while (tmp) { \
652 comp = cmp(elm, tmp); \
653 if (comp < 0) { \
654 res = tmp; \
655 tmp = RB_LEFT(tmp, field); \
656 } \
657 else if (comp > 0) \
658 tmp = RB_RIGHT(tmp, field); \
659 else \
660 return (tmp); \
661 } \
662 return (res); \
663 } \
665 /* ARGSUSED */ \
666 attr struct type * \
667 name##_RB_NEXT(struct type *elm) \
668 { \
669 if (RB_RIGHT(elm, field)) { \
670 elm = RB_RIGHT(elm, field); \
671 while (RB_LEFT(elm, field)) \
672 elm = RB_LEFT(elm, field); \
673 } else { \
674 if (RB_PARENT(elm, field) && \
675 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
676 elm = RB_PARENT(elm, field); \
677 else { \
678 while (RB_PARENT(elm, field) && \
679 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
680 elm = RB_PARENT(elm, field); \
681 elm = RB_PARENT(elm, field); \
682 } \
683 } \
684 return (elm); \
685 } \
687 /* ARGSUSED */ \
688 attr struct type * \
689 name##_RB_PREV(struct type *elm) \
690 { \
691 if (RB_LEFT(elm, field)) { \
692 elm = RB_LEFT(elm, field); \
693 while (RB_RIGHT(elm, field)) \
694 elm = RB_RIGHT(elm, field); \
695 } else { \
696 if (RB_PARENT(elm, field) && \
697 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
698 elm = RB_PARENT(elm, field); \
699 else { \
700 while (RB_PARENT(elm, field) && \
701 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
702 elm = RB_PARENT(elm, field); \
703 elm = RB_PARENT(elm, field); \
704 } \
705 } \
706 return (elm); \
707 } \
709 attr struct type * \
710 name##_RB_MINMAX(struct name *head, int val) \
711 { \
712 struct type *tmp = RB_ROOT(head); \
713 struct type *parent = NULL; \
714 while (tmp) { \
715 parent = tmp; \
716 if (val < 0) \
717 tmp = RB_LEFT(tmp, field); \
718 else \
719 tmp = RB_RIGHT(tmp, field); \
720 } \
721 return (parent); \
724 #define RB_NEGINF -1
725 #define RB_INF 1
727 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
728 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
729 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
730 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
731 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
732 #define RB_PREV(name, x, y) name##_RB_PREV(y)
733 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
734 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
736 #define RB_FOREACH(x, name, head) \
737 for ((x) = RB_MIN(name, head); \
738 (x) != NULL; \
739 (x) = name##_RB_NEXT(x))
741 #define RB_FOREACH_SAFE(x, name, head, y) \
742 for ((x) = RB_MIN(name, head); \
743 ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
744 (x) = (y))
746 #define RB_FOREACH_REVERSE(x, name, head) \
747 for ((x) = RB_MAX(name, head); \
748 (x) != NULL; \
749 (x) = name##_RB_PREV(x))
751 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
752 for ((x) = RB_MAX(name, head); \
753 ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
754 (x) = (y))
757 /*
758 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
760 * Permission to use, copy, modify, and distribute this software for any
761 * purpose with or without fee is hereby granted, provided that the above
762 * copyright notice and this permission notice appear in all copies.
764 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
765 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
766 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
767 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
768 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
769 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
770 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
771 */
773 struct rb_type {
774 int (*t_compare)(const void *, const void *);
775 void (*t_augment)(void *);
776 unsigned int t_offset; /* offset of rb_entry in type */
777 };
779 struct rb_tree {
780 struct rb_entry *rbt_root;
781 };
783 struct rb_entry {
784 struct rb_entry *rbt_parent;
785 struct rb_entry *rbt_left;
786 struct rb_entry *rbt_right;
787 unsigned int rbt_color;
788 };
790 #define RBT_HEAD(_name, _type) \
791 struct _name { \
792 struct rb_tree rbh_root; \
795 #define RBT_ENTRY(_type) struct rb_entry
797 static inline void
798 _rb_init(struct rb_tree *rbt)
800 rbt->rbt_root = NULL;
803 static inline int
804 _rb_empty(struct rb_tree *rbt)
806 return (rbt->rbt_root == NULL);
809 void *_rb_insert(const struct rb_type *, struct rb_tree *, void *);
810 void *_rb_remove(const struct rb_type *, struct rb_tree *, void *);
811 void *_rb_find(const struct rb_type *, struct rb_tree *, const void *);
812 void *_rb_nfind(const struct rb_type *, struct rb_tree *, const void *);
813 void *_rb_root(const struct rb_type *, struct rb_tree *);
814 void *_rb_min(const struct rb_type *, struct rb_tree *);
815 void *_rb_max(const struct rb_type *, struct rb_tree *);
816 void *_rb_next(const struct rb_type *, void *);
817 void *_rb_prev(const struct rb_type *, void *);
818 void *_rb_left(const struct rb_type *, void *);
819 void *_rb_right(const struct rb_type *, void *);
820 void *_rb_parent(const struct rb_type *, void *);
821 void _rb_set_left(const struct rb_type *, void *, void *);
822 void _rb_set_right(const struct rb_type *, void *, void *);
823 void _rb_set_parent(const struct rb_type *, void *, void *);
824 void _rb_poison(const struct rb_type *, void *, unsigned long);
825 int _rb_check(const struct rb_type *, void *, unsigned long);
827 #define RBT_INITIALIZER(_head) { { NULL } }
829 #define RBT_PROTOTYPE(_name, _type, _field, _cmp) \
830 extern const struct rb_type *const _name##_RBT_TYPE; \
832 static inline void \
833 _name##_RBT_INIT(struct _name *head) \
834 { \
835 _rb_init(&head->rbh_root); \
836 } \
838 static inline struct _type * \
839 _name##_RBT_INSERT(struct _name *head, struct _type *elm) \
840 { \
841 return _rb_insert(_name##_RBT_TYPE, &head->rbh_root, elm); \
842 } \
844 static inline struct _type * \
845 _name##_RBT_REMOVE(struct _name *head, struct _type *elm) \
846 { \
847 return _rb_remove(_name##_RBT_TYPE, &head->rbh_root, elm); \
848 } \
850 static inline struct _type * \
851 _name##_RBT_FIND(struct _name *head, const struct _type *key) \
852 { \
853 return _rb_find(_name##_RBT_TYPE, &head->rbh_root, key); \
854 } \
856 static inline struct _type * \
857 _name##_RBT_NFIND(struct _name *head, const struct _type *key) \
858 { \
859 return _rb_nfind(_name##_RBT_TYPE, &head->rbh_root, key); \
860 } \
862 static inline struct _type * \
863 _name##_RBT_ROOT(struct _name *head) \
864 { \
865 return _rb_root(_name##_RBT_TYPE, &head->rbh_root); \
866 } \
868 static inline int \
869 _name##_RBT_EMPTY(struct _name *head) \
870 { \
871 return _rb_empty(&head->rbh_root); \
872 } \
874 static inline struct _type * \
875 _name##_RBT_MIN(struct _name *head) \
876 { \
877 return _rb_min(_name##_RBT_TYPE, &head->rbh_root); \
878 } \
880 static inline struct _type * \
881 _name##_RBT_MAX(struct _name *head) \
882 { \
883 return _rb_max(_name##_RBT_TYPE, &head->rbh_root); \
884 } \
886 static inline struct _type * \
887 _name##_RBT_NEXT(struct _type *elm) \
888 { \
889 return _rb_next(_name##_RBT_TYPE, elm); \
890 } \
892 static inline struct _type * \
893 _name##_RBT_PREV(struct _type *elm) \
894 { \
895 return _rb_prev(_name##_RBT_TYPE, elm); \
896 } \
898 static inline struct _type * \
899 _name##_RBT_LEFT(struct _type *elm) \
900 { \
901 return _rb_left(_name##_RBT_TYPE, elm); \
902 } \
904 static inline struct _type * \
905 _name##_RBT_RIGHT(struct _type *elm) \
906 { \
907 return _rb_right(_name##_RBT_TYPE, elm); \
908 } \
910 static inline struct _type * \
911 _name##_RBT_PARENT(struct _type *elm) \
912 { \
913 return _rb_parent(_name##_RBT_TYPE, elm); \
914 } \
916 static inline void \
917 _name##_RBT_SET_LEFT(struct _type *elm, struct _type *left) \
918 { \
919 _rb_set_left(_name##_RBT_TYPE, elm, left); \
920 } \
922 static inline void \
923 _name##_RBT_SET_RIGHT(struct _type *elm, struct _type *right) \
924 { \
925 _rb_set_right(_name##_RBT_TYPE, elm, right); \
926 } \
928 static inline void \
929 _name##_RBT_SET_PARENT(struct _type *elm, struct _type *parent) \
930 { \
931 _rb_set_parent(_name##_RBT_TYPE, elm, parent); \
932 } \
934 static inline void \
935 _name##_RBT_POISON(struct _type *elm, unsigned long poison) \
936 { \
937 _rb_poison(_name##_RBT_TYPE, elm, poison); \
938 } \
940 static inline int \
941 _name##_RBT_CHECK(struct _type *elm, unsigned long poison) \
942 { \
943 return _rb_check(_name##_RBT_TYPE, elm, poison); \
946 #define RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
947 static int \
948 _name##_RBT_COMPARE(const void *lptr, const void *rptr) \
949 { \
950 const struct _type *l = lptr, *r = rptr; \
951 return _cmp(l, r); \
952 } \
953 static const struct rb_type _name##_RBT_INFO = { \
954 _name##_RBT_COMPARE, \
955 _aug, \
956 offsetof(struct _type, _field), \
957 }; \
958 const struct rb_type *const _name##_RBT_TYPE = &_name##_RBT_INFO
960 #define RBT_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
961 static void \
962 _name##_RBT_AUGMENT(void *ptr) \
963 { \
964 struct _type *p = ptr; \
965 return _aug(p); \
966 } \
967 RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RBT_AUGMENT)
969 #define RBT_GENERATE(_name, _type, _field, _cmp) \
970 RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
972 #define RBT_INIT(_name, _head) _name##_RBT_INIT(_head)
973 #define RBT_INSERT(_name, _head, _elm) _name##_RBT_INSERT(_head, _elm)
974 #define RBT_REMOVE(_name, _head, _elm) _name##_RBT_REMOVE(_head, _elm)
975 #define RBT_FIND(_name, _head, _key) _name##_RBT_FIND(_head, _key)
976 #define RBT_NFIND(_name, _head, _key) _name##_RBT_NFIND(_head, _key)
977 #define RBT_ROOT(_name, _head) _name##_RBT_ROOT(_head)
978 #define RBT_EMPTY(_name, _head) _name##_RBT_EMPTY(_head)
979 #define RBT_MIN(_name, _head) _name##_RBT_MIN(_head)
980 #define RBT_MAX(_name, _head) _name##_RBT_MAX(_head)
981 #define RBT_NEXT(_name, _elm) _name##_RBT_NEXT(_elm)
982 #define RBT_PREV(_name, _elm) _name##_RBT_PREV(_elm)
983 #define RBT_LEFT(_name, _elm) _name##_RBT_LEFT(_elm)
984 #define RBT_RIGHT(_name, _elm) _name##_RBT_RIGHT(_elm)
985 #define RBT_PARENT(_name, _elm) _name##_RBT_PARENT(_elm)
986 #define RBT_SET_LEFT(_name, _elm, _l) _name##_RBT_SET_LEFT(_elm, _l)
987 #define RBT_SET_RIGHT(_name, _elm, _r) _name##_RBT_SET_RIGHT(_elm, _r)
988 #define RBT_SET_PARENT(_name, _elm, _p) _name##_RBT_SET_PARENT(_elm, _p)
989 #define RBT_POISON(_name, _elm, _p) _name##_RBT_POISON(_elm, _p)
990 #define RBT_CHECK(_name, _elm, _p) _name##_RBT_CHECK(_elm, _p)
992 #define RBT_FOREACH(_e, _name, _head) \
993 for ((_e) = RBT_MIN(_name, (_head)); \
994 (_e) != NULL; \
995 (_e) = RBT_NEXT(_name, (_e)))
997 #define RBT_FOREACH_SAFE(_e, _name, _head, _n) \
998 for ((_e) = RBT_MIN(_name, (_head)); \
999 (_e) != NULL && ((_n) = RBT_NEXT(_name, (_e)), 1); \
1000 (_e) = (_n))
1002 #define RBT_FOREACH_REVERSE(_e, _name, _head) \
1003 for ((_e) = RBT_MAX(_name, (_head)); \
1004 (_e) != NULL; \
1005 (_e) = RBT_PREV(_name, (_e)))
1007 #define RBT_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
1008 for ((_e) = RBT_MAX(_name, (_head)); \
1009 (_e) != NULL && ((_n) = RBT_PREV(_name, (_e)), 1); \
1010 (_e) = (_n))
1012 #endif /* _SYS_TREE_H_ */