splay-tree.c 15 KB

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