archive_rb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*-
  2. * Copyright (c) 2001 The NetBSD Foundation, Inc.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to The NetBSD Foundation
  6. * by Matt Thomas <matt@3am-software.com>.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * Based on: NetBSD: rb.c,v 1.6 2010/04/30 13:58:09 joerg Exp
  30. */
  31. #include "archive_platform.h"
  32. #include <stddef.h>
  33. #include "archive_rb.h"
  34. /* Keep in sync with archive_rb.h */
  35. #define RB_DIR_LEFT 0
  36. #define RB_DIR_RIGHT 1
  37. #define RB_DIR_OTHER 1
  38. #define rb_left rb_nodes[RB_DIR_LEFT]
  39. #define rb_right rb_nodes[RB_DIR_RIGHT]
  40. #define RB_FLAG_POSITION 0x2
  41. #define RB_FLAG_RED 0x1
  42. #define RB_FLAG_MASK (RB_FLAG_POSITION|RB_FLAG_RED)
  43. #define RB_FATHER(rb) \
  44. ((struct archive_rb_node *)((rb)->rb_info & ~RB_FLAG_MASK))
  45. #define RB_SET_FATHER(rb, father) \
  46. ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK)))
  47. #define RB_SENTINEL_P(rb) ((rb) == NULL)
  48. #define RB_LEFT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_left)
  49. #define RB_RIGHT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_right)
  50. #define RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb)))
  51. #define RB_CHILDLESS_P(rb) \
  52. (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb)))
  53. #define RB_TWOCHILDREN_P(rb) \
  54. (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb))
  55. #define RB_POSITION(rb) \
  56. (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT)
  57. #define RB_RIGHT_P(rb) (RB_POSITION(rb) == RB_DIR_RIGHT)
  58. #define RB_LEFT_P(rb) (RB_POSITION(rb) == RB_DIR_LEFT)
  59. #define RB_RED_P(rb) (!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0)
  60. #define RB_BLACK_P(rb) (RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0)
  61. #define RB_MARK_RED(rb) ((void)((rb)->rb_info |= RB_FLAG_RED))
  62. #define RB_MARK_BLACK(rb) ((void)((rb)->rb_info &= ~RB_FLAG_RED))
  63. #define RB_INVERT_COLOR(rb) ((void)((rb)->rb_info ^= RB_FLAG_RED))
  64. #define RB_ROOT_P(rbt, rb) ((rbt)->rbt_root == (rb))
  65. #define RB_SET_POSITION(rb, position) \
  66. ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \
  67. ((rb)->rb_info &= ~RB_FLAG_POSITION)))
  68. #define RB_ZERO_PROPERTIES(rb) ((void)((rb)->rb_info &= ~RB_FLAG_MASK))
  69. #define RB_COPY_PROPERTIES(dst, src) \
  70. ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK))
  71. #define RB_SWAP_PROPERTIES(a, b) do { \
  72. uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \
  73. (a)->rb_info ^= xorinfo; \
  74. (b)->rb_info ^= xorinfo; \
  75. } while (/*CONSTCOND*/ 0)
  76. static void __archive_rb_tree_insert_rebalance(struct archive_rb_tree *,
  77. struct archive_rb_node *);
  78. static void __archive_rb_tree_removal_rebalance(struct archive_rb_tree *,
  79. struct archive_rb_node *, unsigned int);
  80. #define RB_SENTINEL_NODE NULL
  81. #define T 1
  82. #define F 0
  83. void
  84. __archive_rb_tree_init(struct archive_rb_tree *rbt,
  85. const struct archive_rb_tree_ops *ops)
  86. {
  87. rbt->rbt_ops = ops;
  88. *((struct archive_rb_node **)&rbt->rbt_root) = RB_SENTINEL_NODE;
  89. }
  90. struct archive_rb_node *
  91. __archive_rb_tree_find_node(struct archive_rb_tree *rbt, const void *key)
  92. {
  93. archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
  94. struct archive_rb_node *parent = rbt->rbt_root;
  95. while (!RB_SENTINEL_P(parent)) {
  96. const signed int diff = (*compare_key)(parent, key);
  97. if (diff == 0)
  98. return parent;
  99. parent = parent->rb_nodes[diff > 0];
  100. }
  101. return NULL;
  102. }
  103. struct archive_rb_node *
  104. __archive_rb_tree_find_node_geq(struct archive_rb_tree *rbt, const void *key)
  105. {
  106. archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
  107. struct archive_rb_node *parent = rbt->rbt_root;
  108. struct archive_rb_node *last = NULL;
  109. while (!RB_SENTINEL_P(parent)) {
  110. const signed int diff = (*compare_key)(parent, key);
  111. if (diff == 0)
  112. return parent;
  113. if (diff < 0)
  114. last = parent;
  115. parent = parent->rb_nodes[diff > 0];
  116. }
  117. return last;
  118. }
  119. struct archive_rb_node *
  120. __archive_rb_tree_find_node_leq(struct archive_rb_tree *rbt, const void *key)
  121. {
  122. archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
  123. struct archive_rb_node *parent = rbt->rbt_root;
  124. struct archive_rb_node *last = NULL;
  125. while (!RB_SENTINEL_P(parent)) {
  126. const signed int diff = (*compare_key)(parent, key);
  127. if (diff == 0)
  128. return parent;
  129. if (diff > 0)
  130. last = parent;
  131. parent = parent->rb_nodes[diff > 0];
  132. }
  133. return last;
  134. }
  135. int
  136. __archive_rb_tree_insert_node(struct archive_rb_tree *rbt,
  137. struct archive_rb_node *self)
  138. {
  139. archive_rbto_compare_nodes_fn compare_nodes = rbt->rbt_ops->rbto_compare_nodes;
  140. struct archive_rb_node *parent, *tmp;
  141. unsigned int position;
  142. int rebalance;
  143. tmp = rbt->rbt_root;
  144. /*
  145. * This is a hack. Because rbt->rbt_root is just a
  146. * struct archive_rb_node *, just like rb_node->rb_nodes[RB_DIR_LEFT],
  147. * we can use this fact to avoid a lot of tests for root and know
  148. * that even at root, updating
  149. * RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
  150. * update rbt->rbt_root.
  151. */
  152. parent = (struct archive_rb_node *)(void *)&rbt->rbt_root;
  153. position = RB_DIR_LEFT;
  154. /*
  155. * Find out where to place this new leaf.
  156. */
  157. while (!RB_SENTINEL_P(tmp)) {
  158. const signed int diff = (*compare_nodes)(tmp, self);
  159. if (diff == 0) {
  160. /*
  161. * Node already exists; don't insert.
  162. */
  163. return F;
  164. }
  165. parent = tmp;
  166. position = (diff > 0);
  167. tmp = parent->rb_nodes[position];
  168. }
  169. /*
  170. * Initialize the node and insert as a leaf into the tree.
  171. */
  172. RB_SET_FATHER(self, parent);
  173. RB_SET_POSITION(self, position);
  174. if (parent == (struct archive_rb_node *)(void *)&rbt->rbt_root) {
  175. RB_MARK_BLACK(self); /* root is always black */
  176. rebalance = F;
  177. } else {
  178. /*
  179. * All new nodes are colored red. We only need to rebalance
  180. * if our parent is also red.
  181. */
  182. RB_MARK_RED(self);
  183. rebalance = RB_RED_P(parent);
  184. }
  185. self->rb_left = parent->rb_nodes[position];
  186. self->rb_right = parent->rb_nodes[position];
  187. parent->rb_nodes[position] = self;
  188. /*
  189. * Rebalance tree after insertion
  190. */
  191. if (rebalance)
  192. __archive_rb_tree_insert_rebalance(rbt, self);
  193. return T;
  194. }
  195. /*
  196. * Swap the location and colors of 'self' and its child @ which. The child
  197. * can not be a sentinel node. This is our rotation function. However,
  198. * since it preserves coloring, it great simplifies both insertion and
  199. * removal since rotation almost always involves the exchanging of colors
  200. * as a separate step.
  201. */
  202. /*ARGSUSED*/
  203. static void
  204. __archive_rb_tree_reparent_nodes(
  205. struct archive_rb_node *old_father, const unsigned int which)
  206. {
  207. const unsigned int other = which ^ RB_DIR_OTHER;
  208. struct archive_rb_node * const grandpa = RB_FATHER(old_father);
  209. struct archive_rb_node * const old_child = old_father->rb_nodes[which];
  210. struct archive_rb_node * const new_father = old_child;
  211. struct archive_rb_node * const new_child = old_father;
  212. if (new_father == NULL)
  213. return;
  214. /*
  215. * Exchange descendant linkages.
  216. */
  217. grandpa->rb_nodes[RB_POSITION(old_father)] = new_father;
  218. new_child->rb_nodes[which] = old_child->rb_nodes[other];
  219. new_father->rb_nodes[other] = new_child;
  220. /*
  221. * Update ancestor linkages
  222. */
  223. RB_SET_FATHER(new_father, grandpa);
  224. RB_SET_FATHER(new_child, new_father);
  225. /*
  226. * Exchange properties between new_father and new_child. The only
  227. * change is that new_child's position is now on the other side.
  228. */
  229. RB_SWAP_PROPERTIES(new_father, new_child);
  230. RB_SET_POSITION(new_child, other);
  231. /*
  232. * Make sure to reparent the new child to ourself.
  233. */
  234. if (!RB_SENTINEL_P(new_child->rb_nodes[which])) {
  235. RB_SET_FATHER(new_child->rb_nodes[which], new_child);
  236. RB_SET_POSITION(new_child->rb_nodes[which], which);
  237. }
  238. }
  239. static void
  240. __archive_rb_tree_insert_rebalance(struct archive_rb_tree *rbt,
  241. struct archive_rb_node *self)
  242. {
  243. struct archive_rb_node * father = RB_FATHER(self);
  244. struct archive_rb_node * grandpa;
  245. struct archive_rb_node * uncle;
  246. unsigned int which;
  247. unsigned int other;
  248. for (;;) {
  249. /*
  250. * We are red and our parent is red, therefore we must have a
  251. * grandfather and he must be black.
  252. */
  253. grandpa = RB_FATHER(father);
  254. which = (father == grandpa->rb_right);
  255. other = which ^ RB_DIR_OTHER;
  256. uncle = grandpa->rb_nodes[other];
  257. if (RB_BLACK_P(uncle))
  258. break;
  259. /*
  260. * Case 1: our uncle is red
  261. * Simply invert the colors of our parent and
  262. * uncle and make our grandparent red. And
  263. * then solve the problem up at his level.
  264. */
  265. RB_MARK_BLACK(uncle);
  266. RB_MARK_BLACK(father);
  267. if (RB_ROOT_P(rbt, grandpa)) {
  268. /*
  269. * If our grandpa is root, don't bother
  270. * setting him to red, just return.
  271. */
  272. return;
  273. }
  274. RB_MARK_RED(grandpa);
  275. self = grandpa;
  276. father = RB_FATHER(self);
  277. if (RB_BLACK_P(father)) {
  278. /*
  279. * If our great-grandpa is black, we're done.
  280. */
  281. return;
  282. }
  283. }
  284. /*
  285. * Case 2&3: our uncle is black.
  286. */
  287. if (self == father->rb_nodes[other]) {
  288. /*
  289. * Case 2: we are on the same side as our uncle
  290. * Swap ourselves with our parent so this case
  291. * becomes case 3. Basically our parent becomes our
  292. * child.
  293. */
  294. __archive_rb_tree_reparent_nodes(father, other);
  295. }
  296. /*
  297. * Case 3: we are opposite a child of a black uncle.
  298. * Swap our parent and grandparent. Since our grandfather
  299. * is black, our father will become black and our new sibling
  300. * (former grandparent) will become red.
  301. */
  302. __archive_rb_tree_reparent_nodes(grandpa, which);
  303. /*
  304. * Final step: Set the root to black.
  305. */
  306. RB_MARK_BLACK(rbt->rbt_root);
  307. }
  308. static void
  309. __archive_rb_tree_prune_node(struct archive_rb_tree *rbt,
  310. struct archive_rb_node *self, int rebalance)
  311. {
  312. const unsigned int which = RB_POSITION(self);
  313. struct archive_rb_node *father = RB_FATHER(self);
  314. /*
  315. * Since we are childless, we know that self->rb_left is pointing
  316. * to the sentinel node.
  317. */
  318. father->rb_nodes[which] = self->rb_left;
  319. /*
  320. * Rebalance if requested.
  321. */
  322. if (rebalance)
  323. __archive_rb_tree_removal_rebalance(rbt, father, which);
  324. }
  325. /*
  326. * When deleting an interior node
  327. */
  328. static void
  329. __archive_rb_tree_swap_prune_and_rebalance(struct archive_rb_tree *rbt,
  330. struct archive_rb_node *self, struct archive_rb_node *standin)
  331. {
  332. const unsigned int standin_which = RB_POSITION(standin);
  333. unsigned int standin_other = standin_which ^ RB_DIR_OTHER;
  334. struct archive_rb_node *standin_son;
  335. struct archive_rb_node *standin_father = RB_FATHER(standin);
  336. int rebalance = RB_BLACK_P(standin);
  337. if (standin_father == self) {
  338. /*
  339. * As a child of self, any children would be opposite of
  340. * our parent.
  341. */
  342. standin_son = standin->rb_nodes[standin_which];
  343. } else {
  344. /*
  345. * Since we aren't a child of self, any children would be
  346. * on the same side as our parent.
  347. */
  348. standin_son = standin->rb_nodes[standin_other];
  349. }
  350. if (RB_RED_P(standin_son)) {
  351. /*
  352. * We know we have a red child so if we flip it to black
  353. * we don't have to rebalance.
  354. */
  355. RB_MARK_BLACK(standin_son);
  356. rebalance = F;
  357. if (standin_father != self) {
  358. /*
  359. * Change the son's parentage to point to his grandpa.
  360. */
  361. RB_SET_FATHER(standin_son, standin_father);
  362. RB_SET_POSITION(standin_son, standin_which);
  363. }
  364. }
  365. if (standin_father == self) {
  366. /*
  367. * If we are about to delete the standin's father, then when
  368. * we call rebalance, we need to use ourselves as our father.
  369. * Otherwise remember our original father. Also, since we are
  370. * our standin's father we only need to reparent the standin's
  371. * brother.
  372. *
  373. * | R --> S |
  374. * | Q S --> Q T |
  375. * | t --> |
  376. *
  377. * Have our son/standin adopt his brother as his new son.
  378. */
  379. standin_father = standin;
  380. } else {
  381. /*
  382. * | R --> S . |
  383. * | / \ | T --> / \ | / |
  384. * | ..... | S --> ..... | T |
  385. *
  386. * Sever standin's connection to his father.
  387. */
  388. standin_father->rb_nodes[standin_which] = standin_son;
  389. /*
  390. * Adopt the far son.
  391. */
  392. standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
  393. RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
  394. /*
  395. * Use standin_other because we need to preserve standin_which
  396. * for the removal_rebalance.
  397. */
  398. standin_other = standin_which;
  399. }
  400. /*
  401. * Move the only remaining son to our standin. If our standin is our
  402. * son, this will be the only son needed to be moved.
  403. */
  404. standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
  405. RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
  406. /*
  407. * Now copy the result of self to standin and then replace
  408. * self with standin in the tree.
  409. */
  410. RB_COPY_PROPERTIES(standin, self);
  411. RB_SET_FATHER(standin, RB_FATHER(self));
  412. RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin;
  413. if (rebalance)
  414. __archive_rb_tree_removal_rebalance(rbt, standin_father, standin_which);
  415. }
  416. /*
  417. * We could do this by doing
  418. * __archive_rb_tree_node_swap(rbt, self, which);
  419. * __archive_rb_tree_prune_node(rbt, self, F);
  420. *
  421. * But it's more efficient to just evaluate and recolor the child.
  422. */
  423. static void
  424. __archive_rb_tree_prune_blackred_branch(
  425. struct archive_rb_node *self, unsigned int which)
  426. {
  427. struct archive_rb_node *father = RB_FATHER(self);
  428. struct archive_rb_node *son = self->rb_nodes[which];
  429. /*
  430. * Remove ourselves from the tree and give our former child our
  431. * properties (position, color, root).
  432. */
  433. RB_COPY_PROPERTIES(son, self);
  434. father->rb_nodes[RB_POSITION(son)] = son;
  435. RB_SET_FATHER(son, father);
  436. }
  437. /*
  438. *
  439. */
  440. void
  441. __archive_rb_tree_remove_node(struct archive_rb_tree *rbt,
  442. struct archive_rb_node *self)
  443. {
  444. struct archive_rb_node *standin;
  445. unsigned int which;
  446. /*
  447. * In the following diagrams, we (the node to be removed) are S. Red
  448. * nodes are lowercase. T could be either red or black.
  449. *
  450. * Remember the major axiom of the red-black tree: the number of
  451. * black nodes from the root to each leaf is constant across all
  452. * leaves, only the number of red nodes varies.
  453. *
  454. * Thus removing a red leaf doesn't require any other changes to a
  455. * red-black tree. So if we must remove a node, attempt to rearrange
  456. * the tree so we can remove a red node.
  457. *
  458. * The simplest case is a childless red node or a childless root node:
  459. *
  460. * | T --> T | or | R --> * |
  461. * | s --> * |
  462. */
  463. if (RB_CHILDLESS_P(self)) {
  464. const int rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self);
  465. __archive_rb_tree_prune_node(rbt, self, rebalance);
  466. return;
  467. }
  468. if (!RB_TWOCHILDREN_P(self)) {
  469. /*
  470. * The next simplest case is the node we are deleting is
  471. * black and has one red child.
  472. *
  473. * | T --> T --> T |
  474. * | S --> R --> R |
  475. * | r --> s --> * |
  476. */
  477. which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT;
  478. __archive_rb_tree_prune_blackred_branch(self, which);
  479. return;
  480. }
  481. /*
  482. * We invert these because we prefer to remove from the inside of
  483. * the tree.
  484. */
  485. which = RB_POSITION(self) ^ RB_DIR_OTHER;
  486. /*
  487. * Let's find the node closes to us opposite of our parent
  488. * Now swap it with ourself, "prune" it, and rebalance, if needed.
  489. */
  490. standin = __archive_rb_tree_iterate(rbt, self, which);
  491. __archive_rb_tree_swap_prune_and_rebalance(rbt, self, standin);
  492. }
  493. static void
  494. __archive_rb_tree_removal_rebalance(struct archive_rb_tree *rbt,
  495. struct archive_rb_node *parent, unsigned int which)
  496. {
  497. while (RB_BLACK_P(parent->rb_nodes[which])) {
  498. unsigned int other = which ^ RB_DIR_OTHER;
  499. struct archive_rb_node *brother = parent->rb_nodes[other];
  500. if (brother == NULL)
  501. return;/* The tree may be broken. */
  502. /*
  503. * For cases 1, 2a, and 2b, our brother's children must
  504. * be black and our father must be black
  505. */
  506. if (RB_BLACK_P(parent)
  507. && RB_BLACK_P(brother->rb_left)
  508. && RB_BLACK_P(brother->rb_right)) {
  509. if (RB_RED_P(brother)) {
  510. /*
  511. * Case 1: Our brother is red, swap its
  512. * position (and colors) with our parent.
  513. * This should now be case 2b (unless C or E
  514. * has a red child which is case 3; thus no
  515. * explicit branch to case 2b).
  516. *
  517. * B -> D
  518. * A d -> b E
  519. * C E -> A C
  520. */
  521. __archive_rb_tree_reparent_nodes(parent, other);
  522. brother = parent->rb_nodes[other];
  523. if (brother == NULL)
  524. return;/* The tree may be broken. */
  525. } else {
  526. /*
  527. * Both our parent and brother are black.
  528. * Change our brother to red, advance up rank
  529. * and go through the loop again.
  530. *
  531. * B -> *B
  532. * *A D -> A d
  533. * C E -> C E
  534. */
  535. RB_MARK_RED(brother);
  536. if (RB_ROOT_P(rbt, parent))
  537. return; /* root == parent == black */
  538. which = RB_POSITION(parent);
  539. parent = RB_FATHER(parent);
  540. continue;
  541. }
  542. }
  543. /*
  544. * Avoid an else here so that case 2a above can hit either
  545. * case 2b, 3, or 4.
  546. */
  547. if (RB_RED_P(parent)
  548. && RB_BLACK_P(brother)
  549. && RB_BLACK_P(brother->rb_left)
  550. && RB_BLACK_P(brother->rb_right)) {
  551. /*
  552. * We are black, our father is red, our brother and
  553. * both nephews are black. Simply invert/exchange the
  554. * colors of our father and brother (to black and red
  555. * respectively).
  556. *
  557. * | f --> F |
  558. * | * B --> * b |
  559. * | N N --> N N |
  560. */
  561. RB_MARK_BLACK(parent);
  562. RB_MARK_RED(brother);
  563. break; /* We're done! */
  564. } else {
  565. /*
  566. * Our brother must be black and have at least one
  567. * red child (it may have two).
  568. */
  569. if (RB_BLACK_P(brother->rb_nodes[other])) {
  570. /*
  571. * Case 3: our brother is black, our near
  572. * nephew is red, and our far nephew is black.
  573. * Swap our brother with our near nephew.
  574. * This result in a tree that matches case 4.
  575. * (Our father could be red or black).
  576. *
  577. * | F --> F |
  578. * | x B --> x B |
  579. * | n --> n |
  580. */
  581. __archive_rb_tree_reparent_nodes(brother, which);
  582. brother = parent->rb_nodes[other];
  583. }
  584. /*
  585. * Case 4: our brother is black and our far nephew
  586. * is red. Swap our father and brother locations and
  587. * change our far nephew to black. (these can be
  588. * done in either order so we change the color first).
  589. * The result is a valid red-black tree and is a
  590. * terminal case. (again we don't care about the
  591. * father's color)
  592. *
  593. * If the father is red, we will get a red-black-black
  594. * tree:
  595. * | f -> f --> b |
  596. * | B -> B --> F N |
  597. * | n -> N --> |
  598. *
  599. * If the father is black, we will get an all black
  600. * tree:
  601. * | F -> F --> B |
  602. * | B -> B --> F N |
  603. * | n -> N --> |
  604. *
  605. * If we had two red nephews, then after the swap,
  606. * our former father would have a red grandson.
  607. */
  608. if (brother->rb_nodes[other] == NULL)
  609. return;/* The tree may be broken. */
  610. RB_MARK_BLACK(brother->rb_nodes[other]);
  611. __archive_rb_tree_reparent_nodes(parent, other);
  612. break; /* We're done! */
  613. }
  614. }
  615. }
  616. struct archive_rb_node *
  617. __archive_rb_tree_iterate(struct archive_rb_tree *rbt,
  618. struct archive_rb_node *self, const unsigned int direction)
  619. {
  620. const unsigned int other = direction ^ RB_DIR_OTHER;
  621. if (self == NULL) {
  622. self = rbt->rbt_root;
  623. if (RB_SENTINEL_P(self))
  624. return NULL;
  625. while (!RB_SENTINEL_P(self->rb_nodes[direction]))
  626. self = self->rb_nodes[direction];
  627. return self;
  628. }
  629. /*
  630. * We can't go any further in this direction. We proceed up in the
  631. * opposite direction until our parent is in direction we want to go.
  632. */
  633. if (RB_SENTINEL_P(self->rb_nodes[direction])) {
  634. while (!RB_ROOT_P(rbt, self)) {
  635. if (other == (unsigned int)RB_POSITION(self))
  636. return RB_FATHER(self);
  637. self = RB_FATHER(self);
  638. }
  639. return NULL;
  640. }
  641. /*
  642. * Advance down one in current direction and go down as far as possible
  643. * in the opposite direction.
  644. */
  645. self = self->rb_nodes[direction];
  646. while (!RB_SENTINEL_P(self->rb_nodes[other]))
  647. self = self->rb_nodes[other];
  648. return self;
  649. }