rbtree.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. static __always_inline void
  93. __rb_insert(struct rb_node *node, struct rb_root *root,
  94. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  95. {
  96. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  97. while (true) {
  98. /*
  99. * Loop invariant: node is red
  100. *
  101. * If there is a black parent, we are done.
  102. * Otherwise, take some corrective action as we don't
  103. * want a red root or two consecutive red nodes.
  104. */
  105. if (!parent) {
  106. rb_set_parent_color(node, NULL, RB_BLACK);
  107. break;
  108. } else if (rb_is_black(parent))
  109. break;
  110. gparent = rb_red_parent(parent);
  111. tmp = gparent->rb_right;
  112. if (parent != tmp) { /* parent == gparent->rb_left */
  113. if (tmp && rb_is_red(tmp)) {
  114. /*
  115. * Case 1 - color flips
  116. *
  117. * G g
  118. * / \ / \
  119. * p u --> P U
  120. * / /
  121. * n N
  122. *
  123. * However, since g's parent might be red, and
  124. * 4) does not allow this, we need to recurse
  125. * at g.
  126. */
  127. rb_set_parent_color(tmp, gparent, RB_BLACK);
  128. rb_set_parent_color(parent, gparent, RB_BLACK);
  129. node = gparent;
  130. parent = rb_parent(node);
  131. rb_set_parent_color(node, parent, RB_RED);
  132. continue;
  133. }
  134. tmp = parent->rb_right;
  135. if (node == tmp) {
  136. /*
  137. * Case 2 - left rotate at parent
  138. *
  139. * G G
  140. * / \ / \
  141. * p U --> n U
  142. * \ /
  143. * n p
  144. *
  145. * This still leaves us in violation of 4), the
  146. * continuation into Case 3 will fix that.
  147. */
  148. parent->rb_right = tmp = node->rb_left;
  149. node->rb_left = parent;
  150. if (tmp)
  151. rb_set_parent_color(tmp, parent,
  152. RB_BLACK);
  153. rb_set_parent_color(parent, node, RB_RED);
  154. augment_rotate(parent, node);
  155. parent = node;
  156. tmp = node->rb_right;
  157. }
  158. /*
  159. * Case 3 - right rotate at gparent
  160. *
  161. * G P
  162. * / \ / \
  163. * p U --> n g
  164. * / \
  165. * n U
  166. */
  167. gparent->rb_left = tmp; /* == parent->rb_right */
  168. parent->rb_right = gparent;
  169. if (tmp)
  170. rb_set_parent_color(tmp, gparent, RB_BLACK);
  171. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  172. augment_rotate(gparent, parent);
  173. break;
  174. } else {
  175. tmp = gparent->rb_left;
  176. if (tmp && rb_is_red(tmp)) {
  177. /* Case 1 - color flips */
  178. rb_set_parent_color(tmp, gparent, RB_BLACK);
  179. rb_set_parent_color(parent, gparent, RB_BLACK);
  180. node = gparent;
  181. parent = rb_parent(node);
  182. rb_set_parent_color(node, parent, RB_RED);
  183. continue;
  184. }
  185. tmp = parent->rb_left;
  186. if (node == tmp) {
  187. /* Case 2 - right rotate at parent */
  188. parent->rb_left = tmp = node->rb_right;
  189. node->rb_right = parent;
  190. if (tmp)
  191. rb_set_parent_color(tmp, parent,
  192. RB_BLACK);
  193. rb_set_parent_color(parent, node, RB_RED);
  194. augment_rotate(parent, node);
  195. parent = node;
  196. tmp = node->rb_left;
  197. }
  198. /* Case 3 - left rotate at gparent */
  199. gparent->rb_right = tmp; /* == parent->rb_left */
  200. parent->rb_left = gparent;
  201. if (tmp)
  202. rb_set_parent_color(tmp, gparent, RB_BLACK);
  203. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  204. augment_rotate(gparent, parent);
  205. break;
  206. }
  207. }
  208. }
  209. static __always_inline void
  210. __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  211. const struct rb_augment_callbacks *augment)
  212. {
  213. struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
  214. while (true) {
  215. /*
  216. * Loop invariants:
  217. * - node is black (or NULL on first iteration)
  218. * - node is not the root (parent is not NULL)
  219. * - All leaf paths going through parent and node have a
  220. * black node count that is 1 lower than other leaf paths.
  221. */
  222. sibling = parent->rb_right;
  223. if (node != sibling) { /* node == parent->rb_left */
  224. if (rb_is_red(sibling)) {
  225. /*
  226. * Case 1 - left rotate at parent
  227. *
  228. * P S
  229. * / \ / \
  230. * N s --> p Sr
  231. * / \ / \
  232. * Sl Sr N Sl
  233. */
  234. parent->rb_right = tmp1 = sibling->rb_left;
  235. 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. sibling->rb_left = tmp1 = tmp2->rb_right;
  286. tmp2->rb_right = sibling;
  287. parent->rb_right = tmp2;
  288. if (tmp1)
  289. rb_set_parent_color(tmp1, sibling,
  290. RB_BLACK);
  291. augment->rotate(sibling, tmp2);
  292. tmp1 = sibling;
  293. sibling = tmp2;
  294. }
  295. /*
  296. * Case 4 - left rotate at parent + color flips
  297. * (p and sl could be either color here.
  298. * After rotation, p becomes black, s acquires
  299. * p's color, and sl keeps its color)
  300. *
  301. * (p) (s)
  302. * / \ / \
  303. * N S --> P Sr
  304. * / \ / \
  305. * (sl) sr N (sl)
  306. */
  307. parent->rb_right = tmp2 = sibling->rb_left;
  308. sibling->rb_left = parent;
  309. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  310. if (tmp2)
  311. rb_set_parent(tmp2, parent);
  312. __rb_rotate_set_parents(parent, sibling, root,
  313. RB_BLACK);
  314. augment->rotate(parent, sibling);
  315. break;
  316. } else {
  317. sibling = parent->rb_left;
  318. if (rb_is_red(sibling)) {
  319. /* Case 1 - right rotate at parent */
  320. parent->rb_left = tmp1 = sibling->rb_right;
  321. sibling->rb_right = parent;
  322. rb_set_parent_color(tmp1, parent, RB_BLACK);
  323. __rb_rotate_set_parents(parent, sibling, root,
  324. RB_RED);
  325. augment->rotate(parent, sibling);
  326. sibling = tmp1;
  327. }
  328. tmp1 = sibling->rb_left;
  329. if (!tmp1 || rb_is_black(tmp1)) {
  330. tmp2 = sibling->rb_right;
  331. if (!tmp2 || rb_is_black(tmp2)) {
  332. /* Case 2 - sibling color flip */
  333. rb_set_parent_color(sibling, parent,
  334. RB_RED);
  335. if (rb_is_red(parent))
  336. rb_set_black(parent);
  337. else {
  338. node = parent;
  339. parent = rb_parent(node);
  340. if (parent)
  341. continue;
  342. }
  343. break;
  344. }
  345. /* Case 3 - right rotate at sibling */
  346. sibling->rb_right = tmp1 = tmp2->rb_left;
  347. tmp2->rb_left = sibling;
  348. parent->rb_left = tmp2;
  349. if (tmp1)
  350. rb_set_parent_color(tmp1, sibling,
  351. RB_BLACK);
  352. augment->rotate(sibling, tmp2);
  353. tmp1 = sibling;
  354. sibling = tmp2;
  355. }
  356. /* Case 4 - left rotate at parent + color flips */
  357. parent->rb_left = tmp2 = sibling->rb_right;
  358. sibling->rb_right = parent;
  359. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  360. if (tmp2)
  361. rb_set_parent(tmp2, parent);
  362. __rb_rotate_set_parents(parent, sibling, root,
  363. RB_BLACK);
  364. augment->rotate(parent, sibling);
  365. break;
  366. }
  367. }
  368. }
  369. static __always_inline void
  370. __rb_erase(struct rb_node *node, struct rb_root *root,
  371. const struct rb_augment_callbacks *augment)
  372. {
  373. struct rb_node *child = node->rb_right, *tmp = node->rb_left;
  374. struct rb_node *parent, *rebalance;
  375. unsigned long pc;
  376. if (!tmp) {
  377. /*
  378. * Case 1: node to erase has no more than 1 child (easy!)
  379. *
  380. * Note that if there is one child it must be red due to 5)
  381. * and node must be black due to 4). We adjust colors locally
  382. * so as to bypass __rb_erase_color() later on.
  383. */
  384. pc = node->__rb_parent_color;
  385. parent = __rb_parent(pc);
  386. __rb_change_child(node, child, parent, root);
  387. if (child) {
  388. child->__rb_parent_color = pc;
  389. rebalance = NULL;
  390. } else
  391. rebalance = __rb_is_black(pc) ? parent : NULL;
  392. tmp = parent;
  393. } else if (!child) {
  394. /* Still case 1, but this time the child is node->rb_left */
  395. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  396. parent = __rb_parent(pc);
  397. __rb_change_child(node, tmp, parent, root);
  398. rebalance = NULL;
  399. tmp = parent;
  400. } else {
  401. struct rb_node *successor = child, *child2;
  402. tmp = child->rb_left;
  403. if (!tmp) {
  404. /*
  405. * Case 2: node's successor is its right child
  406. *
  407. * (n) (s)
  408. * / \ / \
  409. * (x) (s) -> (x) (c)
  410. * \
  411. * (c)
  412. */
  413. parent = successor;
  414. child2 = successor->rb_right;
  415. augment->copy(node, successor);
  416. } else {
  417. /*
  418. * Case 3: node's successor is leftmost under
  419. * node's right child subtree
  420. *
  421. * (n) (s)
  422. * / \ / \
  423. * (x) (y) -> (x) (y)
  424. * / /
  425. * (p) (p)
  426. * / /
  427. * (s) (c)
  428. * \
  429. * (c)
  430. */
  431. do {
  432. parent = successor;
  433. successor = tmp;
  434. tmp = tmp->rb_left;
  435. } while (tmp);
  436. parent->rb_left = child2 = successor->rb_right;
  437. successor->rb_right = child;
  438. rb_set_parent(child, successor);
  439. augment->copy(node, successor);
  440. augment->propagate(parent, successor);
  441. }
  442. successor->rb_left = tmp = node->rb_left;
  443. rb_set_parent(tmp, successor);
  444. pc = node->__rb_parent_color;
  445. tmp = __rb_parent(pc);
  446. __rb_change_child(node, successor, tmp, root);
  447. if (child2) {
  448. successor->__rb_parent_color = pc;
  449. rb_set_parent_color(child2, parent, RB_BLACK);
  450. rebalance = NULL;
  451. } else {
  452. unsigned long pc2 = successor->__rb_parent_color;
  453. successor->__rb_parent_color = pc;
  454. rebalance = __rb_is_black(pc2) ? parent : NULL;
  455. }
  456. tmp = successor;
  457. }
  458. augment->propagate(tmp, NULL);
  459. if (rebalance)
  460. __rb_erase_color(rebalance, root, augment);
  461. }
  462. /*
  463. * Non-augmented rbtree manipulation functions.
  464. *
  465. * We use dummy augmented callbacks here, and have the compiler optimize them
  466. * out of the rb_insert_color() and rb_erase() function definitions.
  467. */
  468. static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
  469. static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
  470. static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
  471. static const struct rb_augment_callbacks dummy_callbacks = {
  472. dummy_propagate, dummy_copy, dummy_rotate
  473. };
  474. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  475. {
  476. __rb_insert(node, root, dummy_rotate);
  477. }
  478. EXPORT_SYMBOL(rb_insert_color);
  479. void rb_erase(struct rb_node *node, struct rb_root *root)
  480. {
  481. __rb_erase(node, root, &dummy_callbacks);
  482. }
  483. EXPORT_SYMBOL(rb_erase);
  484. /*
  485. * Augmented rbtree manipulation functions.
  486. *
  487. * This instantiates the same __always_inline functions as in the non-augmented
  488. * case, but this time with user-defined callbacks.
  489. */
  490. void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  491. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  492. {
  493. __rb_insert(node, root, augment_rotate);
  494. }
  495. EXPORT_SYMBOL(__rb_insert_augmented);
  496. void rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  497. const struct rb_augment_callbacks *augment)
  498. {
  499. __rb_erase(node, root, augment);
  500. }
  501. EXPORT_SYMBOL(rb_erase_augmented);
  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);