tree.h 27 KB

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