rbtree.c 8.9 KB

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