rbtree.c 7.8 KB

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