rbtree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/export.h>
  20. #define RB_RED 0
  21. #define RB_BLACK 1
  22. #define rb_color(r) ((r)->__rb_parent_color & 1)
  23. #define rb_is_red(r) (!rb_color(r))
  24. #define rb_is_black(r) rb_color(r)
  25. #define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0)
  26. #define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0)
  27. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  28. {
  29. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  30. }
  31. static inline void rb_set_color(struct rb_node *rb, int color)
  32. {
  33. rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
  34. }
  35. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  36. {
  37. struct rb_node *right = node->rb_right;
  38. struct rb_node *parent = rb_parent(node);
  39. if ((node->rb_right = right->rb_left))
  40. rb_set_parent(right->rb_left, node);
  41. right->rb_left = node;
  42. rb_set_parent(right, parent);
  43. if (parent)
  44. {
  45. if (node == parent->rb_left)
  46. parent->rb_left = right;
  47. else
  48. parent->rb_right = right;
  49. }
  50. else
  51. root->rb_node = right;
  52. rb_set_parent(node, right);
  53. }
  54. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  55. {
  56. struct rb_node *left = node->rb_left;
  57. struct rb_node *parent = rb_parent(node);
  58. if ((node->rb_left = left->rb_right))
  59. rb_set_parent(left->rb_right, node);
  60. left->rb_right = node;
  61. rb_set_parent(left, parent);
  62. if (parent)
  63. {
  64. if (node == parent->rb_right)
  65. parent->rb_right = left;
  66. else
  67. parent->rb_left = left;
  68. }
  69. else
  70. root->rb_node = left;
  71. rb_set_parent(node, left);
  72. }
  73. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  74. {
  75. struct rb_node *parent, *gparent;
  76. while ((parent = rb_parent(node)) && rb_is_red(parent))
  77. {
  78. gparent = rb_parent(parent);
  79. if (parent == gparent->rb_left)
  80. {
  81. {
  82. register struct rb_node *uncle = gparent->rb_right;
  83. if (uncle && rb_is_red(uncle))
  84. {
  85. rb_set_black(uncle);
  86. rb_set_black(parent);
  87. rb_set_red(gparent);
  88. node = gparent;
  89. continue;
  90. }
  91. }
  92. if (parent->rb_right == node) {
  93. __rb_rotate_left(parent, root);
  94. parent = node;
  95. }
  96. rb_set_black(parent);
  97. rb_set_red(gparent);
  98. __rb_rotate_right(gparent, root);
  99. break;
  100. } else {
  101. {
  102. register struct rb_node *uncle = gparent->rb_left;
  103. if (uncle && rb_is_red(uncle))
  104. {
  105. rb_set_black(uncle);
  106. rb_set_black(parent);
  107. rb_set_red(gparent);
  108. node = gparent;
  109. continue;
  110. }
  111. }
  112. if (parent->rb_left == node) {
  113. __rb_rotate_right(parent, root);
  114. parent = node;
  115. }
  116. rb_set_black(parent);
  117. rb_set_red(gparent);
  118. __rb_rotate_left(gparent, root);
  119. break;
  120. }
  121. }
  122. rb_set_black(root->rb_node);
  123. }
  124. EXPORT_SYMBOL(rb_insert_color);
  125. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  126. struct rb_root *root)
  127. {
  128. struct rb_node *other;
  129. while ((!node || rb_is_black(node)) && node != root->rb_node)
  130. {
  131. if (parent->rb_left == node)
  132. {
  133. other = parent->rb_right;
  134. if (rb_is_red(other))
  135. {
  136. rb_set_black(other);
  137. rb_set_red(parent);
  138. __rb_rotate_left(parent, root);
  139. other = parent->rb_right;
  140. }
  141. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  142. (!other->rb_right || rb_is_black(other->rb_right)))
  143. {
  144. rb_set_red(other);
  145. node = parent;
  146. parent = rb_parent(node);
  147. }
  148. else
  149. {
  150. if (!other->rb_right || rb_is_black(other->rb_right))
  151. {
  152. rb_set_black(other->rb_left);
  153. rb_set_red(other);
  154. __rb_rotate_right(other, root);
  155. other = parent->rb_right;
  156. }
  157. rb_set_color(other, rb_color(parent));
  158. rb_set_black(parent);
  159. rb_set_black(other->rb_right);
  160. __rb_rotate_left(parent, root);
  161. node = root->rb_node;
  162. break;
  163. }
  164. }
  165. else
  166. {
  167. other = parent->rb_left;
  168. if (rb_is_red(other))
  169. {
  170. rb_set_black(other);
  171. rb_set_red(parent);
  172. __rb_rotate_right(parent, root);
  173. other = parent->rb_left;
  174. }
  175. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  176. (!other->rb_right || rb_is_black(other->rb_right)))
  177. {
  178. rb_set_red(other);
  179. node = parent;
  180. parent = rb_parent(node);
  181. }
  182. else
  183. {
  184. if (!other->rb_left || rb_is_black(other->rb_left))
  185. {
  186. rb_set_black(other->rb_right);
  187. rb_set_red(other);
  188. __rb_rotate_left(other, root);
  189. other = parent->rb_left;
  190. }
  191. rb_set_color(other, rb_color(parent));
  192. rb_set_black(parent);
  193. rb_set_black(other->rb_left);
  194. __rb_rotate_right(parent, root);
  195. node = root->rb_node;
  196. break;
  197. }
  198. }
  199. }
  200. if (node)
  201. rb_set_black(node);
  202. }
  203. void rb_erase(struct rb_node *node, struct rb_root *root)
  204. {
  205. struct rb_node *child, *parent;
  206. int color;
  207. if (!node->rb_left)
  208. child = node->rb_right;
  209. else if (!node->rb_right)
  210. child = node->rb_left;
  211. else
  212. {
  213. struct rb_node *old = node, *left;
  214. node = node->rb_right;
  215. while ((left = node->rb_left) != NULL)
  216. node = left;
  217. if (rb_parent(old)) {
  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. child = node->rb_right;
  225. parent = rb_parent(node);
  226. color = rb_color(node);
  227. if (parent == old) {
  228. parent = node;
  229. } else {
  230. if (child)
  231. rb_set_parent(child, parent);
  232. parent->rb_left = child;
  233. node->rb_right = old->rb_right;
  234. rb_set_parent(old->rb_right, node);
  235. }
  236. node->__rb_parent_color = old->__rb_parent_color;
  237. node->rb_left = old->rb_left;
  238. rb_set_parent(old->rb_left, node);
  239. goto color;
  240. }
  241. parent = rb_parent(node);
  242. color = rb_color(node);
  243. if (child)
  244. rb_set_parent(child, parent);
  245. if (parent)
  246. {
  247. if (parent->rb_left == node)
  248. parent->rb_left = child;
  249. else
  250. parent->rb_right = child;
  251. }
  252. else
  253. root->rb_node = child;
  254. color:
  255. if (color == RB_BLACK)
  256. __rb_erase_color(child, parent, root);
  257. }
  258. EXPORT_SYMBOL(rb_erase);
  259. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  260. {
  261. struct rb_node *parent;
  262. up:
  263. func(node, data);
  264. parent = rb_parent(node);
  265. if (!parent)
  266. return;
  267. if (node == parent->rb_left && parent->rb_right)
  268. func(parent->rb_right, data);
  269. else if (parent->rb_left)
  270. func(parent->rb_left, data);
  271. node = parent;
  272. goto up;
  273. }
  274. /*
  275. * after inserting @node into the tree, update the tree to account for
  276. * both the new entry and any damage done by rebalance
  277. */
  278. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  279. {
  280. if (node->rb_left)
  281. node = node->rb_left;
  282. else if (node->rb_right)
  283. node = node->rb_right;
  284. rb_augment_path(node, func, data);
  285. }
  286. EXPORT_SYMBOL(rb_augment_insert);
  287. /*
  288. * before removing the node, find the deepest node on the rebalance path
  289. * that will still be there after @node gets removed
  290. */
  291. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  292. {
  293. struct rb_node *deepest;
  294. if (!node->rb_right && !node->rb_left)
  295. deepest = rb_parent(node);
  296. else if (!node->rb_right)
  297. deepest = node->rb_left;
  298. else if (!node->rb_left)
  299. deepest = node->rb_right;
  300. else {
  301. deepest = rb_next(node);
  302. if (deepest->rb_right)
  303. deepest = deepest->rb_right;
  304. else if (rb_parent(deepest) != node)
  305. deepest = rb_parent(deepest);
  306. }
  307. return deepest;
  308. }
  309. EXPORT_SYMBOL(rb_augment_erase_begin);
  310. /*
  311. * after removal, update the tree to account for the removed entry
  312. * and any rebalance damage.
  313. */
  314. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  315. {
  316. if (node)
  317. rb_augment_path(node, func, data);
  318. }
  319. EXPORT_SYMBOL(rb_augment_erase_end);
  320. /*
  321. * This function returns the first node (in sort order) of the tree.
  322. */
  323. struct rb_node *rb_first(const struct rb_root *root)
  324. {
  325. struct rb_node *n;
  326. n = root->rb_node;
  327. if (!n)
  328. return NULL;
  329. while (n->rb_left)
  330. n = n->rb_left;
  331. return n;
  332. }
  333. EXPORT_SYMBOL(rb_first);
  334. struct rb_node *rb_last(const struct rb_root *root)
  335. {
  336. struct rb_node *n;
  337. n = root->rb_node;
  338. if (!n)
  339. return NULL;
  340. while (n->rb_right)
  341. n = n->rb_right;
  342. return n;
  343. }
  344. EXPORT_SYMBOL(rb_last);
  345. struct rb_node *rb_next(const struct rb_node *node)
  346. {
  347. struct rb_node *parent;
  348. if (RB_EMPTY_NODE(node))
  349. return NULL;
  350. /* If we have a right-hand child, go down and then left as far
  351. as we can. */
  352. if (node->rb_right) {
  353. node = node->rb_right;
  354. while (node->rb_left)
  355. node=node->rb_left;
  356. return (struct rb_node *)node;
  357. }
  358. /* No right-hand children. Everything down and left is
  359. smaller than us, so any 'next' node must be in the general
  360. direction of our parent. Go up the tree; any time the
  361. ancestor is a right-hand child of its parent, keep going
  362. up. First time it's a left-hand child of its parent, said
  363. parent is our 'next' node. */
  364. while ((parent = rb_parent(node)) && node == parent->rb_right)
  365. node = parent;
  366. return parent;
  367. }
  368. EXPORT_SYMBOL(rb_next);
  369. struct rb_node *rb_prev(const struct rb_node *node)
  370. {
  371. struct rb_node *parent;
  372. if (RB_EMPTY_NODE(node))
  373. return NULL;
  374. /* If we have a left-hand child, go down and then right as far
  375. as we can. */
  376. if (node->rb_left) {
  377. node = node->rb_left;
  378. while (node->rb_right)
  379. node=node->rb_right;
  380. return (struct rb_node *)node;
  381. }
  382. /* No left-hand children. Go up till we find an ancestor which
  383. is a right-hand child of its parent */
  384. while ((parent = rb_parent(node)) && node == parent->rb_left)
  385. node = parent;
  386. return parent;
  387. }
  388. EXPORT_SYMBOL(rb_prev);
  389. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  390. struct rb_root *root)
  391. {
  392. struct rb_node *parent = rb_parent(victim);
  393. /* Set the surrounding nodes to point to the replacement */
  394. if (parent) {
  395. if (victim == parent->rb_left)
  396. parent->rb_left = new;
  397. else
  398. parent->rb_right = new;
  399. } else {
  400. root->rb_node = new;
  401. }
  402. if (victim->rb_left)
  403. rb_set_parent(victim->rb_left, new);
  404. if (victim->rb_right)
  405. rb_set_parent(victim->rb_right, new);
  406. /* Copy the pointers/colour from the victim to the replacement */
  407. *new = *victim;
  408. }
  409. EXPORT_SYMBOL(rb_replace_node);