rbtree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. (C) 2012 Michel Lespinasse <walken@google.com>
  6. * SPDX-License-Identifier: GPL-2.0+
  7. linux/lib/rbtree.c
  8. */
  9. #define __UBOOT__
  10. #include <linux/rbtree_augmented.h>
  11. #ifndef __UBOOT__
  12. #include <linux/export.h>
  13. #else
  14. #include <ubi_uboot.h>
  15. #endif
  16. /*
  17. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  18. *
  19. * 1) A node is either red or black
  20. * 2) The root is black
  21. * 3) All leaves (NULL) are black
  22. * 4) Both children of every red node are black
  23. * 5) Every simple path from root to leaves contains the same number
  24. * of black nodes.
  25. *
  26. * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
  27. * consecutive red nodes in a path and every red node is therefore followed by
  28. * a black. So if B is the number of black nodes on every simple path (as per
  29. * 5), then the longest possible path due to 4 is 2B.
  30. *
  31. * We shall indicate color with case, where black nodes are uppercase and red
  32. * nodes will be lowercase. Unknown color nodes shall be drawn as red within
  33. * parentheses and have some accompanying text comment.
  34. */
  35. static inline void rb_set_black(struct rb_node *rb)
  36. {
  37. rb->__rb_parent_color |= RB_BLACK;
  38. }
  39. static inline struct rb_node *rb_red_parent(struct rb_node *red)
  40. {
  41. return (struct rb_node *)red->__rb_parent_color;
  42. }
  43. /*
  44. * Helper function for rotations:
  45. * - old's parent and color get assigned to new
  46. * - old gets assigned new as a parent and 'color' as a color.
  47. */
  48. static inline void
  49. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  50. struct rb_root *root, int color)
  51. {
  52. struct rb_node *parent = rb_parent(old);
  53. new->__rb_parent_color = old->__rb_parent_color;
  54. rb_set_parent_color(old, new, color);
  55. __rb_change_child(old, new, parent, root);
  56. }
  57. static __always_inline void
  58. __rb_insert(struct rb_node *node, struct rb_root *root,
  59. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  60. {
  61. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  62. while (true) {
  63. /*
  64. * Loop invariant: node is red
  65. *
  66. * If there is a black parent, we are done.
  67. * Otherwise, take some corrective action as we don't
  68. * want a red root or two consecutive red nodes.
  69. */
  70. if (!parent) {
  71. rb_set_parent_color(node, NULL, RB_BLACK);
  72. break;
  73. } else if (rb_is_black(parent))
  74. break;
  75. gparent = rb_red_parent(parent);
  76. tmp = gparent->rb_right;
  77. if (parent != tmp) { /* parent == gparent->rb_left */
  78. if (tmp && rb_is_red(tmp)) {
  79. /*
  80. * Case 1 - color flips
  81. *
  82. * G g
  83. * / \ / \
  84. * p u --> P U
  85. * / /
  86. * n N
  87. *
  88. * However, since g's parent might be red, and
  89. * 4) does not allow this, we need to recurse
  90. * at g.
  91. */
  92. rb_set_parent_color(tmp, gparent, RB_BLACK);
  93. rb_set_parent_color(parent, gparent, RB_BLACK);
  94. node = gparent;
  95. parent = rb_parent(node);
  96. rb_set_parent_color(node, parent, RB_RED);
  97. continue;
  98. }
  99. tmp = parent->rb_right;
  100. if (node == tmp) {
  101. /*
  102. * Case 2 - left rotate at parent
  103. *
  104. * G G
  105. * / \ / \
  106. * p U --> n U
  107. * \ /
  108. * n p
  109. *
  110. * This still leaves us in violation of 4), the
  111. * continuation into Case 3 will fix that.
  112. */
  113. parent->rb_right = tmp = node->rb_left;
  114. node->rb_left = parent;
  115. if (tmp)
  116. rb_set_parent_color(tmp, parent,
  117. RB_BLACK);
  118. rb_set_parent_color(parent, node, RB_RED);
  119. augment_rotate(parent, node);
  120. parent = node;
  121. tmp = node->rb_right;
  122. }
  123. /*
  124. * Case 3 - right rotate at gparent
  125. *
  126. * G P
  127. * / \ / \
  128. * p U --> n g
  129. * / \
  130. * n U
  131. */
  132. gparent->rb_left = tmp; /* == parent->rb_right */
  133. parent->rb_right = gparent;
  134. if (tmp)
  135. rb_set_parent_color(tmp, gparent, RB_BLACK);
  136. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  137. augment_rotate(gparent, parent);
  138. break;
  139. } else {
  140. tmp = gparent->rb_left;
  141. if (tmp && rb_is_red(tmp)) {
  142. /* Case 1 - color flips */
  143. rb_set_parent_color(tmp, gparent, RB_BLACK);
  144. rb_set_parent_color(parent, gparent, RB_BLACK);
  145. node = gparent;
  146. parent = rb_parent(node);
  147. rb_set_parent_color(node, parent, RB_RED);
  148. continue;
  149. }
  150. tmp = parent->rb_left;
  151. if (node == tmp) {
  152. /* Case 2 - right rotate at parent */
  153. parent->rb_left = tmp = node->rb_right;
  154. node->rb_right = parent;
  155. if (tmp)
  156. rb_set_parent_color(tmp, parent,
  157. RB_BLACK);
  158. rb_set_parent_color(parent, node, RB_RED);
  159. augment_rotate(parent, node);
  160. parent = node;
  161. tmp = node->rb_left;
  162. }
  163. /* Case 3 - left rotate at gparent */
  164. gparent->rb_right = tmp; /* == parent->rb_left */
  165. parent->rb_left = gparent;
  166. if (tmp)
  167. rb_set_parent_color(tmp, gparent, RB_BLACK);
  168. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  169. augment_rotate(gparent, parent);
  170. break;
  171. }
  172. }
  173. }
  174. /*
  175. * Inline version for rb_erase() use - we want to be able to inline
  176. * and eliminate the dummy_rotate callback there
  177. */
  178. static __always_inline void
  179. ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
  180. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  181. {
  182. struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
  183. while (true) {
  184. /*
  185. * Loop invariants:
  186. * - node is black (or NULL on first iteration)
  187. * - node is not the root (parent is not NULL)
  188. * - All leaf paths going through parent and node have a
  189. * black node count that is 1 lower than other leaf paths.
  190. */
  191. sibling = parent->rb_right;
  192. if (node != sibling) { /* node == parent->rb_left */
  193. if (rb_is_red(sibling)) {
  194. /*
  195. * Case 1 - left rotate at parent
  196. *
  197. * P S
  198. * / \ / \
  199. * N s --> p Sr
  200. * / \ / \
  201. * Sl Sr N Sl
  202. */
  203. parent->rb_right = tmp1 = sibling->rb_left;
  204. sibling->rb_left = parent;
  205. rb_set_parent_color(tmp1, parent, RB_BLACK);
  206. __rb_rotate_set_parents(parent, sibling, root,
  207. RB_RED);
  208. augment_rotate(parent, sibling);
  209. sibling = tmp1;
  210. }
  211. tmp1 = sibling->rb_right;
  212. if (!tmp1 || rb_is_black(tmp1)) {
  213. tmp2 = sibling->rb_left;
  214. if (!tmp2 || rb_is_black(tmp2)) {
  215. /*
  216. * Case 2 - sibling color flip
  217. * (p could be either color here)
  218. *
  219. * (p) (p)
  220. * / \ / \
  221. * N S --> N s
  222. * / \ / \
  223. * Sl Sr Sl Sr
  224. *
  225. * This leaves us violating 5) which
  226. * can be fixed by flipping p to black
  227. * if it was red, or by recursing at p.
  228. * p is red when coming from Case 1.
  229. */
  230. rb_set_parent_color(sibling, parent,
  231. RB_RED);
  232. if (rb_is_red(parent))
  233. rb_set_black(parent);
  234. else {
  235. node = parent;
  236. parent = rb_parent(node);
  237. if (parent)
  238. continue;
  239. }
  240. break;
  241. }
  242. /*
  243. * Case 3 - right rotate at sibling
  244. * (p could be either color here)
  245. *
  246. * (p) (p)
  247. * / \ / \
  248. * N S --> N Sl
  249. * / \ \
  250. * sl Sr s
  251. * \
  252. * Sr
  253. */
  254. sibling->rb_left = tmp1 = tmp2->rb_right;
  255. tmp2->rb_right = sibling;
  256. parent->rb_right = tmp2;
  257. if (tmp1)
  258. rb_set_parent_color(tmp1, sibling,
  259. RB_BLACK);
  260. augment_rotate(sibling, tmp2);
  261. tmp1 = sibling;
  262. sibling = tmp2;
  263. }
  264. /*
  265. * Case 4 - left rotate at parent + color flips
  266. * (p and sl could be either color here.
  267. * After rotation, p becomes black, s acquires
  268. * p's color, and sl keeps its color)
  269. *
  270. * (p) (s)
  271. * / \ / \
  272. * N S --> P Sr
  273. * / \ / \
  274. * (sl) sr N (sl)
  275. */
  276. parent->rb_right = tmp2 = sibling->rb_left;
  277. sibling->rb_left = parent;
  278. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  279. if (tmp2)
  280. rb_set_parent(tmp2, parent);
  281. __rb_rotate_set_parents(parent, sibling, root,
  282. RB_BLACK);
  283. augment_rotate(parent, sibling);
  284. break;
  285. } else {
  286. sibling = parent->rb_left;
  287. if (rb_is_red(sibling)) {
  288. /* Case 1 - right rotate at parent */
  289. parent->rb_left = tmp1 = sibling->rb_right;
  290. sibling->rb_right = parent;
  291. rb_set_parent_color(tmp1, parent, RB_BLACK);
  292. __rb_rotate_set_parents(parent, sibling, root,
  293. RB_RED);
  294. augment_rotate(parent, sibling);
  295. sibling = tmp1;
  296. }
  297. tmp1 = sibling->rb_left;
  298. if (!tmp1 || rb_is_black(tmp1)) {
  299. tmp2 = sibling->rb_right;
  300. if (!tmp2 || rb_is_black(tmp2)) {
  301. /* Case 2 - sibling color flip */
  302. rb_set_parent_color(sibling, parent,
  303. RB_RED);
  304. if (rb_is_red(parent))
  305. rb_set_black(parent);
  306. else {
  307. node = parent;
  308. parent = rb_parent(node);
  309. if (parent)
  310. continue;
  311. }
  312. break;
  313. }
  314. /* Case 3 - right rotate at sibling */
  315. sibling->rb_right = tmp1 = tmp2->rb_left;
  316. tmp2->rb_left = sibling;
  317. parent->rb_left = tmp2;
  318. if (tmp1)
  319. rb_set_parent_color(tmp1, sibling,
  320. RB_BLACK);
  321. augment_rotate(sibling, tmp2);
  322. tmp1 = sibling;
  323. sibling = tmp2;
  324. }
  325. /* Case 4 - left rotate at parent + color flips */
  326. parent->rb_left = tmp2 = sibling->rb_right;
  327. sibling->rb_right = parent;
  328. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  329. if (tmp2)
  330. rb_set_parent(tmp2, parent);
  331. __rb_rotate_set_parents(parent, sibling, root,
  332. RB_BLACK);
  333. augment_rotate(parent, sibling);
  334. break;
  335. }
  336. }
  337. }
  338. /* Non-inline version for rb_erase_augmented() use */
  339. void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  340. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  341. {
  342. ____rb_erase_color(parent, root, augment_rotate);
  343. }
  344. EXPORT_SYMBOL(__rb_erase_color);
  345. /*
  346. * Non-augmented rbtree manipulation functions.
  347. *
  348. * We use dummy augmented callbacks here, and have the compiler optimize them
  349. * out of the rb_insert_color() and rb_erase() function definitions.
  350. */
  351. static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
  352. static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
  353. static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
  354. static const struct rb_augment_callbacks dummy_callbacks = {
  355. dummy_propagate, dummy_copy, dummy_rotate
  356. };
  357. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  358. {
  359. __rb_insert(node, root, dummy_rotate);
  360. }
  361. EXPORT_SYMBOL(rb_insert_color);
  362. void rb_erase(struct rb_node *node, struct rb_root *root)
  363. {
  364. struct rb_node *rebalance;
  365. rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
  366. if (rebalance)
  367. ____rb_erase_color(rebalance, root, dummy_rotate);
  368. }
  369. EXPORT_SYMBOL(rb_erase);
  370. /*
  371. * Augmented rbtree manipulation functions.
  372. *
  373. * This instantiates the same __always_inline functions as in the non-augmented
  374. * case, but this time with user-defined callbacks.
  375. */
  376. void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  377. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  378. {
  379. __rb_insert(node, root, augment_rotate);
  380. }
  381. EXPORT_SYMBOL(__rb_insert_augmented);
  382. /*
  383. * This function returns the first node (in sort order) of the tree.
  384. */
  385. struct rb_node *rb_first(const struct rb_root *root)
  386. {
  387. struct rb_node *n;
  388. n = root->rb_node;
  389. if (!n)
  390. return NULL;
  391. while (n->rb_left)
  392. n = n->rb_left;
  393. return n;
  394. }
  395. EXPORT_SYMBOL(rb_first);
  396. struct rb_node *rb_last(const struct rb_root *root)
  397. {
  398. struct rb_node *n;
  399. n = root->rb_node;
  400. if (!n)
  401. return NULL;
  402. while (n->rb_right)
  403. n = n->rb_right;
  404. return n;
  405. }
  406. EXPORT_SYMBOL(rb_last);
  407. struct rb_node *rb_next(const struct rb_node *node)
  408. {
  409. struct rb_node *parent;
  410. if (RB_EMPTY_NODE(node))
  411. return NULL;
  412. /*
  413. * If we have a right-hand child, go down and then left as far
  414. * as we can.
  415. */
  416. if (node->rb_right) {
  417. node = node->rb_right;
  418. while (node->rb_left)
  419. node=node->rb_left;
  420. return (struct rb_node *)node;
  421. }
  422. /*
  423. * No right-hand children. Everything down and left is smaller than us,
  424. * so any 'next' node must be in the general direction of our parent.
  425. * Go up the tree; any time the ancestor is a right-hand child of its
  426. * parent, keep going up. First time it's a left-hand child of its
  427. * parent, said parent is our 'next' node.
  428. */
  429. while ((parent = rb_parent(node)) && node == parent->rb_right)
  430. node = parent;
  431. return parent;
  432. }
  433. EXPORT_SYMBOL(rb_next);
  434. struct rb_node *rb_prev(const struct rb_node *node)
  435. {
  436. struct rb_node *parent;
  437. if (RB_EMPTY_NODE(node))
  438. return NULL;
  439. /*
  440. * If we have a left-hand child, go down and then right as far
  441. * as we can.
  442. */
  443. if (node->rb_left) {
  444. node = node->rb_left;
  445. while (node->rb_right)
  446. node=node->rb_right;
  447. return (struct rb_node *)node;
  448. }
  449. /*
  450. * No left-hand children. Go up till we find an ancestor which
  451. * is a right-hand child of its parent.
  452. */
  453. while ((parent = rb_parent(node)) && node == parent->rb_left)
  454. node = parent;
  455. return parent;
  456. }
  457. EXPORT_SYMBOL(rb_prev);
  458. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  459. struct rb_root *root)
  460. {
  461. struct rb_node *parent = rb_parent(victim);
  462. /* Set the surrounding nodes to point to the replacement */
  463. __rb_change_child(victim, new, parent, root);
  464. if (victim->rb_left)
  465. rb_set_parent(victim->rb_left, new);
  466. if (victim->rb_right)
  467. rb_set_parent(victim->rb_right, new);
  468. /* Copy the pointers/colour from the victim to the replacement */
  469. *new = *victim;
  470. }
  471. EXPORT_SYMBOL(rb_replace_node);
  472. static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
  473. {
  474. for (;;) {
  475. if (node->rb_left)
  476. node = node->rb_left;
  477. else if (node->rb_right)
  478. node = node->rb_right;
  479. else
  480. return (struct rb_node *)node;
  481. }
  482. }
  483. struct rb_node *rb_next_postorder(const struct rb_node *node)
  484. {
  485. const struct rb_node *parent;
  486. if (!node)
  487. return NULL;
  488. parent = rb_parent(node);
  489. /* If we're sitting on node, we've already seen our children */
  490. if (parent && node == parent->rb_left && parent->rb_right) {
  491. /* If we are the parent's left node, go to the parent's right
  492. * node then all the way down to the left */
  493. return rb_left_deepest_node(parent->rb_right);
  494. } else
  495. /* Otherwise we are the parent's right node, and the parent
  496. * should be next */
  497. return (struct rb_node *)parent;
  498. }
  499. EXPORT_SYMBOL(rb_next_postorder);
  500. struct rb_node *rb_first_postorder(const struct rb_root *root)
  501. {
  502. if (!root->rb_node)
  503. return NULL;
  504. return rb_left_deepest_node(root->rb_node);
  505. }
  506. EXPORT_SYMBOL(rb_first_postorder);