rbtree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (c) 2010-2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. */
  20. #include <assert.h>
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <kern/macros.h>
  24. #include <kern/rbtree.h>
  25. /*
  26. * Return the index of a node in the children array of its parent.
  27. *
  28. * The parent parameter must not be NULL, and must be the parent of the
  29. * given node.
  30. */
  31. static inline int
  32. rbtree_node_index (const struct rbtree_node *node,
  33. const struct rbtree_node *parent)
  34. {
  35. assert (parent);
  36. assert (!node || rbtree_node_parent (node) == parent);
  37. if (parent->children[RBTREE_LEFT] == node)
  38. return (RBTREE_LEFT);
  39. assert (parent->children[RBTREE_RIGHT] == node);
  40. return (RBTREE_RIGHT);
  41. }
  42. // Return the color of a node.
  43. static inline int
  44. rbtree_node_color (const struct rbtree_node *node)
  45. {
  46. return (node->parent & RBTREE_COLOR_MASK);
  47. }
  48. // Return true if the node is red.
  49. static inline int
  50. rbtree_node_is_red (const struct rbtree_node *node)
  51. {
  52. return (rbtree_node_color (node) == RBTREE_COLOR_RED);
  53. }
  54. // Return true if the node is black.
  55. static inline int
  56. rbtree_node_is_black (const struct rbtree_node *node)
  57. {
  58. return (rbtree_node_color (node) == RBTREE_COLOR_BLACK);
  59. }
  60. // Set the parent of a node, retaining its current color.
  61. static inline void
  62. rbtree_node_set_parent (struct rbtree_node *node, struct rbtree_node *parent)
  63. {
  64. assert (rbtree_node_check_alignment (node));
  65. assert (rbtree_node_check_alignment (parent));
  66. node->parent = (uintptr_t)parent | (node->parent & RBTREE_COLOR_MASK);
  67. }
  68. // Set the color of a node, retaining its current parent.
  69. static inline void
  70. rbtree_node_set_color (struct rbtree_node *node, int color)
  71. {
  72. assert ((color & ~RBTREE_COLOR_MASK) == 0);
  73. node->parent = (node->parent & RBTREE_PARENT_MASK) | color;
  74. }
  75. // Set the color of a node to red, retaining its current parent.
  76. static inline void
  77. rbtree_node_set_red (struct rbtree_node *node)
  78. {
  79. rbtree_node_set_color (node, RBTREE_COLOR_RED);
  80. }
  81. // Set the color of a node to black, retaining its current parent.
  82. static inline void
  83. rbtree_node_set_black (struct rbtree_node *node)
  84. {
  85. rbtree_node_set_color (node, RBTREE_COLOR_BLACK);
  86. }
  87. // Return the left-most deepest child node of the given node.
  88. static struct rbtree_node*
  89. rbtree_node_find_deepest (struct rbtree_node *node)
  90. {
  91. assert (node);
  92. while (1)
  93. {
  94. struct rbtree_node *parent = node;
  95. node = node->children[RBTREE_LEFT];
  96. if (! node)
  97. {
  98. node = parent->children[RBTREE_RIGHT];
  99. if (! node)
  100. return (parent);
  101. }
  102. }
  103. }
  104. /*
  105. * Perform a tree rotation, rooted at the given node.
  106. *
  107. * The direction parameter defines the rotation direction and is either
  108. * RBTREE_LEFT or RBTREE_RIGHT.
  109. */
  110. static void
  111. rbtree_rotate (struct rbtree *tree, struct rbtree_node *node, int direction)
  112. {
  113. int left = direction, right = 1 - left;
  114. struct rbtree_node *parent = rbtree_node_parent (node),
  115. *rnode = node->children[right];
  116. node->children[right] = rnode->children[left];
  117. if (rnode->children[left])
  118. rbtree_node_set_parent (rnode->children[left], node);
  119. rnode->children[left] = node;
  120. rbtree_node_set_parent (rnode, parent);
  121. if (unlikely (! parent))
  122. tree->root = rnode;
  123. else
  124. parent->children[rbtree_node_index (node, parent)] = rnode;
  125. rbtree_node_set_parent (node, rnode);
  126. }
  127. void
  128. rbtree_insert_rebalance (struct rbtree *tree, struct rbtree_node *parent,
  129. int index, struct rbtree_node *node)
  130. {
  131. assert (rbtree_node_check_alignment (parent));
  132. assert (rbtree_node_check_alignment (node));
  133. node->parent = (uintptr_t) parent | RBTREE_COLOR_RED;
  134. node->children[RBTREE_LEFT] = NULL;
  135. node->children[RBTREE_RIGHT] = NULL;
  136. if (unlikely (! parent))
  137. tree->root = node;
  138. else
  139. parent->children[index] = node;
  140. while (1)
  141. {
  142. if (! parent)
  143. {
  144. rbtree_node_set_black (node);
  145. break;
  146. }
  147. else if (rbtree_node_is_black (parent))
  148. break;
  149. struct rbtree_node *grand_parent = rbtree_node_parent (parent);
  150. assert (grand_parent);
  151. int left = rbtree_node_index (parent, grand_parent),
  152. right = 1 - left;
  153. struct rbtree_node *uncle = grand_parent->children[right];
  154. // Uncle is red. Flip colors and repeat at grand parent.
  155. if (uncle && rbtree_node_is_red (uncle))
  156. {
  157. rbtree_node_set_black (uncle);
  158. rbtree_node_set_black (parent);
  159. rbtree_node_set_red (grand_parent);
  160. node = grand_parent;
  161. parent = rbtree_node_parent (node);
  162. continue;
  163. }
  164. // Node is the right child of its parent. Rotate left at parent.
  165. if (parent->children[right] == node)
  166. {
  167. rbtree_rotate (tree, parent, left);
  168. parent = node;
  169. }
  170. /*
  171. * Node is the left child of its parent. Handle colors, rotate right
  172. * at grand parent, and leave.
  173. */
  174. rbtree_node_set_black (parent);
  175. rbtree_node_set_red (grand_parent);
  176. rbtree_rotate (tree, grand_parent, right);
  177. break;
  178. }
  179. assert (rbtree_node_is_black (tree->root));
  180. }
  181. void*
  182. rbtree_replace_slot (struct rbtree *tree, rbtree_slot_t slot,
  183. struct rbtree_node *node)
  184. {
  185. struct rbtree_node *prev, *parent = rbtree_slot_parent (slot);
  186. if (! parent)
  187. {
  188. prev = tree->root;
  189. tree->root = node;
  190. }
  191. else
  192. {
  193. int index = rbtree_slot_index (slot);
  194. assert (rbtree_check_index (index));
  195. prev = parent->children[index];
  196. parent->children[index] = node;
  197. }
  198. assert (prev);
  199. *node = *prev;
  200. for (size_t i = 0; i < ARRAY_SIZE (node->children); i++)
  201. if (node->children[i])
  202. rbtree_node_set_parent (node->children[i], node);
  203. return (prev);
  204. }
  205. void
  206. rbtree_remove (struct rbtree *tree, struct rbtree_node *node)
  207. {
  208. struct rbtree_node *child, *parent;
  209. int color;
  210. if (!node->children[RBTREE_LEFT])
  211. child = node->children[RBTREE_RIGHT];
  212. else if (!node->children[RBTREE_RIGHT])
  213. child = node->children[RBTREE_LEFT];
  214. else
  215. { // Two-children case: replace the node with its successor.
  216. struct rbtree_node *successor = node->children[RBTREE_RIGHT];
  217. while (successor->children[RBTREE_LEFT])
  218. successor = successor->children[RBTREE_LEFT];
  219. color = rbtree_node_color (successor);
  220. child = successor->children[RBTREE_RIGHT];
  221. parent = rbtree_node_parent (node);
  222. if (unlikely (! parent))
  223. tree->root = successor;
  224. else
  225. parent->children[rbtree_node_index (node, parent)] = successor;
  226. parent = rbtree_node_parent (successor);
  227. // Set parent directly to keep the original color.
  228. successor->parent = node->parent;
  229. successor->children[RBTREE_LEFT] = node->children[RBTREE_LEFT];
  230. rbtree_node_set_parent (successor->children[RBTREE_LEFT], successor);
  231. if (node == parent)
  232. parent = successor;
  233. else
  234. {
  235. successor->children[RBTREE_RIGHT] = node->children[RBTREE_RIGHT];
  236. rbtree_node_set_parent (successor->children[RBTREE_RIGHT],
  237. successor);
  238. parent->children[RBTREE_LEFT] = child;
  239. if (child)
  240. rbtree_node_set_parent (child, parent);
  241. }
  242. goto update_color;
  243. }
  244. // Node has at most one child.
  245. color = rbtree_node_color (node);
  246. parent = rbtree_node_parent (node);
  247. if (child)
  248. rbtree_node_set_parent (child, parent);
  249. if (unlikely (! parent))
  250. tree->root = child;
  251. else
  252. parent->children[rbtree_node_index (node, parent)] = child;
  253. /*
  254. * The node has been removed, update the colors. The child pointer can
  255. * be NULL, in which case it is considered a black leaf.
  256. */
  257. update_color:
  258. if (color == RBTREE_COLOR_RED)
  259. return;
  260. while (1)
  261. {
  262. if (child && rbtree_node_is_red (child))
  263. {
  264. rbtree_node_set_black (child);
  265. break;
  266. }
  267. else if (! parent)
  268. break;
  269. int left = rbtree_node_index (child, parent), right = 1 - left;
  270. struct rbtree_node *brother = parent->children[right];
  271. /*
  272. * Brother is red. Recolor and rotate left at parent so that brother
  273. * becomes black.
  274. */
  275. if (rbtree_node_is_red (brother))
  276. {
  277. rbtree_node_set_black (brother);
  278. rbtree_node_set_red (parent);
  279. rbtree_rotate (tree, parent, left);
  280. brother = parent->children[right];
  281. }
  282. assert (brother);
  283. // Brother has no red child. Recolor and repeat at parent.
  284. if ((!brother->children[RBTREE_LEFT] ||
  285. rbtree_node_is_black (brother->children[RBTREE_LEFT])) &&
  286. (!brother->children[RBTREE_RIGHT] ||
  287. rbtree_node_is_black (brother->children[RBTREE_RIGHT])))
  288. {
  289. rbtree_node_set_red (brother);
  290. child = parent;
  291. parent = rbtree_node_parent (child);
  292. continue;
  293. }
  294. // Brother's right child is black. Recolor and rotate right at brother.
  295. if (!brother->children[right] ||
  296. rbtree_node_is_black (brother->children[right]))
  297. {
  298. rbtree_node_set_black (brother->children[left]);
  299. rbtree_node_set_red (brother);
  300. rbtree_rotate (tree, brother, right);
  301. brother = parent->children[right];
  302. }
  303. /*
  304. * Brother's left child is black. Exchange parent and brother colors
  305. * (we already know brother is black), set brother's right child black,
  306. * rotate left at parent and leave.
  307. */
  308. assert (brother->children[right]);
  309. rbtree_node_set_color (brother, rbtree_node_color (parent));
  310. rbtree_node_set_black (parent);
  311. rbtree_node_set_black (brother->children[right]);
  312. rbtree_rotate (tree, parent, left);
  313. break;
  314. }
  315. assert (!tree->root || rbtree_node_is_black (tree->root));
  316. }
  317. struct rbtree_node*
  318. rbtree_nearest (struct rbtree_node *parent, int index, int direction)
  319. {
  320. assert (rbtree_check_index (direction));
  321. if (! parent)
  322. return (NULL);
  323. assert (rbtree_check_index (index));
  324. return (index != direction ? parent : rbtree_walk (parent, direction));
  325. }
  326. struct rbtree_node*
  327. rbtree_firstlast (const struct rbtree *tree, int direction)
  328. {
  329. assert (rbtree_check_index (direction));
  330. struct rbtree_node *prev = NULL, *cur;
  331. for (cur = tree->root; cur; cur = cur->children[direction])
  332. prev = cur;
  333. return (prev);
  334. }
  335. struct rbtree_node*
  336. rbtree_walk (struct rbtree_node *node, int direction)
  337. {
  338. assert (rbtree_check_index (direction));
  339. int left = direction, right = 1 - left;
  340. if (! node)
  341. return (NULL);
  342. else if (node->children[left])
  343. {
  344. node = node->children[left];
  345. while (node->children[right] != NULL)
  346. node = node->children[right];
  347. }
  348. else
  349. {
  350. while (1)
  351. {
  352. struct rbtree_node *parent = rbtree_node_parent (node);
  353. if (! parent)
  354. return (NULL);
  355. int index = rbtree_node_index (node, parent);
  356. node = parent;
  357. if (index == right)
  358. break;
  359. }
  360. }
  361. return (node);
  362. }
  363. struct rbtree_node*
  364. rbtree_postwalk_deepest (const struct rbtree *tree)
  365. {
  366. struct rbtree_node *node = tree->root;
  367. return (node ? rbtree_node_find_deepest (node) : NULL);
  368. }
  369. struct rbtree_node*
  370. rbtree_postwalk_unlink (struct rbtree_node *node)
  371. {
  372. if (! node)
  373. return (NULL);
  374. assert (!node->children[RBTREE_LEFT]);
  375. assert (!node->children[RBTREE_RIGHT]);
  376. struct rbtree_node *parent = rbtree_node_parent (node);
  377. if (! parent)
  378. return (NULL);
  379. int index = rbtree_node_index (node, parent);
  380. parent->children[index] = NULL;
  381. node = parent->children[RBTREE_RIGHT];
  382. if (! node)
  383. return (parent);
  384. return (rbtree_node_find_deepest (node));
  385. }