rbtree.c 14 KB

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