libite
tree.h
Go to the documentation of this file.
1 /* $OpenBSD: tree.h,v 1.14 2015/05/25 03:07:49 deraadt 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  */
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #ifndef _SYS_TREE_H_
33 #define _SYS_TREE_H_
34 
67 #define SPLAY_HEAD(name, type) \
68 struct name { \
69  struct type *sph_root; /* root of the tree */ \
70 }
71 
72 #define SPLAY_INITIALIZER(root) \
73  { NULL }
74 
75 #define SPLAY_INIT(root) do { \
76  (root)->sph_root = NULL; \
77 } while (0)
78 
79 #define SPLAY_ENTRY(type) \
80 struct { \
81  struct type *spe_left; /* left element */ \
82  struct type *spe_right; /* right element */ \
83 }
84 
85 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
86 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
87 #define SPLAY_ROOT(head) (head)->sph_root
88 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
89 
90 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
91 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
92  SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
93  SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
94  (head)->sph_root = tmp; \
95 } while (0)
96 
97 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
98  SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
99  SPLAY_LEFT(tmp, field) = (head)->sph_root; \
100  (head)->sph_root = tmp; \
101 } while (0)
102 
103 #define SPLAY_LINKLEFT(head, tmp, field) do { \
104  SPLAY_LEFT(tmp, field) = (head)->sph_root; \
105  tmp = (head)->sph_root; \
106  (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
107 } while (0)
108 
109 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
110  SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
111  tmp = (head)->sph_root; \
112  (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
113 } while (0)
114 
115 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
116  SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
117  SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
118  SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
119  SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
120 } while (0)
121 
122 /* Generates prototypes and inline functions */
123 
124 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
125 void name##_SPLAY(struct name *, struct type *); \
126 void name##_SPLAY_MINMAX(struct name *, int); \
127 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
128 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
129  \
130 /* Finds the node with the same key as elm */ \
131 static __inline struct type * \
132 name##_SPLAY_FIND(struct name *head, struct type *elm) \
133 { \
134  if (SPLAY_EMPTY(head)) \
135  return(NULL); \
136  name##_SPLAY(head, elm); \
137  if ((cmp)(elm, (head)->sph_root) == 0) \
138  return (head->sph_root); \
139  return (NULL); \
140 } \
141  \
142 static __inline struct type * \
143 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
144 { \
145  name##_SPLAY(head, elm); \
146  if (SPLAY_RIGHT(elm, field) != NULL) { \
147  elm = SPLAY_RIGHT(elm, field); \
148  while (SPLAY_LEFT(elm, field) != NULL) { \
149  elm = SPLAY_LEFT(elm, field); \
150  } \
151  } else \
152  elm = NULL; \
153  return (elm); \
154 } \
155  \
156 static __inline struct type * \
157 name##_SPLAY_MIN_MAX(struct name *head, int val) \
158 { \
159  name##_SPLAY_MINMAX(head, val); \
160  return (SPLAY_ROOT(head)); \
161 }
162 
163 /* Main splay operation.
164  * Moves node close to the key of elm to top
165  */
166 #define SPLAY_GENERATE(name, type, field, cmp) \
167 struct type * \
168 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
169 { \
170  if (SPLAY_EMPTY(head)) { \
171  SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
172  } else { \
173  int __comp; \
174  name##_SPLAY(head, elm); \
175  __comp = (cmp)(elm, (head)->sph_root); \
176  if(__comp < 0) { \
177  SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
178  SPLAY_RIGHT(elm, field) = (head)->sph_root; \
179  SPLAY_LEFT((head)->sph_root, field) = NULL; \
180  } else if (__comp > 0) { \
181  SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
182  SPLAY_LEFT(elm, field) = (head)->sph_root; \
183  SPLAY_RIGHT((head)->sph_root, field) = NULL; \
184  } else \
185  return ((head)->sph_root); \
186  } \
187  (head)->sph_root = (elm); \
188  return (NULL); \
189 } \
190  \
191 struct type * \
192 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
193 { \
194  struct type *__tmp; \
195  if (SPLAY_EMPTY(head)) \
196  return (NULL); \
197  name##_SPLAY(head, elm); \
198  if ((cmp)(elm, (head)->sph_root) == 0) { \
199  if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
200  (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
201  } else { \
202  __tmp = SPLAY_RIGHT((head)->sph_root, field); \
203  (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
204  name##_SPLAY(head, elm); \
205  SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
206  } \
207  return (elm); \
208  } \
209  return (NULL); \
210 } \
211  \
212 void \
213 name##_SPLAY(struct name *head, struct type *elm) \
214 { \
215  struct type __node, *__left, *__right, *__tmp; \
216  int __comp; \
217 \
218  SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
219  __left = __right = &__node; \
220 \
221  while ((__comp = (cmp)(elm, (head)->sph_root))) { \
222  if (__comp < 0) { \
223  __tmp = SPLAY_LEFT((head)->sph_root, field); \
224  if (__tmp == NULL) \
225  break; \
226  if ((cmp)(elm, __tmp) < 0){ \
227  SPLAY_ROTATE_RIGHT(head, __tmp, field); \
228  if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
229  break; \
230  } \
231  SPLAY_LINKLEFT(head, __right, field); \
232  } else if (__comp > 0) { \
233  __tmp = SPLAY_RIGHT((head)->sph_root, field); \
234  if (__tmp == NULL) \
235  break; \
236  if ((cmp)(elm, __tmp) > 0){ \
237  SPLAY_ROTATE_LEFT(head, __tmp, field); \
238  if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
239  break; \
240  } \
241  SPLAY_LINKRIGHT(head, __left, field); \
242  } \
243  } \
244  SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
245 } \
246  \
247 /* Splay with either the minimum or the maximum element \
248  * Used to find minimum or maximum element in tree. \
249  */ \
250 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
251 { \
252  struct type __node, *__left, *__right, *__tmp; \
253 \
254  SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
255  __left = __right = &__node; \
256 \
257  while (1) { \
258  if (__comp < 0) { \
259  __tmp = SPLAY_LEFT((head)->sph_root, field); \
260  if (__tmp == NULL) \
261  break; \
262  if (__comp < 0){ \
263  SPLAY_ROTATE_RIGHT(head, __tmp, field); \
264  if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
265  break; \
266  } \
267  SPLAY_LINKLEFT(head, __right, field); \
268  } else if (__comp > 0) { \
269  __tmp = SPLAY_RIGHT((head)->sph_root, field); \
270  if (__tmp == NULL) \
271  break; \
272  if (__comp > 0) { \
273  SPLAY_ROTATE_LEFT(head, __tmp, field); \
274  if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
275  break; \
276  } \
277  SPLAY_LINKRIGHT(head, __left, field); \
278  } \
279  } \
280  SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
281 }
282 
283 #define SPLAY_NEGINF -1
284 #define SPLAY_INF 1
285 
286 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
287 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
288 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
289 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
290 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
291  : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
292 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
293  : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
294 
295 #define SPLAY_FOREACH(x, name, head) \
296  for ((x) = SPLAY_MIN(name, head); \
297  (x) != NULL; \
298  (x) = SPLAY_NEXT(name, head, x))
299 
300 /* Macros that define a red-black tree */
301 #define RB_HEAD(name, type) \
302 struct name { \
303  struct type *rbh_root; /* root of the tree */ \
304 }
305 
306 #define RB_INITIALIZER(root) \
307  { NULL }
308 
309 #define RB_INIT(root) do { \
310  (root)->rbh_root = NULL; \
311 } while (0)
312 
313 #define RB_BLACK 0
314 #define RB_RED 1
315 #define RB_ENTRY(type) \
316 struct { \
317  struct type *rbe_left; /* left element */ \
318  struct type *rbe_right; /* right element */ \
319  struct type *rbe_parent; /* parent element */ \
320  int rbe_color; /* node color */ \
321 }
322 
323 #define RB_LEFT(elm, field) (elm)->field.rbe_left
324 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
325 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
326 #define RB_COLOR(elm, field) (elm)->field.rbe_color
327 #define RB_ROOT(head) (head)->rbh_root
328 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
329 
330 #define RB_SET(elm, parent, field) do { \
331  RB_PARENT(elm, field) = parent; \
332  RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
333  RB_COLOR(elm, field) = RB_RED; \
334 } while (0)
335 
336 #define RB_SET_BLACKRED(black, red, field) do { \
337  RB_COLOR(black, field) = RB_BLACK; \
338  RB_COLOR(red, field) = RB_RED; \
339 } while (0)
340 
341 #ifndef RB_AUGMENT
342 #define RB_AUGMENT(x) do {} while (0)
343 #endif
344 
345 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
346  (tmp) = RB_RIGHT(elm, field); \
347  if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
348  RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
349  } \
350  RB_AUGMENT(elm); \
351  if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
352  if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
353  RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
354  else \
355  RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
356  } else \
357  (head)->rbh_root = (tmp); \
358  RB_LEFT(tmp, field) = (elm); \
359  RB_PARENT(elm, field) = (tmp); \
360  RB_AUGMENT(tmp); \
361  if ((RB_PARENT(tmp, field))) \
362  RB_AUGMENT(RB_PARENT(tmp, field)); \
363 } while (0)
364 
365 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
366  (tmp) = RB_LEFT(elm, field); \
367  if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
368  RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
369  } \
370  RB_AUGMENT(elm); \
371  if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
372  if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
373  RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
374  else \
375  RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
376  } else \
377  (head)->rbh_root = (tmp); \
378  RB_RIGHT(tmp, field) = (elm); \
379  RB_PARENT(elm, field) = (tmp); \
380  RB_AUGMENT(tmp); \
381  if ((RB_PARENT(tmp, field))) \
382  RB_AUGMENT(RB_PARENT(tmp, field)); \
383 } while (0)
384 
385 /* Generates prototypes and inline functions */
386 #define RB_PROTOTYPE(name, type, field, cmp) \
387  RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
388 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
389  RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
390 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
391 attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
392 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
393 attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
394 attr struct type *name##_RB_INSERT(struct name *, struct type *); \
395 attr struct type *name##_RB_FIND(struct name *, struct type *); \
396 attr struct type *name##_RB_NFIND(struct name *, struct type *); \
397 attr struct type *name##_RB_NEXT(struct type *); \
398 attr struct type *name##_RB_PREV(struct type *); \
399 attr struct type *name##_RB_MINMAX(struct name *, int); \
400  \
401 
402 /* Main rb operation.
403  * Moves node close to the key of elm to top
404  */
405 #define RB_GENERATE(name, type, field, cmp) \
406  RB_GENERATE_INTERNAL(name, type, field, cmp,)
407 #define RB_GENERATE_STATIC(name, type, field, cmp) \
408  RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
409 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
410 attr void \
411 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
412 { \
413  struct type *parent, *gparent, *tmp; \
414  while ((parent = RB_PARENT(elm, field)) && \
415  RB_COLOR(parent, field) == RB_RED) { \
416  gparent = RB_PARENT(parent, field); \
417  if (parent == RB_LEFT(gparent, field)) { \
418  tmp = RB_RIGHT(gparent, field); \
419  if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
420  RB_COLOR(tmp, field) = RB_BLACK; \
421  RB_SET_BLACKRED(parent, gparent, field);\
422  elm = gparent; \
423  continue; \
424  } \
425  if (RB_RIGHT(parent, field) == elm) { \
426  RB_ROTATE_LEFT(head, parent, tmp, field);\
427  tmp = parent; \
428  parent = elm; \
429  elm = tmp; \
430  } \
431  RB_SET_BLACKRED(parent, gparent, field); \
432  RB_ROTATE_RIGHT(head, gparent, tmp, field); \
433  } else { \
434  tmp = RB_LEFT(gparent, field); \
435  if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
436  RB_COLOR(tmp, field) = RB_BLACK; \
437  RB_SET_BLACKRED(parent, gparent, field);\
438  elm = gparent; \
439  continue; \
440  } \
441  if (RB_LEFT(parent, field) == elm) { \
442  RB_ROTATE_RIGHT(head, parent, tmp, field);\
443  tmp = parent; \
444  parent = elm; \
445  elm = tmp; \
446  } \
447  RB_SET_BLACKRED(parent, gparent, field); \
448  RB_ROTATE_LEFT(head, gparent, tmp, field); \
449  } \
450  } \
451  RB_COLOR(head->rbh_root, field) = RB_BLACK; \
452 } \
453  \
454 attr void \
455 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
456 { \
457  struct type *tmp; \
458  while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
459  elm != RB_ROOT(head)) { \
460  if (RB_LEFT(parent, field) == elm) { \
461  tmp = RB_RIGHT(parent, field); \
462  if (RB_COLOR(tmp, field) == RB_RED) { \
463  RB_SET_BLACKRED(tmp, parent, field); \
464  RB_ROTATE_LEFT(head, parent, tmp, field);\
465  tmp = RB_RIGHT(parent, field); \
466  } \
467  if ((RB_LEFT(tmp, field) == NULL || \
468  RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
469  (RB_RIGHT(tmp, field) == NULL || \
470  RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
471  RB_COLOR(tmp, field) = RB_RED; \
472  elm = parent; \
473  parent = RB_PARENT(elm, field); \
474  } else { \
475  if (RB_RIGHT(tmp, field) == NULL || \
476  RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
477  struct type *oleft; \
478  if ((oleft = RB_LEFT(tmp, field)))\
479  RB_COLOR(oleft, field) = RB_BLACK;\
480  RB_COLOR(tmp, field) = RB_RED; \
481  RB_ROTATE_RIGHT(head, tmp, oleft, field);\
482  tmp = RB_RIGHT(parent, field); \
483  } \
484  RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
485  RB_COLOR(parent, field) = RB_BLACK; \
486  if (RB_RIGHT(tmp, field)) \
487  RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
488  RB_ROTATE_LEFT(head, parent, tmp, field);\
489  elm = RB_ROOT(head); \
490  break; \
491  } \
492  } else { \
493  tmp = RB_LEFT(parent, field); \
494  if (RB_COLOR(tmp, field) == RB_RED) { \
495  RB_SET_BLACKRED(tmp, parent, field); \
496  RB_ROTATE_RIGHT(head, parent, tmp, field);\
497  tmp = RB_LEFT(parent, field); \
498  } \
499  if ((RB_LEFT(tmp, field) == NULL || \
500  RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
501  (RB_RIGHT(tmp, field) == NULL || \
502  RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
503  RB_COLOR(tmp, field) = RB_RED; \
504  elm = parent; \
505  parent = RB_PARENT(elm, field); \
506  } else { \
507  if (RB_LEFT(tmp, field) == NULL || \
508  RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
509  struct type *oright; \
510  if ((oright = RB_RIGHT(tmp, field)))\
511  RB_COLOR(oright, field) = RB_BLACK;\
512  RB_COLOR(tmp, field) = RB_RED; \
513  RB_ROTATE_LEFT(head, tmp, oright, field);\
514  tmp = RB_LEFT(parent, field); \
515  } \
516  RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
517  RB_COLOR(parent, field) = RB_BLACK; \
518  if (RB_LEFT(tmp, field)) \
519  RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
520  RB_ROTATE_RIGHT(head, parent, tmp, field);\
521  elm = RB_ROOT(head); \
522  break; \
523  } \
524  } \
525  } \
526  if (elm) \
527  RB_COLOR(elm, field) = RB_BLACK; \
528 } \
529  \
530 attr struct type * \
531 name##_RB_REMOVE(struct name *head, struct type *elm) \
532 { \
533  struct type *child, *parent, *old = elm; \
534  int color; \
535  if (RB_LEFT(elm, field) == NULL) \
536  child = RB_RIGHT(elm, field); \
537  else if (RB_RIGHT(elm, field) == NULL) \
538  child = RB_LEFT(elm, field); \
539  else { \
540  struct type *left; \
541  elm = RB_RIGHT(elm, field); \
542  while ((left = RB_LEFT(elm, field))) \
543  elm = left; \
544  child = RB_RIGHT(elm, field); \
545  parent = RB_PARENT(elm, field); \
546  color = RB_COLOR(elm, field); \
547  if (child) \
548  RB_PARENT(child, field) = parent; \
549  if (parent) { \
550  if (RB_LEFT(parent, field) == elm) \
551  RB_LEFT(parent, field) = child; \
552  else \
553  RB_RIGHT(parent, field) = child; \
554  RB_AUGMENT(parent); \
555  } else \
556  RB_ROOT(head) = child; \
557  if (RB_PARENT(elm, field) == old) \
558  parent = elm; \
559  (elm)->field = (old)->field; \
560  if (RB_PARENT(old, field)) { \
561  if (RB_LEFT(RB_PARENT(old, field), field) == old)\
562  RB_LEFT(RB_PARENT(old, field), field) = elm;\
563  else \
564  RB_RIGHT(RB_PARENT(old, field), field) = elm;\
565  RB_AUGMENT(RB_PARENT(old, field)); \
566  } else \
567  RB_ROOT(head) = elm; \
568  RB_PARENT(RB_LEFT(old, field), field) = elm; \
569  if (RB_RIGHT(old, field)) \
570  RB_PARENT(RB_RIGHT(old, field), field) = elm; \
571  if (parent) { \
572  left = parent; \
573  do { \
574  RB_AUGMENT(left); \
575  } while ((left = RB_PARENT(left, field))); \
576  } \
577  goto color; \
578  } \
579  parent = RB_PARENT(elm, field); \
580  color = RB_COLOR(elm, field); \
581  if (child) \
582  RB_PARENT(child, field) = parent; \
583  if (parent) { \
584  if (RB_LEFT(parent, field) == elm) \
585  RB_LEFT(parent, field) = child; \
586  else \
587  RB_RIGHT(parent, field) = child; \
588  RB_AUGMENT(parent); \
589  } else \
590  RB_ROOT(head) = child; \
591 color: \
592  if (color == RB_BLACK) \
593  name##_RB_REMOVE_COLOR(head, parent, child); \
594  return (old); \
595 } \
596  \
597 /* Inserts a node into the RB tree */ \
598 attr struct type * \
599 name##_RB_INSERT(struct name *head, struct type *elm) \
600 { \
601  struct type *tmp; \
602  struct type *parent = NULL; \
603  int comp = 0; \
604  tmp = RB_ROOT(head); \
605  while (tmp) { \
606  parent = tmp; \
607  comp = (cmp)(elm, parent); \
608  if (comp < 0) \
609  tmp = RB_LEFT(tmp, field); \
610  else if (comp > 0) \
611  tmp = RB_RIGHT(tmp, field); \
612  else \
613  return (tmp); \
614  } \
615  RB_SET(elm, parent, field); \
616  if (parent != NULL) { \
617  if (comp < 0) \
618  RB_LEFT(parent, field) = elm; \
619  else \
620  RB_RIGHT(parent, field) = elm; \
621  RB_AUGMENT(parent); \
622  } else \
623  RB_ROOT(head) = elm; \
624  name##_RB_INSERT_COLOR(head, elm); \
625  return (NULL); \
626 } \
627  \
628 /* Finds the node with the same key as elm */ \
629 attr struct type * \
630 name##_RB_FIND(struct name *head, struct type *elm) \
631 { \
632  struct type *tmp = RB_ROOT(head); \
633  int comp; \
634  while (tmp) { \
635  comp = cmp(elm, tmp); \
636  if (comp < 0) \
637  tmp = RB_LEFT(tmp, field); \
638  else if (comp > 0) \
639  tmp = RB_RIGHT(tmp, field); \
640  else \
641  return (tmp); \
642  } \
643  return (NULL); \
644 } \
645  \
646 /* Finds the first node greater than or equal to the search key */ \
647 attr struct type * \
648 name##_RB_NFIND(struct name *head, struct type *elm) \
649 { \
650  struct type *tmp = RB_ROOT(head); \
651  struct type *res = NULL; \
652  int comp; \
653  while (tmp) { \
654  comp = cmp(elm, tmp); \
655  if (comp < 0) { \
656  res = tmp; \
657  tmp = RB_LEFT(tmp, field); \
658  } \
659  else if (comp > 0) \
660  tmp = RB_RIGHT(tmp, field); \
661  else \
662  return (tmp); \
663  } \
664  return (res); \
665 } \
666  \
667 /* ARGSUSED */ \
668 attr struct type * \
669 name##_RB_NEXT(struct type *elm) \
670 { \
671  if (RB_RIGHT(elm, field)) { \
672  elm = RB_RIGHT(elm, field); \
673  while (RB_LEFT(elm, field)) \
674  elm = RB_LEFT(elm, field); \
675  } else { \
676  if (RB_PARENT(elm, field) && \
677  (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
678  elm = RB_PARENT(elm, field); \
679  else { \
680  while (RB_PARENT(elm, field) && \
681  (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
682  elm = RB_PARENT(elm, field); \
683  elm = RB_PARENT(elm, field); \
684  } \
685  } \
686  return (elm); \
687 } \
688  \
689 /* ARGSUSED */ \
690 attr struct type * \
691 name##_RB_PREV(struct type *elm) \
692 { \
693  if (RB_LEFT(elm, field)) { \
694  elm = RB_LEFT(elm, field); \
695  while (RB_RIGHT(elm, field)) \
696  elm = RB_RIGHT(elm, field); \
697  } else { \
698  if (RB_PARENT(elm, field) && \
699  (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
700  elm = RB_PARENT(elm, field); \
701  else { \
702  while (RB_PARENT(elm, field) && \
703  (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
704  elm = RB_PARENT(elm, field); \
705  elm = RB_PARENT(elm, field); \
706  } \
707  } \
708  return (elm); \
709 } \
710  \
711 attr struct type * \
712 name##_RB_MINMAX(struct name *head, int val) \
713 { \
714  struct type *tmp = RB_ROOT(head); \
715  struct type *parent = NULL; \
716  while (tmp) { \
717  parent = tmp; \
718  if (val < 0) \
719  tmp = RB_LEFT(tmp, field); \
720  else \
721  tmp = RB_RIGHT(tmp, field); \
722  } \
723  return (parent); \
724 }
725 
726 #define RB_NEGINF -1
727 #define RB_INF 1
728 
729 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
730 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
731 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
732 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
733 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
734 #define RB_PREV(name, x, y) name##_RB_PREV(y)
735 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
736 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
737 
738 #define RB_FOREACH(x, name, head) \
739  for ((x) = RB_MIN(name, head); \
740  (x) != NULL; \
741  (x) = name##_RB_NEXT(x))
742 
743 #define RB_FOREACH_SAFE(x, name, head, y) \
744  for ((x) = RB_MIN(name, head); \
745  ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
746  (x) = (y))
747 
748 #define RB_FOREACH_REVERSE(x, name, head) \
749  for ((x) = RB_MAX(name, head); \
750  (x) != NULL; \
751  (x) = name##_RB_PREV(x))
752 
753 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
754  for ((x) = RB_MAX(name, head); \
755  ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
756  (x) = (y))
757 
758 #endif /* _SYS_TREE_H_ */
759 
760 #ifdef __cplusplus
761 }
762 #endif