rbtree.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "rbtree.h"
  19. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  20. {
  21. struct rb_node *right = node->rb_right;
  22. struct rb_node *parent = rb_parent(node);
  23. if ((node->rb_right = right->rb_left))
  24. rb_set_parent(right->rb_left, node);
  25. right->rb_left = node;
  26. rb_set_parent(right, parent);
  27. if (parent)
  28. {
  29. if (node == parent->rb_left)
  30. parent->rb_left = right;
  31. else
  32. parent->rb_right = right;
  33. }
  34. else
  35. root->rb_node = right;
  36. rb_set_parent(node, right);
  37. }
  38. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  39. {
  40. struct rb_node *left = node->rb_left;
  41. struct rb_node *parent = rb_parent(node);
  42. if ((node->rb_left = left->rb_right))
  43. rb_set_parent(left->rb_right, node);
  44. left->rb_right = node;
  45. rb_set_parent(left, parent);
  46. if (parent)
  47. {
  48. if (node == parent->rb_right)
  49. parent->rb_right = left;
  50. else
  51. parent->rb_left = left;
  52. }
  53. else
  54. root->rb_node = left;
  55. rb_set_parent(node, left);
  56. }
  57. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  58. {
  59. struct rb_node *parent, *gparent;
  60. while ((parent = rb_parent(node)) && rb_is_red(parent))
  61. {
  62. gparent = rb_parent(parent);
  63. if (parent == gparent->rb_left)
  64. {
  65. {
  66. register struct rb_node *uncle = gparent->rb_right;
  67. if (uncle && rb_is_red(uncle))
  68. {
  69. rb_set_black(uncle);
  70. rb_set_black(parent);
  71. rb_set_red(gparent);
  72. node = gparent;
  73. continue;
  74. }
  75. }
  76. if (parent->rb_right == node)
  77. {
  78. register struct rb_node *tmp;
  79. __rb_rotate_left(parent, root);
  80. tmp = parent;
  81. parent = node;
  82. node = tmp;
  83. }
  84. rb_set_black(parent);
  85. rb_set_red(gparent);
  86. __rb_rotate_right(gparent, root);
  87. } else {
  88. {
  89. register struct rb_node *uncle = gparent->rb_left;
  90. if (uncle && rb_is_red(uncle))
  91. {
  92. rb_set_black(uncle);
  93. rb_set_black(parent);
  94. rb_set_red(gparent);
  95. node = gparent;
  96. continue;
  97. }
  98. }
  99. if (parent->rb_left == node)
  100. {
  101. register struct rb_node *tmp;
  102. __rb_rotate_right(parent, root);
  103. tmp = parent;
  104. parent = node;
  105. node = tmp;
  106. }
  107. rb_set_black(parent);
  108. rb_set_red(gparent);
  109. __rb_rotate_left(gparent, root);
  110. }
  111. }
  112. rb_set_black(root->rb_node);
  113. }
  114. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  115. struct rb_root *root)
  116. {
  117. struct rb_node *other;
  118. while ((!node || rb_is_black(node)) && node != root->rb_node)
  119. {
  120. if (parent->rb_left == node)
  121. {
  122. other = parent->rb_right;
  123. if (rb_is_red(other))
  124. {
  125. rb_set_black(other);
  126. rb_set_red(parent);
  127. __rb_rotate_left(parent, root);
  128. other = parent->rb_right;
  129. }
  130. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  131. (!other->rb_right || rb_is_black(other->rb_right)))
  132. {
  133. rb_set_red(other);
  134. node = parent;
  135. parent = rb_parent(node);
  136. }
  137. else
  138. {
  139. if (!other->rb_right || rb_is_black(other->rb_right))
  140. {
  141. rb_set_black(other->rb_left);
  142. rb_set_red(other);
  143. __rb_rotate_right(other, root);
  144. other = parent->rb_right;
  145. }
  146. rb_set_color(other, rb_color(parent));
  147. rb_set_black(parent);
  148. rb_set_black(other->rb_right);
  149. __rb_rotate_left(parent, root);
  150. node = root->rb_node;
  151. break;
  152. }
  153. }
  154. else
  155. {
  156. other = parent->rb_left;
  157. if (rb_is_red(other))
  158. {
  159. rb_set_black(other);
  160. rb_set_red(parent);
  161. __rb_rotate_right(parent, root);
  162. other = parent->rb_left;
  163. }
  164. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  165. (!other->rb_right || rb_is_black(other->rb_right)))
  166. {
  167. rb_set_red(other);
  168. node = parent;
  169. parent = rb_parent(node);
  170. }
  171. else
  172. {
  173. if (!other->rb_left || rb_is_black(other->rb_left))
  174. {
  175. rb_set_black(other->rb_right);
  176. rb_set_red(other);
  177. __rb_rotate_left(other, root);
  178. other = parent->rb_left;
  179. }
  180. rb_set_color(other, rb_color(parent));
  181. rb_set_black(parent);
  182. rb_set_black(other->rb_left);
  183. __rb_rotate_right(parent, root);
  184. node = root->rb_node;
  185. break;
  186. }
  187. }
  188. }
  189. if (node)
  190. rb_set_black(node);
  191. }
  192. void rb_erase(struct rb_node *node, struct rb_root *root)
  193. {
  194. struct rb_node *child, *parent;
  195. int color;
  196. if (!node->rb_left)
  197. child = node->rb_right;
  198. else if (!node->rb_right)
  199. child = node->rb_left;
  200. else
  201. {
  202. struct rb_node *old = node, *left;
  203. node = node->rb_right;
  204. while ((left = node->rb_left) != NULL)
  205. node = left;
  206. child = node->rb_right;
  207. parent = rb_parent(node);
  208. color = rb_color(node);
  209. if (child)
  210. rb_set_parent(child, parent);
  211. if (parent == old) {
  212. parent->rb_right = child;
  213. parent = node;
  214. } else
  215. parent->rb_left = child;
  216. node->rb_parent_color = old->rb_parent_color;
  217. node->rb_right = old->rb_right;
  218. node->rb_left = old->rb_left;
  219. if (rb_parent(old))
  220. {
  221. if (rb_parent(old)->rb_left == old)
  222. rb_parent(old)->rb_left = node;
  223. else
  224. rb_parent(old)->rb_right = node;
  225. } else
  226. root->rb_node = node;
  227. rb_set_parent(old->rb_left, node);
  228. if (old->rb_right)
  229. rb_set_parent(old->rb_right, 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. /*
  250. * This function returns the first node (in sort order) of the tree.
  251. */
  252. struct rb_node *rb_first(const struct rb_root *root)
  253. {
  254. struct rb_node *n;
  255. n = root->rb_node;
  256. if (!n)
  257. return NULL;
  258. while (n->rb_left)
  259. n = n->rb_left;
  260. return n;
  261. }
  262. struct rb_node *rb_last(const struct rb_root *root)
  263. {
  264. struct rb_node *n;
  265. n = root->rb_node;
  266. if (!n)
  267. return NULL;
  268. while (n->rb_right)
  269. n = n->rb_right;
  270. return n;
  271. }
  272. struct rb_node *rb_next(const struct rb_node *node)
  273. {
  274. struct rb_node *parent;
  275. if (rb_parent(node) == node)
  276. return NULL;
  277. /* If we have a right-hand child, go down and then left as far
  278. as we can. */
  279. if (node->rb_right) {
  280. node = node->rb_right;
  281. while (node->rb_left)
  282. node=node->rb_left;
  283. return (struct rb_node *)node;
  284. }
  285. /* No right-hand children. Everything down and left is
  286. smaller than us, so any 'next' node must be in the general
  287. direction of our parent. Go up the tree; any time the
  288. ancestor is a right-hand child of its parent, keep going
  289. up. First time it's a left-hand child of its parent, said
  290. parent is our 'next' node. */
  291. while ((parent = rb_parent(node)) && node == parent->rb_right)
  292. node = parent;
  293. return parent;
  294. }
  295. struct rb_node *rb_prev(const struct rb_node *node)
  296. {
  297. struct rb_node *parent;
  298. if (rb_parent(node) == node)
  299. return NULL;
  300. /* If we have a left-hand child, go down and then right as far
  301. as we can. */
  302. if (node->rb_left) {
  303. node = node->rb_left;
  304. while (node->rb_right)
  305. node=node->rb_right;
  306. return (struct rb_node *)node;
  307. }
  308. /* No left-hand children. Go up till we find an ancestor which
  309. is a right-hand child of its parent */
  310. while ((parent = rb_parent(node)) && node == parent->rb_left)
  311. node = parent;
  312. return parent;
  313. }
  314. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  315. struct rb_root *root)
  316. {
  317. struct rb_node *parent = rb_parent(victim);
  318. /* Set the surrounding nodes to point to the replacement */
  319. if (parent) {
  320. if (victim == parent->rb_left)
  321. parent->rb_left = new;
  322. else
  323. parent->rb_right = new;
  324. } else {
  325. root->rb_node = new;
  326. }
  327. if (victim->rb_left)
  328. rb_set_parent(victim->rb_left, new);
  329. if (victim->rb_right)
  330. rb_set_parent(victim->rb_right, new);
  331. /* Copy the pointers/colour from the victim to the replacement */
  332. *new = *victim;
  333. }