splay-tree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * SPDX-License-Identifier: GPL-3.0+
  16. * License-Filename: LICENSE
  17. *
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "splay-tree.h"
  26. /* forward decl. */
  27. static struct splay_tree_node_n *splay (splay_tree sp, splay_tree_key key);
  28. /* delete whole sp tree */
  29. splay_tree
  30. splay_tree_delete (splay_tree sp)
  31. {
  32. splay_tree_node spn = (splay_tree_node) 0;
  33. splay_tree_node spn2 = (splay_tree_node) 0;
  34. if (sp == (splay_tree) 0)
  35. {
  36. return ((splay_tree) 0);
  37. }
  38. /* if there is data */
  39. if (sp->root)
  40. {
  41. /* for every node, this does not cause stack smashing */
  42. spn = splay_tree_min (sp);
  43. while (spn)
  44. {
  45. spn2 = splay_tree_successor (sp, spn->key);
  46. splay_tree_remove (sp, spn->key);
  47. spn = spn2;
  48. }
  49. }
  50. /* wipe the pointers in the struct to make valgrind happy */
  51. sp->root = (splay_tree_node) 0;
  52. /* The comparision function. */
  53. sp->comp = (splay_tree_compare_fn) 0;
  54. /* The deallocate-key function. NULL if no cleanup is necessary. */
  55. sp->delete_key = (splay_tree_delete_key_fn) 0;
  56. /* The deallocate-value function. NULL if no cleanup is necessary. */
  57. sp->delete_value = (splay_tree_delete_value_fn) 0;
  58. free ((void *) sp);
  59. return ((splay_tree) 0);
  60. }
  61. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  62. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  63. values. */
  64. splay_tree
  65. splay_tree_new (splay_tree_compare_fn compare_fn,
  66. splay_tree_delete_key_fn delete_key_fn,
  67. splay_tree_delete_value_fn delete_value_fn)
  68. {
  69. splay_tree sp = (splay_tree) 0;
  70. /* there must be a compare function, the free() functions are optional */
  71. if (compare_fn == (splay_tree_compare_fn) 0)
  72. {
  73. return ((splay_tree) 0);
  74. }
  75. sp = (splay_tree) calloc (1, sizeof (struct splay_tree_t));
  76. if (sp == (splay_tree) 0)
  77. {
  78. return ((splay_tree) 0);
  79. }
  80. /* set root node to use and the functions */
  81. sp->root = (splay_tree_node) 0;
  82. sp->comp = compare_fn;
  83. sp->delete_key = delete_key_fn;
  84. sp->delete_value = delete_value_fn;
  85. return ((splay_tree) sp);
  86. }
  87. /* Insert a new node (associating KEY with DATA) into SP. If a
  88. previous node with the indicated KEY exists, its data is not replaced
  89. with the new value. */
  90. void
  91. splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
  92. {
  93. splay_tree_node spn = (splay_tree_node) 0;
  94. int comparison = 0;
  95. if (sp == (splay_tree) 0)
  96. {
  97. /* no tree */
  98. return;
  99. }
  100. spn = splay_tree_lookup (sp, key);
  101. if (spn != (splay_tree_node) 0)
  102. {
  103. /* did already exist */
  104. return;
  105. }
  106. /* Create a new node, and insert it at the root. */
  107. spn = (splay_tree_node) calloc (1, sizeof (struct splay_tree_node_n));
  108. if (spn == (splay_tree_node) 0)
  109. {
  110. /* shouldnothappen */
  111. return;
  112. }
  113. /* set node data */
  114. spn->key = key;
  115. spn->value = value;
  116. if (sp->root == (splay_tree_node) 0)
  117. {
  118. /* first entry */
  119. sp->root = spn;
  120. return;
  121. }
  122. /* add in tree */
  123. comparison = (*sp->comp) (sp->root->key, key);
  124. if (comparison < 0)
  125. {
  126. spn->left = sp->root;
  127. spn->right = spn->left->right;
  128. spn->left->right = (splay_tree_node) 0;
  129. }
  130. else
  131. {
  132. /* > or == */
  133. spn->right = sp->root;
  134. spn->left = spn->right->left;
  135. spn->right->left = (splay_tree_node) 0;
  136. }
  137. sp->root = spn;
  138. return;
  139. }
  140. /* insert and allow duplicates of key */
  141. void
  142. splay_tree_insert_duplicates (splay_tree sp, splay_tree_key key,
  143. splay_tree_value value)
  144. {
  145. if (sp == (splay_tree) 0)
  146. {
  147. /* no tree */
  148. return;
  149. }
  150. splay_tree_insert (sp, key, value);
  151. return;
  152. }
  153. /* Remove KEY from SP. It is not an error if it did not exist. */
  154. void
  155. splay_tree_remove (splay_tree sp, splay_tree_key key)
  156. {
  157. splay_tree_node spn = (splay_tree_node) 0;
  158. splay_tree_node node = (splay_tree_node) 0;
  159. splay_tree_node left = (splay_tree_node) 0;
  160. splay_tree_node right = (splay_tree_node) 0;
  161. if (sp == (splay_tree) 0)
  162. {
  163. /* no tree */
  164. return;
  165. }
  166. if (sp->root == (splay_tree_node) 0)
  167. {
  168. /* no data */
  169. return;
  170. }
  171. spn = splay_tree_lookup (sp, key);
  172. if (spn == (splay_tree_node) 0)
  173. {
  174. return;
  175. }
  176. /* found entry to delete now in spn */
  177. node = sp->root;
  178. left = sp->root->left;
  179. right = sp->root->right;
  180. /* One of the children is now the root. Doesn't matter much
  181. which, so long as we preserve the properties of the tree. */
  182. if (left)
  183. {
  184. sp->root = left;
  185. /* If there was a right child as well, hang it off the
  186. right-most leaf of the left child. */
  187. if (right)
  188. {
  189. while (left->right)
  190. {
  191. left = left->right;
  192. }
  193. left->right = right;
  194. }
  195. }
  196. else
  197. {
  198. sp->root = right;
  199. }
  200. /* free() key if needed */
  201. if (sp->delete_key)
  202. {
  203. /* avoid free(0) */
  204. if (node->key)
  205. {
  206. (*sp->delete_key) (node->key);
  207. }
  208. node->key = (splay_tree_key) 0;
  209. }
  210. /* free() value if needed */
  211. if (sp->delete_value)
  212. {
  213. /* avoid free(0) */
  214. if (node->value)
  215. {
  216. (*sp->delete_value) (node->value);
  217. }
  218. node->value = (splay_tree_value) 0;
  219. }
  220. /* free() node itself and wipe the pointers */
  221. node->left = (splay_tree_node) 0;
  222. node->right = (splay_tree_node) 0;
  223. free ((void *) node);
  224. return;
  225. }
  226. /* Lookup KEY in SP, returning VALUE if present, and NULL
  227. otherwise. */
  228. splay_tree_node
  229. splay_tree_lookup (splay_tree sp, splay_tree_key key)
  230. {
  231. splay_tree_node spn = (splay_tree_node) 0;
  232. if (sp == (splay_tree) 0)
  233. {
  234. /* no tree */
  235. return ((splay_tree_node) 0);
  236. }
  237. if (sp->root == (splay_tree_node) 0)
  238. {
  239. /* no data */
  240. return ((splay_tree_node) 0);
  241. }
  242. if ((*sp->comp) (sp->root->key, key) == 0)
  243. {
  244. /* found */
  245. return ((splay_tree_node) sp->root);
  246. }
  247. spn = splay (sp, key);
  248. if (spn)
  249. {
  250. if ((*sp->comp) (sp->root->key, key) == 0)
  251. {
  252. /* found */
  253. return ((splay_tree_node) sp->root);
  254. }
  255. }
  256. /* not found */
  257. return ((splay_tree_node) 0);
  258. }
  259. /* Call FN, passing it the DATA, for every node in SP, following an
  260. in-order traversal. If FN every returns a non-zero value, the
  261. iteration ceases immediately, and the value is returned.
  262. Otherwise, this function returns 0.
  263. */
  264. int
  265. splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
  266. {
  267. splay_tree_node spn = (splay_tree_node) 0;
  268. splay_tree_key spnkey = (splay_tree_key) 0;
  269. int status = 0;
  270. /* <nil> splaytree */
  271. if (sp == (splay_tree) 0)
  272. {
  273. return (0);
  274. }
  275. /* no data */
  276. if (sp->root == (splay_tree_node) 0)
  277. {
  278. return (0);
  279. }
  280. /* no routine() to run */
  281. if (fn == (splay_tree_foreach_fn) 0)
  282. {
  283. return (0);
  284. }
  285. /* */
  286. status = 0;
  287. spn = splay_tree_min (sp);
  288. while (spn)
  289. {
  290. /* save key if spn disappears */
  291. spnkey = spn->key;
  292. status = (*fn) (spn, data);
  293. if (status)
  294. {
  295. break;
  296. }
  297. spn = splay_tree_successor (sp, spnkey);
  298. }
  299. /* set if callback() routine set it otherwise 0 */
  300. return (status);
  301. }
  302. /* Return the node in SP with the smallest key. */
  303. splay_tree_node
  304. splay_tree_min (splay_tree sp)
  305. {
  306. splay_tree_node n = (splay_tree_node) 0;
  307. /* <nil> splaytree */
  308. if (sp == (splay_tree) 0)
  309. {
  310. return ((splay_tree_node) 0);
  311. }
  312. /* no data */
  313. if (sp->root == (splay_tree_node) 0)
  314. {
  315. return ((splay_tree_node) 0);
  316. }
  317. n = sp->root;
  318. /* scan l */
  319. while (n->left)
  320. {
  321. n = n->left;
  322. }
  323. return ((splay_tree_node) n);
  324. }
  325. /* Return the node in SP with the highest key. */
  326. splay_tree_node
  327. splay_tree_max (splay_tree sp)
  328. {
  329. splay_tree_node n = (splay_tree_node) 0;
  330. /* <nil> splaytree */
  331. if (sp == (splay_tree) 0)
  332. {
  333. return ((splay_tree_node) 0);
  334. }
  335. /* no data */
  336. if (sp->root == (splay_tree_node) 0)
  337. {
  338. return ((splay_tree_node) 0);
  339. }
  340. n = sp->root;
  341. /* scan l */
  342. while (n->right)
  343. {
  344. n = n->right;
  345. }
  346. return ((splay_tree_node) n);
  347. }
  348. /* return 1 if splay tree has data, otherwise 0 */
  349. int
  350. splay_tree_has_data (splay_tree sp)
  351. {
  352. if (sp == (splay_tree) 0)
  353. {
  354. return (0);
  355. }
  356. if (sp->root)
  357. {
  358. return (1);
  359. }
  360. else
  361. {
  362. return (0);
  363. }
  364. }
  365. /* Return the immediate successor KEY, or NULL if there is no
  366. successor. KEY need not be present in the tree. */
  367. splay_tree_node
  368. splay_tree_successor (splay_tree sp, splay_tree_key key)
  369. {
  370. int comparison = 0;
  371. splay_tree_node node = (splay_tree_node) 0;
  372. if (sp == (splay_tree) 0)
  373. {
  374. /* no tree */
  375. return ((splay_tree_node) 0);
  376. }
  377. /* If the tree is empty, there is certainly no successor. */
  378. if (sp->root == (splay_tree_node) 0)
  379. {
  380. return ((splay_tree_node) 0);
  381. }
  382. /* Splay the tree around KEY. That will leave either the KEY
  383. itself, its predecessor, or its successor at the root. */
  384. node = splay (sp, key);
  385. if (node == (splay_tree_node) 0)
  386. {
  387. return ((splay_tree_node) 0);
  388. }
  389. /* */
  390. comparison = (*sp->comp) (sp->root->key, key);
  391. /* If the successor is at the root, just return it. */
  392. if (comparison > 0)
  393. {
  394. return ((splay_tree_node) sp->root);
  395. }
  396. /* Otherwise, find the leftmost element of the right subtree. */
  397. node = sp->root->right;
  398. if (node)
  399. {
  400. while (node->left)
  401. {
  402. node = node->left;
  403. }
  404. }
  405. return ((splay_tree_node) node);
  406. }
  407. /* free() wrappers to free key/value */
  408. void
  409. splay_tree_free_value (splay_tree_value value)
  410. {
  411. if (value)
  412. {
  413. free ((void *) value);
  414. }
  415. return;
  416. }
  417. void
  418. splay_tree_free_key (splay_tree_key key)
  419. {
  420. if (key)
  421. {
  422. free ((void *) key);
  423. }
  424. return;
  425. }
  426. /* Splay-tree comparison function, treating the keys as ints. */
  427. int
  428. splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
  429. {
  430. if ((int) k1 < (int) k2)
  431. {
  432. return ((int) -1);
  433. }
  434. else if ((int) k1 > (int) k2)
  435. {
  436. return (1);
  437. }
  438. else
  439. {
  440. return (0);
  441. }
  442. }
  443. /* Splay-tree comparison function, treating the keys as pointers.
  444. Note this is possibly not portable on all systems according to standards
  445. and may have undefined results. there seems no good solution for this.
  446. (a char datatype does not have to be a single byte in c for example)
  447. */
  448. int
  449. splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
  450. {
  451. /* 0==0 */
  452. if ((k1 == (splay_tree_key) 0) && (k2 == (splay_tree_key) 0))
  453. {
  454. return (0);
  455. }
  456. /* possible undefined results at this, says the c standard. has to be understood as (signed char *) */
  457. if ((char *) k1 < (char *) k2)
  458. {
  459. return ((int) -1);
  460. }
  461. else if ((char *) k1 > (char *) k2)
  462. {
  463. return (1);
  464. }
  465. else
  466. {
  467. return (0);
  468. }
  469. }
  470. /* Comparison function for a splay tree in which the keys are strings.
  471. K1 and K2 have the dynamic type "const char *". Returns <0, 0, or
  472. >0 to indicate whether K1 is less than, equal to, or greater than
  473. K2, respectively.
  474. similar issues here when as compare pointers and c portable src.
  475. */
  476. int
  477. splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2)
  478. {
  479. const char *s1 = (const char *) k1;
  480. const char *s2 = (const char *) k2;
  481. int ret = 0;
  482. if ((k1 == (splay_tree_key) 0) && (k2 == (splay_tree_key) 0))
  483. {
  484. return (0);
  485. }
  486. if (s1 == (const char *) 0)
  487. {
  488. /* to avoid crashes only */
  489. return (0);
  490. }
  491. if (s2 == (const char *) 0)
  492. {
  493. /* to avoid crashes only */
  494. return (0);
  495. }
  496. /* check if same pointer. possible not portable c. */
  497. if (s1 == s2)
  498. {
  499. return (0);
  500. }
  501. ret = strcmp (s1, s2);
  502. return ((int) ret);
  503. }
  504. /* */
  505. static struct splay_tree_node_n *
  506. splay (splay_tree sp, splay_tree_key key)
  507. {
  508. struct splay_tree_node_n *t = (struct splay_tree_node_n *) 0;
  509. struct splay_tree_node_n *l = (struct splay_tree_node_n *) 0;
  510. struct splay_tree_node_n *r = (struct splay_tree_node_n *) 0;
  511. struct splay_tree_node_n *y = (struct splay_tree_node_n *) 0;
  512. int comparevalue = 0;
  513. int comparevalue2 = 0;
  514. struct splay_tree_node_n tmp = {
  515. /* The key. */
  516. (splay_tree_key) 0,
  517. /* The value. */
  518. (splay_tree_value) 0,
  519. /* The left and right children, respectively. */
  520. (struct splay_tree_node_n *) 0, /* left */
  521. (struct splay_tree_node_n *) 0 /* right */
  522. };
  523. /* no tree */
  524. if (sp == (splay_tree) 0)
  525. {
  526. return ((struct splay_tree_node_n *) 0);
  527. }
  528. /* no data in root. return 0 */
  529. if (sp->root == (struct splay_tree_node_n *) 0)
  530. {
  531. return ((struct splay_tree_node_n *) 0);
  532. }
  533. /* current root */
  534. t = sp->root;
  535. tmp.left = (struct splay_tree_node_n *) 0;
  536. tmp.right = (struct splay_tree_node_n *) 0;
  537. l = &tmp;
  538. r = &tmp;
  539. labelstart:
  540. /* */
  541. comparevalue = (*sp->comp) (key, t->key);
  542. if (comparevalue < 0)
  543. {
  544. if (t->left == (struct splay_tree_node_n *) 0)
  545. {
  546. goto labelend;
  547. }
  548. /* */
  549. comparevalue2 = (*sp->comp) (key, t->left->key);
  550. if (comparevalue2 < 0)
  551. {
  552. y = t->left;
  553. t->left = y->right;
  554. y->right = t;
  555. t = y;
  556. if (t->left == (struct splay_tree_node_n *) 0)
  557. {
  558. goto labelend;
  559. }
  560. }
  561. /* */
  562. r->left = t;
  563. r = t;
  564. t = t->left;
  565. }
  566. else if (comparevalue > 0)
  567. {
  568. if (t->right == (struct splay_tree_node_n *) 0)
  569. {
  570. goto labelend;
  571. }
  572. /* */
  573. comparevalue2 = (*sp->comp) (key, t->right->key);
  574. if (comparevalue2 > 0)
  575. {
  576. /* */
  577. y = t->right;
  578. t->right = y->left;
  579. y->left = t;
  580. t = y;
  581. if (t->right == (struct splay_tree_node_n *) 0)
  582. {
  583. goto labelend;
  584. }
  585. }
  586. /* */
  587. l->right = t;
  588. l = t;
  589. t = t->right;
  590. }
  591. else
  592. {
  593. /* here if (comparevalue == 0) */
  594. goto labelend;
  595. }
  596. goto labelstart;
  597. labelend:
  598. l->right = t->left;
  599. r->left = t->right;
  600. t->left = tmp.right;
  601. t->right = tmp.left;
  602. sp->root = t;
  603. return ((struct splay_tree_node_n *) t);
  604. }
  605. /* end */