rbtree.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. struct rb_node *o_left;
  144. if ((o_left = other->rb_left))
  145. rb_set_black(o_left);
  146. rb_set_red(other);
  147. __rb_rotate_right(other, root);
  148. other = parent->rb_right;
  149. }
  150. rb_set_color(other, rb_color(parent));
  151. rb_set_black(parent);
  152. if (other->rb_right)
  153. rb_set_black(other->rb_right);
  154. __rb_rotate_left(parent, root);
  155. node = root->rb_node;
  156. break;
  157. }
  158. }
  159. else
  160. {
  161. other = parent->rb_left;
  162. if (rb_is_red(other))
  163. {
  164. rb_set_black(other);
  165. rb_set_red(parent);
  166. __rb_rotate_right(parent, root);
  167. other = parent->rb_left;
  168. }
  169. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  170. (!other->rb_right || rb_is_black(other->rb_right)))
  171. {
  172. rb_set_red(other);
  173. node = parent;
  174. parent = rb_parent(node);
  175. }
  176. else
  177. {
  178. if (!other->rb_left || rb_is_black(other->rb_left))
  179. {
  180. register struct rb_node *o_right;
  181. if ((o_right = other->rb_right))
  182. rb_set_black(o_right);
  183. rb_set_red(other);
  184. __rb_rotate_left(other, root);
  185. other = parent->rb_left;
  186. }
  187. rb_set_color(other, rb_color(parent));
  188. rb_set_black(parent);
  189. if (other->rb_left)
  190. rb_set_black(other->rb_left);
  191. __rb_rotate_right(parent, root);
  192. node = root->rb_node;
  193. break;
  194. }
  195. }
  196. }
  197. if (node)
  198. rb_set_black(node);
  199. }
  200. void rb_erase(struct rb_node *node, struct rb_root *root)
  201. {
  202. struct rb_node *child, *parent;
  203. int color;
  204. if (!node->rb_left)
  205. child = node->rb_right;
  206. else if (!node->rb_right)
  207. child = node->rb_left;
  208. else
  209. {
  210. struct rb_node *old = node, *left;
  211. node = node->rb_right;
  212. while ((left = node->rb_left) != NULL)
  213. node = left;
  214. child = node->rb_right;
  215. parent = rb_parent(node);
  216. color = rb_color(node);
  217. if (child)
  218. rb_set_parent(child, parent);
  219. if (parent == old) {
  220. parent->rb_right = child;
  221. parent = node;
  222. } else
  223. parent->rb_left = child;
  224. node->rb_parent_color = old->rb_parent_color;
  225. node->rb_right = old->rb_right;
  226. node->rb_left = old->rb_left;
  227. if (rb_parent(old))
  228. {
  229. if (rb_parent(old)->rb_left == old)
  230. rb_parent(old)->rb_left = node;
  231. else
  232. rb_parent(old)->rb_right = node;
  233. } else
  234. root->rb_node = node;
  235. rb_set_parent(old->rb_left, node);
  236. if (old->rb_right)
  237. rb_set_parent(old->rb_right, node);
  238. goto color;
  239. }
  240. parent = rb_parent(node);
  241. color = rb_color(node);
  242. if (child)
  243. rb_set_parent(child, parent);
  244. if (parent)
  245. {
  246. if (parent->rb_left == node)
  247. parent->rb_left = child;
  248. else
  249. parent->rb_right = child;
  250. }
  251. else
  252. root->rb_node = child;
  253. color:
  254. if (color == RB_BLACK)
  255. __rb_erase_color(child, parent, root);
  256. }
  257. EXPORT_SYMBOL(rb_erase);
  258. /*
  259. * This function returns the first node (in sort order) of the tree.
  260. */
  261. struct rb_node *rb_first(struct rb_root *root)
  262. {
  263. struct rb_node *n;
  264. n = root->rb_node;
  265. if (!n)
  266. return NULL;
  267. while (n->rb_left)
  268. n = n->rb_left;
  269. return n;
  270. }
  271. EXPORT_SYMBOL(rb_first);
  272. struct rb_node *rb_last(struct rb_root *root)
  273. {
  274. struct rb_node *n;
  275. n = root->rb_node;
  276. if (!n)
  277. return NULL;
  278. while (n->rb_right)
  279. n = n->rb_right;
  280. return n;
  281. }
  282. EXPORT_SYMBOL(rb_last);
  283. struct rb_node *rb_next(struct rb_node *node)
  284. {
  285. struct rb_node *parent;
  286. if (rb_parent(node) == node)
  287. return NULL;
  288. /* If we have a right-hand child, go down and then left as far
  289. as we can. */
  290. if (node->rb_right) {
  291. node = node->rb_right;
  292. while (node->rb_left)
  293. node=node->rb_left;
  294. return node;
  295. }
  296. /* No right-hand children. Everything down and left is
  297. smaller than us, so any 'next' node must be in the general
  298. direction of our parent. Go up the tree; any time the
  299. ancestor is a right-hand child of its parent, keep going
  300. up. First time it's a left-hand child of its parent, said
  301. parent is our 'next' node. */
  302. while ((parent = rb_parent(node)) && node == parent->rb_right)
  303. node = parent;
  304. return parent;
  305. }
  306. EXPORT_SYMBOL(rb_next);
  307. struct rb_node *rb_prev(struct rb_node *node)
  308. {
  309. struct rb_node *parent;
  310. if (rb_parent(node) == node)
  311. return NULL;
  312. /* If we have a left-hand child, go down and then right as far
  313. as we can. */
  314. if (node->rb_left) {
  315. node = node->rb_left;
  316. while (node->rb_right)
  317. node=node->rb_right;
  318. return node;
  319. }
  320. /* No left-hand children. Go up till we find an ancestor which
  321. is a right-hand child of its parent */
  322. while ((parent = rb_parent(node)) && node == parent->rb_left)
  323. node = parent;
  324. return parent;
  325. }
  326. EXPORT_SYMBOL(rb_prev);
  327. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  328. struct rb_root *root)
  329. {
  330. struct rb_node *parent = rb_parent(victim);
  331. /* Set the surrounding nodes to point to the replacement */
  332. if (parent) {
  333. if (victim == parent->rb_left)
  334. parent->rb_left = new;
  335. else
  336. parent->rb_right = new;
  337. } else {
  338. root->rb_node = new;
  339. }
  340. if (victim->rb_left)
  341. rb_set_parent(victim->rb_left, new);
  342. if (victim->rb_right)
  343. rb_set_parent(victim->rb_right, new);
  344. /* Copy the pointers/colour from the victim to the replacement */
  345. *new = *victim;
  346. }
  347. EXPORT_SYMBOL(rb_replace_node);