tree.h 26 KB

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