splay-tree.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU CC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /* For an easily readable description of splay-trees, see:
  18. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  19. Algorithms. Harper-Collins, Inc. 1991. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #ifdef HAVE_STDLIB_H
  24. #include <stdlib.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include <stdio.h>
  30. #include "libiberty.h"
  31. #include "splay-tree.h"
  32. static void splay_tree_delete_helper (splay_tree, splay_tree_node);
  33. static inline void rotate_left (splay_tree_node *,
  34. splay_tree_node, splay_tree_node);
  35. static inline void rotate_right (splay_tree_node *,
  36. splay_tree_node, splay_tree_node);
  37. static void splay_tree_splay (splay_tree, splay_tree_key);
  38. static int splay_tree_foreach_helper (splay_tree_node,
  39. splay_tree_foreach_fn, void*);
  40. /* Deallocate NODE (a member of SP), and all its sub-trees. */
  41. static void
  42. splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
  43. {
  44. splay_tree_node pending = 0;
  45. splay_tree_node active = 0;
  46. if (!node)
  47. return;
  48. #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
  49. #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
  50. KDEL (node->key);
  51. VDEL (node->value);
  52. /* We use the "key" field to hold the "next" pointer. */
  53. node->key = (splay_tree_key)pending;
  54. pending = (splay_tree_node)node;
  55. /* Now, keep processing the pending list until there aren't any
  56. more. This is a little more complicated than just recursing, but
  57. it doesn't toast the stack for large trees. */
  58. while (pending)
  59. {
  60. active = pending;
  61. pending = 0;
  62. while (active)
  63. {
  64. splay_tree_node temp;
  65. /* active points to a node which has its key and value
  66. deallocated, we just need to process left and right. */
  67. if (active->left)
  68. {
  69. KDEL (active->left->key);
  70. VDEL (active->left->value);
  71. active->left->key = (splay_tree_key)pending;
  72. pending = (splay_tree_node)(active->left);
  73. }
  74. if (active->right)
  75. {
  76. KDEL (active->right->key);
  77. VDEL (active->right->value);
  78. active->right->key = (splay_tree_key)pending;
  79. pending = (splay_tree_node)(active->right);
  80. }
  81. temp = active;
  82. active = (splay_tree_node)(temp->key);
  83. (*sp->deallocate) ((char*) temp, sp->allocate_data);
  84. }
  85. }
  86. #undef KDEL
  87. #undef VDEL
  88. }
  89. /* Rotate the edge joining the left child N with its parent P. PP is the
  90. grandparents' pointer to P. */
  91. static inline void
  92. rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  93. {
  94. splay_tree_node tmp;
  95. tmp = n->right;
  96. n->right = p;
  97. p->left = tmp;
  98. *pp = n;
  99. }
  100. /* Rotate the edge joining the right child N with its parent P. PP is the
  101. grandparents' pointer to P. */
  102. static inline void
  103. rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
  104. {
  105. splay_tree_node tmp;
  106. tmp = n->left;
  107. n->left = p;
  108. p->right = tmp;
  109. *pp = n;
  110. }
  111. /* Bottom up splay of key. */
  112. static void
  113. splay_tree_splay (splay_tree sp, splay_tree_key key)
  114. {
  115. if (sp->root == 0)
  116. return;
  117. do {
  118. int cmp1, cmp2;
  119. splay_tree_node n, c;
  120. n = sp->root;
  121. cmp1 = (*sp->comp) (key, n->key);
  122. /* Found. */
  123. if (cmp1 == 0)
  124. return;
  125. /* Left or right? If no child, then we're done. */
  126. if (cmp1 < 0)
  127. c = n->left;
  128. else
  129. c = n->right;
  130. if (!c)
  131. return;
  132. /* Next one left or right? If found or no child, we're done
  133. after one rotation. */
  134. cmp2 = (*sp->comp) (key, c->key);
  135. if (cmp2 == 0
  136. || (cmp2 < 0 && !c->left)
  137. || (cmp2 > 0 && !c->right))
  138. {
  139. if (cmp1 < 0)
  140. rotate_left (&sp->root, n, c);
  141. else
  142. rotate_right (&sp->root, n, c);
  143. return;
  144. }
  145. /* Now we have the four cases of double-rotation. */
  146. if (cmp1 < 0 && cmp2 < 0)
  147. {
  148. rotate_left (&n->left, c, c->left);
  149. rotate_left (&sp->root, n, n->left);
  150. }
  151. else if (cmp1 > 0 && cmp2 > 0)
  152. {
  153. rotate_right (&n->right, c, c->right);
  154. rotate_right (&sp->root, n, n->right);
  155. }
  156. else if (cmp1 < 0 && cmp2 > 0)
  157. {
  158. rotate_right (&n->left, c, c->right);
  159. rotate_left (&sp->root, n, n->left);
  160. }
  161. else if (cmp1 > 0 && cmp2 < 0)
  162. {
  163. rotate_left (&n->right, c, c->left);
  164. rotate_right (&sp->root, n, n->right);
  165. }
  166. } while (1);
  167. }
  168. /* Call FN, passing it the DATA, for every node below NODE, all of
  169. which are from SP, following an in-order traversal. If FN every
  170. returns a non-zero value, the iteration ceases immediately, and the
  171. value is returned. Otherwise, this function returns 0. */
  172. static int
  173. splay_tree_foreach_helper (splay_tree_node node,
  174. splay_tree_foreach_fn fn, void *data)
  175. {
  176. int val;
  177. splay_tree_node *stack;
  178. int stack_ptr, stack_size;
  179. /* A non-recursive implementation is used to avoid filling the stack
  180. for large trees. Splay trees are worst case O(n) in the depth of
  181. the tree. */
  182. #define INITIAL_STACK_SIZE 100
  183. stack_size = INITIAL_STACK_SIZE;
  184. stack_ptr = 0;
  185. stack = XNEWVEC (splay_tree_node, stack_size);
  186. val = 0;
  187. for (;;)
  188. {
  189. while (node != NULL)
  190. {
  191. if (stack_ptr == stack_size)
  192. {
  193. stack_size *= 2;
  194. stack = XRESIZEVEC (splay_tree_node, stack, stack_size);
  195. }
  196. stack[stack_ptr++] = node;
  197. node = node->left;
  198. }
  199. if (stack_ptr == 0)
  200. break;
  201. node = stack[--stack_ptr];
  202. val = (*fn) (node, data);
  203. if (val)
  204. break;
  205. node = node->right;
  206. }
  207. XDELETEVEC (stack);
  208. return val;
  209. }
  210. /* An allocator and deallocator based on xmalloc. */
  211. static void *
  212. splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
  213. {
  214. return (void *) xmalloc (size);
  215. }
  216. static void
  217. splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
  218. {
  219. free (object);
  220. }
  221. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  222. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  223. values. Use xmalloc to allocate the splay tree structure, and any
  224. nodes added. */
  225. splay_tree
  226. splay_tree_new (splay_tree_compare_fn compare_fn,
  227. splay_tree_delete_key_fn delete_key_fn,
  228. splay_tree_delete_value_fn delete_value_fn)
  229. {
  230. return (splay_tree_new_with_allocator
  231. (compare_fn, delete_key_fn, delete_value_fn,
  232. splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
  233. }
  234. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  235. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  236. values. */
  237. splay_tree
  238. splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn,
  239. splay_tree_delete_key_fn delete_key_fn,
  240. splay_tree_delete_value_fn delete_value_fn,
  241. splay_tree_allocate_fn allocate_fn,
  242. splay_tree_deallocate_fn deallocate_fn,
  243. void *allocate_data)
  244. {
  245. return
  246. splay_tree_new_typed_alloc (compare_fn, delete_key_fn, delete_value_fn,
  247. allocate_fn, allocate_fn, deallocate_fn,
  248. allocate_data);
  249. }
  250. /*
  251. @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
  252. (splay_tree_compare_fn @var{compare_fn}, @
  253. splay_tree_delete_key_fn @var{delete_key_fn}, @
  254. splay_tree_delete_value_fn @var{delete_value_fn}, @
  255. splay_tree_allocate_fn @var{tree_allocate_fn}, @
  256. splay_tree_allocate_fn @var{node_allocate_fn}, @
  257. splay_tree_deallocate_fn @var{deallocate_fn}, @
  258. void * @var{allocate_data})
  259. This function creates a splay tree that uses two different allocators
  260. @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
  261. tree itself and its nodes respectively. This is useful when variables of
  262. different types need to be allocated with different allocators.
  263. The splay tree will use @var{compare_fn} to compare nodes,
  264. @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
  265. deallocate values. Keys and values will be deallocated when the
  266. tree is deleted using splay_tree_delete or when a node is removed
  267. using splay_tree_remove. splay_tree_insert will release the previously
  268. inserted key and value using @var{delete_key_fn} and @var{delete_value_fn}
  269. if the inserted key is already found in the tree.
  270. @end deftypefn
  271. */
  272. splay_tree
  273. splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn,
  274. splay_tree_delete_key_fn delete_key_fn,
  275. splay_tree_delete_value_fn delete_value_fn,
  276. splay_tree_allocate_fn tree_allocate_fn,
  277. splay_tree_allocate_fn node_allocate_fn,
  278. splay_tree_deallocate_fn deallocate_fn,
  279. void * allocate_data)
  280. {
  281. splay_tree sp = (splay_tree) (*tree_allocate_fn)
  282. (sizeof (struct splay_tree_s), allocate_data);
  283. sp->root = 0;
  284. sp->comp = compare_fn;
  285. sp->delete_key = delete_key_fn;
  286. sp->delete_value = delete_value_fn;
  287. sp->allocate = node_allocate_fn;
  288. sp->deallocate = deallocate_fn;
  289. sp->allocate_data = allocate_data;
  290. return sp;
  291. }
  292. /* Deallocate SP. */
  293. void
  294. splay_tree_delete (splay_tree sp)
  295. {
  296. splay_tree_delete_helper (sp, sp->root);
  297. (*sp->deallocate) ((char*) sp, sp->allocate_data);
  298. }
  299. /* Insert a new node (associating KEY with DATA) into SP. If a
  300. previous node with the indicated KEY exists, its data is replaced
  301. with the new value. Returns the new node. */
  302. splay_tree_node
  303. splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
  304. {
  305. int comparison = 0;
  306. splay_tree_splay (sp, key);
  307. if (sp->root)
  308. comparison = (*sp->comp)(sp->root->key, key);
  309. if (sp->root && comparison == 0)
  310. {
  311. /* If the root of the tree already has the indicated KEY, delete
  312. the old key and old value, and replace them with KEY and VALUE. */
  313. if (sp->delete_key)
  314. (*sp->delete_key) (sp->root->key);
  315. if (sp->delete_value)
  316. (*sp->delete_value)(sp->root->value);
  317. sp->root->key = key;
  318. sp->root->value = value;
  319. }
  320. else
  321. {
  322. /* Create a new node, and insert it at the root. */
  323. splay_tree_node node;
  324. node = ((splay_tree_node)
  325. (*sp->allocate) (sizeof (struct splay_tree_node_s),
  326. sp->allocate_data));
  327. node->key = key;
  328. node->value = value;
  329. if (!sp->root)
  330. node->left = node->right = 0;
  331. else if (comparison < 0)
  332. {
  333. node->left = sp->root;
  334. node->right = node->left->right;
  335. node->left->right = 0;
  336. }
  337. else
  338. {
  339. node->right = sp->root;
  340. node->left = node->right->left;
  341. node->right->left = 0;
  342. }
  343. sp->root = node;
  344. }
  345. return sp->root;
  346. }
  347. /* Remove KEY from SP. It is not an error if it did not exist. */
  348. void
  349. splay_tree_remove (splay_tree sp, splay_tree_key key)
  350. {
  351. splay_tree_splay (sp, key);
  352. if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
  353. {
  354. splay_tree_node left, right;
  355. left = sp->root->left;
  356. right = sp->root->right;
  357. /* Delete the root node itself. */
  358. if (sp->delete_key)
  359. (*sp->delete_key) (sp->root->key);
  360. if (sp->delete_value)
  361. (*sp->delete_value) (sp->root->value);
  362. (*sp->deallocate) (sp->root, sp->allocate_data);
  363. /* One of the children is now the root. Doesn't matter much
  364. which, so long as we preserve the properties of the tree. */
  365. if (left)
  366. {
  367. sp->root = left;
  368. /* If there was a right child as well, hang it off the
  369. right-most leaf of the left child. */
  370. if (right)
  371. {
  372. while (left->right)
  373. left = left->right;
  374. left->right = right;
  375. }
  376. }
  377. else
  378. sp->root = right;
  379. }
  380. }
  381. /* Lookup KEY in SP, returning VALUE if present, and NULL
  382. otherwise. */
  383. splay_tree_node
  384. splay_tree_lookup (splay_tree sp, splay_tree_key key)
  385. {
  386. splay_tree_splay (sp, key);
  387. if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
  388. return sp->root;
  389. else
  390. return 0;
  391. }
  392. /* Return the node in SP with the greatest key. */
  393. splay_tree_node
  394. splay_tree_max (splay_tree sp)
  395. {
  396. splay_tree_node n = sp->root;
  397. if (!n)
  398. return NULL;
  399. while (n->right)
  400. n = n->right;
  401. return n;
  402. }
  403. /* Return the node in SP with the smallest key. */
  404. splay_tree_node
  405. splay_tree_min (splay_tree sp)
  406. {
  407. splay_tree_node n = sp->root;
  408. if (!n)
  409. return NULL;
  410. while (n->left)
  411. n = n->left;
  412. return n;
  413. }
  414. /* Return the immediate predecessor KEY, or NULL if there is no
  415. predecessor. KEY need not be present in the tree. */
  416. splay_tree_node
  417. splay_tree_predecessor (splay_tree sp, splay_tree_key key)
  418. {
  419. int comparison;
  420. splay_tree_node node;
  421. /* If the tree is empty, there is certainly no predecessor. */
  422. if (!sp->root)
  423. return NULL;
  424. /* Splay the tree around KEY. That will leave either the KEY
  425. itself, its predecessor, or its successor at the root. */
  426. splay_tree_splay (sp, key);
  427. comparison = (*sp->comp)(sp->root->key, key);
  428. /* If the predecessor is at the root, just return it. */
  429. if (comparison < 0)
  430. return sp->root;
  431. /* Otherwise, find the rightmost element of the left subtree. */
  432. node = sp->root->left;
  433. if (node)
  434. while (node->right)
  435. node = node->right;
  436. return node;
  437. }
  438. /* Return the immediate successor KEY, or NULL if there is no
  439. successor. KEY need not be present in the tree. */
  440. splay_tree_node
  441. splay_tree_successor (splay_tree sp, splay_tree_key key)
  442. {
  443. int comparison;
  444. splay_tree_node node;
  445. /* If the tree is empty, there is certainly no successor. */
  446. if (!sp->root)
  447. return NULL;
  448. /* Splay the tree around KEY. That will leave either the KEY
  449. itself, its predecessor, or its successor at the root. */
  450. splay_tree_splay (sp, key);
  451. comparison = (*sp->comp)(sp->root->key, key);
  452. /* If the successor is at the root, just return it. */
  453. if (comparison > 0)
  454. return sp->root;
  455. /* Otherwise, find the leftmost element of the right subtree. */
  456. node = sp->root->right;
  457. if (node)
  458. while (node->left)
  459. node = node->left;
  460. return node;
  461. }
  462. /* Call FN, passing it the DATA, for every node in SP, following an
  463. in-order traversal. If FN every returns a non-zero value, the
  464. iteration ceases immediately, and the value is returned.
  465. Otherwise, this function returns 0. */
  466. int
  467. splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
  468. {
  469. return splay_tree_foreach_helper (sp->root, fn, data);
  470. }
  471. /* Splay-tree comparison function, treating the keys as ints. */
  472. int
  473. splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
  474. {
  475. if ((int) k1 < (int) k2)
  476. return -1;
  477. else if ((int) k1 > (int) k2)
  478. return 1;
  479. else
  480. return 0;
  481. }
  482. /* Splay-tree comparison function, treating the keys as pointers. */
  483. int
  484. splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
  485. {
  486. if ((char*) k1 < (char*) k2)
  487. return -1;
  488. else if ((char*) k1 > (char*) k2)
  489. return 1;
  490. else
  491. return 0;
  492. }
  493. /* Splay-tree comparison function, treating the keys as strings. */
  494. int
  495. splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2)
  496. {
  497. return strcmp ((char *) k1, (char *) k2);
  498. }
  499. /* Splay-tree delete function, simply using free. */
  500. void
  501. splay_tree_delete_pointers (splay_tree_value value)
  502. {
  503. free ((void *) value);
  504. }