splay-tree.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* A splay-tree datatype.
  2. Copyright (C) 1998-2015 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. #include "splay-tree.h"
  31. extern int splay_compare (splay_tree_key, splay_tree_key);
  32. /* Rotate the edge joining the left child N with its parent P. PP is the
  33. grandparents' pointer to P. */
  34. static inline void
  35. rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  36. {
  37. splay_tree_node tmp;
  38. tmp = n->right;
  39. n->right = p;
  40. p->left = tmp;
  41. *pp = n;
  42. }
  43. /* Rotate the edge joining the right child N with its parent P. PP is the
  44. grandparents' pointer to P. */
  45. static inline void
  46. rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  47. {
  48. splay_tree_node tmp;
  49. tmp = n->left;
  50. n->left = p;
  51. p->right = tmp;
  52. *pp = n;
  53. }
  54. /* Bottom up splay of KEY. */
  55. static void
  56. splay_tree_splay (splay_tree sp, splay_tree_key key)
  57. {
  58. if (sp->root == NULL)
  59. return;
  60. do {
  61. int cmp1, cmp2;
  62. splay_tree_node n, c;
  63. n = sp->root;
  64. cmp1 = splay_compare (key, &n->key);
  65. /* Found. */
  66. if (cmp1 == 0)
  67. return;
  68. /* Left or right? If no child, then we're done. */
  69. if (cmp1 < 0)
  70. c = n->left;
  71. else
  72. c = n->right;
  73. if (!c)
  74. return;
  75. /* Next one left or right? If found or no child, we're done
  76. after one rotation. */
  77. cmp2 = splay_compare (key, &c->key);
  78. if (cmp2 == 0
  79. || (cmp2 < 0 && !c->left)
  80. || (cmp2 > 0 && !c->right))
  81. {
  82. if (cmp1 < 0)
  83. rotate_left (&sp->root, n, c);
  84. else
  85. rotate_right (&sp->root, n, c);
  86. return;
  87. }
  88. /* Now we have the four cases of double-rotation. */
  89. if (cmp1 < 0 && cmp2 < 0)
  90. {
  91. rotate_left (&n->left, c, c->left);
  92. rotate_left (&sp->root, n, n->left);
  93. }
  94. else if (cmp1 > 0 && cmp2 > 0)
  95. {
  96. rotate_right (&n->right, c, c->right);
  97. rotate_right (&sp->root, n, n->right);
  98. }
  99. else if (cmp1 < 0 && cmp2 > 0)
  100. {
  101. rotate_right (&n->left, c, c->right);
  102. rotate_left (&sp->root, n, n->left);
  103. }
  104. else if (cmp1 > 0 && cmp2 < 0)
  105. {
  106. rotate_left (&n->right, c, c->left);
  107. rotate_right (&sp->root, n, n->right);
  108. }
  109. } while (1);
  110. }
  111. /* Insert a new NODE into SP. The NODE shouldn't exist in the tree. */
  112. attribute_hidden void
  113. splay_tree_insert (splay_tree sp, splay_tree_node node)
  114. {
  115. int comparison = 0;
  116. splay_tree_splay (sp, &node->key);
  117. if (sp->root)
  118. comparison = splay_compare (&sp->root->key, &node->key);
  119. if (sp->root && comparison == 0)
  120. gomp_fatal ("Duplicate node");
  121. else
  122. {
  123. /* Insert it at the root. */
  124. if (sp->root == NULL)
  125. node->left = node->right = NULL;
  126. else if (comparison < 0)
  127. {
  128. node->left = sp->root;
  129. node->right = node->left->right;
  130. node->left->right = NULL;
  131. }
  132. else
  133. {
  134. node->right = sp->root;
  135. node->left = node->right->left;
  136. node->right->left = NULL;
  137. }
  138. sp->root = node;
  139. }
  140. }
  141. /* Remove node with KEY from SP. It is not an error if it did not exist. */
  142. attribute_hidden void
  143. splay_tree_remove (splay_tree sp, splay_tree_key key)
  144. {
  145. splay_tree_splay (sp, key);
  146. if (sp->root && splay_compare (&sp->root->key, key) == 0)
  147. {
  148. splay_tree_node left, right;
  149. left = sp->root->left;
  150. right = sp->root->right;
  151. /* One of the children is now the root. Doesn't matter much
  152. which, so long as we preserve the properties of the tree. */
  153. if (left)
  154. {
  155. sp->root = left;
  156. /* If there was a right child as well, hang it off the
  157. right-most leaf of the left child. */
  158. if (right)
  159. {
  160. while (left->right)
  161. left = left->right;
  162. left->right = right;
  163. }
  164. }
  165. else
  166. sp->root = right;
  167. }
  168. }
  169. /* Lookup KEY in SP, returning NODE if present, and NULL
  170. otherwise. */
  171. attribute_hidden splay_tree_key
  172. splay_tree_lookup (splay_tree sp, splay_tree_key key)
  173. {
  174. splay_tree_splay (sp, key);
  175. if (sp->root && splay_compare (&sp->root->key, key) == 0)
  176. return &sp->root->key;
  177. else
  178. return NULL;
  179. }