You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

804 satır
27KB

  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_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 (/*CONSTCOND*/ 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 (/*CONSTCOND*/ 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)) != NULL) { \
  316. RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
  317. } \
  318. RB_AUGMENT(elm); \
  319. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  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 (/*CONSTCOND*/ 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)) != NULL) { \
  335. RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
  336. } \
  337. RB_AUGMENT(elm); \
  338. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  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 (/*CONSTCOND*/ 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, __unused static)
  356. #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
  357. RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
  358. RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
  359. RB_PROTOTYPE_INSERT(name, type, attr); \
  360. RB_PROTOTYPE_REMOVE(name, type, attr); \
  361. RB_PROTOTYPE_FIND(name, type, attr); \
  362. RB_PROTOTYPE_NFIND(name, type, attr); \
  363. RB_PROTOTYPE_NEXT(name, type, attr); \
  364. RB_PROTOTYPE_PREV(name, type, attr); \
  365. RB_PROTOTYPE_MINMAX(name, type, attr);
  366. #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
  367. attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
  368. #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
  369. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *)
  370. #define RB_PROTOTYPE_REMOVE(name, type, attr) \
  371. attr struct type *name##_RB_REMOVE(struct name *, struct type *)
  372. #define RB_PROTOTYPE_INSERT(name, type, attr) \
  373. attr struct type *name##_RB_INSERT(struct name *, struct type *)
  374. #define RB_PROTOTYPE_FIND(name, type, attr) \
  375. attr struct type *name##_RB_FIND(struct name *, struct type *)
  376. #define RB_PROTOTYPE_NFIND(name, type, attr) \
  377. attr struct type *name##_RB_NFIND(struct name *, struct type *)
  378. #define RB_PROTOTYPE_NEXT(name, type, attr) \
  379. attr struct type *name##_RB_NEXT(struct type *)
  380. #define RB_PROTOTYPE_PREV(name, type, attr) \
  381. attr struct type *name##_RB_PREV(struct type *)
  382. #define RB_PROTOTYPE_MINMAX(name, type, attr) \
  383. attr struct type *name##_RB_MINMAX(struct name *, int)
  384. /* Main rb operation.
  385. * Moves node close to the key of elm to top
  386. */
  387. #define RB_GENERATE(name, type, field, cmp) \
  388. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  389. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  390. RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
  391. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  392. RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
  393. RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
  394. RB_GENERATE_INSERT(name, type, field, cmp, attr) \
  395. RB_GENERATE_REMOVE(name, type, field, attr) \
  396. RB_GENERATE_FIND(name, type, field, cmp, attr) \
  397. RB_GENERATE_NFIND(name, type, field, cmp, attr) \
  398. RB_GENERATE_NEXT(name, type, field, attr) \
  399. RB_GENERATE_PREV(name, type, field, attr) \
  400. RB_GENERATE_MINMAX(name, type, field, attr)
  401. #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
  402. attr void \
  403. name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  404. { \
  405. struct type *parent, *gparent, *tmp; \
  406. while ((parent = RB_PARENT(elm, field)) != NULL && \
  407. RB_COLOR(parent, field) == RB_RED) { \
  408. gparent = RB_PARENT(parent, field); \
  409. if (parent == RB_LEFT(gparent, field)) { \
  410. tmp = RB_RIGHT(gparent, field); \
  411. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  412. RB_COLOR(tmp, field) = RB_BLACK; \
  413. RB_SET_BLACKRED(parent, gparent, field);\
  414. elm = gparent; \
  415. continue; \
  416. } \
  417. if (RB_RIGHT(parent, field) == elm) { \
  418. RB_ROTATE_LEFT(head, parent, tmp, field);\
  419. tmp = parent; \
  420. parent = elm; \
  421. elm = tmp; \
  422. } \
  423. RB_SET_BLACKRED(parent, gparent, field); \
  424. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  425. } else { \
  426. tmp = RB_LEFT(gparent, field); \
  427. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  428. RB_COLOR(tmp, field) = RB_BLACK; \
  429. RB_SET_BLACKRED(parent, gparent, field);\
  430. elm = gparent; \
  431. continue; \
  432. } \
  433. if (RB_LEFT(parent, field) == elm) { \
  434. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  435. tmp = parent; \
  436. parent = elm; \
  437. elm = tmp; \
  438. } \
  439. RB_SET_BLACKRED(parent, gparent, field); \
  440. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  441. } \
  442. } \
  443. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  444. }
  445. #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
  446. attr void \
  447. name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
  448. { \
  449. struct type *tmp; \
  450. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  451. elm != RB_ROOT(head)) { \
  452. if (RB_LEFT(parent, field) == elm) { \
  453. tmp = RB_RIGHT(parent, field); \
  454. if (RB_COLOR(tmp, field) == RB_RED) { \
  455. RB_SET_BLACKRED(tmp, parent, field); \
  456. RB_ROTATE_LEFT(head, parent, tmp, field);\
  457. tmp = RB_RIGHT(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_RIGHT(tmp, field) == NULL || \
  468. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
  469. struct type *oleft; \
  470. if ((oleft = RB_LEFT(tmp, field)) \
  471. != NULL) \
  472. RB_COLOR(oleft, field) = RB_BLACK;\
  473. RB_COLOR(tmp, field) = RB_RED; \
  474. RB_ROTATE_RIGHT(head, tmp, oleft, field);\
  475. tmp = RB_RIGHT(parent, field); \
  476. } \
  477. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  478. RB_COLOR(parent, field) = RB_BLACK; \
  479. if (RB_RIGHT(tmp, field)) \
  480. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
  481. RB_ROTATE_LEFT(head, parent, tmp, field);\
  482. elm = RB_ROOT(head); \
  483. break; \
  484. } \
  485. } else { \
  486. tmp = RB_LEFT(parent, field); \
  487. if (RB_COLOR(tmp, field) == RB_RED) { \
  488. RB_SET_BLACKRED(tmp, parent, field); \
  489. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  490. tmp = RB_LEFT(parent, field); \
  491. } \
  492. if ((RB_LEFT(tmp, field) == NULL || \
  493. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  494. (RB_RIGHT(tmp, field) == NULL || \
  495. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
  496. RB_COLOR(tmp, field) = RB_RED; \
  497. elm = parent; \
  498. parent = RB_PARENT(elm, field); \
  499. } else { \
  500. if (RB_LEFT(tmp, field) == NULL || \
  501. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
  502. struct type *oright; \
  503. if ((oright = RB_RIGHT(tmp, field)) \
  504. != NULL) \
  505. RB_COLOR(oright, field) = RB_BLACK;\
  506. RB_COLOR(tmp, field) = RB_RED; \
  507. RB_ROTATE_LEFT(head, tmp, oright, field);\
  508. tmp = RB_LEFT(parent, field); \
  509. } \
  510. RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
  511. RB_COLOR(parent, field) = RB_BLACK; \
  512. if (RB_LEFT(tmp, field)) \
  513. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
  514. RB_ROTATE_RIGHT(head, parent, tmp, field);\
  515. elm = RB_ROOT(head); \
  516. break; \
  517. } \
  518. } \
  519. } \
  520. if (elm) \
  521. RB_COLOR(elm, field) = RB_BLACK; \
  522. }
  523. #define RB_GENERATE_REMOVE(name, type, field, attr) \
  524. attr struct type * \
  525. name##_RB_REMOVE(struct name *head, struct type *elm) \
  526. { \
  527. struct type *child, *parent, *old = elm; \
  528. int color; \
  529. if (RB_LEFT(elm, field) == NULL) \
  530. child = RB_RIGHT(elm, field); \
  531. else if (RB_RIGHT(elm, field) == NULL) \
  532. child = RB_LEFT(elm, field); \
  533. else { \
  534. struct type *left; \
  535. elm = RB_RIGHT(elm, field); \
  536. while ((left = RB_LEFT(elm, field)) != NULL) \
  537. elm = left; \
  538. child = RB_RIGHT(elm, field); \
  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. if (RB_PARENT(elm, field) == old) \
  552. parent = elm; \
  553. (elm)->field = (old)->field; \
  554. if (RB_PARENT(old, field)) { \
  555. if (RB_LEFT(RB_PARENT(old, field), field) == old)\
  556. RB_LEFT(RB_PARENT(old, field), field) = elm;\
  557. else \
  558. RB_RIGHT(RB_PARENT(old, field), field) = elm;\
  559. RB_AUGMENT(RB_PARENT(old, field)); \
  560. } else \
  561. RB_ROOT(head) = elm; \
  562. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  563. if (RB_RIGHT(old, field)) \
  564. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  565. if (parent) { \
  566. left = parent; \
  567. do { \
  568. RB_AUGMENT(left); \
  569. } while ((left = RB_PARENT(left, field)) != NULL); \
  570. } \
  571. goto color; \
  572. } \
  573. parent = RB_PARENT(elm, field); \
  574. color = RB_COLOR(elm, field); \
  575. if (child) \
  576. RB_PARENT(child, field) = parent; \
  577. if (parent) { \
  578. if (RB_LEFT(parent, field) == elm) \
  579. RB_LEFT(parent, field) = child; \
  580. else \
  581. RB_RIGHT(parent, field) = child; \
  582. RB_AUGMENT(parent); \
  583. } else \
  584. RB_ROOT(head) = child; \
  585. color: \
  586. if (color == RB_BLACK) \
  587. name##_RB_REMOVE_COLOR(head, parent, child); \
  588. return (old); \
  589. } \
  590. #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
  591. /* Inserts a node into the RB tree */ \
  592. attr struct type * \
  593. name##_RB_INSERT(struct name *head, struct type *elm) \
  594. { \
  595. struct type *tmp; \
  596. struct type *parent = NULL; \
  597. int comp = 0; \
  598. tmp = RB_ROOT(head); \
  599. while (tmp) { \
  600. parent = tmp; \
  601. comp = (cmp)(elm, parent); \
  602. if (comp < 0) \
  603. tmp = RB_LEFT(tmp, field); \
  604. else if (comp > 0) \
  605. tmp = RB_RIGHT(tmp, field); \
  606. else \
  607. return (tmp); \
  608. } \
  609. RB_SET(elm, parent, field); \
  610. if (parent != NULL) { \
  611. if (comp < 0) \
  612. RB_LEFT(parent, field) = elm; \
  613. else \
  614. RB_RIGHT(parent, field) = elm; \
  615. RB_AUGMENT(parent); \
  616. } else \
  617. RB_ROOT(head) = elm; \
  618. name##_RB_INSERT_COLOR(head, elm); \
  619. return (NULL); \
  620. }
  621. #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
  622. /* Finds the node with the same key as elm */ \
  623. attr struct type * \
  624. name##_RB_FIND(struct name *head, struct type *elm) \
  625. { \
  626. struct type *tmp = RB_ROOT(head); \
  627. int comp; \
  628. while (tmp) { \
  629. comp = cmp(elm, tmp); \
  630. if (comp < 0) \
  631. tmp = RB_LEFT(tmp, field); \
  632. else if (comp > 0) \
  633. tmp = RB_RIGHT(tmp, field); \
  634. else \
  635. return (tmp); \
  636. } \
  637. return (NULL); \
  638. }
  639. #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
  640. /* Finds the first node greater than or equal to the search key */ \
  641. attr struct type * \
  642. name##_RB_NFIND(struct name *head, struct type *elm) \
  643. { \
  644. struct type *tmp = RB_ROOT(head); \
  645. struct type *res = NULL; \
  646. int comp; \
  647. while (tmp) { \
  648. comp = cmp(elm, tmp); \
  649. if (comp < 0) { \
  650. res = tmp; \
  651. tmp = RB_LEFT(tmp, field); \
  652. } \
  653. else if (comp > 0) \
  654. tmp = RB_RIGHT(tmp, field); \
  655. else \
  656. return (tmp); \
  657. } \
  658. return (res); \
  659. }
  660. #define RB_GENERATE_NEXT(name, type, field, attr) \
  661. /* ARGSUSED */ \
  662. attr struct type * \
  663. name##_RB_NEXT(struct type *elm) \
  664. { \
  665. if (RB_RIGHT(elm, field)) { \
  666. elm = RB_RIGHT(elm, field); \
  667. while (RB_LEFT(elm, field)) \
  668. elm = RB_LEFT(elm, field); \
  669. } else { \
  670. if (RB_PARENT(elm, field) && \
  671. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  672. elm = RB_PARENT(elm, field); \
  673. else { \
  674. while (RB_PARENT(elm, field) && \
  675. (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
  676. elm = RB_PARENT(elm, field); \
  677. elm = RB_PARENT(elm, field); \
  678. } \
  679. } \
  680. return (elm); \
  681. }
  682. #define RB_GENERATE_PREV(name, type, field, attr) \
  683. /* ARGSUSED */ \
  684. attr struct type * \
  685. name##_RB_PREV(struct type *elm) \
  686. { \
  687. if (RB_LEFT(elm, field)) { \
  688. elm = RB_LEFT(elm, field); \
  689. while (RB_RIGHT(elm, field)) \
  690. elm = RB_RIGHT(elm, field); \
  691. } else { \
  692. if (RB_PARENT(elm, field) && \
  693. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  694. elm = RB_PARENT(elm, field); \
  695. else { \
  696. while (RB_PARENT(elm, field) && \
  697. (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
  698. elm = RB_PARENT(elm, field); \
  699. elm = RB_PARENT(elm, field); \
  700. } \
  701. } \
  702. return (elm); \
  703. }
  704. #define RB_GENERATE_MINMAX(name, type, field, attr) \
  705. attr struct type * \
  706. name##_RB_MINMAX(struct name *head, int val) \
  707. { \
  708. struct type *tmp = RB_ROOT(head); \
  709. struct type *parent = NULL; \
  710. while (tmp) { \
  711. parent = tmp; \
  712. if (val < 0) \
  713. tmp = RB_LEFT(tmp, field); \
  714. else \
  715. tmp = RB_RIGHT(tmp, field); \
  716. } \
  717. return (parent); \
  718. }
  719. #define RB_NEGINF -1
  720. #define RB_INF 1
  721. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  722. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  723. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  724. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  725. #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
  726. #define RB_PREV(name, x, y) name##_RB_PREV(y)
  727. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  728. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  729. #define RB_FOREACH(x, name, head) \
  730. for ((x) = RB_MIN(name, head); \
  731. (x) != NULL; \
  732. (x) = name##_RB_NEXT(x))
  733. #define RB_FOREACH_FROM(x, name, y) \
  734. for ((x) = (y); \
  735. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  736. (x) = (y))
  737. #define RB_FOREACH_SAFE(x, name, head, y) \
  738. for ((x) = RB_MIN(name, head); \
  739. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  740. (x) = (y))
  741. #define RB_FOREACH_REVERSE(x, name, head) \
  742. for ((x) = RB_MAX(name, head); \
  743. (x) != NULL; \
  744. (x) = name##_RB_PREV(x))
  745. #define RB_FOREACH_REVERSE_FROM(x, name, y) \
  746. for ((x) = (y); \
  747. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  748. (x) = (y))
  749. #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
  750. for ((x) = RB_MAX(name, head); \
  751. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  752. (x) = (y))
  753. #endif /* _SYS_TREE_H_ */