tree.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
  356. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
  357. attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
  358. attr struct type *name##_RB_INSERT(struct name *, struct type *); \
  359. attr struct type *name##_RB_FIND(struct name *, struct type *); \
  360. attr struct type *name##_RB_NFIND(struct name *, struct type *); \
  361. attr struct type *name##_RB_NEXT(struct type *); \
  362. attr struct type *name##_RB_PREV(struct type *); \
  363. attr struct type *name##_RB_MINMAX(struct name *, int); \
  364. \
  365. /* Main rb operation.
  366. * Moves node close to the key of elm to top
  367. */
  368. #define RB_GENERATE(name, type, field, cmp) \
  369. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  370. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  371. RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
  372. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  373. attr void \
  374. name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  375. { \
  376. struct type *parent, *gparent, *tmp; \
  377. while ((parent = RB_PARENT(elm, field)) != NULL && \
  378. RB_COLOR(parent, field) == RB_RED) { \
  379. gparent = RB_PARENT(parent, field); \
  380. if (parent == RB_LEFT(gparent, field)) { \
  381. tmp = RB_RIGHT(gparent, field); \
  382. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  383. RB_COLOR(tmp, field) = RB_BLACK; \
  384. RB_SET_BLACKRED(parent, gparent, field);\
  385. elm = gparent; \
  386. continue; \
  387. } \
  388. if (RB_RIGHT(parent, field) == elm) { \
  389. RB_ROTATE_LEFT(head, parent, tmp, field);\
  390. tmp = parent; \
  391. parent = elm; \
  392. elm = tmp; \
  393. } \
  394. RB_SET_BLACKRED(parent, gparent, field); \
  395. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  396. } else { \
  397. tmp = RB_LEFT(gparent, field); \
  398. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  399. RB_COLOR(tmp, field) = RB_BLACK; \
  400. RB_SET_BLACKRED(parent, gparent, field);\
  401. elm = gparent; \
  402. continue; \
  403. } \
  404. if (RB_LEFT(parent, field) == elm) { \
  405. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  406. tmp = parent; \
  407. parent = elm; \
  408. elm = tmp; \
  409. } \
  410. RB_SET_BLACKRED(parent, gparent, field); \
  411. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  412. } \
  413. } \
  414. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  415. } \
  416. \
  417. attr void \
  418. name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
  419. { \
  420. struct type *tmp; \
  421. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  422. elm != RB_ROOT(head)) { \
  423. if (RB_LEFT(parent, field) == elm) { \
  424. tmp = RB_RIGHT(parent, field); \
  425. if (RB_COLOR(tmp, field) == RB_RED) { \
  426. RB_SET_BLACKRED(tmp, parent, field); \
  427. RB_ROTATE_LEFT(head, parent, tmp, field);\
  428. tmp = RB_RIGHT(parent, field); \
  429. } \
  430. if ((RB_LEFT(tmp, field) == NULL || \
  431. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  432. (RB_RIGHT(tmp, field) == NULL || \
  433. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  434. RB_COLOR(tmp, field) = RB_RED; \
  435. elm = parent; \
  436. parent = RB_PARENT(elm, field); \
  437. } else { \
  438. if (RB_RIGHT(tmp, field) == NULL || \
  439. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
  440. struct type *oleft; \
  441. if ((oleft = RB_LEFT(tmp, field)) \
  442. != NULL) \
  443. RB_COLOR(oleft, field) = RB_BLACK;\
  444. RB_COLOR(tmp, field) = RB_RED; \
  445. RB_ROTATE_RIGHT(head, tmp, oleft, field);\
  446. tmp = RB_RIGHT(parent, field); \
  447. } \
  448. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  449. RB_COLOR(parent, field) = RB_BLACK; \
  450. if (RB_RIGHT(tmp, field)) \
  451. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
  452. RB_ROTATE_LEFT(head, parent, tmp, field);\
  453. elm = RB_ROOT(head); \
  454. break; \
  455. } \
  456. } else { \
  457. tmp = RB_LEFT(parent, field); \
  458. if (RB_COLOR(tmp, field) == RB_RED) { \
  459. RB_SET_BLACKRED(tmp, parent, field); \
  460. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  461. tmp = RB_LEFT(parent, field); \
  462. } \
  463. if ((RB_LEFT(tmp, field) == NULL || \
  464. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  465. (RB_RIGHT(tmp, field) == NULL || \
  466. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  467. RB_COLOR(tmp, field) = RB_RED; \
  468. elm = parent; \
  469. parent = RB_PARENT(elm, field); \
  470. } else { \
  471. if (RB_LEFT(tmp, field) == NULL || \
  472. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
  473. struct type *oright; \
  474. if ((oright = RB_RIGHT(tmp, field)) \
  475. != NULL) \
  476. RB_COLOR(oright, field) = RB_BLACK;\
  477. RB_COLOR(tmp, field) = RB_RED; \
  478. RB_ROTATE_LEFT(head, tmp, oright, field);\
  479. tmp = RB_LEFT(parent, field); \
  480. } \
  481. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  482. RB_COLOR(parent, field) = RB_BLACK; \
  483. if (RB_LEFT(tmp, field)) \
  484. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
  485. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  486. elm = RB_ROOT(head); \
  487. break; \
  488. } \
  489. } \
  490. } \
  491. if (elm) \
  492. RB_COLOR(elm, field) = RB_BLACK; \
  493. } \
  494. \
  495. attr struct type * \
  496. name##_RB_REMOVE(struct name *head, struct type *elm) \
  497. { \
  498. struct type *child, *parent, *old = elm; \
  499. int color; \
  500. if (RB_LEFT(elm, field) == NULL) \
  501. child = RB_RIGHT(elm, field); \
  502. else if (RB_RIGHT(elm, field) == NULL) \
  503. child = RB_LEFT(elm, field); \
  504. else { \
  505. struct type *left; \
  506. elm = RB_RIGHT(elm, field); \
  507. while ((left = RB_LEFT(elm, field)) != NULL) \
  508. elm = left; \
  509. child = RB_RIGHT(elm, field); \
  510. parent = RB_PARENT(elm, field); \
  511. color = RB_COLOR(elm, field); \
  512. if (child) \
  513. RB_PARENT(child, field) = parent; \
  514. if (parent) { \
  515. if (RB_LEFT(parent, field) == elm) \
  516. RB_LEFT(parent, field) = child; \
  517. else \
  518. RB_RIGHT(parent, field) = child; \
  519. RB_AUGMENT(parent); \
  520. } else \
  521. RB_ROOT(head) = child; \
  522. if (RB_PARENT(elm, field) == old) \
  523. parent = elm; \
  524. (elm)->field = (old)->field; \
  525. if (RB_PARENT(old, field)) { \
  526. if (RB_LEFT(RB_PARENT(old, field), field) == old)\
  527. RB_LEFT(RB_PARENT(old, field), field) = elm;\
  528. else \
  529. RB_RIGHT(RB_PARENT(old, field), field) = elm;\
  530. RB_AUGMENT(RB_PARENT(old, field)); \
  531. } else \
  532. RB_ROOT(head) = elm; \
  533. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  534. if (RB_RIGHT(old, field)) \
  535. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  536. if (parent) { \
  537. left = parent; \
  538. do { \
  539. RB_AUGMENT(left); \
  540. } while ((left = RB_PARENT(left, field)) != NULL); \
  541. } \
  542. goto color; \
  543. } \
  544. parent = RB_PARENT(elm, field); \
  545. color = RB_COLOR(elm, field); \
  546. if (child) \
  547. RB_PARENT(child, field) = parent; \
  548. if (parent) { \
  549. if (RB_LEFT(parent, field) == elm) \
  550. RB_LEFT(parent, field) = child; \
  551. else \
  552. RB_RIGHT(parent, field) = child; \
  553. RB_AUGMENT(parent); \
  554. } else \
  555. RB_ROOT(head) = child; \
  556. color: \
  557. if (color == RB_BLACK) \
  558. name##_RB_REMOVE_COLOR(head, parent, child); \
  559. return (old); \
  560. } \
  561. \
  562. /* Inserts a node into the RB tree */ \
  563. attr struct type * \
  564. name##_RB_INSERT(struct name *head, struct type *elm) \
  565. { \
  566. struct type *tmp; \
  567. struct type *parent = NULL; \
  568. int comp = 0; \
  569. tmp = RB_ROOT(head); \
  570. while (tmp) { \
  571. parent = tmp; \
  572. comp = (cmp)(elm, parent); \
  573. if (comp < 0) \
  574. tmp = RB_LEFT(tmp, field); \
  575. else if (comp > 0) \
  576. tmp = RB_RIGHT(tmp, field); \
  577. else \
  578. return (tmp); \
  579. } \
  580. RB_SET(elm, parent, field); \
  581. if (parent != NULL) { \
  582. if (comp < 0) \
  583. RB_LEFT(parent, field) = elm; \
  584. else \
  585. RB_RIGHT(parent, field) = elm; \
  586. RB_AUGMENT(parent); \
  587. } else \
  588. RB_ROOT(head) = elm; \
  589. name##_RB_INSERT_COLOR(head, elm); \
  590. return (NULL); \
  591. } \
  592. \
  593. /* Finds the node with the same key as elm */ \
  594. attr struct type * \
  595. name##_RB_FIND(struct name *head, struct type *elm) \
  596. { \
  597. struct type *tmp = RB_ROOT(head); \
  598. int comp; \
  599. while (tmp) { \
  600. comp = cmp(elm, tmp); \
  601. if (comp < 0) \
  602. tmp = RB_LEFT(tmp, field); \
  603. else if (comp > 0) \
  604. tmp = RB_RIGHT(tmp, field); \
  605. else \
  606. return (tmp); \
  607. } \
  608. return (NULL); \
  609. } \
  610. \
  611. /* Finds the first node greater than or equal to the search key */ \
  612. attr struct type * \
  613. name##_RB_NFIND(struct name *head, struct type *elm) \
  614. { \
  615. struct type *tmp = RB_ROOT(head); \
  616. struct type *res = NULL; \
  617. int comp; \
  618. while (tmp) { \
  619. comp = cmp(elm, tmp); \
  620. if (comp < 0) { \
  621. res = tmp; \
  622. tmp = RB_LEFT(tmp, field); \
  623. } \
  624. else if (comp > 0) \
  625. tmp = RB_RIGHT(tmp, field); \
  626. else \
  627. return (tmp); \
  628. } \
  629. return (res); \
  630. } \
  631. \
  632. /* ARGSUSED */ \
  633. attr struct type * \
  634. name##_RB_NEXT(struct type *elm) \
  635. { \
  636. if (RB_RIGHT(elm, field)) { \
  637. elm = RB_RIGHT(elm, field); \
  638. while (RB_LEFT(elm, field)) \
  639. elm = RB_LEFT(elm, field); \
  640. } else { \
  641. if (RB_PARENT(elm, field) && \
  642. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  643. elm = RB_PARENT(elm, field); \
  644. else { \
  645. while (RB_PARENT(elm, field) && \
  646. (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
  647. elm = RB_PARENT(elm, field); \
  648. elm = RB_PARENT(elm, field); \
  649. } \
  650. } \
  651. return (elm); \
  652. } \
  653. \
  654. /* ARGSUSED */ \
  655. attr struct type * \
  656. name##_RB_PREV(struct type *elm) \
  657. { \
  658. if (RB_LEFT(elm, field)) { \
  659. elm = RB_LEFT(elm, field); \
  660. while (RB_RIGHT(elm, field)) \
  661. elm = RB_RIGHT(elm, field); \
  662. } else { \
  663. if (RB_PARENT(elm, field) && \
  664. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  665. elm = RB_PARENT(elm, field); \
  666. else { \
  667. while (RB_PARENT(elm, field) && \
  668. (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
  669. elm = RB_PARENT(elm, field); \
  670. elm = RB_PARENT(elm, field); \
  671. } \
  672. } \
  673. return (elm); \
  674. } \
  675. \
  676. attr struct type * \
  677. name##_RB_MINMAX(struct name *head, int val) \
  678. { \
  679. struct type *tmp = RB_ROOT(head); \
  680. struct type *parent = NULL; \
  681. while (tmp) { \
  682. parent = tmp; \
  683. if (val < 0) \
  684. tmp = RB_LEFT(tmp, field); \
  685. else \
  686. tmp = RB_RIGHT(tmp, field); \
  687. } \
  688. return (parent); \
  689. }
  690. #define RB_NEGINF -1
  691. #define RB_INF 1
  692. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  693. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  694. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  695. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  696. #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
  697. #define RB_PREV(name, x, y) name##_RB_PREV(y)
  698. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  699. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  700. #define RB_FOREACH(x, name, head) \
  701. for ((x) = RB_MIN(name, head); \
  702. (x) != NULL; \
  703. (x) = name##_RB_NEXT(x))
  704. #define RB_FOREACH_FROM(x, name, y) \
  705. for ((x) = (y); \
  706. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  707. (x) = (y))
  708. #define RB_FOREACH_SAFE(x, name, head, y) \
  709. for ((x) = RB_MIN(name, head); \
  710. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  711. (x) = (y))
  712. #define RB_FOREACH_REVERSE(x, name, head) \
  713. for ((x) = RB_MAX(name, head); \
  714. (x) != NULL; \
  715. (x) = name##_RB_PREV(x))
  716. #define RB_FOREACH_REVERSE_FROM(x, name, y) \
  717. for ((x) = (y); \
  718. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  719. (x) = (y))
  720. #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
  721. for ((x) = RB_MAX(name, head); \
  722. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  723. (x) = (y))
  724. #endif /* _SYS_TREE_H_ */