tree.h 25 KB

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