tree.h 27 KB

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