rbtree.c 15 KB

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