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. child = node->rb_right;
  209. parent = rb_parent(node);
  210. color = rb_color(node);
  211. if (child)
  212. rb_set_parent(child, parent);
  213. if (parent == old) {
  214. parent->rb_right = child;
  215. parent = node;
  216. } else
  217. parent->rb_left = child;
  218. node->rb_parent_color = old->rb_parent_color;
  219. node->rb_right = old->rb_right;
  220. node->rb_left = old->rb_left;
  221. if (rb_parent(old))
  222. {
  223. if (rb_parent(old)->rb_left == old)
  224. rb_parent(old)->rb_left = node;
  225. else
  226. rb_parent(old)->rb_right = node;
  227. } else
  228. root->rb_node = node;
  229. rb_set_parent(old->rb_left, node);
  230. if (old->rb_right)
  231. rb_set_parent(old->rb_right, node);
  232. goto color;
  233. }
  234. parent = rb_parent(node);
  235. color = rb_color(node);
  236. if (child)
  237. rb_set_parent(child, parent);
  238. if (parent)
  239. {
  240. if (parent->rb_left == node)
  241. parent->rb_left = child;
  242. else
  243. parent->rb_right = child;
  244. }
  245. else
  246. root->rb_node = child;
  247. color:
  248. if (color == RB_BLACK)
  249. __rb_erase_color(child, parent, root);
  250. }
  251. EXPORT_SYMBOL(rb_erase);
  252. /*
  253. * This function returns the first node (in sort order) of the tree.
  254. */
  255. struct rb_node *rb_first(const struct rb_root *root)
  256. {
  257. struct rb_node *n;
  258. n = root->rb_node;
  259. if (!n)
  260. return NULL;
  261. while (n->rb_left)
  262. n = n->rb_left;
  263. return n;
  264. }
  265. EXPORT_SYMBOL(rb_first);
  266. struct rb_node *rb_last(const struct rb_root *root)
  267. {
  268. struct rb_node *n;
  269. n = root->rb_node;
  270. if (!n)
  271. return NULL;
  272. while (n->rb_right)
  273. n = n->rb_right;
  274. return n;
  275. }
  276. EXPORT_SYMBOL(rb_last);
  277. struct rb_node *rb_next(const struct rb_node *node)
  278. {
  279. struct rb_node *parent;
  280. if (rb_parent(node) == node)
  281. return NULL;
  282. /* If we have a right-hand child, go down and then left as far
  283. as we can. */
  284. if (node->rb_right) {
  285. node = node->rb_right;
  286. while (node->rb_left)
  287. node=node->rb_left;
  288. return (struct rb_node *)node;
  289. }
  290. /* No right-hand children. Everything down and left is
  291. smaller than us, so any 'next' node must be in the general
  292. direction of our parent. Go up the tree; any time the
  293. ancestor is a right-hand child of its parent, keep going
  294. up. First time it's a left-hand child of its parent, said
  295. parent is our 'next' node. */
  296. while ((parent = rb_parent(node)) && node == parent->rb_right)
  297. node = parent;
  298. return parent;
  299. }
  300. EXPORT_SYMBOL(rb_next);
  301. struct rb_node *rb_prev(const struct rb_node *node)
  302. {
  303. struct rb_node *parent;
  304. if (rb_parent(node) == node)
  305. return NULL;
  306. /* If we have a left-hand child, go down and then right as far
  307. as we can. */
  308. if (node->rb_left) {
  309. node = node->rb_left;
  310. while (node->rb_right)
  311. node=node->rb_right;
  312. return (struct rb_node *)node;
  313. }
  314. /* No left-hand children. Go up till we find an ancestor which
  315. is a right-hand child of its parent */
  316. while ((parent = rb_parent(node)) && node == parent->rb_left)
  317. node = parent;
  318. return parent;
  319. }
  320. EXPORT_SYMBOL(rb_prev);
  321. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  322. struct rb_root *root)
  323. {
  324. struct rb_node *parent = rb_parent(victim);
  325. /* Set the surrounding nodes to point to the replacement */
  326. if (parent) {
  327. if (victim == parent->rb_left)
  328. parent->rb_left = new;
  329. else
  330. parent->rb_right = new;
  331. } else {
  332. root->rb_node = new;
  333. }
  334. if (victim->rb_left)
  335. rb_set_parent(victim->rb_left, new);
  336. if (victim->rb_right)
  337. rb_set_parent(victim->rb_right, new);
  338. /* Copy the pointers/colour from the victim to the replacement */
  339. *new = *victim;
  340. }
  341. EXPORT_SYMBOL(rb_replace_node);