splay-tree.c 15 KB

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