rbtree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/module.h>
  20. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  21. {
  22. struct rb_node *right = node->rb_right;
  23. struct rb_node *parent = rb_parent(node);
  24. if ((node->rb_right = right->rb_left))
  25. rb_set_parent(right->rb_left, node);
  26. right->rb_left = node;
  27. rb_set_parent(right, parent);
  28. if (parent)
  29. {
  30. if (node == parent->rb_left)
  31. parent->rb_left = right;
  32. else
  33. parent->rb_right = right;
  34. }
  35. else
  36. root->rb_node = right;
  37. rb_set_parent(node, right);
  38. }
  39. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  40. {
  41. struct rb_node *left = node->rb_left;
  42. struct rb_node *parent = rb_parent(node);
  43. if ((node->rb_left = left->rb_right))
  44. rb_set_parent(left->rb_right, node);
  45. left->rb_right = node;
  46. rb_set_parent(left, parent);
  47. if (parent)
  48. {
  49. if (node == parent->rb_right)
  50. parent->rb_right = left;
  51. else
  52. parent->rb_left = left;
  53. }
  54. else
  55. root->rb_node = left;
  56. rb_set_parent(node, left);
  57. }
  58. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  59. {
  60. struct rb_node *parent, *gparent;
  61. while ((parent = rb_parent(node)) && rb_is_red(parent))
  62. {
  63. gparent = rb_parent(parent);
  64. if (parent == gparent->rb_left)
  65. {
  66. {
  67. register struct rb_node *uncle = gparent->rb_right;
  68. if (uncle && rb_is_red(uncle))
  69. {
  70. rb_set_black(uncle);
  71. rb_set_black(parent);
  72. rb_set_red(gparent);
  73. node = gparent;
  74. continue;
  75. }
  76. }
  77. if (parent->rb_right == node)
  78. {
  79. register struct rb_node *tmp;
  80. __rb_rotate_left(parent, root);
  81. tmp = parent;
  82. parent = node;
  83. node = tmp;
  84. }
  85. rb_set_black(parent);
  86. rb_set_red(gparent);
  87. __rb_rotate_right(gparent, root);
  88. } else {
  89. {
  90. register struct rb_node *uncle = gparent->rb_left;
  91. if (uncle && rb_is_red(uncle))
  92. {
  93. rb_set_black(uncle);
  94. rb_set_black(parent);
  95. rb_set_red(gparent);
  96. node = gparent;
  97. continue;
  98. }
  99. }
  100. if (parent->rb_left == node)
  101. {
  102. register struct rb_node *tmp;
  103. __rb_rotate_right(parent, root);
  104. tmp = parent;
  105. parent = node;
  106. node = tmp;
  107. }
  108. rb_set_black(parent);
  109. rb_set_red(gparent);
  110. __rb_rotate_left(gparent, root);
  111. }
  112. }
  113. rb_set_black(root->rb_node);
  114. }
  115. EXPORT_SYMBOL(rb_insert_color);
  116. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  117. struct rb_root *root)
  118. {
  119. struct rb_node *other;
  120. while ((!node || rb_is_black(node)) && node != root->rb_node)
  121. {
  122. if (parent->rb_left == node)
  123. {
  124. other = parent->rb_right;
  125. if (rb_is_red(other))
  126. {
  127. rb_set_black(other);
  128. rb_set_red(parent);
  129. __rb_rotate_left(parent, root);
  130. other = parent->rb_right;
  131. }
  132. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  133. (!other->rb_right || rb_is_black(other->rb_right)))
  134. {
  135. rb_set_red(other);
  136. node = parent;
  137. parent = rb_parent(node);
  138. }
  139. else
  140. {
  141. if (!other->rb_right || rb_is_black(other->rb_right))
  142. {
  143. rb_set_black(other->rb_left);
  144. rb_set_red(other);
  145. __rb_rotate_right(other, root);
  146. other = parent->rb_right;
  147. }
  148. rb_set_color(other, rb_color(parent));
  149. rb_set_black(parent);
  150. rb_set_black(other->rb_right);
  151. __rb_rotate_left(parent, root);
  152. node = root->rb_node;
  153. break;
  154. }
  155. }
  156. else
  157. {
  158. other = parent->rb_left;
  159. if (rb_is_red(other))
  160. {
  161. rb_set_black(other);
  162. rb_set_red(parent);
  163. __rb_rotate_right(parent, root);
  164. other = parent->rb_left;
  165. }
  166. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  167. (!other->rb_right || rb_is_black(other->rb_right)))
  168. {
  169. rb_set_red(other);
  170. node = parent;
  171. parent = rb_parent(node);
  172. }
  173. else
  174. {
  175. if (!other->rb_left || rb_is_black(other->rb_left))
  176. {
  177. rb_set_black(other->rb_right);
  178. rb_set_red(other);
  179. __rb_rotate_left(other, root);
  180. other = parent->rb_left;
  181. }
  182. rb_set_color(other, rb_color(parent));
  183. rb_set_black(parent);
  184. rb_set_black(other->rb_left);
  185. __rb_rotate_right(parent, root);
  186. node = root->rb_node;
  187. break;
  188. }
  189. }
  190. }
  191. if (node)
  192. rb_set_black(node);
  193. }
  194. void rb_erase(struct rb_node *node, struct rb_root *root)
  195. {
  196. struct rb_node *child, *parent;
  197. int color;
  198. if (!node->rb_left)
  199. child = node->rb_right;
  200. else if (!node->rb_right)
  201. child = node->rb_left;
  202. else
  203. {
  204. struct rb_node *old = node, *left;
  205. node = node->rb_right;
  206. while ((left = node->rb_left) != NULL)
  207. node = left;
  208. if (rb_parent(old)) {
  209. if (rb_parent(old)->rb_left == old)
  210. rb_parent(old)->rb_left = node;
  211. else
  212. rb_parent(old)->rb_right = node;
  213. } else
  214. root->rb_node = node;
  215. child = node->rb_right;
  216. parent = rb_parent(node);
  217. color = rb_color(node);
  218. if (parent == old) {
  219. parent = node;
  220. } else {
  221. if (child)
  222. rb_set_parent(child, parent);
  223. parent->rb_left = child;
  224. node->rb_right = old->rb_right;
  225. rb_set_parent(old->rb_right, node);
  226. }
  227. node->rb_parent_color = old->rb_parent_color;
  228. node->rb_left = old->rb_left;
  229. rb_set_parent(old->rb_left, node);
  230. goto color;
  231. }
  232. parent = rb_parent(node);
  233. color = rb_color(node);
  234. if (child)
  235. rb_set_parent(child, parent);
  236. if (parent)
  237. {
  238. if (parent->rb_left == node)
  239. parent->rb_left = child;
  240. else
  241. parent->rb_right = child;
  242. }
  243. else
  244. root->rb_node = child;
  245. color:
  246. if (color == RB_BLACK)
  247. __rb_erase_color(child, parent, root);
  248. }
  249. EXPORT_SYMBOL(rb_erase);
  250. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  251. {
  252. struct rb_node *parent;
  253. up:
  254. func(node, data);
  255. parent = rb_parent(node);
  256. if (!parent)
  257. return;
  258. if (node == parent->rb_left && parent->rb_right)
  259. func(parent->rb_right, data);
  260. else if (parent->rb_left)
  261. func(parent->rb_left, data);
  262. node = parent;
  263. goto up;
  264. }
  265. /*
  266. * after inserting @node into the tree, update the tree to account for
  267. * both the new entry and any damage done by rebalance
  268. */
  269. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  270. {
  271. if (node->rb_left)
  272. node = node->rb_left;
  273. else if (node->rb_right)
  274. node = node->rb_right;
  275. rb_augment_path(node, func, data);
  276. }
  277. EXPORT_SYMBOL(rb_augment_insert);
  278. /*
  279. * before removing the node, find the deepest node on the rebalance path
  280. * that will still be there after @node gets removed
  281. */
  282. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  283. {
  284. struct rb_node *deepest;
  285. if (!node->rb_right && !node->rb_left)
  286. deepest = rb_parent(node);
  287. else if (!node->rb_right)
  288. deepest = node->rb_left;
  289. else if (!node->rb_left)
  290. deepest = node->rb_right;
  291. else {
  292. deepest = rb_next(node);
  293. if (deepest->rb_right)
  294. deepest = deepest->rb_right;
  295. else if (rb_parent(deepest) != node)
  296. deepest = rb_parent(deepest);
  297. }
  298. return deepest;
  299. }
  300. EXPORT_SYMBOL(rb_augment_erase_begin);
  301. /*
  302. * after removal, update the tree to account for the removed entry
  303. * and any rebalance damage.
  304. */
  305. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  306. {
  307. if (node)
  308. rb_augment_path(node, func, data);
  309. }
  310. EXPORT_SYMBOL(rb_augment_erase_end);
  311. /*
  312. * This function returns the first node (in sort order) of the tree.
  313. */
  314. struct rb_node *rb_first(const struct rb_root *root)
  315. {
  316. struct rb_node *n;
  317. n = root->rb_node;
  318. if (!n)
  319. return NULL;
  320. while (n->rb_left)
  321. n = n->rb_left;
  322. return n;
  323. }
  324. EXPORT_SYMBOL(rb_first);
  325. struct rb_node *rb_last(const struct rb_root *root)
  326. {
  327. struct rb_node *n;
  328. n = root->rb_node;
  329. if (!n)
  330. return NULL;
  331. while (n->rb_right)
  332. n = n->rb_right;
  333. return n;
  334. }
  335. EXPORT_SYMBOL(rb_last);
  336. struct rb_node *rb_next(const struct rb_node *node)
  337. {
  338. struct rb_node *parent;
  339. if (rb_parent(node) == node)
  340. return NULL;
  341. /* If we have a right-hand child, go down and then left as far
  342. as we can. */
  343. if (node->rb_right) {
  344. node = node->rb_right;
  345. while (node->rb_left)
  346. node=node->rb_left;
  347. return (struct rb_node *)node;
  348. }
  349. /* No right-hand children. Everything down and left is
  350. smaller than us, so any 'next' node must be in the general
  351. direction of our parent. Go up the tree; any time the
  352. ancestor is a right-hand child of its parent, keep going
  353. up. First time it's a left-hand child of its parent, said
  354. parent is our 'next' node. */
  355. while ((parent = rb_parent(node)) && node == parent->rb_right)
  356. node = parent;
  357. return parent;
  358. }
  359. EXPORT_SYMBOL(rb_next);
  360. struct rb_node *rb_prev(const struct rb_node *node)
  361. {
  362. struct rb_node *parent;
  363. if (rb_parent(node) == node)
  364. return NULL;
  365. /* If we have a left-hand child, go down and then right as far
  366. as we can. */
  367. if (node->rb_left) {
  368. node = node->rb_left;
  369. while (node->rb_right)
  370. node=node->rb_right;
  371. return (struct rb_node *)node;
  372. }
  373. /* No left-hand children. Go up till we find an ancestor which
  374. is a right-hand child of its parent */
  375. while ((parent = rb_parent(node)) && node == parent->rb_left)
  376. node = parent;
  377. return parent;
  378. }
  379. EXPORT_SYMBOL(rb_prev);
  380. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  381. struct rb_root *root)
  382. {
  383. struct rb_node *parent = rb_parent(victim);
  384. /* Set the surrounding nodes to point to the replacement */
  385. if (parent) {
  386. if (victim == parent->rb_left)
  387. parent->rb_left = new;
  388. else
  389. parent->rb_right = new;
  390. } else {
  391. root->rb_node = new;
  392. }
  393. if (victim->rb_left)
  394. rb_set_parent(victim->rb_left, new);
  395. if (victim->rb_right)
  396. rb_set_parent(victim->rb_right, new);
  397. /* Copy the pointers/colour from the victim to the replacement */
  398. *new = *victim;
  399. }
  400. EXPORT_SYMBOL(rb_replace_node);