tree.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */
  2. /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
  3. /* $FreeBSD$ */
  4. /*-
  5. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef _SYS_TREE_H_
  29. #define _SYS_TREE_H_
  30. #ifndef NULL
  31. #define NULL RT_NULL
  32. #endif
  33. // #include <sys/cdefs.h>
  34. /*
  35. * This file defines data structures for different types of trees:
  36. * splay trees and red-black trees.
  37. *
  38. * A splay tree is a self-organizing data structure. Every operation
  39. * on the tree causes a splay to happen. The splay moves the requested
  40. * node to the root of the tree and partly rebalances it.
  41. *
  42. * This has the benefit that request locality causes faster lookups as
  43. * the requested nodes move to the top of the tree. On the other hand,
  44. * every lookup causes memory writes.
  45. *
  46. * The Balance Theorem bounds the total access time for m operations
  47. * and n inserts on an initially empty tree as O((m + n)lg n). The
  48. * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
  49. *
  50. * A red-black tree is a binary search tree with the node color as an
  51. * extra attribute. It fulfills a set of conditions:
  52. * - every search path from the root to a leaf consists of the
  53. * same number of black nodes,
  54. * - each red node (except for the root) has a black parent,
  55. * - each leaf node is black.
  56. *
  57. * Every operation on a red-black tree is bounded as O(lg n).
  58. * The maximum height of a red-black tree is 2lg (n+1).
  59. */
  60. #define SPLAY_HEAD(name, type) \
  61. struct name { \
  62. struct type *sph_root; /* root of the tree */ \
  63. }
  64. #define SPLAY_INITIALIZER(root) \
  65. { NULL }
  66. #define SPLAY_INIT(root) do { \
  67. (root)->sph_root = NULL; \
  68. } while (/*CONSTCOND*/ 0)
  69. #define SPLAY_ENTRY(type) \
  70. struct { \
  71. struct type *spe_left; /* left element */ \
  72. struct type *spe_right; /* right element */ \
  73. }
  74. #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
  75. #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
  76. #define SPLAY_ROOT(head) (head)->sph_root
  77. #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
  78. /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
  79. #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
  80. SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
  81. SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
  82. (head)->sph_root = tmp; \
  83. } while (/*CONSTCOND*/ 0)
  84. #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
  85. SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
  86. SPLAY_LEFT(tmp, field) = (head)->sph_root; \
  87. (head)->sph_root = tmp; \
  88. } while (/*CONSTCOND*/ 0)
  89. #define SPLAY_LINKLEFT(head, tmp, field) do { \
  90. SPLAY_LEFT(tmp, field) = (head)->sph_root; \
  91. tmp = (head)->sph_root; \
  92. (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
  93. } while (/*CONSTCOND*/ 0)
  94. #define SPLAY_LINKRIGHT(head, tmp, field) do { \
  95. SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
  96. tmp = (head)->sph_root; \
  97. (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
  98. } while (/*CONSTCOND*/ 0)
  99. #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
  100. SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
  101. SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
  102. SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
  103. SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
  104. } while (/*CONSTCOND*/ 0)
  105. /* Generates prototypes and inline functions */
  106. #define SPLAY_PROTOTYPE(name, type, field, cmp) \
  107. void name##_SPLAY(struct name *, struct type *); \
  108. void name##_SPLAY_MINMAX(struct name *, int); \
  109. struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
  110. struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
  111. \
  112. /* Finds the node with the same key as elm */ \
  113. rt_inline struct type * \
  114. name##_SPLAY_FIND(struct name *head, struct type *elm) \
  115. { \
  116. if (SPLAY_EMPTY(head)) \
  117. return(NULL); \
  118. name##_SPLAY(head, elm); \
  119. if ((cmp)(elm, (head)->sph_root) == 0) \
  120. return (head->sph_root); \
  121. return (NULL); \
  122. } \
  123. \
  124. rt_inline struct type * \
  125. name##_SPLAY_NEXT(struct name *head, struct type *elm) \
  126. { \
  127. name##_SPLAY(head, elm); \
  128. if (SPLAY_RIGHT(elm, field) != NULL) { \
  129. elm = SPLAY_RIGHT(elm, field); \
  130. while (SPLAY_LEFT(elm, field) != NULL) { \
  131. elm = SPLAY_LEFT(elm, field); \
  132. } \
  133. } else \
  134. elm = NULL; \
  135. return (elm); \
  136. } \
  137. \
  138. rt_inline struct type * \
  139. name##_SPLAY_MIN_MAX(struct name *head, int val) \
  140. { \
  141. name##_SPLAY_MINMAX(head, val); \
  142. return (SPLAY_ROOT(head)); \
  143. }
  144. /* Main splay operation.
  145. * Moves node close to the key of elm to top
  146. */
  147. #define SPLAY_GENERATE(name, type, field, cmp) \
  148. struct type * \
  149. name##_SPLAY_INSERT(struct name *head, struct type *elm) \
  150. { \
  151. if (SPLAY_EMPTY(head)) { \
  152. SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
  153. } else { \
  154. int __comp; \
  155. name##_SPLAY(head, elm); \
  156. __comp = (cmp)(elm, (head)->sph_root); \
  157. if(__comp < 0) { \
  158. SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
  159. SPLAY_RIGHT(elm, field) = (head)->sph_root; \
  160. SPLAY_LEFT((head)->sph_root, field) = NULL; \
  161. } else if (__comp > 0) { \
  162. SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
  163. SPLAY_LEFT(elm, field) = (head)->sph_root; \
  164. SPLAY_RIGHT((head)->sph_root, field) = NULL; \
  165. } else \
  166. return ((head)->sph_root); \
  167. } \
  168. (head)->sph_root = (elm); \
  169. return (NULL); \
  170. } \
  171. \
  172. struct type * \
  173. name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
  174. { \
  175. struct type *__tmp; \
  176. if (SPLAY_EMPTY(head)) \
  177. return (NULL); \
  178. name##_SPLAY(head, elm); \
  179. if ((cmp)(elm, (head)->sph_root) == 0) { \
  180. if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
  181. (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
  182. } else { \
  183. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  184. (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
  185. name##_SPLAY(head, elm); \
  186. SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
  187. } \
  188. return (elm); \
  189. } \
  190. return (NULL); \
  191. } \
  192. \
  193. void \
  194. name##_SPLAY(struct name *head, struct type *elm) \
  195. { \
  196. struct type __node, *__left, *__right, *__tmp; \
  197. int __comp; \
  198. \
  199. SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
  200. __left = __right = &__node; \
  201. \
  202. while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
  203. if (__comp < 0) { \
  204. __tmp = SPLAY_LEFT((head)->sph_root, field); \
  205. if (__tmp == NULL) \
  206. break; \
  207. if ((cmp)(elm, __tmp) < 0){ \
  208. SPLAY_ROTATE_RIGHT(head, __tmp, field); \
  209. if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
  210. break; \
  211. } \
  212. SPLAY_LINKLEFT(head, __right, field); \
  213. } else if (__comp > 0) { \
  214. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  215. if (__tmp == NULL) \
  216. break; \
  217. if ((cmp)(elm, __tmp) > 0){ \
  218. SPLAY_ROTATE_LEFT(head, __tmp, field); \
  219. if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
  220. break; \
  221. } \
  222. SPLAY_LINKRIGHT(head, __left, field); \
  223. } \
  224. } \
  225. SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
  226. } \
  227. \
  228. /* Splay with either the minimum or the maximum element \
  229. * Used to find minimum or maximum element in tree. \
  230. */ \
  231. void name##_SPLAY_MINMAX(struct name *head, int __comp) \
  232. { \
  233. struct type __node, *__left, *__right, *__tmp; \
  234. \
  235. SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
  236. __left = __right = &__node; \
  237. \
  238. while (1) { \
  239. if (__comp < 0) { \
  240. __tmp = SPLAY_LEFT((head)->sph_root, field); \
  241. if (__tmp == NULL) \
  242. break; \
  243. if (__comp < 0){ \
  244. SPLAY_ROTATE_RIGHT(head, __tmp, field); \
  245. if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
  246. break; \
  247. } \
  248. SPLAY_LINKLEFT(head, __right, field); \
  249. } else if (__comp > 0) { \
  250. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  251. if (__tmp == NULL) \
  252. break; \
  253. if (__comp > 0) { \
  254. SPLAY_ROTATE_LEFT(head, __tmp, field); \
  255. if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
  256. break; \
  257. } \
  258. SPLAY_LINKRIGHT(head, __left, field); \
  259. } \
  260. } \
  261. SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
  262. }
  263. #define SPLAY_NEGINF -1
  264. #define SPLAY_INF 1
  265. #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
  266. #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
  267. #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
  268. #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
  269. #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
  270. : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
  271. #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
  272. : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
  273. #define SPLAY_FOREACH(x, name, head) \
  274. for ((x) = SPLAY_MIN(name, head); \
  275. (x) != NULL; \
  276. (x) = SPLAY_NEXT(name, head, x))
  277. /* Macros that define a red-black tree */
  278. #define RB_HEAD(name, type) \
  279. struct name { \
  280. struct type *rbh_root; /* root of the tree */ \
  281. }
  282. #define RB_INITIALIZER(root) \
  283. { NULL }
  284. #define RB_INIT(root) do { \
  285. (root)->rbh_root = NULL; \
  286. } while (/*CONSTCOND*/ 0)
  287. #define RB_BLACK 0
  288. #define RB_RED 1
  289. #define RB_ENTRY(type) \
  290. struct { \
  291. struct type *rbe_left; /* left element */ \
  292. struct type *rbe_right; /* right element */ \
  293. struct type *rbe_parent; /* parent element */ \
  294. int rbe_color; /* node color */ \
  295. }
  296. #define RB_LEFT(elm, field) (elm)->field.rbe_left
  297. #define RB_RIGHT(elm, field) (elm)->field.rbe_right
  298. #define RB_PARENT(elm, field) (elm)->field.rbe_parent
  299. #define RB_COLOR(elm, field) (elm)->field.rbe_color
  300. #define RB_ROOT(head) (head)->rbh_root
  301. #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
  302. #define RB_SET(elm, parent, field) do { \
  303. RB_PARENT(elm, field) = parent; \
  304. RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
  305. RB_COLOR(elm, field) = RB_RED; \
  306. } while (/*CONSTCOND*/ 0)
  307. #define RB_SET_BLACKRED(black, red, field) do { \
  308. RB_COLOR(black, field) = RB_BLACK; \
  309. RB_COLOR(red, field) = RB_RED; \
  310. } while (/*CONSTCOND*/ 0)
  311. #ifndef RB_AUGMENT
  312. #define RB_AUGMENT(x) do {} while (0)
  313. #endif
  314. #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
  315. (tmp) = RB_RIGHT(elm, field); \
  316. if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
  317. RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
  318. } \
  319. RB_AUGMENT(elm); \
  320. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  321. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  322. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  323. else \
  324. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  325. } else \
  326. (head)->rbh_root = (tmp); \
  327. RB_LEFT(tmp, field) = (elm); \
  328. RB_PARENT(elm, field) = (tmp); \
  329. RB_AUGMENT(tmp); \
  330. if ((RB_PARENT(tmp, field))) \
  331. RB_AUGMENT(RB_PARENT(tmp, field)); \
  332. } while (/*CONSTCOND*/ 0)
  333. #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
  334. (tmp) = RB_LEFT(elm, field); \
  335. if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
  336. RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
  337. } \
  338. RB_AUGMENT(elm); \
  339. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  340. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  341. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  342. else \
  343. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  344. } else \
  345. (head)->rbh_root = (tmp); \
  346. RB_RIGHT(tmp, field) = (elm); \
  347. RB_PARENT(elm, field) = (tmp); \
  348. RB_AUGMENT(tmp); \
  349. if ((RB_PARENT(tmp, field))) \
  350. RB_AUGMENT(RB_PARENT(tmp, field)); \
  351. } while (/*CONSTCOND*/ 0)
  352. /* Generates prototypes and inline functions */
  353. #define RB_PROTOTYPE(name, type, field, cmp) \
  354. RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
  355. #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
  356. RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
  357. #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
  358. attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
  359. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
  360. attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
  361. attr struct type *name##_RB_INSERT(struct name *, struct type *); \
  362. attr struct type *name##_RB_FIND(struct name *, struct type *); \
  363. attr struct type *name##_RB_NFIND(struct name *, struct type *); \
  364. attr struct type *name##_RB_NEXT(struct type *); \
  365. attr struct type *name##_RB_PREV(struct type *); \
  366. attr struct type *name##_RB_MINMAX(struct name *, int); \
  367. \
  368. /* Main rb operation.
  369. * Moves node close to the key of elm to top
  370. */
  371. #define RB_GENERATE(name, type, field, cmp) \
  372. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  373. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  374. RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
  375. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  376. attr void \
  377. name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  378. { \
  379. struct type *parent, *gparent, *tmp; \
  380. while ((parent = RB_PARENT(elm, field)) != NULL && \
  381. RB_COLOR(parent, field) == RB_RED) { \
  382. gparent = RB_PARENT(parent, field); \
  383. if (parent == RB_LEFT(gparent, field)) { \
  384. tmp = RB_RIGHT(gparent, field); \
  385. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  386. RB_COLOR(tmp, field) = RB_BLACK; \
  387. RB_SET_BLACKRED(parent, gparent, field);\
  388. elm = gparent; \
  389. continue; \
  390. } \
  391. if (RB_RIGHT(parent, field) == elm) { \
  392. RB_ROTATE_LEFT(head, parent, tmp, field);\
  393. tmp = parent; \
  394. parent = elm; \
  395. elm = tmp; \
  396. } \
  397. RB_SET_BLACKRED(parent, gparent, field); \
  398. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  399. } else { \
  400. tmp = RB_LEFT(gparent, field); \
  401. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  402. RB_COLOR(tmp, field) = RB_BLACK; \
  403. RB_SET_BLACKRED(parent, gparent, field);\
  404. elm = gparent; \
  405. continue; \
  406. } \
  407. if (RB_LEFT(parent, field) == elm) { \
  408. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  409. tmp = parent; \
  410. parent = elm; \
  411. elm = tmp; \
  412. } \
  413. RB_SET_BLACKRED(parent, gparent, field); \
  414. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  415. } \
  416. } \
  417. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  418. } \
  419. \
  420. attr void \
  421. name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
  422. { \
  423. struct type *tmp; \
  424. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  425. elm != RB_ROOT(head)) { \
  426. if (RB_LEFT(parent, field) == elm) { \
  427. tmp = RB_RIGHT(parent, field); \
  428. if (RB_COLOR(tmp, field) == RB_RED) { \
  429. RB_SET_BLACKRED(tmp, parent, field); \
  430. RB_ROTATE_LEFT(head, parent, tmp, field);\
  431. tmp = RB_RIGHT(parent, field); \
  432. } \
  433. if ((RB_LEFT(tmp, field) == NULL || \
  434. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  435. (RB_RIGHT(tmp, field) == NULL || \
  436. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  437. RB_COLOR(tmp, field) = RB_RED; \
  438. elm = parent; \
  439. parent = RB_PARENT(elm, field); \
  440. } else { \
  441. if (RB_RIGHT(tmp, field) == NULL || \
  442. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
  443. struct type *oleft; \
  444. if ((oleft = RB_LEFT(tmp, field)) \
  445. != NULL) \
  446. RB_COLOR(oleft, field) = RB_BLACK;\
  447. RB_COLOR(tmp, field) = RB_RED; \
  448. RB_ROTATE_RIGHT(head, tmp, oleft, field);\
  449. tmp = RB_RIGHT(parent, field); \
  450. } \
  451. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  452. RB_COLOR(parent, field) = RB_BLACK; \
  453. if (RB_RIGHT(tmp, field)) \
  454. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
  455. RB_ROTATE_LEFT(head, parent, tmp, field);\
  456. elm = RB_ROOT(head); \
  457. break; \
  458. } \
  459. } else { \
  460. tmp = RB_LEFT(parent, field); \
  461. if (RB_COLOR(tmp, field) == RB_RED) { \
  462. RB_SET_BLACKRED(tmp, parent, field); \
  463. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  464. tmp = RB_LEFT(parent, field); \
  465. } \
  466. if ((RB_LEFT(tmp, field) == NULL || \
  467. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  468. (RB_RIGHT(tmp, field) == NULL || \
  469. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  470. RB_COLOR(tmp, field) = RB_RED; \
  471. elm = parent; \
  472. parent = RB_PARENT(elm, field); \
  473. } else { \
  474. if (RB_LEFT(tmp, field) == NULL || \
  475. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
  476. struct type *oright; \
  477. if ((oright = RB_RIGHT(tmp, field)) \
  478. != NULL) \
  479. RB_COLOR(oright, field) = RB_BLACK;\
  480. RB_COLOR(tmp, field) = RB_RED; \
  481. RB_ROTATE_LEFT(head, tmp, oright, field);\
  482. tmp = RB_LEFT(parent, field); \
  483. } \
  484. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  485. RB_COLOR(parent, field) = RB_BLACK; \
  486. if (RB_LEFT(tmp, field)) \
  487. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
  488. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  489. elm = RB_ROOT(head); \
  490. break; \
  491. } \
  492. } \
  493. } \
  494. if (elm) \
  495. RB_COLOR(elm, field) = RB_BLACK; \
  496. } \
  497. \
  498. attr struct type * \
  499. name##_RB_REMOVE(struct name *head, struct type *elm) \
  500. { \
  501. struct type *child, *parent, *old = elm; \
  502. int color; \
  503. if (RB_LEFT(elm, field) == NULL) \
  504. child = RB_RIGHT(elm, field); \
  505. else if (RB_RIGHT(elm, field) == NULL) \
  506. child = RB_LEFT(elm, field); \
  507. else { \
  508. struct type *left; \
  509. elm = RB_RIGHT(elm, field); \
  510. while ((left = RB_LEFT(elm, field)) != NULL) \
  511. elm = left; \
  512. child = RB_RIGHT(elm, field); \
  513. parent = RB_PARENT(elm, field); \
  514. color = RB_COLOR(elm, field); \
  515. if (child) \
  516. RB_PARENT(child, field) = parent; \
  517. if (parent) { \
  518. if (RB_LEFT(parent, field) == elm) \
  519. RB_LEFT(parent, field) = child; \
  520. else \
  521. RB_RIGHT(parent, field) = child; \
  522. RB_AUGMENT(parent); \
  523. } else \
  524. RB_ROOT(head) = child; \
  525. if (RB_PARENT(elm, field) == old) \
  526. parent = elm; \
  527. (elm)->field = (old)->field; \
  528. if (RB_PARENT(old, field)) { \
  529. if (RB_LEFT(RB_PARENT(old, field), field) == old)\
  530. RB_LEFT(RB_PARENT(old, field), field) = elm;\
  531. else \
  532. RB_RIGHT(RB_PARENT(old, field), field) = elm;\
  533. RB_AUGMENT(RB_PARENT(old, field)); \
  534. } else \
  535. RB_ROOT(head) = elm; \
  536. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  537. if (RB_RIGHT(old, field)) \
  538. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  539. if (parent) { \
  540. left = parent; \
  541. do { \
  542. RB_AUGMENT(left); \
  543. } while ((left = RB_PARENT(left, field)) != NULL); \
  544. } \
  545. goto color; \
  546. } \
  547. parent = RB_PARENT(elm, field); \
  548. color = RB_COLOR(elm, field); \
  549. if (child) \
  550. RB_PARENT(child, field) = parent; \
  551. if (parent) { \
  552. if (RB_LEFT(parent, field) == elm) \
  553. RB_LEFT(parent, field) = child; \
  554. else \
  555. RB_RIGHT(parent, field) = child; \
  556. RB_AUGMENT(parent); \
  557. } else \
  558. RB_ROOT(head) = child; \
  559. color: \
  560. if (color == RB_BLACK) \
  561. name##_RB_REMOVE_COLOR(head, parent, child); \
  562. return (old); \
  563. } \
  564. \
  565. /* Inserts a node into the RB tree */ \
  566. attr struct type * \
  567. name##_RB_INSERT(struct name *head, struct type *elm) \
  568. { \
  569. struct type *tmp; \
  570. struct type *parent = NULL; \
  571. int comp = 0; \
  572. tmp = RB_ROOT(head); \
  573. while (tmp) { \
  574. parent = tmp; \
  575. comp = (cmp)(elm, parent); \
  576. if (comp < 0) \
  577. tmp = RB_LEFT(tmp, field); \
  578. else if (comp > 0) \
  579. tmp = RB_RIGHT(tmp, field); \
  580. else \
  581. return (tmp); \
  582. } \
  583. RB_SET(elm, parent, field); \
  584. if (parent != NULL) { \
  585. if (comp < 0) \
  586. RB_LEFT(parent, field) = elm; \
  587. else \
  588. RB_RIGHT(parent, field) = elm; \
  589. RB_AUGMENT(parent); \
  590. } else \
  591. RB_ROOT(head) = elm; \
  592. name##_RB_INSERT_COLOR(head, elm); \
  593. return (NULL); \
  594. } \
  595. \
  596. /* Finds the node with the same key as elm */ \
  597. attr struct type * \
  598. name##_RB_FIND(struct name *head, struct type *elm) \
  599. { \
  600. struct type *tmp = RB_ROOT(head); \
  601. int comp; \
  602. while (tmp) { \
  603. comp = cmp(elm, tmp); \
  604. if (comp < 0) \
  605. tmp = RB_LEFT(tmp, field); \
  606. else if (comp > 0) \
  607. tmp = RB_RIGHT(tmp, field); \
  608. else \
  609. return (tmp); \
  610. } \
  611. return (NULL); \
  612. } \
  613. \
  614. /* Finds the first node greater than or equal to the search key */ \
  615. attr struct type * \
  616. name##_RB_NFIND(struct name *head, struct type *elm) \
  617. { \
  618. struct type *tmp = RB_ROOT(head); \
  619. struct type *res = NULL; \
  620. int comp; \
  621. while (tmp) { \
  622. comp = cmp(elm, tmp); \
  623. if (comp < 0) { \
  624. res = tmp; \
  625. tmp = RB_LEFT(tmp, field); \
  626. } \
  627. else if (comp > 0) \
  628. tmp = RB_RIGHT(tmp, field); \
  629. else \
  630. return (tmp); \
  631. } \
  632. return (res); \
  633. } \
  634. \
  635. /* ARGSUSED */ \
  636. attr struct type * \
  637. name##_RB_NEXT(struct type *elm) \
  638. { \
  639. if (RB_RIGHT(elm, field)) { \
  640. elm = RB_RIGHT(elm, field); \
  641. while (RB_LEFT(elm, field)) \
  642. elm = RB_LEFT(elm, field); \
  643. } else { \
  644. if (RB_PARENT(elm, field) && \
  645. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  646. elm = RB_PARENT(elm, field); \
  647. else { \
  648. while (RB_PARENT(elm, field) && \
  649. (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
  650. elm = RB_PARENT(elm, field); \
  651. elm = RB_PARENT(elm, field); \
  652. } \
  653. } \
  654. return (elm); \
  655. } \
  656. \
  657. /* ARGSUSED */ \
  658. attr struct type * \
  659. name##_RB_PREV(struct type *elm) \
  660. { \
  661. if (RB_LEFT(elm, field)) { \
  662. elm = RB_LEFT(elm, field); \
  663. while (RB_RIGHT(elm, field)) \
  664. elm = RB_RIGHT(elm, field); \
  665. } else { \
  666. if (RB_PARENT(elm, field) && \
  667. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  668. elm = RB_PARENT(elm, field); \
  669. else { \
  670. while (RB_PARENT(elm, field) && \
  671. (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
  672. elm = RB_PARENT(elm, field); \
  673. elm = RB_PARENT(elm, field); \
  674. } \
  675. } \
  676. return (elm); \
  677. } \
  678. \
  679. attr struct type * \
  680. name##_RB_MINMAX(struct name *head, int val) \
  681. { \
  682. struct type *tmp = RB_ROOT(head); \
  683. struct type *parent = NULL; \
  684. while (tmp) { \
  685. parent = tmp; \
  686. if (val < 0) \
  687. tmp = RB_LEFT(tmp, field); \
  688. else \
  689. tmp = RB_RIGHT(tmp, field); \
  690. } \
  691. return (parent); \
  692. }
  693. #define RB_NEGINF -1
  694. #define RB_INF 1
  695. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  696. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  697. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  698. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  699. #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
  700. #define RB_PREV(name, x, y) name##_RB_PREV(y)
  701. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  702. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  703. #define RB_FOREACH(x, name, head) \
  704. for ((x) = RB_MIN(name, head); \
  705. (x) != NULL; \
  706. (x) = name##_RB_NEXT(x))
  707. #define RB_FOREACH_REVERSE(x, name, head) \
  708. for ((x) = RB_MAX(name, head); \
  709. (x) != NULL; \
  710. (x) = name##_RB_PREV(x))
  711. #endif /* _SYS_TREE_H_ */