rbtree.c 11 KB

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