rbtree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. __always_inline void
  180. __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  181. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  182. {
  183. struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
  184. while (true) {
  185. /*
  186. * Loop invariants:
  187. * - node is black (or NULL on first iteration)
  188. * - node is not the root (parent is not NULL)
  189. * - All leaf paths going through parent and node have a
  190. * black node count that is 1 lower than other leaf paths.
  191. */
  192. sibling = parent->rb_right;
  193. if (node != sibling) { /* node == parent->rb_left */
  194. if (rb_is_red(sibling)) {
  195. /*
  196. * Case 1 - left rotate at parent
  197. *
  198. * P S
  199. * / \ / \
  200. * N s --> p Sr
  201. * / \ / \
  202. * Sl Sr N Sl
  203. */
  204. parent->rb_right = tmp1 = sibling->rb_left;
  205. sibling->rb_left = parent;
  206. rb_set_parent_color(tmp1, parent, RB_BLACK);
  207. __rb_rotate_set_parents(parent, sibling, root,
  208. RB_RED);
  209. augment_rotate(parent, sibling);
  210. sibling = tmp1;
  211. }
  212. tmp1 = sibling->rb_right;
  213. if (!tmp1 || rb_is_black(tmp1)) {
  214. tmp2 = sibling->rb_left;
  215. if (!tmp2 || rb_is_black(tmp2)) {
  216. /*
  217. * Case 2 - sibling color flip
  218. * (p could be either color here)
  219. *
  220. * (p) (p)
  221. * / \ / \
  222. * N S --> N s
  223. * / \ / \
  224. * Sl Sr Sl Sr
  225. *
  226. * This leaves us violating 5) which
  227. * can be fixed by flipping p to black
  228. * if it was red, or by recursing at p.
  229. * p is red when coming from Case 1.
  230. */
  231. rb_set_parent_color(sibling, parent,
  232. RB_RED);
  233. if (rb_is_red(parent))
  234. rb_set_black(parent);
  235. else {
  236. node = parent;
  237. parent = rb_parent(node);
  238. if (parent)
  239. continue;
  240. }
  241. break;
  242. }
  243. /*
  244. * Case 3 - right rotate at sibling
  245. * (p could be either color here)
  246. *
  247. * (p) (p)
  248. * / \ / \
  249. * N S --> N Sl
  250. * / \ \
  251. * sl Sr s
  252. * \
  253. * Sr
  254. */
  255. sibling->rb_left = tmp1 = tmp2->rb_right;
  256. tmp2->rb_right = sibling;
  257. parent->rb_right = tmp2;
  258. if (tmp1)
  259. rb_set_parent_color(tmp1, sibling,
  260. RB_BLACK);
  261. augment_rotate(sibling, tmp2);
  262. tmp1 = sibling;
  263. sibling = tmp2;
  264. }
  265. /*
  266. * Case 4 - left rotate at parent + color flips
  267. * (p and sl could be either color here.
  268. * After rotation, p becomes black, s acquires
  269. * p's color, and sl keeps its color)
  270. *
  271. * (p) (s)
  272. * / \ / \
  273. * N S --> P Sr
  274. * / \ / \
  275. * (sl) sr N (sl)
  276. */
  277. parent->rb_right = tmp2 = sibling->rb_left;
  278. sibling->rb_left = parent;
  279. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  280. if (tmp2)
  281. rb_set_parent(tmp2, parent);
  282. __rb_rotate_set_parents(parent, sibling, root,
  283. RB_BLACK);
  284. augment_rotate(parent, sibling);
  285. break;
  286. } else {
  287. sibling = parent->rb_left;
  288. if (rb_is_red(sibling)) {
  289. /* Case 1 - right rotate at parent */
  290. parent->rb_left = tmp1 = sibling->rb_right;
  291. sibling->rb_right = parent;
  292. rb_set_parent_color(tmp1, parent, RB_BLACK);
  293. __rb_rotate_set_parents(parent, sibling, root,
  294. RB_RED);
  295. augment_rotate(parent, sibling);
  296. sibling = tmp1;
  297. }
  298. tmp1 = sibling->rb_left;
  299. if (!tmp1 || rb_is_black(tmp1)) {
  300. tmp2 = sibling->rb_right;
  301. if (!tmp2 || rb_is_black(tmp2)) {
  302. /* Case 2 - sibling color flip */
  303. rb_set_parent_color(sibling, parent,
  304. RB_RED);
  305. if (rb_is_red(parent))
  306. rb_set_black(parent);
  307. else {
  308. node = parent;
  309. parent = rb_parent(node);
  310. if (parent)
  311. continue;
  312. }
  313. break;
  314. }
  315. /* Case 3 - right rotate at sibling */
  316. sibling->rb_right = tmp1 = tmp2->rb_left;
  317. tmp2->rb_left = sibling;
  318. parent->rb_left = tmp2;
  319. if (tmp1)
  320. rb_set_parent_color(tmp1, sibling,
  321. RB_BLACK);
  322. augment_rotate(sibling, tmp2);
  323. tmp1 = sibling;
  324. sibling = tmp2;
  325. }
  326. /* Case 4 - left rotate at parent + color flips */
  327. parent->rb_left = tmp2 = sibling->rb_right;
  328. sibling->rb_right = parent;
  329. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  330. if (tmp2)
  331. rb_set_parent(tmp2, parent);
  332. __rb_rotate_set_parents(parent, sibling, root,
  333. RB_BLACK);
  334. augment_rotate(parent, sibling);
  335. break;
  336. }
  337. }
  338. }
  339. EXPORT_SYMBOL(__rb_erase_color);
  340. /*
  341. * Non-augmented rbtree manipulation functions.
  342. *
  343. * We use dummy augmented callbacks here, and have the compiler optimize them
  344. * out of the rb_insert_color() and rb_erase() function definitions.
  345. */
  346. static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
  347. static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
  348. static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
  349. static const struct rb_augment_callbacks dummy_callbacks = {
  350. dummy_propagate, dummy_copy, dummy_rotate
  351. };
  352. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  353. {
  354. __rb_insert(node, root, dummy_rotate);
  355. }
  356. EXPORT_SYMBOL(rb_insert_color);
  357. void rb_erase(struct rb_node *node, struct rb_root *root)
  358. {
  359. rb_erase_augmented(node, root, &dummy_callbacks);
  360. }
  361. EXPORT_SYMBOL(rb_erase);
  362. /*
  363. * Augmented rbtree manipulation functions.
  364. *
  365. * This instantiates the same __always_inline functions as in the non-augmented
  366. * case, but this time with user-defined callbacks.
  367. */
  368. void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  369. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  370. {
  371. __rb_insert(node, root, augment_rotate);
  372. }
  373. EXPORT_SYMBOL(__rb_insert_augmented);
  374. /*
  375. * This function returns the first node (in sort order) of the tree.
  376. */
  377. struct rb_node *rb_first(const struct rb_root *root)
  378. {
  379. struct rb_node *n;
  380. n = root->rb_node;
  381. if (!n)
  382. return NULL;
  383. while (n->rb_left)
  384. n = n->rb_left;
  385. return n;
  386. }
  387. EXPORT_SYMBOL(rb_first);
  388. struct rb_node *rb_last(const struct rb_root *root)
  389. {
  390. struct rb_node *n;
  391. n = root->rb_node;
  392. if (!n)
  393. return NULL;
  394. while (n->rb_right)
  395. n = n->rb_right;
  396. return n;
  397. }
  398. EXPORT_SYMBOL(rb_last);
  399. struct rb_node *rb_next(const struct rb_node *node)
  400. {
  401. struct rb_node *parent;
  402. if (RB_EMPTY_NODE(node))
  403. return NULL;
  404. /*
  405. * If we have a right-hand child, go down and then left as far
  406. * as we can.
  407. */
  408. if (node->rb_right) {
  409. node = node->rb_right;
  410. while (node->rb_left)
  411. node=node->rb_left;
  412. return (struct rb_node *)node;
  413. }
  414. /*
  415. * No right-hand children. Everything down and left is smaller than us,
  416. * so any 'next' node must be in the general direction of our parent.
  417. * Go up the tree; any time the ancestor is a right-hand child of its
  418. * parent, keep going up. First time it's a left-hand child of its
  419. * parent, said parent is our 'next' node.
  420. */
  421. while ((parent = rb_parent(node)) && node == parent->rb_right)
  422. node = parent;
  423. return parent;
  424. }
  425. EXPORT_SYMBOL(rb_next);
  426. struct rb_node *rb_prev(const struct rb_node *node)
  427. {
  428. struct rb_node *parent;
  429. if (RB_EMPTY_NODE(node))
  430. return NULL;
  431. /*
  432. * If we have a left-hand child, go down and then right as far
  433. * as we can.
  434. */
  435. if (node->rb_left) {
  436. node = node->rb_left;
  437. while (node->rb_right)
  438. node=node->rb_right;
  439. return (struct rb_node *)node;
  440. }
  441. /*
  442. * No left-hand children. Go up till we find an ancestor which
  443. * is a right-hand child of its parent.
  444. */
  445. while ((parent = rb_parent(node)) && node == parent->rb_left)
  446. node = parent;
  447. return parent;
  448. }
  449. EXPORT_SYMBOL(rb_prev);
  450. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  451. struct rb_root *root)
  452. {
  453. struct rb_node *parent = rb_parent(victim);
  454. /* Set the surrounding nodes to point to the replacement */
  455. __rb_change_child(victim, new, parent, root);
  456. if (victim->rb_left)
  457. rb_set_parent(victim->rb_left, new);
  458. if (victim->rb_right)
  459. rb_set_parent(victim->rb_right, new);
  460. /* Copy the pointers/colour from the victim to the replacement */
  461. *new = *victim;
  462. }
  463. EXPORT_SYMBOL(rb_replace_node);