rbtree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 ((!node || rb_is_black(node)) && node != root->rb_node)
  233. {
  234. if (parent->rb_left == node)
  235. {
  236. other = parent->rb_right;
  237. if (rb_is_red(other))
  238. {
  239. rb_set_black(other);
  240. rb_set_red(parent);
  241. __rb_rotate_left(parent, root);
  242. other = parent->rb_right;
  243. }
  244. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  245. (!other->rb_right || rb_is_black(other->rb_right)))
  246. {
  247. rb_set_red(other);
  248. node = parent;
  249. parent = rb_parent(node);
  250. }
  251. else
  252. {
  253. if (!other->rb_right || rb_is_black(other->rb_right))
  254. {
  255. rb_set_black(other->rb_left);
  256. rb_set_red(other);
  257. __rb_rotate_right(other, root);
  258. other = parent->rb_right;
  259. }
  260. rb_set_color(other, rb_color(parent));
  261. rb_set_black(parent);
  262. rb_set_black(other->rb_right);
  263. __rb_rotate_left(parent, root);
  264. node = root->rb_node;
  265. break;
  266. }
  267. }
  268. else
  269. {
  270. other = parent->rb_left;
  271. if (rb_is_red(other))
  272. {
  273. rb_set_black(other);
  274. rb_set_red(parent);
  275. __rb_rotate_right(parent, root);
  276. other = parent->rb_left;
  277. }
  278. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  279. (!other->rb_right || rb_is_black(other->rb_right)))
  280. {
  281. rb_set_red(other);
  282. node = parent;
  283. parent = rb_parent(node);
  284. }
  285. else
  286. {
  287. if (!other->rb_left || rb_is_black(other->rb_left))
  288. {
  289. rb_set_black(other->rb_right);
  290. rb_set_red(other);
  291. __rb_rotate_left(other, root);
  292. other = parent->rb_left;
  293. }
  294. rb_set_color(other, rb_color(parent));
  295. rb_set_black(parent);
  296. rb_set_black(other->rb_left);
  297. __rb_rotate_right(parent, root);
  298. node = root->rb_node;
  299. break;
  300. }
  301. }
  302. }
  303. if (node)
  304. rb_set_black(node);
  305. }
  306. void rb_erase(struct rb_node *node, struct rb_root *root)
  307. {
  308. struct rb_node *child, *parent;
  309. int color;
  310. if (!node->rb_left)
  311. child = node->rb_right;
  312. else if (!node->rb_right)
  313. child = node->rb_left;
  314. else
  315. {
  316. struct rb_node *old = node, *left;
  317. node = node->rb_right;
  318. while ((left = node->rb_left) != NULL)
  319. node = left;
  320. if (rb_parent(old)) {
  321. if (rb_parent(old)->rb_left == old)
  322. rb_parent(old)->rb_left = node;
  323. else
  324. rb_parent(old)->rb_right = node;
  325. } else
  326. root->rb_node = node;
  327. child = node->rb_right;
  328. parent = rb_parent(node);
  329. color = rb_color(node);
  330. if (parent == old) {
  331. parent = node;
  332. } else {
  333. if (child)
  334. rb_set_parent(child, parent);
  335. parent->rb_left = child;
  336. node->rb_right = old->rb_right;
  337. rb_set_parent(old->rb_right, node);
  338. }
  339. node->__rb_parent_color = old->__rb_parent_color;
  340. node->rb_left = old->rb_left;
  341. rb_set_parent(old->rb_left, node);
  342. goto color;
  343. }
  344. parent = rb_parent(node);
  345. color = rb_color(node);
  346. if (child)
  347. rb_set_parent(child, parent);
  348. if (parent)
  349. {
  350. if (parent->rb_left == node)
  351. parent->rb_left = child;
  352. else
  353. parent->rb_right = child;
  354. }
  355. else
  356. root->rb_node = child;
  357. color:
  358. if (color == RB_BLACK)
  359. __rb_erase_color(child, parent, root);
  360. }
  361. EXPORT_SYMBOL(rb_erase);
  362. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  363. {
  364. struct rb_node *parent;
  365. up:
  366. func(node, data);
  367. parent = rb_parent(node);
  368. if (!parent)
  369. return;
  370. if (node == parent->rb_left && parent->rb_right)
  371. func(parent->rb_right, data);
  372. else if (parent->rb_left)
  373. func(parent->rb_left, data);
  374. node = parent;
  375. goto up;
  376. }
  377. /*
  378. * after inserting @node into the tree, update the tree to account for
  379. * both the new entry and any damage done by rebalance
  380. */
  381. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  382. {
  383. if (node->rb_left)
  384. node = node->rb_left;
  385. else if (node->rb_right)
  386. node = node->rb_right;
  387. rb_augment_path(node, func, data);
  388. }
  389. EXPORT_SYMBOL(rb_augment_insert);
  390. /*
  391. * before removing the node, find the deepest node on the rebalance path
  392. * that will still be there after @node gets removed
  393. */
  394. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  395. {
  396. struct rb_node *deepest;
  397. if (!node->rb_right && !node->rb_left)
  398. deepest = rb_parent(node);
  399. else if (!node->rb_right)
  400. deepest = node->rb_left;
  401. else if (!node->rb_left)
  402. deepest = node->rb_right;
  403. else {
  404. deepest = rb_next(node);
  405. if (deepest->rb_right)
  406. deepest = deepest->rb_right;
  407. else if (rb_parent(deepest) != node)
  408. deepest = rb_parent(deepest);
  409. }
  410. return deepest;
  411. }
  412. EXPORT_SYMBOL(rb_augment_erase_begin);
  413. /*
  414. * after removal, update the tree to account for the removed entry
  415. * and any rebalance damage.
  416. */
  417. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  418. {
  419. if (node)
  420. rb_augment_path(node, func, data);
  421. }
  422. EXPORT_SYMBOL(rb_augment_erase_end);
  423. /*
  424. * This function returns the first node (in sort order) of the tree.
  425. */
  426. struct rb_node *rb_first(const struct rb_root *root)
  427. {
  428. struct rb_node *n;
  429. n = root->rb_node;
  430. if (!n)
  431. return NULL;
  432. while (n->rb_left)
  433. n = n->rb_left;
  434. return n;
  435. }
  436. EXPORT_SYMBOL(rb_first);
  437. struct rb_node *rb_last(const struct rb_root *root)
  438. {
  439. struct rb_node *n;
  440. n = root->rb_node;
  441. if (!n)
  442. return NULL;
  443. while (n->rb_right)
  444. n = n->rb_right;
  445. return n;
  446. }
  447. EXPORT_SYMBOL(rb_last);
  448. struct rb_node *rb_next(const struct rb_node *node)
  449. {
  450. struct rb_node *parent;
  451. if (RB_EMPTY_NODE(node))
  452. return NULL;
  453. /* If we have a right-hand child, go down and then left as far
  454. as we can. */
  455. if (node->rb_right) {
  456. node = node->rb_right;
  457. while (node->rb_left)
  458. node=node->rb_left;
  459. return (struct rb_node *)node;
  460. }
  461. /* No right-hand children. Everything down and left is
  462. smaller than us, so any 'next' node must be in the general
  463. direction of our parent. Go up the tree; any time the
  464. ancestor is a right-hand child of its parent, keep going
  465. up. First time it's a left-hand child of its parent, said
  466. parent is our 'next' node. */
  467. while ((parent = rb_parent(node)) && node == parent->rb_right)
  468. node = parent;
  469. return parent;
  470. }
  471. EXPORT_SYMBOL(rb_next);
  472. struct rb_node *rb_prev(const struct rb_node *node)
  473. {
  474. struct rb_node *parent;
  475. if (RB_EMPTY_NODE(node))
  476. return NULL;
  477. /* If we have a left-hand child, go down and then right as far
  478. as we can. */
  479. if (node->rb_left) {
  480. node = node->rb_left;
  481. while (node->rb_right)
  482. node=node->rb_right;
  483. return (struct rb_node *)node;
  484. }
  485. /* No left-hand children. Go up till we find an ancestor which
  486. is a right-hand child of its parent */
  487. while ((parent = rb_parent(node)) && node == parent->rb_left)
  488. node = parent;
  489. return parent;
  490. }
  491. EXPORT_SYMBOL(rb_prev);
  492. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  493. struct rb_root *root)
  494. {
  495. struct rb_node *parent = rb_parent(victim);
  496. /* Set the surrounding nodes to point to the replacement */
  497. if (parent) {
  498. if (victim == parent->rb_left)
  499. parent->rb_left = new;
  500. else
  501. parent->rb_right = new;
  502. } else {
  503. root->rb_node = new;
  504. }
  505. if (victim->rb_left)
  506. rb_set_parent(victim->rb_left, new);
  507. if (victim->rb_right)
  508. rb_set_parent(victim->rb_right, new);
  509. /* Copy the pointers/colour from the victim to the replacement */
  510. *new = *victim;
  511. }
  512. EXPORT_SYMBOL(rb_replace_node);