sys-tree.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* $OpenBSD: tree.h,v 1.13 2011/07/09 00:19:45 pirofti Exp $ */
  2. /*
  3. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* OPENBSD ORIGINAL: sys/sys/tree.h */
  27. #include "config.h"
  28. #ifdef NO_ATTRIBUTE_ON_RETURN_TYPE
  29. # define __attribute__(x)
  30. #endif
  31. #ifndef _SYS_TREE_H_
  32. #define _SYS_TREE_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 (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 (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 (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 (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 (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 (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 __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 __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 __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))) { \
  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 (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_ROOT(head) (head)->rbh_root
  300. #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
  301. #define RB_SET(elm, parent, field) do { \
  302. RB_PARENT(elm, field) = parent; \
  303. RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
  304. RB_COLOR(elm, field) = RB_RED; \
  305. } while (0)
  306. #define RB_SET_BLACKRED(black, red, field) do { \
  307. RB_COLOR(black, field) = RB_BLACK; \
  308. RB_COLOR(red, field) = RB_RED; \
  309. } while (0)
  310. #ifndef RB_AUGMENT
  311. #define RB_AUGMENT(x) do {} while (0)
  312. #endif
  313. #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
  314. (tmp) = RB_RIGHT(elm, field); \
  315. if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
  316. RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
  317. } \
  318. RB_AUGMENT(elm); \
  319. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
  320. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  321. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  322. else \
  323. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  324. } else \
  325. (head)->rbh_root = (tmp); \
  326. RB_LEFT(tmp, field) = (elm); \
  327. RB_PARENT(elm, field) = (tmp); \
  328. RB_AUGMENT(tmp); \
  329. if ((RB_PARENT(tmp, field))) \
  330. RB_AUGMENT(RB_PARENT(tmp, field)); \
  331. } while (0)
  332. #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
  333. (tmp) = RB_LEFT(elm, field); \
  334. if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
  335. RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
  336. } \
  337. RB_AUGMENT(elm); \
  338. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
  339. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  340. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  341. else \
  342. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  343. } else \
  344. (head)->rbh_root = (tmp); \
  345. RB_RIGHT(tmp, field) = (elm); \
  346. RB_PARENT(elm, field) = (tmp); \
  347. RB_AUGMENT(tmp); \
  348. if ((RB_PARENT(tmp, field))) \
  349. RB_AUGMENT(RB_PARENT(tmp, field)); \
  350. } while (0)
  351. /* Generates prototypes and inline functions */
  352. #define RB_PROTOTYPE(name, type, field, cmp) \
  353. RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
  354. #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
  355. RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
  356. #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
  357. attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
  358. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
  359. attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
  360. attr struct type *name##_RB_INSERT(struct name *, struct type *); \
  361. attr struct type *name##_RB_FIND(struct name *, struct type *); \
  362. attr struct type *name##_RB_NFIND(struct name *, struct type *); \
  363. attr struct type *name##_RB_NEXT(struct type *); \
  364. attr struct type *name##_RB_PREV(struct type *); \
  365. attr struct type *name##_RB_MINMAX(struct name *, int); \
  366. \
  367. /* Main rb operation.
  368. * Moves node close to the key of elm to top
  369. */
  370. #define RB_GENERATE(name, type, field, cmp) \
  371. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  372. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  373. RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
  374. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  375. attr void \
  376. name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  377. { \
  378. struct type *parent, *gparent, *tmp; \
  379. while ((parent = RB_PARENT(elm, field)) && \
  380. RB_COLOR(parent, field) == RB_RED) { \
  381. gparent = RB_PARENT(parent, field); \
  382. if (parent == RB_LEFT(gparent, field)) { \
  383. tmp = RB_RIGHT(gparent, field); \
  384. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  385. RB_COLOR(tmp, field) = RB_BLACK; \
  386. RB_SET_BLACKRED(parent, gparent, field);\
  387. elm = gparent; \
  388. continue; \
  389. } \
  390. if (RB_RIGHT(parent, field) == elm) { \
  391. RB_ROTATE_LEFT(head, parent, tmp, field);\
  392. tmp = parent; \
  393. parent = elm; \
  394. elm = tmp; \
  395. } \
  396. RB_SET_BLACKRED(parent, gparent, field); \
  397. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  398. } else { \
  399. tmp = RB_LEFT(gparent, field); \
  400. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  401. RB_COLOR(tmp, field) = RB_BLACK; \
  402. RB_SET_BLACKRED(parent, gparent, field);\
  403. elm = gparent; \
  404. continue; \
  405. } \
  406. if (RB_LEFT(parent, field) == elm) { \
  407. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  408. tmp = parent; \
  409. parent = elm; \
  410. elm = tmp; \
  411. } \
  412. RB_SET_BLACKRED(parent, gparent, field); \
  413. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  414. } \
  415. } \
  416. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  417. } \
  418. \
  419. attr void \
  420. name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
  421. { \
  422. struct type *tmp; \
  423. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  424. elm != RB_ROOT(head)) { \
  425. if (RB_LEFT(parent, field) == elm) { \
  426. tmp = RB_RIGHT(parent, field); \
  427. if (RB_COLOR(tmp, field) == RB_RED) { \
  428. RB_SET_BLACKRED(tmp, parent, field); \
  429. RB_ROTATE_LEFT(head, parent, tmp, field);\
  430. tmp = RB_RIGHT(parent, field); \
  431. } \
  432. if ((RB_LEFT(tmp, field) == NULL || \
  433. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  434. (RB_RIGHT(tmp, field) == NULL || \
  435. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  436. RB_COLOR(tmp, field) = RB_RED; \
  437. elm = parent; \
  438. parent = RB_PARENT(elm, field); \
  439. } else { \
  440. if (RB_RIGHT(tmp, field) == NULL || \
  441. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
  442. struct type *oleft; \
  443. if ((oleft = RB_LEFT(tmp, field)))\
  444. RB_COLOR(oleft, field) = RB_BLACK;\
  445. RB_COLOR(tmp, field) = RB_RED; \
  446. RB_ROTATE_RIGHT(head, tmp, oleft, field);\
  447. tmp = RB_RIGHT(parent, field); \
  448. } \
  449. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  450. RB_COLOR(parent, field) = RB_BLACK; \
  451. if (RB_RIGHT(tmp, field)) \
  452. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
  453. RB_ROTATE_LEFT(head, parent, tmp, field);\
  454. elm = RB_ROOT(head); \
  455. break; \
  456. } \
  457. } else { \
  458. tmp = RB_LEFT(parent, field); \
  459. if (RB_COLOR(tmp, field) == RB_RED) { \
  460. RB_SET_BLACKRED(tmp, parent, field); \
  461. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  462. tmp = RB_LEFT(parent, field); \
  463. } \
  464. if ((RB_LEFT(tmp, field) == NULL || \
  465. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  466. (RB_RIGHT(tmp, field) == NULL || \
  467. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  468. RB_COLOR(tmp, field) = RB_RED; \
  469. elm = parent; \
  470. parent = RB_PARENT(elm, field); \
  471. } else { \
  472. if (RB_LEFT(tmp, field) == NULL || \
  473. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
  474. struct type *oright; \
  475. if ((oright = RB_RIGHT(tmp, field)))\
  476. RB_COLOR(oright, field) = RB_BLACK;\
  477. RB_COLOR(tmp, field) = RB_RED; \
  478. RB_ROTATE_LEFT(head, tmp, oright, field);\
  479. tmp = RB_LEFT(parent, field); \
  480. } \
  481. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  482. RB_COLOR(parent, field) = RB_BLACK; \
  483. if (RB_LEFT(tmp, field)) \
  484. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
  485. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  486. elm = RB_ROOT(head); \
  487. break; \
  488. } \
  489. } \
  490. } \
  491. if (elm) \
  492. RB_COLOR(elm, field) = RB_BLACK; \
  493. } \
  494. \
  495. attr struct type * \
  496. name##_RB_REMOVE(struct name *head, struct type *elm) \
  497. { \
  498. struct type *child, *parent, *old = elm; \
  499. int color; \
  500. if (RB_LEFT(elm, field) == NULL) \
  501. child = RB_RIGHT(elm, field); \
  502. else if (RB_RIGHT(elm, field) == NULL) \
  503. child = RB_LEFT(elm, field); \
  504. else { \
  505. struct type *left; \
  506. elm = RB_RIGHT(elm, field); \
  507. while ((left = RB_LEFT(elm, field))) \
  508. elm = left; \
  509. child = RB_RIGHT(elm, field); \
  510. parent = RB_PARENT(elm, field); \
  511. color = RB_COLOR(elm, field); \
  512. if (child) \
  513. RB_PARENT(child, field) = parent; \
  514. if (parent) { \
  515. if (RB_LEFT(parent, field) == elm) \
  516. RB_LEFT(parent, field) = child; \
  517. else \
  518. RB_RIGHT(parent, field) = child; \
  519. RB_AUGMENT(parent); \
  520. } else \
  521. RB_ROOT(head) = child; \
  522. if (RB_PARENT(elm, field) == old) \
  523. parent = elm; \
  524. (elm)->field = (old)->field; \
  525. if (RB_PARENT(old, field)) { \
  526. if (RB_LEFT(RB_PARENT(old, field), field) == old)\
  527. RB_LEFT(RB_PARENT(old, field), field) = elm;\
  528. else \
  529. RB_RIGHT(RB_PARENT(old, field), field) = elm;\
  530. RB_AUGMENT(RB_PARENT(old, field)); \
  531. } else \
  532. RB_ROOT(head) = elm; \
  533. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  534. if (RB_RIGHT(old, field)) \
  535. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  536. if (parent) { \
  537. left = parent; \
  538. do { \
  539. RB_AUGMENT(left); \
  540. } while ((left = RB_PARENT(left, field))); \
  541. } \
  542. goto color; \
  543. } \
  544. parent = RB_PARENT(elm, field); \
  545. color = RB_COLOR(elm, field); \
  546. if (child) \
  547. RB_PARENT(child, field) = parent; \
  548. if (parent) { \
  549. if (RB_LEFT(parent, field) == elm) \
  550. RB_LEFT(parent, field) = child; \
  551. else \
  552. RB_RIGHT(parent, field) = child; \
  553. RB_AUGMENT(parent); \
  554. } else \
  555. RB_ROOT(head) = child; \
  556. color: \
  557. if (color == RB_BLACK) \
  558. name##_RB_REMOVE_COLOR(head, parent, child); \
  559. return (old); \
  560. } \
  561. \
  562. /* Inserts a node into the RB tree */ \
  563. attr struct type * \
  564. name##_RB_INSERT(struct name *head, struct type *elm) \
  565. { \
  566. struct type *tmp; \
  567. struct type *parent = NULL; \
  568. int comp = 0; \
  569. tmp = RB_ROOT(head); \
  570. while (tmp) { \
  571. parent = tmp; \
  572. comp = (cmp)(elm, parent); \
  573. if (comp < 0) \
  574. tmp = RB_LEFT(tmp, field); \
  575. else if (comp > 0) \
  576. tmp = RB_RIGHT(tmp, field); \
  577. else \
  578. return (tmp); \
  579. } \
  580. RB_SET(elm, parent, field); \
  581. if (parent != NULL) { \
  582. if (comp < 0) \
  583. RB_LEFT(parent, field) = elm; \
  584. else \
  585. RB_RIGHT(parent, field) = elm; \
  586. RB_AUGMENT(parent); \
  587. } else \
  588. RB_ROOT(head) = elm; \
  589. name##_RB_INSERT_COLOR(head, elm); \
  590. return (NULL); \
  591. } \
  592. \
  593. /* Finds the node with the same key as elm */ \
  594. attr struct type * \
  595. name##_RB_FIND(struct name *head, struct type *elm) \
  596. { \
  597. struct type *tmp = RB_ROOT(head); \
  598. int comp; \
  599. while (tmp) { \
  600. comp = cmp(elm, tmp); \
  601. if (comp < 0) \
  602. tmp = RB_LEFT(tmp, field); \
  603. else if (comp > 0) \
  604. tmp = RB_RIGHT(tmp, field); \
  605. else \
  606. return (tmp); \
  607. } \
  608. return (NULL); \
  609. } \
  610. \
  611. /* Finds the first node greater than or equal to the search key */ \
  612. attr struct type * \
  613. name##_RB_NFIND(struct name *head, struct type *elm) \
  614. { \
  615. struct type *tmp = RB_ROOT(head); \
  616. struct type *res = NULL; \
  617. int comp; \
  618. while (tmp) { \
  619. comp = cmp(elm, tmp); \
  620. if (comp < 0) { \
  621. res = tmp; \
  622. tmp = RB_LEFT(tmp, field); \
  623. } \
  624. else if (comp > 0) \
  625. tmp = RB_RIGHT(tmp, field); \
  626. else \
  627. return (tmp); \
  628. } \
  629. return (res); \
  630. } \
  631. \
  632. /* ARGSUSED */ \
  633. attr struct type * \
  634. name##_RB_NEXT(struct type *elm) \
  635. { \
  636. if (RB_RIGHT(elm, field)) { \
  637. elm = RB_RIGHT(elm, field); \
  638. while (RB_LEFT(elm, field)) \
  639. elm = RB_LEFT(elm, field); \
  640. } else { \
  641. if (RB_PARENT(elm, field) && \
  642. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  643. elm = RB_PARENT(elm, field); \
  644. else { \
  645. while (RB_PARENT(elm, field) && \
  646. (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
  647. elm = RB_PARENT(elm, field); \
  648. elm = RB_PARENT(elm, field); \
  649. } \
  650. } \
  651. return (elm); \
  652. } \
  653. \
  654. /* ARGSUSED */ \
  655. attr struct type * \
  656. name##_RB_PREV(struct type *elm) \
  657. { \
  658. if (RB_LEFT(elm, field)) { \
  659. elm = RB_LEFT(elm, field); \
  660. while (RB_RIGHT(elm, field)) \
  661. elm = RB_RIGHT(elm, field); \
  662. } else { \
  663. if (RB_PARENT(elm, field) && \
  664. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  665. elm = RB_PARENT(elm, field); \
  666. else { \
  667. while (RB_PARENT(elm, field) && \
  668. (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
  669. elm = RB_PARENT(elm, field); \
  670. elm = RB_PARENT(elm, field); \
  671. } \
  672. } \
  673. return (elm); \
  674. } \
  675. \
  676. attr struct type * \
  677. name##_RB_MINMAX(struct name *head, int val) \
  678. { \
  679. struct type *tmp = RB_ROOT(head); \
  680. struct type *parent = NULL; \
  681. while (tmp) { \
  682. parent = tmp; \
  683. if (val < 0) \
  684. tmp = RB_LEFT(tmp, field); \
  685. else \
  686. tmp = RB_RIGHT(tmp, field); \
  687. } \
  688. return (parent); \
  689. }
  690. #define RB_NEGINF -1
  691. #define RB_INF 1
  692. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  693. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  694. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  695. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  696. #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
  697. #define RB_PREV(name, x, y) name##_RB_PREV(y)
  698. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  699. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  700. #define RB_FOREACH(x, name, head) \
  701. for ((x) = RB_MIN(name, head); \
  702. (x) != NULL; \
  703. (x) = name##_RB_NEXT(x))
  704. #define RB_FOREACH_SAFE(x, name, head, y) \
  705. for ((x) = RB_MIN(name, head); \
  706. ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
  707. (x) = (y))
  708. #define RB_FOREACH_REVERSE(x, name, head) \
  709. for ((x) = RB_MAX(name, head); \
  710. (x) != NULL; \
  711. (x) = name##_RB_PREV(x))
  712. #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
  713. for ((x) = RB_MAX(name, head); \
  714. ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
  715. (x) = (y))
  716. #endif /* _SYS_TREE_H_ */