rbtree.c 11 KB

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