rbtree.c 9.3 KB

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