rbtree.c 15 KB

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