OpenSDE Packages Database (without history before r20070)
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.

777 lines
26 KiB

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