splay-tree.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* A splay-tree datatype.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Mark Mitchell (mark@markmitchell.com).
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* The splay tree code copied from include/splay-tree.h and adjusted,
  22. so that all the data lives directly in splay_tree_node_s structure
  23. and no extra allocations are needed. */
  24. /* For an easily readable description of splay-trees, see:
  25. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  26. Algorithms. Harper-Collins, Inc. 1991.
  27. The major feature of splay trees is that all basic tree operations
  28. are amortized O(log n) time for a tree with n nodes. */
  29. #include "libgomp.h"
  30. /* Rotate the edge joining the left child N with its parent P. PP is the
  31. grandparents' pointer to P. */
  32. static inline void
  33. rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  34. {
  35. splay_tree_node tmp;
  36. tmp = n->right;
  37. n->right = p;
  38. p->left = tmp;
  39. *pp = n;
  40. }
  41. /* Rotate the edge joining the right child N with its parent P. PP is the
  42. grandparents' pointer to P. */
  43. static inline void
  44. rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  45. {
  46. splay_tree_node tmp;
  47. tmp = n->left;
  48. n->left = p;
  49. p->right = tmp;
  50. *pp = n;
  51. }
  52. /* Bottom up splay of KEY. */
  53. static void
  54. splay_tree_splay (splay_tree sp, splay_tree_key key)
  55. {
  56. if (sp->root == NULL)
  57. return;
  58. do {
  59. int cmp1, cmp2;
  60. splay_tree_node n, c;
  61. n = sp->root;
  62. cmp1 = splay_compare (key, &n->key);
  63. /* Found. */
  64. if (cmp1 == 0)
  65. return;
  66. /* Left or right? If no child, then we're done. */
  67. if (cmp1 < 0)
  68. c = n->left;
  69. else
  70. c = n->right;
  71. if (!c)
  72. return;
  73. /* Next one left or right? If found or no child, we're done
  74. after one rotation. */
  75. cmp2 = splay_compare (key, &c->key);
  76. if (cmp2 == 0
  77. || (cmp2 < 0 && !c->left)
  78. || (cmp2 > 0 && !c->right))
  79. {
  80. if (cmp1 < 0)
  81. rotate_left (&sp->root, n, c);
  82. else
  83. rotate_right (&sp->root, n, c);
  84. return;
  85. }
  86. /* Now we have the four cases of double-rotation. */
  87. if (cmp1 < 0 && cmp2 < 0)
  88. {
  89. rotate_left (&n->left, c, c->left);
  90. rotate_left (&sp->root, n, n->left);
  91. }
  92. else if (cmp1 > 0 && cmp2 > 0)
  93. {
  94. rotate_right (&n->right, c, c->right);
  95. rotate_right (&sp->root, n, n->right);
  96. }
  97. else if (cmp1 < 0 && cmp2 > 0)
  98. {
  99. rotate_right (&n->left, c, c->right);
  100. rotate_left (&sp->root, n, n->left);
  101. }
  102. else if (cmp1 > 0 && cmp2 < 0)
  103. {
  104. rotate_left (&n->right, c, c->left);
  105. rotate_right (&sp->root, n, n->right);
  106. }
  107. } while (1);
  108. }
  109. /* Insert a new NODE into SP. The NODE shouldn't exist in the tree. */
  110. attribute_hidden void
  111. splay_tree_insert (splay_tree sp, splay_tree_node node)
  112. {
  113. int comparison = 0;
  114. splay_tree_splay (sp, &node->key);
  115. if (sp->root)
  116. comparison = splay_compare (&sp->root->key, &node->key);
  117. if (sp->root && comparison == 0)
  118. gomp_fatal ("Duplicate node");
  119. else
  120. {
  121. /* Insert it at the root. */
  122. if (sp->root == NULL)
  123. node->left = node->right = NULL;
  124. else if (comparison < 0)
  125. {
  126. node->left = sp->root;
  127. node->right = node->left->right;
  128. node->left->right = NULL;
  129. }
  130. else
  131. {
  132. node->right = sp->root;
  133. node->left = node->right->left;
  134. node->right->left = NULL;
  135. }
  136. sp->root = node;
  137. }
  138. }
  139. /* Remove node with KEY from SP. It is not an error if it did not exist. */
  140. attribute_hidden void
  141. splay_tree_remove (splay_tree sp, splay_tree_key key)
  142. {
  143. splay_tree_splay (sp, key);
  144. if (sp->root && splay_compare (&sp->root->key, key) == 0)
  145. {
  146. splay_tree_node left, right;
  147. left = sp->root->left;
  148. right = sp->root->right;
  149. /* One of the children is now the root. Doesn't matter much
  150. which, so long as we preserve the properties of the tree. */
  151. if (left)
  152. {
  153. sp->root = left;
  154. /* If there was a right child as well, hang it off the
  155. right-most leaf of the left child. */
  156. if (right)
  157. {
  158. while (left->right)
  159. left = left->right;
  160. left->right = right;
  161. }
  162. }
  163. else
  164. sp->root = right;
  165. }
  166. }
  167. /* Lookup KEY in SP, returning NODE if present, and NULL
  168. otherwise. */
  169. attribute_hidden splay_tree_key
  170. splay_tree_lookup (splay_tree sp, splay_tree_key key)
  171. {
  172. splay_tree_splay (sp, key);
  173. if (sp->root && splay_compare (&sp->root->key, key) == 0)
  174. return &sp->root->key;
  175. else
  176. return NULL;
  177. }
  178. /* Helper function for splay_tree_foreach.
  179. Run FUNC on every node in KEY. */
  180. static void
  181. splay_tree_foreach_internal (splay_tree_node node, splay_tree_callback func,
  182. void *data)
  183. {
  184. if (!node)
  185. return;
  186. func (&node->key, data);
  187. splay_tree_foreach_internal (node->left, func, data);
  188. /* Yeah, whatever. GCC can fix my tail recursion. */
  189. splay_tree_foreach_internal (node->right, func, data);
  190. }
  191. /* Run FUNC on each of the nodes in SP. */
  192. attribute_hidden void
  193. splay_tree_foreach (splay_tree sp, splay_tree_callback func, void *data)
  194. {
  195. splay_tree_foreach_internal (sp->root, func, data);
  196. }