rbtree.c 8.4 KB

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