tree.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*-
  2. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef UV_TREE_H_
  26. #define UV_TREE_H_
  27. #ifndef UV__UNUSED
  28. # if __GNUC__
  29. # define UV__UNUSED __attribute__((unused))
  30. # else
  31. # define UV__UNUSED
  32. # endif
  33. #endif
  34. /*
  35. * This file defines data structures for red-black trees.
  36. * A red-black tree is a binary search tree with the node color as an
  37. * extra attribute. It fulfills a set of conditions:
  38. * - every search path from the root to a leaf consists of the
  39. * same number of black nodes,
  40. * - each red node (except for the root) has a black parent,
  41. * - each leaf node is black.
  42. *
  43. * Every operation on a red-black tree is bounded as O(lg n).
  44. * The maximum height of a red-black tree is 2lg (n+1).
  45. */
  46. /* Macros that define a red-black tree */
  47. #define RB_HEAD(name, type) \
  48. struct name { \
  49. struct type *rbh_root; /* root of the tree */ \
  50. }
  51. #define RB_INITIALIZER(root) \
  52. { NULL }
  53. #define RB_INIT(root) do { \
  54. (root)->rbh_root = NULL; \
  55. } while (/*CONSTCOND*/ 0)
  56. #define RB_BLACK 0
  57. #define RB_RED 1
  58. #define RB_ENTRY(type) \
  59. struct { \
  60. struct type *rbe_left; /* left element */ \
  61. struct type *rbe_right; /* right element */ \
  62. struct type *rbe_parent; /* parent element */ \
  63. int rbe_color; /* node color */ \
  64. }
  65. #define RB_LEFT(elm, field) (elm)->field.rbe_left
  66. #define RB_RIGHT(elm, field) (elm)->field.rbe_right
  67. #define RB_PARENT(elm, field) (elm)->field.rbe_parent
  68. #define RB_COLOR(elm, field) (elm)->field.rbe_color
  69. #define RB_ROOT(head) (head)->rbh_root
  70. #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
  71. #define RB_SET(elm, parent, field) do { \
  72. RB_PARENT(elm, field) = parent; \
  73. RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
  74. RB_COLOR(elm, field) = RB_RED; \
  75. } while (/*CONSTCOND*/ 0)
  76. #define RB_SET_BLACKRED(black, red, field) do { \
  77. RB_COLOR(black, field) = RB_BLACK; \
  78. RB_COLOR(red, field) = RB_RED; \
  79. } while (/*CONSTCOND*/ 0)
  80. #ifndef RB_AUGMENT
  81. #define RB_AUGMENT(x) do {} while (0)
  82. #endif
  83. #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
  84. (tmp) = RB_RIGHT(elm, field); \
  85. if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
  86. RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
  87. } \
  88. RB_AUGMENT(elm); \
  89. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  90. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  91. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  92. else \
  93. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  94. } else \
  95. (head)->rbh_root = (tmp); \
  96. RB_LEFT(tmp, field) = (elm); \
  97. RB_PARENT(elm, field) = (tmp); \
  98. RB_AUGMENT(tmp); \
  99. if ((RB_PARENT(tmp, field))) \
  100. RB_AUGMENT(RB_PARENT(tmp, field)); \
  101. } while (/*CONSTCOND*/ 0)
  102. #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
  103. (tmp) = RB_LEFT(elm, field); \
  104. if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
  105. RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
  106. } \
  107. RB_AUGMENT(elm); \
  108. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
  109. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  110. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  111. else \
  112. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  113. } else \
  114. (head)->rbh_root = (tmp); \
  115. RB_RIGHT(tmp, field) = (elm); \
  116. RB_PARENT(elm, field) = (tmp); \
  117. RB_AUGMENT(tmp); \
  118. if ((RB_PARENT(tmp, field))) \
  119. RB_AUGMENT(RB_PARENT(tmp, field)); \
  120. } while (/*CONSTCOND*/ 0)
  121. /* Generates prototypes and inline functions */
  122. #define RB_PROTOTYPE(name, type, field, cmp) \
  123. RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
  124. #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
  125. RB_PROTOTYPE_INTERNAL(name, type, field, cmp, UV__UNUSED static)
  126. #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
  127. attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
  128. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
  129. attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
  130. attr struct type *name##_RB_INSERT(struct name *, struct type *); \
  131. attr struct type *name##_RB_FIND(struct name *, struct type *); \
  132. attr struct type *name##_RB_NFIND(struct name *, struct type *); \
  133. attr struct type *name##_RB_NEXT(struct type *); \
  134. attr struct type *name##_RB_PREV(struct type *); \
  135. attr struct type *name##_RB_MINMAX(struct name *, int); \
  136. \
  137. /* Main rb operation.
  138. * Moves node close to the key of elm to top
  139. */
  140. #define RB_GENERATE(name, type, field, cmp) \
  141. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  142. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  143. RB_GENERATE_INTERNAL(name, type, field, cmp, UV__UNUSED static)
  144. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  145. attr void \
  146. name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  147. { \
  148. struct type *parent, *gparent, *tmp; \
  149. while ((parent = RB_PARENT(elm, field)) != NULL && \
  150. RB_COLOR(parent, field) == RB_RED) { \
  151. gparent = RB_PARENT(parent, field); \
  152. if (parent == RB_LEFT(gparent, field)) { \
  153. tmp = RB_RIGHT(gparent, field); \
  154. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  155. RB_COLOR(tmp, field) = RB_BLACK; \
  156. RB_SET_BLACKRED(parent, gparent, field); \
  157. elm = gparent; \
  158. continue; \
  159. } \
  160. if (RB_RIGHT(parent, field) == elm) { \
  161. RB_ROTATE_LEFT(head, parent, tmp, field); \
  162. tmp = parent; \
  163. parent = elm; \
  164. elm = tmp; \
  165. } \
  166. RB_SET_BLACKRED(parent, gparent, field); \
  167. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  168. } else { \
  169. tmp = RB_LEFT(gparent, field); \
  170. if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
  171. RB_COLOR(tmp, field) = RB_BLACK; \
  172. RB_SET_BLACKRED(parent, gparent, field); \
  173. elm = gparent; \
  174. continue; \
  175. } \
  176. if (RB_LEFT(parent, field) == elm) { \
  177. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  178. tmp = parent; \
  179. parent = elm; \
  180. elm = tmp; \
  181. } \
  182. RB_SET_BLACKRED(parent, gparent, field); \
  183. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  184. } \
  185. } \
  186. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  187. } \
  188. \
  189. attr void \
  190. name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \
  191. struct type *elm) \
  192. { \
  193. struct type *tmp; \
  194. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  195. elm != RB_ROOT(head)) { \
  196. if (RB_LEFT(parent, field) == elm) { \
  197. tmp = RB_RIGHT(parent, field); \
  198. if (RB_COLOR(tmp, field) == RB_RED) { \
  199. RB_SET_BLACKRED(tmp, parent, field); \
  200. RB_ROTATE_LEFT(head, parent, tmp, field); \
  201. tmp = RB_RIGHT(parent, field); \
  202. } \
  203. if ((RB_LEFT(tmp, field) == NULL || \
  204. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \
  205. (RB_RIGHT(tmp, field) == NULL || \
  206. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \
  207. RB_COLOR(tmp, field) = RB_RED; \
  208. elm = parent; \
  209. parent = RB_PARENT(elm, field); \
  210. } else { \
  211. if (RB_RIGHT(tmp, field) == NULL || \
  212. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) { \
  213. struct type *oleft; \
  214. if ((oleft = RB_LEFT(tmp, field)) \
  215. != NULL) \
  216. RB_COLOR(oleft, field) = RB_BLACK; \
  217. RB_COLOR(tmp, field) = RB_RED; \
  218. RB_ROTATE_RIGHT(head, tmp, oleft, field); \
  219. tmp = RB_RIGHT(parent, field); \
  220. } \
  221. RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
  222. RB_COLOR(parent, field) = RB_BLACK; \
  223. if (RB_RIGHT(tmp, field)) \
  224. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
  225. RB_ROTATE_LEFT(head, parent, tmp, field); \
  226. elm = RB_ROOT(head); \
  227. break; \
  228. } \
  229. } else { \
  230. tmp = RB_LEFT(parent, field); \
  231. if (RB_COLOR(tmp, field) == RB_RED) { \
  232. RB_SET_BLACKRED(tmp, parent, field); \
  233. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  234. tmp = RB_LEFT(parent, field); \
  235. } \
  236. if ((RB_LEFT(tmp, field) == NULL || \
  237. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \
  238. (RB_RIGHT(tmp, field) == NULL || \
  239. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \
  240. RB_COLOR(tmp, field) = RB_RED; \
  241. elm = parent; \
  242. parent = RB_PARENT(elm, field); \
  243. } else { \
  244. if (RB_LEFT(tmp, field) == NULL || \
  245. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) { \
  246. struct type *oright; \
  247. if ((oright = RB_RIGHT(tmp, field)) \
  248. != NULL) \
  249. RB_COLOR(oright, field) = RB_BLACK; \
  250. RB_COLOR(tmp, field) = RB_RED; \
  251. RB_ROTATE_LEFT(head, tmp, oright, field); \
  252. tmp = RB_LEFT(parent, field); \
  253. } \
  254. RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
  255. RB_COLOR(parent, field) = RB_BLACK; \
  256. if (RB_LEFT(tmp, field)) \
  257. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
  258. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  259. elm = RB_ROOT(head); \
  260. break; \
  261. } \
  262. } \
  263. } \
  264. if (elm) \
  265. RB_COLOR(elm, field) = RB_BLACK; \
  266. } \
  267. \
  268. attr struct type * \
  269. name##_RB_REMOVE(struct name *head, struct type *elm) \
  270. { \
  271. struct type *child, *parent, *old = elm; \
  272. int color; \
  273. if (RB_LEFT(elm, field) == NULL) \
  274. child = RB_RIGHT(elm, field); \
  275. else if (RB_RIGHT(elm, field) == NULL) \
  276. child = RB_LEFT(elm, field); \
  277. else { \
  278. struct type *left; \
  279. elm = RB_RIGHT(elm, field); \
  280. while ((left = RB_LEFT(elm, field)) != NULL) \
  281. elm = left; \
  282. child = RB_RIGHT(elm, field); \
  283. parent = RB_PARENT(elm, field); \
  284. color = RB_COLOR(elm, field); \
  285. if (child) \
  286. RB_PARENT(child, field) = parent; \
  287. if (parent) { \
  288. if (RB_LEFT(parent, field) == elm) \
  289. RB_LEFT(parent, field) = child; \
  290. else \
  291. RB_RIGHT(parent, field) = child; \
  292. RB_AUGMENT(parent); \
  293. } else \
  294. RB_ROOT(head) = child; \
  295. if (RB_PARENT(elm, field) == old) \
  296. parent = elm; \
  297. (elm)->field = (old)->field; \
  298. if (RB_PARENT(old, field)) { \
  299. if (RB_LEFT(RB_PARENT(old, field), field) == old) \
  300. RB_LEFT(RB_PARENT(old, field), field) = elm; \
  301. else \
  302. RB_RIGHT(RB_PARENT(old, field), field) = elm; \
  303. RB_AUGMENT(RB_PARENT(old, field)); \
  304. } else \
  305. RB_ROOT(head) = elm; \
  306. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  307. if (RB_RIGHT(old, field)) \
  308. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  309. if (parent) { \
  310. left = parent; \
  311. do { \
  312. RB_AUGMENT(left); \
  313. } while ((left = RB_PARENT(left, field)) != NULL); \
  314. } \
  315. goto color; \
  316. } \
  317. parent = RB_PARENT(elm, field); \
  318. color = RB_COLOR(elm, field); \
  319. if (child) \
  320. RB_PARENT(child, field) = parent; \
  321. if (parent) { \
  322. if (RB_LEFT(parent, field) == elm) \
  323. RB_LEFT(parent, field) = child; \
  324. else \
  325. RB_RIGHT(parent, field) = child; \
  326. RB_AUGMENT(parent); \
  327. } else \
  328. RB_ROOT(head) = child; \
  329. color: \
  330. if (color == RB_BLACK) \
  331. name##_RB_REMOVE_COLOR(head, parent, child); \
  332. return (old); \
  333. } \
  334. \
  335. /* Inserts a node into the RB tree */ \
  336. attr struct type * \
  337. name##_RB_INSERT(struct name *head, struct type *elm) \
  338. { \
  339. struct type *tmp; \
  340. struct type *parent = NULL; \
  341. int comp = 0; \
  342. tmp = RB_ROOT(head); \
  343. while (tmp) { \
  344. parent = tmp; \
  345. comp = (cmp)(elm, parent); \
  346. if (comp < 0) \
  347. tmp = RB_LEFT(tmp, field); \
  348. else if (comp > 0) \
  349. tmp = RB_RIGHT(tmp, field); \
  350. else \
  351. return (tmp); \
  352. } \
  353. RB_SET(elm, parent, field); \
  354. if (parent != NULL) { \
  355. if (comp < 0) \
  356. RB_LEFT(parent, field) = elm; \
  357. else \
  358. RB_RIGHT(parent, field) = elm; \
  359. RB_AUGMENT(parent); \
  360. } else \
  361. RB_ROOT(head) = elm; \
  362. name##_RB_INSERT_COLOR(head, elm); \
  363. return (NULL); \
  364. } \
  365. \
  366. /* Finds the node with the same key as elm */ \
  367. attr struct type * \
  368. name##_RB_FIND(struct name *head, struct type *elm) \
  369. { \
  370. struct type *tmp = RB_ROOT(head); \
  371. int comp; \
  372. while (tmp) { \
  373. comp = cmp(elm, tmp); \
  374. if (comp < 0) \
  375. tmp = RB_LEFT(tmp, field); \
  376. else if (comp > 0) \
  377. tmp = RB_RIGHT(tmp, field); \
  378. else \
  379. return (tmp); \
  380. } \
  381. return (NULL); \
  382. } \
  383. \
  384. /* Finds the first node greater than or equal to the search key */ \
  385. attr struct type * \
  386. name##_RB_NFIND(struct name *head, struct type *elm) \
  387. { \
  388. struct type *tmp = RB_ROOT(head); \
  389. struct type *res = NULL; \
  390. int comp; \
  391. while (tmp) { \
  392. comp = cmp(elm, tmp); \
  393. if (comp < 0) { \
  394. res = tmp; \
  395. tmp = RB_LEFT(tmp, field); \
  396. } \
  397. else if (comp > 0) \
  398. tmp = RB_RIGHT(tmp, field); \
  399. else \
  400. return (tmp); \
  401. } \
  402. return (res); \
  403. } \
  404. \
  405. /* ARGSUSED */ \
  406. attr struct type * \
  407. name##_RB_NEXT(struct type *elm) \
  408. { \
  409. if (RB_RIGHT(elm, field)) { \
  410. elm = RB_RIGHT(elm, field); \
  411. while (RB_LEFT(elm, field)) \
  412. elm = RB_LEFT(elm, field); \
  413. } else { \
  414. if (RB_PARENT(elm, field) && \
  415. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  416. elm = RB_PARENT(elm, field); \
  417. else { \
  418. while (RB_PARENT(elm, field) && \
  419. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  420. elm = RB_PARENT(elm, field); \
  421. elm = RB_PARENT(elm, field); \
  422. } \
  423. } \
  424. return (elm); \
  425. } \
  426. \
  427. /* ARGSUSED */ \
  428. attr struct type * \
  429. name##_RB_PREV(struct type *elm) \
  430. { \
  431. if (RB_LEFT(elm, field)) { \
  432. elm = RB_LEFT(elm, field); \
  433. while (RB_RIGHT(elm, field)) \
  434. elm = RB_RIGHT(elm, field); \
  435. } else { \
  436. if (RB_PARENT(elm, field) && \
  437. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  438. elm = RB_PARENT(elm, field); \
  439. else { \
  440. while (RB_PARENT(elm, field) && \
  441. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  442. elm = RB_PARENT(elm, field); \
  443. elm = RB_PARENT(elm, field); \
  444. } \
  445. } \
  446. return (elm); \
  447. } \
  448. \
  449. attr struct type * \
  450. name##_RB_MINMAX(struct name *head, int val) \
  451. { \
  452. struct type *tmp = RB_ROOT(head); \
  453. struct type *parent = NULL; \
  454. while (tmp) { \
  455. parent = tmp; \
  456. if (val < 0) \
  457. tmp = RB_LEFT(tmp, field); \
  458. else \
  459. tmp = RB_RIGHT(tmp, field); \
  460. } \
  461. return (parent); \
  462. }
  463. #define RB_NEGINF -1
  464. #define RB_INF 1
  465. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  466. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  467. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  468. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  469. #define RB_NEXT(name, x) name##_RB_NEXT(x)
  470. #define RB_PREV(name, x) name##_RB_PREV(x)
  471. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  472. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  473. #define RB_FOREACH(x, name, head) \
  474. for ((x) = RB_MIN(name, head); \
  475. (x) != NULL; \
  476. (x) = name##_RB_NEXT(x))
  477. #define RB_FOREACH_FROM(x, name, y) \
  478. for ((x) = (y); \
  479. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  480. (x) = (y))
  481. #define RB_FOREACH_SAFE(x, name, head, y) \
  482. for ((x) = RB_MIN(name, head); \
  483. ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
  484. (x) = (y))
  485. #define RB_FOREACH_REVERSE(x, name, head) \
  486. for ((x) = RB_MAX(name, head); \
  487. (x) != NULL; \
  488. (x) = name##_RB_PREV(x))
  489. #define RB_FOREACH_REVERSE_FROM(x, name, y) \
  490. for ((x) = (y); \
  491. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  492. (x) = (y))
  493. #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
  494. for ((x) = RB_MAX(name, head); \
  495. ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
  496. (x) = (y))
  497. #endif /* UV_TREE_H_ */