rbtree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * =============================================================================
  3. *
  4. * Filename: rbtree.c
  5. *
  6. * Description: rbtree(Red-Black tree) implementation adapted from linux
  7. * kernel thus can be used in userspace c program.
  8. *
  9. * Created: 09/02/2012 11:38:12 PM
  10. *
  11. * Author: Fu Haiping (forhappy), haipingf@gmail.com
  12. * Company: ICT ( Institute Of Computing Technology, CAS )
  13. *
  14. * =============================================================================
  15. */
  16. /*
  17. Red Black Trees
  18. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  19. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  20. This program is free software; you can redistribute it and/or modify
  21. it under the terms of the GNU General Public License as published by
  22. the Free Software Foundation; either version 2 of the License, or
  23. (at your option) any later version.
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License
  29. along with this program; if not, write to the Free Software
  30. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  31. linux/lib/rbtree.c
  32. */
  33. #include "rbtree.h"
  34. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  35. {
  36. struct rb_node *right = node->rb_right;
  37. struct rb_node *parent = rb_parent(node);
  38. if ((node->rb_right = right->rb_left))
  39. rb_set_parent(right->rb_left, node);
  40. right->rb_left = node;
  41. rb_set_parent(right, parent);
  42. if (parent)
  43. {
  44. if (node == parent->rb_left)
  45. parent->rb_left = right;
  46. else
  47. parent->rb_right = right;
  48. }
  49. else
  50. root->rb_node = right;
  51. rb_set_parent(node, right);
  52. }
  53. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  54. {
  55. struct rb_node *left = node->rb_left;
  56. struct rb_node *parent = rb_parent(node);
  57. if ((node->rb_left = left->rb_right))
  58. rb_set_parent(left->rb_right, node);
  59. left->rb_right = node;
  60. rb_set_parent(left, parent);
  61. if (parent)
  62. {
  63. if (node == parent->rb_right)
  64. parent->rb_right = left;
  65. else
  66. parent->rb_left = left;
  67. }
  68. else
  69. root->rb_node = left;
  70. rb_set_parent(node, left);
  71. }
  72. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  73. {
  74. struct rb_node *parent, *gparent;
  75. while ((parent = rb_parent(node)) && rb_is_red(parent))
  76. {
  77. gparent = rb_parent(parent);
  78. if (parent == gparent->rb_left)
  79. {
  80. {
  81. struct rb_node *uncle = gparent->rb_right;
  82. if (uncle && rb_is_red(uncle))
  83. {
  84. rb_set_black(uncle);
  85. rb_set_black(parent);
  86. rb_set_red(gparent);
  87. node = gparent;
  88. continue;
  89. }
  90. }
  91. if (parent->rb_right == node)
  92. {
  93. struct rb_node *tmp;
  94. __rb_rotate_left(parent, root);
  95. tmp = parent;
  96. parent = node;
  97. node = tmp;
  98. }
  99. rb_set_black(parent);
  100. rb_set_red(gparent);
  101. __rb_rotate_right(gparent, root);
  102. } else {
  103. {
  104. struct rb_node *uncle = gparent->rb_left;
  105. if (uncle && rb_is_red(uncle))
  106. {
  107. rb_set_black(uncle);
  108. rb_set_black(parent);
  109. rb_set_red(gparent);
  110. node = gparent;
  111. continue;
  112. }
  113. }
  114. if (parent->rb_left == node)
  115. {
  116. struct rb_node *tmp;
  117. __rb_rotate_right(parent, root);
  118. tmp = parent;
  119. parent = node;
  120. node = tmp;
  121. }
  122. rb_set_black(parent);
  123. rb_set_red(gparent);
  124. __rb_rotate_left(gparent, root);
  125. }
  126. }
  127. rb_set_black(root->rb_node);
  128. }
  129. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  130. struct rb_root *root)
  131. {
  132. struct rb_node *other;
  133. while ((!node || rb_is_black(node)) && node != root->rb_node)
  134. {
  135. if (parent->rb_left == node)
  136. {
  137. other = parent->rb_right;
  138. if (rb_is_red(other))
  139. {
  140. rb_set_black(other);
  141. rb_set_red(parent);
  142. __rb_rotate_left(parent, root);
  143. other = parent->rb_right;
  144. }
  145. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  146. (!other->rb_right || rb_is_black(other->rb_right)))
  147. {
  148. rb_set_red(other);
  149. node = parent;
  150. parent = rb_parent(node);
  151. }
  152. else
  153. {
  154. if (!other->rb_right || rb_is_black(other->rb_right))
  155. {
  156. rb_set_black(other->rb_left);
  157. rb_set_red(other);
  158. __rb_rotate_right(other, root);
  159. other = parent->rb_right;
  160. }
  161. rb_set_color(other, rb_color(parent));
  162. rb_set_black(parent);
  163. rb_set_black(other->rb_right);
  164. __rb_rotate_left(parent, root);
  165. node = root->rb_node;
  166. break;
  167. }
  168. }
  169. else
  170. {
  171. other = parent->rb_left;
  172. if (rb_is_red(other))
  173. {
  174. rb_set_black(other);
  175. rb_set_red(parent);
  176. __rb_rotate_right(parent, root);
  177. other = parent->rb_left;
  178. }
  179. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  180. (!other->rb_right || rb_is_black(other->rb_right)))
  181. {
  182. rb_set_red(other);
  183. node = parent;
  184. parent = rb_parent(node);
  185. }
  186. else
  187. {
  188. if (!other->rb_left || rb_is_black(other->rb_left))
  189. {
  190. rb_set_black(other->rb_right);
  191. rb_set_red(other);
  192. __rb_rotate_left(other, root);
  193. other = parent->rb_left;
  194. }
  195. rb_set_color(other, rb_color(parent));
  196. rb_set_black(parent);
  197. rb_set_black(other->rb_left);
  198. __rb_rotate_right(parent, root);
  199. node = root->rb_node;
  200. break;
  201. }
  202. }
  203. }
  204. if (node)
  205. rb_set_black(node);
  206. }
  207. void rb_erase(struct rb_node *node, struct rb_root *root)
  208. {
  209. struct rb_node *child, *parent;
  210. int color;
  211. if (!node->rb_left)
  212. child = node->rb_right;
  213. else if (!node->rb_right)
  214. child = node->rb_left;
  215. else
  216. {
  217. struct rb_node *old = node, *left;
  218. node = node->rb_right;
  219. while ((left = node->rb_left) != NULL)
  220. node = left;
  221. if (rb_parent(old)) {
  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. if (child)
  235. rb_set_parent(child, parent);
  236. parent->rb_left = child;
  237. node->rb_right = old->rb_right;
  238. rb_set_parent(old->rb_right, node);
  239. }
  240. node->rb_parent_color = old->rb_parent_color;
  241. node->rb_left = old->rb_left;
  242. rb_set_parent(old->rb_left, node);
  243. goto color;
  244. }
  245. parent = rb_parent(node);
  246. color = rb_color(node);
  247. if (child)
  248. rb_set_parent(child, parent);
  249. if (parent)
  250. {
  251. if (parent->rb_left == node)
  252. parent->rb_left = child;
  253. else
  254. parent->rb_right = child;
  255. }
  256. else
  257. root->rb_node = child;
  258. color:
  259. if (color == RB_BLACK)
  260. __rb_erase_color(child, parent, root);
  261. }
  262. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  263. {
  264. struct rb_node *parent;
  265. up:
  266. func(node, data);
  267. parent = rb_parent(node);
  268. if (!parent)
  269. return;
  270. if (node == parent->rb_left && parent->rb_right)
  271. func(parent->rb_right, data);
  272. else if (parent->rb_left)
  273. func(parent->rb_left, data);
  274. node = parent;
  275. goto up;
  276. }
  277. /*
  278. * after inserting @node into the tree, update the tree to account for
  279. * both the new entry and any damage done by rebalance
  280. */
  281. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  282. {
  283. if (node->rb_left)
  284. node = node->rb_left;
  285. else if (node->rb_right)
  286. node = node->rb_right;
  287. rb_augment_path(node, func, data);
  288. }
  289. /*
  290. * before removing the node, find the deepest node on the rebalance path
  291. * that will still be there after @node gets removed
  292. */
  293. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  294. {
  295. struct rb_node *deepest;
  296. if (!node->rb_right && !node->rb_left)
  297. deepest = rb_parent(node);
  298. else if (!node->rb_right)
  299. deepest = node->rb_left;
  300. else if (!node->rb_left)
  301. deepest = node->rb_right;
  302. else {
  303. deepest = rb_next(node);
  304. if (deepest->rb_right)
  305. deepest = deepest->rb_right;
  306. else if (rb_parent(deepest) != node)
  307. deepest = rb_parent(deepest);
  308. }
  309. return deepest;
  310. }
  311. /*
  312. * after removal, update the tree to account for the removed entry
  313. * and any rebalance damage.
  314. */
  315. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  316. {
  317. if (node)
  318. rb_augment_path(node, func, data);
  319. }
  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. struct rb_node *rb_last(const struct rb_root *root)
  334. {
  335. struct rb_node *n;
  336. n = root->rb_node;
  337. if (!n)
  338. return NULL;
  339. while (n->rb_right)
  340. n = n->rb_right;
  341. return n;
  342. }
  343. struct rb_node *rb_next(const struct rb_node *node)
  344. {
  345. struct rb_node *parent;
  346. if (rb_parent(node) == node)
  347. return NULL;
  348. /* If we have a right-hand child, go down and then left as far
  349. as we can. */
  350. if (node->rb_right) {
  351. node = node->rb_right;
  352. while (node->rb_left)
  353. node=node->rb_left;
  354. return (struct rb_node *)node;
  355. }
  356. /* No right-hand children. Everything down and left is
  357. smaller than us, so any 'next' node must be in the general
  358. direction of our parent. Go up the tree; any time the
  359. ancestor is a right-hand child of its parent, keep going
  360. up. First time it's a left-hand child of its parent, said
  361. parent is our 'next' node. */
  362. while ((parent = rb_parent(node)) && node == parent->rb_right)
  363. node = parent;
  364. return parent;
  365. }
  366. struct rb_node *rb_prev(const struct rb_node *node)
  367. {
  368. struct rb_node *parent;
  369. if (rb_parent(node) == node)
  370. return NULL;
  371. /* If we have a left-hand child, go down and then right as far
  372. as we can. */
  373. if (node->rb_left) {
  374. node = node->rb_left;
  375. while (node->rb_right)
  376. node=node->rb_right;
  377. return (struct rb_node *)node;
  378. }
  379. /* No left-hand children. Go up till we find an ancestor which
  380. is a right-hand child of its parent */
  381. while ((parent = rb_parent(node)) && node == parent->rb_left)
  382. node = parent;
  383. return parent;
  384. }
  385. void rb_replace_node(struct rb_node *victim, struct rb_node *newer,
  386. struct rb_root *root)
  387. {
  388. struct rb_node *parent = rb_parent(victim);
  389. /* Set the surrounding nodes to point to the replacement */
  390. if (parent) {
  391. if (victim == parent->rb_left)
  392. parent->rb_left = newer;
  393. else
  394. parent->rb_right = newer;
  395. } else {
  396. root->rb_node = newer;
  397. }
  398. if (victim->rb_left)
  399. rb_set_parent(victim->rb_left, newer);
  400. if (victim->rb_right)
  401. rb_set_parent(victim->rb_right, newer);
  402. /* Copy the pointers/colour from the victim to the replacement */
  403. *newer = *victim;
  404. }