rbtree.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /*
  251. * This function returns the first node (in sort order) of the tree.
  252. */
  253. struct rb_node *rb_first(const struct rb_root *root)
  254. {
  255. struct rb_node *n;
  256. n = root->rb_node;
  257. if (!n)
  258. return NULL;
  259. while (n->rb_left)
  260. n = n->rb_left;
  261. return n;
  262. }
  263. EXPORT_SYMBOL(rb_first);
  264. struct rb_node *rb_last(const struct rb_root *root)
  265. {
  266. struct rb_node *n;
  267. n = root->rb_node;
  268. if (!n)
  269. return NULL;
  270. while (n->rb_right)
  271. n = n->rb_right;
  272. return n;
  273. }
  274. EXPORT_SYMBOL(rb_last);
  275. struct rb_node *rb_next(const struct rb_node *node)
  276. {
  277. struct rb_node *parent;
  278. if (rb_parent(node) == node)
  279. return NULL;
  280. /* If we have a right-hand child, go down and then left as far
  281. as we can. */
  282. if (node->rb_right) {
  283. node = node->rb_right;
  284. while (node->rb_left)
  285. node=node->rb_left;
  286. return (struct rb_node *)node;
  287. }
  288. /* No right-hand children. Everything down and left is
  289. smaller than us, so any 'next' node must be in the general
  290. direction of our parent. Go up the tree; any time the
  291. ancestor is a right-hand child of its parent, keep going
  292. up. First time it's a left-hand child of its parent, said
  293. parent is our 'next' node. */
  294. while ((parent = rb_parent(node)) && node == parent->rb_right)
  295. node = parent;
  296. return parent;
  297. }
  298. EXPORT_SYMBOL(rb_next);
  299. struct rb_node *rb_prev(const struct rb_node *node)
  300. {
  301. struct rb_node *parent;
  302. if (rb_parent(node) == node)
  303. return NULL;
  304. /* If we have a left-hand child, go down and then right as far
  305. as we can. */
  306. if (node->rb_left) {
  307. node = node->rb_left;
  308. while (node->rb_right)
  309. node=node->rb_right;
  310. return (struct rb_node *)node;
  311. }
  312. /* No left-hand children. Go up till we find an ancestor which
  313. is a right-hand child of its parent */
  314. while ((parent = rb_parent(node)) && node == parent->rb_left)
  315. node = parent;
  316. return parent;
  317. }
  318. EXPORT_SYMBOL(rb_prev);
  319. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  320. struct rb_root *root)
  321. {
  322. struct rb_node *parent = rb_parent(victim);
  323. /* Set the surrounding nodes to point to the replacement */
  324. if (parent) {
  325. if (victim == parent->rb_left)
  326. parent->rb_left = new;
  327. else
  328. parent->rb_right = new;
  329. } else {
  330. root->rb_node = new;
  331. }
  332. if (victim->rb_left)
  333. rb_set_parent(victim->rb_left, new);
  334. if (victim->rb_right)
  335. rb_set_parent(victim->rb_right, new);
  336. /* Copy the pointers/colour from the victim to the replacement */
  337. *new = *victim;
  338. }
  339. EXPORT_SYMBOL(rb_replace_node);