rbtree.c 17 KB

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