rbtree.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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.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. #define RB_RED 0
  41. #define RB_BLACK 1
  42. #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
  43. #define __rb_color(pc) ((pc) & 1)
  44. #define __rb_is_black(pc) __rb_color(pc)
  45. #define __rb_is_red(pc) (!__rb_color(pc))
  46. #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
  47. #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
  48. #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
  49. static inline void rb_set_black(struct rb_node *rb)
  50. {
  51. rb->__rb_parent_color |= RB_BLACK;
  52. }
  53. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  54. {
  55. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  56. }
  57. static inline void rb_set_parent_color(struct rb_node *rb,
  58. struct rb_node *p, int color)
  59. {
  60. rb->__rb_parent_color = (unsigned long)p | color;
  61. }
  62. static inline struct rb_node *rb_red_parent(struct rb_node *red)
  63. {
  64. return (struct rb_node *)red->__rb_parent_color;
  65. }
  66. static inline void
  67. __rb_change_child(struct rb_node *old, struct rb_node *new,
  68. struct rb_node *parent, struct rb_root *root)
  69. {
  70. if (parent) {
  71. if (parent->rb_left == old)
  72. parent->rb_left = new;
  73. else
  74. parent->rb_right = new;
  75. } else
  76. root->rb_node = new;
  77. }
  78. /*
  79. * Helper function for rotations:
  80. * - old's parent and color get assigned to new
  81. * - old gets assigned new as a parent and 'color' as a color.
  82. */
  83. static inline void
  84. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  85. struct rb_root *root, int color)
  86. {
  87. struct rb_node *parent = rb_parent(old);
  88. new->__rb_parent_color = old->__rb_parent_color;
  89. rb_set_parent_color(old, new, color);
  90. __rb_change_child(old, new, parent, root);
  91. }
  92. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  93. {
  94. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  95. while (true) {
  96. /*
  97. * Loop invariant: node is red
  98. *
  99. * If there is a black parent, we are done.
  100. * Otherwise, take some corrective action as we don't
  101. * want a red root or two consecutive red nodes.
  102. */
  103. if (!parent) {
  104. rb_set_parent_color(node, NULL, RB_BLACK);
  105. break;
  106. } else if (rb_is_black(parent))
  107. break;
  108. gparent = rb_red_parent(parent);
  109. tmp = gparent->rb_right;
  110. if (parent != tmp) { /* parent == gparent->rb_left */
  111. if (tmp && rb_is_red(tmp)) {
  112. /*
  113. * Case 1 - color flips
  114. *
  115. * G g
  116. * / \ / \
  117. * p u --> P U
  118. * / /
  119. * n N
  120. *
  121. * However, since g's parent might be red, and
  122. * 4) does not allow this, we need to recurse
  123. * at g.
  124. */
  125. rb_set_parent_color(tmp, gparent, RB_BLACK);
  126. rb_set_parent_color(parent, gparent, RB_BLACK);
  127. node = gparent;
  128. parent = rb_parent(node);
  129. rb_set_parent_color(node, parent, RB_RED);
  130. continue;
  131. }
  132. tmp = parent->rb_right;
  133. if (node == tmp) {
  134. /*
  135. * Case 2 - left rotate at parent
  136. *
  137. * G G
  138. * / \ / \
  139. * p U --> n U
  140. * \ /
  141. * n p
  142. *
  143. * This still leaves us in violation of 4), the
  144. * continuation into Case 3 will fix that.
  145. */
  146. parent->rb_right = tmp = node->rb_left;
  147. node->rb_left = parent;
  148. if (tmp)
  149. rb_set_parent_color(tmp, parent,
  150. RB_BLACK);
  151. rb_set_parent_color(parent, node, RB_RED);
  152. parent = node;
  153. tmp = node->rb_right;
  154. }
  155. /*
  156. * Case 3 - right rotate at gparent
  157. *
  158. * G P
  159. * / \ / \
  160. * p U --> n g
  161. * / \
  162. * n U
  163. */
  164. gparent->rb_left = tmp; /* == parent->rb_right */
  165. parent->rb_right = gparent;
  166. if (tmp)
  167. rb_set_parent_color(tmp, gparent, RB_BLACK);
  168. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  169. break;
  170. } else {
  171. tmp = gparent->rb_left;
  172. if (tmp && rb_is_red(tmp)) {
  173. /* Case 1 - color flips */
  174. rb_set_parent_color(tmp, gparent, RB_BLACK);
  175. rb_set_parent_color(parent, gparent, RB_BLACK);
  176. node = gparent;
  177. parent = rb_parent(node);
  178. rb_set_parent_color(node, parent, RB_RED);
  179. continue;
  180. }
  181. tmp = parent->rb_left;
  182. if (node == tmp) {
  183. /* Case 2 - right rotate at parent */
  184. parent->rb_left = tmp = node->rb_right;
  185. node->rb_right = parent;
  186. if (tmp)
  187. rb_set_parent_color(tmp, parent,
  188. RB_BLACK);
  189. rb_set_parent_color(parent, node, RB_RED);
  190. parent = node;
  191. tmp = node->rb_left;
  192. }
  193. /* Case 3 - left rotate at gparent */
  194. gparent->rb_right = tmp; /* == parent->rb_left */
  195. 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. break;
  200. }
  201. }
  202. }
  203. EXPORT_SYMBOL(rb_insert_color);
  204. static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
  205. {
  206. struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
  207. while (true) {
  208. /*
  209. * Loop invariants:
  210. * - node is black (or NULL on first iteration)
  211. * - node is not the root (parent is not NULL)
  212. * - All leaf paths going through parent and node have a
  213. * black node count that is 1 lower than other leaf paths.
  214. */
  215. sibling = parent->rb_right;
  216. if (node != sibling) { /* node == parent->rb_left */
  217. if (rb_is_red(sibling)) {
  218. /*
  219. * Case 1 - left rotate at parent
  220. *
  221. * P S
  222. * / \ / \
  223. * N s --> p Sr
  224. * / \ / \
  225. * Sl Sr N Sl
  226. */
  227. parent->rb_right = tmp1 = sibling->rb_left;
  228. sibling->rb_left = parent;
  229. rb_set_parent_color(tmp1, parent, RB_BLACK);
  230. __rb_rotate_set_parents(parent, sibling, root,
  231. RB_RED);
  232. sibling = tmp1;
  233. }
  234. tmp1 = sibling->rb_right;
  235. if (!tmp1 || rb_is_black(tmp1)) {
  236. tmp2 = sibling->rb_left;
  237. if (!tmp2 || rb_is_black(tmp2)) {
  238. /*
  239. * Case 2 - sibling color flip
  240. * (p could be either color here)
  241. *
  242. * (p) (p)
  243. * / \ / \
  244. * N S --> N s
  245. * / \ / \
  246. * Sl Sr Sl Sr
  247. *
  248. * This leaves us violating 5) which
  249. * can be fixed by flipping p to black
  250. * if it was red, or by recursing at p.
  251. * p is red when coming from Case 1.
  252. */
  253. rb_set_parent_color(sibling, parent,
  254. RB_RED);
  255. if (rb_is_red(parent))
  256. rb_set_black(parent);
  257. else {
  258. node = parent;
  259. parent = rb_parent(node);
  260. if (parent)
  261. continue;
  262. }
  263. break;
  264. }
  265. /*
  266. * Case 3 - right rotate at sibling
  267. * (p could be either color here)
  268. *
  269. * (p) (p)
  270. * / \ / \
  271. * N S --> N Sl
  272. * / \ \
  273. * sl Sr s
  274. * \
  275. * Sr
  276. */
  277. sibling->rb_left = tmp1 = tmp2->rb_right;
  278. tmp2->rb_right = sibling;
  279. parent->rb_right = tmp2;
  280. if (tmp1)
  281. rb_set_parent_color(tmp1, sibling,
  282. RB_BLACK);
  283. tmp1 = sibling;
  284. sibling = tmp2;
  285. }
  286. /*
  287. * Case 4 - left rotate at parent + color flips
  288. * (p and sl could be either color here.
  289. * After rotation, p becomes black, s acquires
  290. * p's color, and sl keeps its color)
  291. *
  292. * (p) (s)
  293. * / \ / \
  294. * N S --> P Sr
  295. * / \ / \
  296. * (sl) sr N (sl)
  297. */
  298. parent->rb_right = tmp2 = sibling->rb_left;
  299. sibling->rb_left = parent;
  300. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  301. if (tmp2)
  302. rb_set_parent(tmp2, parent);
  303. __rb_rotate_set_parents(parent, sibling, root,
  304. RB_BLACK);
  305. break;
  306. } else {
  307. sibling = parent->rb_left;
  308. if (rb_is_red(sibling)) {
  309. /* Case 1 - right rotate at parent */
  310. parent->rb_left = tmp1 = sibling->rb_right;
  311. sibling->rb_right = parent;
  312. rb_set_parent_color(tmp1, parent, RB_BLACK);
  313. __rb_rotate_set_parents(parent, sibling, root,
  314. RB_RED);
  315. sibling = tmp1;
  316. }
  317. tmp1 = sibling->rb_left;
  318. if (!tmp1 || rb_is_black(tmp1)) {
  319. tmp2 = sibling->rb_right;
  320. if (!tmp2 || rb_is_black(tmp2)) {
  321. /* Case 2 - sibling color flip */
  322. rb_set_parent_color(sibling, parent,
  323. RB_RED);
  324. if (rb_is_red(parent))
  325. rb_set_black(parent);
  326. else {
  327. node = parent;
  328. parent = rb_parent(node);
  329. if (parent)
  330. continue;
  331. }
  332. break;
  333. }
  334. /* Case 3 - right rotate at sibling */
  335. sibling->rb_right = tmp1 = tmp2->rb_left;
  336. tmp2->rb_left = sibling;
  337. parent->rb_left = tmp2;
  338. if (tmp1)
  339. rb_set_parent_color(tmp1, sibling,
  340. RB_BLACK);
  341. tmp1 = sibling;
  342. sibling = tmp2;
  343. }
  344. /* Case 4 - left rotate at parent + color flips */
  345. parent->rb_left = tmp2 = sibling->rb_right;
  346. sibling->rb_right = parent;
  347. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  348. if (tmp2)
  349. rb_set_parent(tmp2, parent);
  350. __rb_rotate_set_parents(parent, sibling, root,
  351. RB_BLACK);
  352. break;
  353. }
  354. }
  355. }
  356. void rb_erase(struct rb_node *node, struct rb_root *root)
  357. {
  358. struct rb_node *child = node->rb_right, *tmp = node->rb_left;
  359. struct rb_node *parent, *rebalance;
  360. unsigned long pc;
  361. if (!tmp) {
  362. /*
  363. * Case 1: node to erase has no more than 1 child (easy!)
  364. *
  365. * Note that if there is one child it must be red due to 5)
  366. * and node must be black due to 4). We adjust colors locally
  367. * so as to bypass __rb_erase_color() later on.
  368. */
  369. pc = node->__rb_parent_color;
  370. parent = __rb_parent(pc);
  371. __rb_change_child(node, child, parent, root);
  372. if (child) {
  373. child->__rb_parent_color = pc;
  374. rebalance = NULL;
  375. } else
  376. rebalance = __rb_is_black(pc) ? parent : NULL;
  377. } else if (!child) {
  378. /* Still case 1, but this time the child is node->rb_left */
  379. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  380. parent = __rb_parent(pc);
  381. __rb_change_child(node, tmp, parent, root);
  382. rebalance = NULL;
  383. } else {
  384. struct rb_node *successor = child, *child2;
  385. tmp = child->rb_left;
  386. if (!tmp) {
  387. /*
  388. * Case 2: node's successor is its right child
  389. *
  390. * (n) (s)
  391. * / \ / \
  392. * (x) (s) -> (x) (c)
  393. * \
  394. * (c)
  395. */
  396. parent = child;
  397. child2 = child->rb_right;
  398. } else {
  399. /*
  400. * Case 3: node's successor is leftmost under
  401. * node's right child subtree
  402. *
  403. * (n) (s)
  404. * / \ / \
  405. * (x) (y) -> (x) (y)
  406. * / /
  407. * (p) (p)
  408. * / /
  409. * (s) (c)
  410. * \
  411. * (c)
  412. */
  413. do {
  414. parent = successor;
  415. successor = tmp;
  416. tmp = tmp->rb_left;
  417. } while (tmp);
  418. parent->rb_left = child2 = successor->rb_right;
  419. successor->rb_right = child;
  420. rb_set_parent(child, successor);
  421. }
  422. successor->rb_left = tmp = node->rb_left;
  423. rb_set_parent(tmp, successor);
  424. pc = node->__rb_parent_color;
  425. tmp = __rb_parent(pc);
  426. __rb_change_child(node, successor, tmp, root);
  427. if (child2) {
  428. successor->__rb_parent_color = pc;
  429. rb_set_parent_color(child2, parent, RB_BLACK);
  430. rebalance = NULL;
  431. } else {
  432. unsigned long pc2 = successor->__rb_parent_color;
  433. successor->__rb_parent_color = pc;
  434. rebalance = __rb_is_black(pc2) ? parent : NULL;
  435. }
  436. }
  437. if (rebalance)
  438. __rb_erase_color(rebalance, root);
  439. }
  440. EXPORT_SYMBOL(rb_erase);
  441. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  442. {
  443. struct rb_node *parent;
  444. up:
  445. func(node, data);
  446. parent = rb_parent(node);
  447. if (!parent)
  448. return;
  449. if (node == parent->rb_left && parent->rb_right)
  450. func(parent->rb_right, data);
  451. else if (parent->rb_left)
  452. func(parent->rb_left, data);
  453. node = parent;
  454. goto up;
  455. }
  456. /*
  457. * after inserting @node into the tree, update the tree to account for
  458. * both the new entry and any damage done by rebalance
  459. */
  460. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  461. {
  462. if (node->rb_left)
  463. node = node->rb_left;
  464. else if (node->rb_right)
  465. node = node->rb_right;
  466. rb_augment_path(node, func, data);
  467. }
  468. EXPORT_SYMBOL(rb_augment_insert);
  469. /*
  470. * before removing the node, find the deepest node on the rebalance path
  471. * that will still be there after @node gets removed
  472. */
  473. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  474. {
  475. struct rb_node *deepest;
  476. if (!node->rb_right && !node->rb_left)
  477. deepest = rb_parent(node);
  478. else if (!node->rb_right)
  479. deepest = node->rb_left;
  480. else if (!node->rb_left)
  481. deepest = node->rb_right;
  482. else {
  483. deepest = rb_next(node);
  484. if (deepest->rb_right)
  485. deepest = deepest->rb_right;
  486. else if (rb_parent(deepest) != node)
  487. deepest = rb_parent(deepest);
  488. }
  489. return deepest;
  490. }
  491. EXPORT_SYMBOL(rb_augment_erase_begin);
  492. /*
  493. * after removal, update the tree to account for the removed entry
  494. * and any rebalance damage.
  495. */
  496. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  497. {
  498. if (node)
  499. rb_augment_path(node, func, data);
  500. }
  501. EXPORT_SYMBOL(rb_augment_erase_end);
  502. /*
  503. * This function returns the first node (in sort order) of the tree.
  504. */
  505. struct rb_node *rb_first(const struct rb_root *root)
  506. {
  507. struct rb_node *n;
  508. n = root->rb_node;
  509. if (!n)
  510. return NULL;
  511. while (n->rb_left)
  512. n = n->rb_left;
  513. return n;
  514. }
  515. EXPORT_SYMBOL(rb_first);
  516. struct rb_node *rb_last(const struct rb_root *root)
  517. {
  518. struct rb_node *n;
  519. n = root->rb_node;
  520. if (!n)
  521. return NULL;
  522. while (n->rb_right)
  523. n = n->rb_right;
  524. return n;
  525. }
  526. EXPORT_SYMBOL(rb_last);
  527. struct rb_node *rb_next(const struct rb_node *node)
  528. {
  529. struct rb_node *parent;
  530. if (RB_EMPTY_NODE(node))
  531. return NULL;
  532. /*
  533. * If we have a right-hand child, go down and then left as far
  534. * as we can.
  535. */
  536. if (node->rb_right) {
  537. node = node->rb_right;
  538. while (node->rb_left)
  539. node=node->rb_left;
  540. return (struct rb_node *)node;
  541. }
  542. /*
  543. * No right-hand children. Everything down and left is smaller than us,
  544. * so any 'next' node must be in the general direction of our parent.
  545. * Go up the tree; any time the ancestor is a right-hand child of its
  546. * parent, keep going up. First time it's a left-hand child of its
  547. * parent, said parent is our 'next' node.
  548. */
  549. while ((parent = rb_parent(node)) && node == parent->rb_right)
  550. node = parent;
  551. return parent;
  552. }
  553. EXPORT_SYMBOL(rb_next);
  554. struct rb_node *rb_prev(const struct rb_node *node)
  555. {
  556. struct rb_node *parent;
  557. if (RB_EMPTY_NODE(node))
  558. return NULL;
  559. /*
  560. * If we have a left-hand child, go down and then right as far
  561. * as we can.
  562. */
  563. if (node->rb_left) {
  564. node = node->rb_left;
  565. while (node->rb_right)
  566. node=node->rb_right;
  567. return (struct rb_node *)node;
  568. }
  569. /*
  570. * No left-hand children. Go up till we find an ancestor which
  571. * is a right-hand child of its parent.
  572. */
  573. while ((parent = rb_parent(node)) && node == parent->rb_left)
  574. node = parent;
  575. return parent;
  576. }
  577. EXPORT_SYMBOL(rb_prev);
  578. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  579. struct rb_root *root)
  580. {
  581. struct rb_node *parent = rb_parent(victim);
  582. /* Set the surrounding nodes to point to the replacement */
  583. __rb_change_child(victim, new, parent, root);
  584. if (victim->rb_left)
  585. rb_set_parent(victim->rb_left, new);
  586. if (victim->rb_right)
  587. rb_set_parent(victim->rb_right, new);
  588. /* Copy the pointers/colour from the victim to the replacement */
  589. *new = *victim;
  590. }
  591. EXPORT_SYMBOL(rb_replace_node);