rbtree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. linux/lib/rbtree.c
  17. */
  18. #include <linux/rbtree.h>
  19. #include <linux/export.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.
  37. */
  38. #define RB_RED 0
  39. #define RB_BLACK 1
  40. #define rb_color(r) ((r)->__rb_parent_color & 1)
  41. #define rb_is_red(r) (!rb_color(r))
  42. #define rb_is_black(r) rb_color(r)
  43. #define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0)
  44. #define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0)
  45. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  46. {
  47. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  48. }
  49. static inline void rb_set_color(struct rb_node *rb, int color)
  50. {
  51. rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
  52. }
  53. static inline void rb_set_parent_color(struct rb_node *rb,
  54. struct rb_node *p, int color)
  55. {
  56. rb->__rb_parent_color = (unsigned long)p | color;
  57. }
  58. static inline struct rb_node *rb_red_parent(struct rb_node *red)
  59. {
  60. return (struct rb_node *)red->__rb_parent_color;
  61. }
  62. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  63. {
  64. struct rb_node *right = node->rb_right;
  65. struct rb_node *parent = rb_parent(node);
  66. if ((node->rb_right = right->rb_left))
  67. rb_set_parent(right->rb_left, node);
  68. right->rb_left = node;
  69. rb_set_parent(right, parent);
  70. if (parent)
  71. {
  72. if (node == parent->rb_left)
  73. parent->rb_left = right;
  74. else
  75. parent->rb_right = right;
  76. }
  77. else
  78. root->rb_node = right;
  79. rb_set_parent(node, right);
  80. }
  81. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  82. {
  83. struct rb_node *left = node->rb_left;
  84. struct rb_node *parent = rb_parent(node);
  85. if ((node->rb_left = left->rb_right))
  86. rb_set_parent(left->rb_right, node);
  87. left->rb_right = node;
  88. rb_set_parent(left, parent);
  89. if (parent)
  90. {
  91. if (node == parent->rb_right)
  92. parent->rb_right = left;
  93. else
  94. parent->rb_left = left;
  95. }
  96. else
  97. root->rb_node = left;
  98. rb_set_parent(node, left);
  99. }
  100. /*
  101. * Helper function for rotations:
  102. * - old's parent and color get assigned to new
  103. * - old gets assigned new as a parent and 'color' as a color.
  104. */
  105. static inline void
  106. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  107. struct rb_root *root, int color)
  108. {
  109. struct rb_node *parent = rb_parent(old);
  110. new->__rb_parent_color = old->__rb_parent_color;
  111. rb_set_parent_color(old, new, color);
  112. if (parent) {
  113. if (parent->rb_left == old)
  114. parent->rb_left = new;
  115. else
  116. parent->rb_right = new;
  117. } else
  118. root->rb_node = new;
  119. }
  120. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  121. {
  122. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  123. while (true) {
  124. /*
  125. * Loop invariant: node is red
  126. *
  127. * If there is a black parent, we are done.
  128. * Otherwise, take some corrective action as we don't
  129. * want a red root or two consecutive red nodes.
  130. */
  131. if (!parent) {
  132. rb_set_parent_color(node, NULL, RB_BLACK);
  133. break;
  134. } else if (rb_is_black(parent))
  135. break;
  136. gparent = rb_red_parent(parent);
  137. if (parent == gparent->rb_left) {
  138. tmp = gparent->rb_right;
  139. if (tmp && rb_is_red(tmp)) {
  140. /*
  141. * Case 1 - color flips
  142. *
  143. * G g
  144. * / \ / \
  145. * p u --> P U
  146. * / /
  147. * n N
  148. *
  149. * However, since g's parent might be red, and
  150. * 4) does not allow this, we need to recurse
  151. * at g.
  152. */
  153. rb_set_parent_color(tmp, gparent, RB_BLACK);
  154. rb_set_parent_color(parent, gparent, RB_BLACK);
  155. node = gparent;
  156. parent = rb_parent(node);
  157. rb_set_parent_color(node, parent, RB_RED);
  158. continue;
  159. }
  160. if (parent->rb_right == node) {
  161. /*
  162. * Case 2 - left rotate at parent
  163. *
  164. * G G
  165. * / \ / \
  166. * p U --> n U
  167. * \ /
  168. * n p
  169. *
  170. * This still leaves us in violation of 4), the
  171. * continuation into Case 3 will fix that.
  172. */
  173. parent->rb_right = tmp = node->rb_left;
  174. node->rb_left = parent;
  175. if (tmp)
  176. rb_set_parent_color(tmp, parent,
  177. RB_BLACK);
  178. rb_set_parent_color(parent, node, RB_RED);
  179. parent = node;
  180. }
  181. /*
  182. * Case 3 - right rotate at gparent
  183. *
  184. * G P
  185. * / \ / \
  186. * p U --> n g
  187. * / \
  188. * n U
  189. */
  190. gparent->rb_left = tmp = parent->rb_right;
  191. parent->rb_right = gparent;
  192. if (tmp)
  193. rb_set_parent_color(tmp, gparent, RB_BLACK);
  194. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  195. break;
  196. } else {
  197. tmp = gparent->rb_left;
  198. if (tmp && rb_is_red(tmp)) {
  199. /* Case 1 - color flips */
  200. rb_set_parent_color(tmp, gparent, RB_BLACK);
  201. rb_set_parent_color(parent, gparent, RB_BLACK);
  202. node = gparent;
  203. parent = rb_parent(node);
  204. rb_set_parent_color(node, parent, RB_RED);
  205. continue;
  206. }
  207. if (parent->rb_left == node) {
  208. /* Case 2 - right rotate at parent */
  209. parent->rb_left = tmp = node->rb_right;
  210. node->rb_right = parent;
  211. if (tmp)
  212. rb_set_parent_color(tmp, parent,
  213. RB_BLACK);
  214. rb_set_parent_color(parent, node, RB_RED);
  215. parent = node;
  216. }
  217. /* Case 3 - left rotate at gparent */
  218. gparent->rb_right = tmp = parent->rb_left;
  219. parent->rb_left = gparent;
  220. if (tmp)
  221. rb_set_parent_color(tmp, gparent, RB_BLACK);
  222. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  223. break;
  224. }
  225. }
  226. }
  227. EXPORT_SYMBOL(rb_insert_color);
  228. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  229. struct rb_root *root)
  230. {
  231. struct rb_node *other;
  232. while (true) {
  233. /*
  234. * Loop invariant: all leaf paths going through node have a
  235. * black node count that is 1 lower than other leaf paths.
  236. *
  237. * If node is red, we can flip it to black to adjust.
  238. * If node is the root, all leaf paths go through it.
  239. * Otherwise, we need to adjust the tree through color flips
  240. * and tree rotations as per one of the 4 cases below.
  241. */
  242. if (node && rb_is_red(node)) {
  243. rb_set_black(node);
  244. break;
  245. } else if (!parent) {
  246. break;
  247. } else if (parent->rb_left == node) {
  248. other = parent->rb_right;
  249. if (rb_is_red(other))
  250. {
  251. rb_set_black(other);
  252. rb_set_red(parent);
  253. __rb_rotate_left(parent, root);
  254. other = parent->rb_right;
  255. }
  256. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  257. (!other->rb_right || rb_is_black(other->rb_right)))
  258. {
  259. rb_set_red(other);
  260. node = parent;
  261. parent = rb_parent(node);
  262. }
  263. else
  264. {
  265. if (!other->rb_right || rb_is_black(other->rb_right))
  266. {
  267. rb_set_black(other->rb_left);
  268. rb_set_red(other);
  269. __rb_rotate_right(other, root);
  270. other = parent->rb_right;
  271. }
  272. rb_set_color(other, rb_color(parent));
  273. rb_set_black(parent);
  274. rb_set_black(other->rb_right);
  275. __rb_rotate_left(parent, root);
  276. break;
  277. }
  278. } else {
  279. other = parent->rb_left;
  280. if (rb_is_red(other))
  281. {
  282. rb_set_black(other);
  283. rb_set_red(parent);
  284. __rb_rotate_right(parent, root);
  285. other = parent->rb_left;
  286. }
  287. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  288. (!other->rb_right || rb_is_black(other->rb_right)))
  289. {
  290. rb_set_red(other);
  291. node = parent;
  292. parent = rb_parent(node);
  293. }
  294. else
  295. {
  296. if (!other->rb_left || rb_is_black(other->rb_left))
  297. {
  298. rb_set_black(other->rb_right);
  299. rb_set_red(other);
  300. __rb_rotate_left(other, root);
  301. other = parent->rb_left;
  302. }
  303. rb_set_color(other, rb_color(parent));
  304. rb_set_black(parent);
  305. rb_set_black(other->rb_left);
  306. __rb_rotate_right(parent, root);
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. void rb_erase(struct rb_node *node, struct rb_root *root)
  313. {
  314. struct rb_node *child, *parent;
  315. int color;
  316. if (!node->rb_left)
  317. child = node->rb_right;
  318. else if (!node->rb_right)
  319. child = node->rb_left;
  320. else
  321. {
  322. struct rb_node *old = node, *left;
  323. node = node->rb_right;
  324. while ((left = node->rb_left) != NULL)
  325. node = left;
  326. if (rb_parent(old)) {
  327. if (rb_parent(old)->rb_left == old)
  328. rb_parent(old)->rb_left = node;
  329. else
  330. rb_parent(old)->rb_right = node;
  331. } else
  332. root->rb_node = node;
  333. child = node->rb_right;
  334. parent = rb_parent(node);
  335. color = rb_color(node);
  336. if (parent == old) {
  337. parent = node;
  338. } else {
  339. if (child)
  340. rb_set_parent(child, parent);
  341. parent->rb_left = child;
  342. node->rb_right = old->rb_right;
  343. rb_set_parent(old->rb_right, node);
  344. }
  345. node->__rb_parent_color = old->__rb_parent_color;
  346. node->rb_left = old->rb_left;
  347. rb_set_parent(old->rb_left, node);
  348. goto color;
  349. }
  350. parent = rb_parent(node);
  351. color = rb_color(node);
  352. if (child)
  353. rb_set_parent(child, parent);
  354. if (parent)
  355. {
  356. if (parent->rb_left == node)
  357. parent->rb_left = child;
  358. else
  359. parent->rb_right = child;
  360. }
  361. else
  362. root->rb_node = child;
  363. color:
  364. if (color == RB_BLACK)
  365. __rb_erase_color(child, parent, root);
  366. }
  367. EXPORT_SYMBOL(rb_erase);
  368. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  369. {
  370. struct rb_node *parent;
  371. up:
  372. func(node, data);
  373. parent = rb_parent(node);
  374. if (!parent)
  375. return;
  376. if (node == parent->rb_left && parent->rb_right)
  377. func(parent->rb_right, data);
  378. else if (parent->rb_left)
  379. func(parent->rb_left, data);
  380. node = parent;
  381. goto up;
  382. }
  383. /*
  384. * after inserting @node into the tree, update the tree to account for
  385. * both the new entry and any damage done by rebalance
  386. */
  387. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  388. {
  389. if (node->rb_left)
  390. node = node->rb_left;
  391. else if (node->rb_right)
  392. node = node->rb_right;
  393. rb_augment_path(node, func, data);
  394. }
  395. EXPORT_SYMBOL(rb_augment_insert);
  396. /*
  397. * before removing the node, find the deepest node on the rebalance path
  398. * that will still be there after @node gets removed
  399. */
  400. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  401. {
  402. struct rb_node *deepest;
  403. if (!node->rb_right && !node->rb_left)
  404. deepest = rb_parent(node);
  405. else if (!node->rb_right)
  406. deepest = node->rb_left;
  407. else if (!node->rb_left)
  408. deepest = node->rb_right;
  409. else {
  410. deepest = rb_next(node);
  411. if (deepest->rb_right)
  412. deepest = deepest->rb_right;
  413. else if (rb_parent(deepest) != node)
  414. deepest = rb_parent(deepest);
  415. }
  416. return deepest;
  417. }
  418. EXPORT_SYMBOL(rb_augment_erase_begin);
  419. /*
  420. * after removal, update the tree to account for the removed entry
  421. * and any rebalance damage.
  422. */
  423. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  424. {
  425. if (node)
  426. rb_augment_path(node, func, data);
  427. }
  428. EXPORT_SYMBOL(rb_augment_erase_end);
  429. /*
  430. * This function returns the first node (in sort order) of the tree.
  431. */
  432. struct rb_node *rb_first(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_left)
  439. n = n->rb_left;
  440. return n;
  441. }
  442. EXPORT_SYMBOL(rb_first);
  443. struct rb_node *rb_last(const struct rb_root *root)
  444. {
  445. struct rb_node *n;
  446. n = root->rb_node;
  447. if (!n)
  448. return NULL;
  449. while (n->rb_right)
  450. n = n->rb_right;
  451. return n;
  452. }
  453. EXPORT_SYMBOL(rb_last);
  454. struct rb_node *rb_next(const struct rb_node *node)
  455. {
  456. struct rb_node *parent;
  457. if (RB_EMPTY_NODE(node))
  458. return NULL;
  459. /* If we have a right-hand child, go down and then left as far
  460. as we can. */
  461. if (node->rb_right) {
  462. node = node->rb_right;
  463. while (node->rb_left)
  464. node=node->rb_left;
  465. return (struct rb_node *)node;
  466. }
  467. /* No right-hand children. Everything down and left is
  468. smaller than us, so any 'next' node must be in the general
  469. direction of our parent. Go up the tree; any time the
  470. ancestor is a right-hand child of its parent, keep going
  471. up. First time it's a left-hand child of its parent, said
  472. parent is our 'next' node. */
  473. while ((parent = rb_parent(node)) && node == parent->rb_right)
  474. node = parent;
  475. return parent;
  476. }
  477. EXPORT_SYMBOL(rb_next);
  478. struct rb_node *rb_prev(const struct rb_node *node)
  479. {
  480. struct rb_node *parent;
  481. if (RB_EMPTY_NODE(node))
  482. return NULL;
  483. /* If we have a left-hand child, go down and then right as far
  484. as we can. */
  485. if (node->rb_left) {
  486. node = node->rb_left;
  487. while (node->rb_right)
  488. node=node->rb_right;
  489. return (struct rb_node *)node;
  490. }
  491. /* No left-hand children. Go up till we find an ancestor which
  492. is a right-hand child of its parent */
  493. while ((parent = rb_parent(node)) && node == parent->rb_left)
  494. node = parent;
  495. return parent;
  496. }
  497. EXPORT_SYMBOL(rb_prev);
  498. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  499. struct rb_root *root)
  500. {
  501. struct rb_node *parent = rb_parent(victim);
  502. /* Set the surrounding nodes to point to the replacement */
  503. if (parent) {
  504. if (victim == parent->rb_left)
  505. parent->rb_left = new;
  506. else
  507. parent->rb_right = new;
  508. } else {
  509. root->rb_node = new;
  510. }
  511. if (victim->rb_left)
  512. rb_set_parent(victim->rb_left, new);
  513. if (victim->rb_right)
  514. rb_set_parent(victim->rb_right, new);
  515. /* Copy the pointers/colour from the victim to the replacement */
  516. *new = *victim;
  517. }
  518. EXPORT_SYMBOL(rb_replace_node);