tree.h 25 KB

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