rbtree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (c) 2010, 2012 Richard Braun.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <kern/assert.h>
  26. #include <kern/rbtree.h>
  27. #include <kern/rbtree_i.h>
  28. #include <sys/types.h>
  29. #define unlikely(expr) __builtin_expect(!!(expr), 0)
  30. /*
  31. * Return the index of a node in the children array of its parent.
  32. *
  33. * The parent parameter must not be null, and must be the parent of the
  34. * given node.
  35. */
  36. static inline int rbtree_index(const struct rbtree_node *node,
  37. const struct rbtree_node *parent)
  38. {
  39. assert(parent != NULL);
  40. assert((node == NULL) || (rbtree_parent(node) == parent));
  41. if (parent->children[RBTREE_LEFT] == node)
  42. return RBTREE_LEFT;
  43. assert(parent->children[RBTREE_RIGHT] == node);
  44. return RBTREE_RIGHT;
  45. }
  46. /*
  47. * Return the color of a node.
  48. */
  49. static inline int rbtree_color(const struct rbtree_node *node)
  50. {
  51. return node->parent & RBTREE_COLOR_MASK;
  52. }
  53. /*
  54. * Return true if the node is red.
  55. */
  56. static inline int rbtree_is_red(const struct rbtree_node *node)
  57. {
  58. return rbtree_color(node) == RBTREE_COLOR_RED;
  59. }
  60. /*
  61. * Return true if the node is black.
  62. */
  63. static inline int rbtree_is_black(const struct rbtree_node *node)
  64. {
  65. return rbtree_color(node) == RBTREE_COLOR_BLACK;
  66. }
  67. /*
  68. * Set the parent of a node, retaining its current color.
  69. */
  70. static inline void rbtree_set_parent(struct rbtree_node *node,
  71. struct rbtree_node *parent)
  72. {
  73. assert(rbtree_check_alignment(node));
  74. assert(rbtree_check_alignment(parent));
  75. node->parent = (unsigned long)parent | (node->parent & RBTREE_COLOR_MASK);
  76. }
  77. /*
  78. * Set the color of a node, retaining its current parent.
  79. */
  80. static inline void rbtree_set_color(struct rbtree_node *node, int color)
  81. {
  82. assert((color & ~RBTREE_COLOR_MASK) == 0);
  83. node->parent = (node->parent & RBTREE_PARENT_MASK) | color;
  84. }
  85. /*
  86. * Set the color of a node to red, retaining its current parent.
  87. */
  88. static inline void rbtree_set_red(struct rbtree_node *node)
  89. {
  90. rbtree_set_color(node, RBTREE_COLOR_RED);
  91. }
  92. /*
  93. * Set the color of a node to black, retaining its current parent.
  94. */
  95. static inline void rbtree_set_black(struct rbtree_node *node)
  96. {
  97. rbtree_set_color(node, RBTREE_COLOR_BLACK);
  98. }
  99. /*
  100. * Perform a tree rotation, rooted at the given node.
  101. *
  102. * The direction parameter defines the rotation direction and is either
  103. * RBTREE_LEFT or RBTREE_RIGHT.
  104. */
  105. static void rbtree_rotate(struct rbtree *tree, struct rbtree_node *node,
  106. int direction)
  107. {
  108. struct rbtree_node *parent, *rnode;
  109. int left, right;
  110. left = direction;
  111. right = 1 - left;
  112. parent = rbtree_parent(node);
  113. rnode = node->children[right];
  114. node->children[right] = rnode->children[left];
  115. if (rnode->children[left] != NULL)
  116. rbtree_set_parent(rnode->children[left], node);
  117. rnode->children[left] = node;
  118. rbtree_set_parent(rnode, parent);
  119. if (unlikely(parent == NULL))
  120. tree->root = rnode;
  121. else
  122. parent->children[rbtree_index(node, parent)] = rnode;
  123. rbtree_set_parent(node, rnode);
  124. }
  125. void rbtree_insert_rebalance(struct rbtree *tree, struct rbtree_node *parent,
  126. int index, struct rbtree_node *node)
  127. {
  128. struct rbtree_node *grand_parent, *uncle, *tmp;
  129. int left, right;
  130. assert(rbtree_check_alignment(parent));
  131. assert(rbtree_check_alignment(node));
  132. node->parent = (unsigned long)parent | RBTREE_COLOR_RED;
  133. node->children[RBTREE_LEFT] = NULL;
  134. node->children[RBTREE_RIGHT] = NULL;
  135. if (unlikely(parent == NULL))
  136. tree->root = node;
  137. else
  138. parent->children[index] = node;
  139. for (;;) {
  140. if (parent == NULL) {
  141. rbtree_set_black(node);
  142. break;
  143. }
  144. if (rbtree_is_black(parent))
  145. break;
  146. grand_parent = rbtree_parent(parent);
  147. assert(grand_parent != NULL);
  148. left = rbtree_index(parent, grand_parent);
  149. right = 1 - left;
  150. uncle = grand_parent->children[right];
  151. /*
  152. * Uncle is red. Flip colors and repeat at grand parent.
  153. */
  154. if ((uncle != NULL) && rbtree_is_red(uncle)) {
  155. rbtree_set_black(uncle);
  156. rbtree_set_black(parent);
  157. rbtree_set_red(grand_parent);
  158. node = grand_parent;
  159. parent = rbtree_parent(node);
  160. continue;
  161. }
  162. /*
  163. * Node is the right child of its parent. Rotate left at parent.
  164. */
  165. if (parent->children[right] == node) {
  166. rbtree_rotate(tree, parent, left);
  167. tmp = node;
  168. node = parent;
  169. parent = tmp;
  170. }
  171. /*
  172. * Node is the left child of its parent. Handle colors, rotate right
  173. * at grand parent, and leave.
  174. */
  175. rbtree_set_black(parent);
  176. rbtree_set_red(grand_parent);
  177. rbtree_rotate(tree, grand_parent, right);
  178. break;
  179. }
  180. assert(rbtree_is_black(tree->root));
  181. }
  182. void rbtree_remove(struct rbtree *tree, struct rbtree_node *node)
  183. {
  184. struct rbtree_node *child, *parent, *brother;
  185. int color, left, right;
  186. if (node->children[RBTREE_LEFT] == NULL)
  187. child = node->children[RBTREE_RIGHT];
  188. else if (node->children[RBTREE_RIGHT] == NULL)
  189. child = node->children[RBTREE_LEFT];
  190. else {
  191. struct rbtree_node *successor;
  192. /*
  193. * Two-children case: replace the node with its successor.
  194. */
  195. successor = node->children[RBTREE_RIGHT];
  196. while (successor->children[RBTREE_LEFT] != NULL)
  197. successor = successor->children[RBTREE_LEFT];
  198. color = rbtree_color(successor);
  199. child = successor->children[RBTREE_RIGHT];
  200. parent = rbtree_parent(node);
  201. if (unlikely(parent == NULL))
  202. tree->root = successor;
  203. else
  204. parent->children[rbtree_index(node, parent)] = successor;
  205. parent = rbtree_parent(successor);
  206. /*
  207. * Set parent directly to keep the original color.
  208. */
  209. successor->parent = node->parent;
  210. successor->children[RBTREE_LEFT] = node->children[RBTREE_LEFT];
  211. rbtree_set_parent(successor->children[RBTREE_LEFT], successor);
  212. if (node == parent)
  213. parent = successor;
  214. else {
  215. successor->children[RBTREE_RIGHT] = node->children[RBTREE_RIGHT];
  216. rbtree_set_parent(successor->children[RBTREE_RIGHT], successor);
  217. parent->children[RBTREE_LEFT] = child;
  218. if (child != NULL)
  219. rbtree_set_parent(child, parent);
  220. }
  221. goto update_color;
  222. }
  223. /*
  224. * Node has at most one child.
  225. */
  226. color = rbtree_color(node);
  227. parent = rbtree_parent(node);
  228. if (child != NULL)
  229. rbtree_set_parent(child, parent);
  230. if (unlikely(parent == NULL))
  231. tree->root = child;
  232. else
  233. parent->children[rbtree_index(node, parent)] = child;
  234. /*
  235. * The node has been removed, update the colors. The child pointer can
  236. * be null, in which case it is considered a black leaf.
  237. */
  238. update_color:
  239. if (color == RBTREE_COLOR_RED)
  240. return;
  241. for (;;) {
  242. if ((child != NULL) && rbtree_is_red(child)) {
  243. rbtree_set_black(child);
  244. break;
  245. }
  246. if (parent == NULL)
  247. break;
  248. left = rbtree_index(child, parent);
  249. right = 1 - left;
  250. brother = parent->children[right];
  251. /*
  252. * Brother is red. Recolor and rotate left at parent so that brother
  253. * becomes black.
  254. */
  255. if (rbtree_is_red(brother)) {
  256. rbtree_set_black(brother);
  257. rbtree_set_red(parent);
  258. rbtree_rotate(tree, parent, left);
  259. brother = parent->children[right];
  260. }
  261. /*
  262. * Brother has no red child. Recolor and repeat at parent.
  263. */
  264. if (((brother->children[RBTREE_LEFT] == NULL)
  265. || rbtree_is_black(brother->children[RBTREE_LEFT]))
  266. && ((brother->children[RBTREE_RIGHT] == NULL)
  267. || rbtree_is_black(brother->children[RBTREE_RIGHT]))) {
  268. rbtree_set_red(brother);
  269. child = parent;
  270. parent = rbtree_parent(child);
  271. continue;
  272. }
  273. /*
  274. * Brother's right child is black. Recolor and rotate right at brother.
  275. */
  276. if ((brother->children[right] == NULL)
  277. || rbtree_is_black(brother->children[right])) {
  278. rbtree_set_black(brother->children[left]);
  279. rbtree_set_red(brother);
  280. rbtree_rotate(tree, brother, right);
  281. brother = parent->children[right];
  282. }
  283. /*
  284. * Brother's left child is black. Exchange parent and brother colors
  285. * (we already know brother is black), set brother's right child black,
  286. * rotate left at parent and leave.
  287. */
  288. rbtree_set_color(brother, rbtree_color(parent));
  289. rbtree_set_black(parent);
  290. rbtree_set_black(brother->children[right]);
  291. rbtree_rotate(tree, parent, left);
  292. break;
  293. }
  294. assert((tree->root == NULL) || rbtree_is_black(tree->root));
  295. }
  296. struct rbtree_node * rbtree_nearest(struct rbtree_node *parent, int index,
  297. int direction)
  298. {
  299. assert(rbtree_check_index(direction));
  300. if (parent == NULL)
  301. return NULL;
  302. assert(rbtree_check_index(index));
  303. if (index != direction)
  304. return parent;
  305. return rbtree_walk(parent, direction);
  306. }
  307. struct rbtree_node * rbtree_firstlast(const struct rbtree *tree, int direction)
  308. {
  309. struct rbtree_node *prev, *cur;
  310. assert(rbtree_check_index(direction));
  311. prev = NULL;
  312. for (cur = tree->root; cur != NULL; cur = cur->children[direction])
  313. prev = cur;
  314. return prev;
  315. }
  316. struct rbtree_node * rbtree_walk(struct rbtree_node *node, int direction)
  317. {
  318. int left, right;
  319. assert(rbtree_check_index(direction));
  320. left = direction;
  321. right = 1 - left;
  322. if (node == NULL)
  323. return NULL;
  324. if (node->children[left] != NULL) {
  325. node = node->children[left];
  326. while (node->children[right] != NULL)
  327. node = node->children[right];
  328. } else {
  329. struct rbtree_node *parent;
  330. int index;
  331. for (;;) {
  332. parent = rbtree_parent(node);
  333. if (parent == NULL)
  334. return NULL;
  335. index = rbtree_index(node, parent);
  336. node = parent;
  337. if (index == right)
  338. break;
  339. }
  340. }
  341. return node;
  342. }
  343. /*
  344. * Return the left-most deepest child node of the given node.
  345. */
  346. static struct rbtree_node * rbtree_find_deepest(struct rbtree_node *node)
  347. {
  348. struct rbtree_node *parent;
  349. assert(node != NULL);
  350. for (;;) {
  351. parent = node;
  352. node = node->children[RBTREE_LEFT];
  353. if (node == NULL) {
  354. node = parent->children[RBTREE_RIGHT];
  355. if (node == NULL)
  356. return parent;
  357. }
  358. }
  359. }
  360. struct rbtree_node * rbtree_postwalk_deepest(const struct rbtree *tree)
  361. {
  362. struct rbtree_node *node;
  363. node = tree->root;
  364. if (node == NULL)
  365. return NULL;
  366. return rbtree_find_deepest(node);
  367. }
  368. struct rbtree_node * rbtree_postwalk_unlink(struct rbtree_node *node)
  369. {
  370. struct rbtree_node *parent;
  371. int index;
  372. if (node == NULL)
  373. return NULL;
  374. assert(node->children[RBTREE_LEFT] == NULL);
  375. assert(node->children[RBTREE_RIGHT] == NULL);
  376. parent = rbtree_parent(node);
  377. if (parent == NULL)
  378. return NULL;
  379. index = rbtree_index(node, parent);
  380. parent->children[index] = NULL;
  381. node = parent->children[RBTREE_RIGHT];
  382. if (node == NULL)
  383. return parent;
  384. return rbtree_find_deepest(node);
  385. }