tree.h 29 KB

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