d4dag.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Pedo Anton
  3. * Junio H Hamano gitster@pobox.com
  4. * rewrite of the GNU GPL javascript sugiyama Copyright Simon Speich 2013, 2021
  5. * The GNU GPL Free version 3 javascript is at https://github.com/speich/dGraph
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * These are the four essential freedoms with GNU GPL software:
  21. * 1: freedom to run the program, for any purpose
  22. * 2: freedom to study how the program works, and change it to make it do what you wish
  23. * 3: freedom to redistribute copies to help your Free Software friends
  24. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  25. * , ,
  26. * / \
  27. * ((__-^^-,-^^-__))
  28. * `-_---' `---_-'
  29. * `--|o` 'o|--'
  30. * \ ` /
  31. * ): :(
  32. * :o_o:
  33. * "-"
  34. *
  35. * This uses modified splay tree algorithm from GNU GCC compiler 12 in directory liberty
  36. * This modified version added new routines and verified correct with 400+ million nodes.
  37. * A splay-tree datatype.
  38. * Copyright (C) 1998-2021 Free Software Foundation, Inc.
  39. * Contributed by Mark Mitchell (mark@markmitchell.com).
  40. * This file is part of GNU CC.
  41. *
  42. * GNU CC is free software; you can redistribute it and/or modify it
  43. * under the terms of the GNU General Public License as published by
  44. * the Free Software Foundation; either version 2, or (at your option)
  45. * any later version.
  46. *
  47. * GNU CC is distributed in the hope that it will be useful, but
  48. * WITHOUT ANY WARRANTY; without even the implied warranty of
  49. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  50. * General Public License for more details.
  51. *
  52. * You should have received a copy of the GNU General Public License
  53. * along with GNU CC; see the file COPYING. If not, write to
  54. * the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  55. * Boston, MA 02110-1301, USA.
  56. *
  57. * For an easily readable description of splay-trees, see:
  58. *
  59. * Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  60. * Algorithms. Harper-Collins, Inc. 1991.
  61. *
  62. * The splay algorithm is a fast lookup based on the observation
  63. * when a item is searched often the next search is close to the
  64. * earlier item and the tree is rearranged for this at every lookup
  65. *
  66. * SPDX-License-Identifier: GPL-3.0+
  67. * License-Filename: LICENSE
  68. */
  69. /**
  70. * all routines have prefix d4d_
  71. * all static routines and vars have prefix d4d___
  72. * the prefix d4d_ has no special meaning but hopefully
  73. * does not problems because of limited C namespace
  74. */
  75. /* for positioning the radare2 buchheim algorithm can be used */
  76. /* only in debug version linkage needed to printf */
  77. #if D4D_DEBUG
  78. #include <stdio.h>
  79. #define print_debug(str, ...) printf(str" - %s:%u\n", ##__VA_ARGS__, __FILE__, __LINE__);
  80. #endif
  81. /* datatypoes used are
  82. * int, as signed 32bits integer value
  83. * unsigned int, as 32bits integer value
  84. * char, as signed 8bits integer value
  85. * unsigned char, as 8bits integer value
  86. * unsigned long long int, as 64 bits unsigned integer value which can hold a 64 bits pointer
  87. */
  88. /**
  89. * set here version number as int
  90. */
  91. #define D4DAG_VERSION 10
  92. /* prefix local routines */
  93. #define CN(x) d4dag___ ## x
  94. /* defs and prototypes of routines for user program */
  95. #include "d4dag.h"
  96. /* mallocer/freeer */
  97. typedef void *(*malloc_fn)(unsigned int n);
  98. typedef void (*free_fn)(void *ptr);
  99. /* this should be at least 64bits */
  100. #ifndef uintptr_t
  101. typedef unsigned long long int uintptr_t;
  102. #endif
  103. /* Use typedefs for the key and data types to facilitate changing
  104. these types, if necessary. These types should be sufficiently wide
  105. that any pointer or scalar can be cast to these types, and then
  106. cast back, without loss of precision. */
  107. typedef uintptr_t splay_tree_key; /* 64bits unsigned int */
  108. typedef uintptr_t splay_tree_value;
  109. /* Forward declaration for a node in the tree. */
  110. typedef struct splay_tree_node_s *splay_tree_node;
  111. /* The type of a function which compares two splay-tree keys. The
  112. function should return values as for qsort. */
  113. typedef int (*splay_tree_compare_fn)(splay_tree_key, splay_tree_key);
  114. /* The type of a function used to deallocate any resources associated
  115. with the key. If you provide this function, the splay tree
  116. will take the ownership of the memory of the splay_tree_key arg
  117. of splay_tree_insert. This function is called to release the keys
  118. present in the tree when calling splay_tree_delete or splay_tree_remove.
  119. If splay_tree_insert is called with a key equal to a key already
  120. present in the tree, the old key and old value will be released. */
  121. typedef void (*splay_tree_delete_key_fn)(splay_tree_key);
  122. /* The type of a function used to deallocate any resources associated
  123. with the value. If you provide this function, the memory of the
  124. splay_tree_value arg of splay_tree_insert is managed similarly to
  125. the splay_tree_key memory: see splay_tree_delete_key_fn. */
  126. typedef void (*splay_tree_delete_value_fn)(splay_tree_value);
  127. /* The type of a function used to iterate over the tree. */
  128. typedef int (*splay_tree_foreach_fn)(splay_tree_node, void *);
  129. /* The type of a function used to allocate memory for tree root and
  130. node structures. The first argument is the number of bytes needed;
  131. the second is a data pointer the splay tree functions pass through
  132. to the allocator. This function must never return zero. */
  133. /* old typedef void *(*splay_tree_allocate_fn)(int, void *); */
  134. typedef void *(*splay_tree_allocate_fn)(unsigned int, void *);
  135. /* The type of a function used to free memory allocated using the
  136. corresponding splay_tree_allocate_fn. The first argument is the
  137. memory to be freed; the latter is a data pointer the splay tree
  138. functions pass through to the freer. */
  139. typedef void (*splay_tree_deallocate_fn)(void *, void *);
  140. /* The nodes in the splay tree. */
  141. struct splay_tree_node_s {
  142. /* The key. */
  143. splay_tree_key key;
  144. /* The value. */
  145. splay_tree_value value;
  146. /* The left and right children, respectively. */
  147. splay_tree_node left;
  148. splay_tree_node right;
  149. };
  150. /* The splay tree itself. */
  151. struct splay_tree_s {
  152. /* The root of the tree. */
  153. splay_tree_node root;
  154. /* The comparision function. */
  155. splay_tree_compare_fn comp;
  156. /* The deallocate-key function. NULL if no cleanup is necessary. */
  157. splay_tree_delete_key_fn delete_key;
  158. /* The deallocate-value function. NULL if no cleanup is necessary. */
  159. splay_tree_delete_value_fn delete_value;
  160. /* Node allocate function. Takes allocate_data as a parameter. */
  161. splay_tree_allocate_fn allocate;
  162. /* Free function for nodes and trees. Takes allocate_data as a parameter. */
  163. splay_tree_deallocate_fn deallocate;
  164. /* Parameter for allocate/free functions. */
  165. void *allocate_data;
  166. };
  167. typedef struct splay_tree_s *splay_tree;
  168. /* toplevel control struct */
  169. struct d4d__maing {
  170. /* wrappers for malloc/free and all malloc's will be below 4Gb at once */
  171. malloc_fn d4d__malloc;
  172. free_fn d4d__free;
  173. };
  174. /* splay from gcc compiler */
  175. static void splay_tree_delete_helper(splay_tree sp, splay_tree_node node);
  176. static void rotate_left(splay_tree_node * pp, splay_tree_node p, splay_tree_node n);
  177. static void rotate_right(splay_tree_node * pp, splay_tree_node p, splay_tree_node n);
  178. static void splay_tree_splay(splay_tree sp, splay_tree_key key);
  179. static void *splay_tree_xmalloc_allocate(unsigned int size, void *data);
  180. static void splay_tree_xmalloc_deallocate(void *object, void *data);
  181. static splay_tree splay_tree_new(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn);
  182. static void CN(splay_tree_delete) (splay_tree sp);
  183. static splay_tree_node CN(splay_tree_insert) (splay_tree sp, splay_tree_key key, splay_tree_value value);
  184. static void CN(splay_tree_remove) (splay_tree sp, splay_tree_key key);
  185. static splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key key);
  186. static splay_tree_node splay_tree_max(splay_tree sp);
  187. static splay_tree_node CN(splay_tree_min) (splay_tree sp);
  188. static splay_tree_node CN(splay_tree_predecessor) (splay_tree sp, splay_tree_key key);
  189. static splay_tree_node splay_tree_successor(splay_tree sp, splay_tree_key key);
  190. static int splay_tree_foreach(splay_tree sp, splay_tree_foreach_fn fn, void *data);
  191. static int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2);
  192. static int splay_tree_compare_pointers(splay_tree_key k1, splay_tree_key k2);
  193. static int splay_tree_compare_strings(splay_tree_key k1, splay_tree_key k2);
  194. static void splay_tree_delete_pointers(splay_tree_value value);
  195. static splay_tree splay_tree_new_typed_alloc(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn, splay_tree_allocate_fn tree_allocate_fn,
  196. splay_tree_allocate_fn node_allocate_fn, splay_tree_deallocate_fn deallocate_fn, void *allocate_data);
  197. static splay_tree splay_tree_new_with_allocator(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn, splay_tree_allocate_fn allocate_fn,
  198. splay_tree_deallocate_fn deallocate_fn, void *allocate_data);
  199. /* main control */
  200. static struct d4d__maing *d4d__main = (struct d4d__maing *)0;
  201. /* forward decl. */
  202. static void d4d__memzero(void *ptr, unsigned int n);
  203. /**
  204. * returns version number as int
  205. */
  206. int d4d_version(void)
  207. {
  208. return (D4DAG_VERSION);
  209. }
  210. /**
  211. *
  212. * returns:
  213. * 0 if oke
  214. * -1 if missing malloc/free pointer
  215. * -2 if malloc failed
  216. * For embedded devices this source has no linkage to libraries
  217. * which means here pointer to malloc/free must be supplied.
  218. * when malloc returns zero then these routines crashes
  219. */
  220. int d4d_init(void *(*mallocer)(unsigned int n), void (*freeer)(void *ptr))
  221. {
  222. /* char tenbytes[10];
  223. * sizeof(0, tenbytes) is 10 when compiled as c++ and 8 when compiled as c
  224. */
  225. /* malloc/free pointers must be specified */
  226. if (!mallocer) {
  227. return (-1);
  228. }
  229. if (!freeer) {
  230. return (-1);
  231. }
  232. d4d__main = (struct d4d__maing *)(*mallocer) ((unsigned int)sizeof(struct d4d__maing));
  233. if (!d4d__main) {
  234. return (-2);
  235. }
  236. /* set pointers to malloc/free in toplevel control */
  237. d4d__memzero(d4d__main, sizeof(struct d4d__maing));
  238. d4d__main->d4d__malloc = mallocer;
  239. d4d__main->d4d__free = freeer;
  240. /* small splay tree test */
  241. {
  242. splay_tree spt = (splay_tree) 0;
  243. unsigned long long int n = 0; /* 64bits */
  244. splay_tree_node spn = (splay_tree_node) 0;
  245. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  246. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  247. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  248. );
  249. /* create small splay tree does not use realloc() */
  250. for (n = 0; n < 10; n++) {
  251. spn /* splay_tree_node */ =
  252. CN(splay_tree_insert) ((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  253. if (!spn) { /* shouldnothappen */
  254. }
  255. }
  256. CN(splay_tree_delete) ((splay_tree) spt);
  257. }
  258. return (0);
  259. }
  260. /**
  261. *
  262. */
  263. int d4d_deinit(void)
  264. {
  265. /* check if active */
  266. if (!d4d__main) {
  267. return (0);
  268. }
  269. /* free structs */
  270. /* free control */
  271. d4d__main->d4d__free(d4d__main);
  272. d4d__main = (struct d4d__maing *)0;
  273. return (0);
  274. }
  275. /* like memset(ptr,0,n)
  276. * note that intel icc compiler does inline this and gcc not
  277. */
  278. static void d4d__memzero(void *ptr, unsigned int n)
  279. {
  280. unsigned char *p = (unsigned char *)0;
  281. p = (unsigned char *)ptr;
  282. while (n) {
  283. *p = 0;
  284. p++;
  285. n--;
  286. }
  287. return;
  288. }
  289. /* zz3 marker */
  290. /* Deallocate NODE (a member of SP), and all its sub-trees. */
  291. static void splay_tree_delete_helper(splay_tree sp, splay_tree_node node)
  292. {
  293. splay_tree_node pending = (splay_tree_node) 0;
  294. splay_tree_node active = (splay_tree_node) 0;
  295. if (!node) {
  296. return;
  297. }
  298. if (sp->delete_key) {
  299. (*sp->delete_key) (node->key);
  300. }
  301. if (sp->delete_value) {
  302. (*sp->delete_value) (node->value);
  303. }
  304. /* We use the "key" field to hold the "next" pointer. */
  305. node->key = (splay_tree_key) pending;
  306. pending = (splay_tree_node) node;
  307. /* Now, keep processing the pending list until there aren't any
  308. more. This is a little more complicated than just recursing, but
  309. it doesn't toast the stack for large trees. */
  310. while (pending) {
  311. active = pending;
  312. pending = 0;
  313. while (active) {
  314. splay_tree_node temp = (splay_tree_node) 0;
  315. /* active points to a node which has its key and value
  316. deallocated, we just need to process left and right. */
  317. if (active->left) {
  318. if (sp->delete_key) {
  319. (*sp->delete_key) (active->left->key);
  320. }
  321. if (sp->delete_value) {
  322. (*sp->delete_value)
  323. (active->left->value);
  324. }
  325. active->left->key = (splay_tree_key) pending;
  326. pending = (splay_tree_node) (active->left);
  327. }
  328. if (active->right) {
  329. if (sp->delete_key) {
  330. (*sp->delete_key) (active->right->key);
  331. }
  332. if (sp->delete_value) {
  333. (*sp->delete_value) (active->right->value);
  334. }
  335. active->right->key = (splay_tree_key) pending;
  336. pending = (splay_tree_node) (active->right);
  337. }
  338. temp = active;
  339. active = (splay_tree_node) (temp->key);
  340. (*sp->deallocate) ((char *)temp, sp->allocate_data);
  341. }
  342. }
  343. return;
  344. }
  345. /* Rotate the edge joining the left child N with its parent P. PP is the
  346. grandparents' pointer to P. */
  347. static void rotate_left(splay_tree_node * pp, splay_tree_node p, splay_tree_node n)
  348. {
  349. splay_tree_node tmp = (splay_tree_node) 0;
  350. tmp = n->right;
  351. n->right = p;
  352. p->left = tmp;
  353. *pp = n;
  354. return;
  355. }
  356. /* Rotate the edge joining the right child N with its parent P. PP is the
  357. grandparents' pointer to P. */
  358. static void rotate_right(splay_tree_node * pp, splay_tree_node p, splay_tree_node n)
  359. {
  360. splay_tree_node tmp = (splay_tree_node) 0;
  361. tmp = n->left;
  362. n->left = p;
  363. p->right = tmp;
  364. *pp = n;
  365. return;
  366. }
  367. /* Bottom up splay of key. */
  368. static void splay_tree_splay(splay_tree sp, splay_tree_key key)
  369. {
  370. int cmp1 = 0;
  371. int cmp2 = 0;;
  372. splay_tree_node n = (splay_tree_node) 0;
  373. splay_tree_node c = (splay_tree_node) 0;
  374. if (!sp->root) {
  375. /* no data */
  376. return;
  377. }
  378. do {
  379. n = sp->root;
  380. cmp1 = (*sp->comp) (key, n->key);
  381. /* Found. */
  382. if (cmp1 == 0) {
  383. return;
  384. }
  385. /* Left or right? If no child, then we're done. */
  386. if (cmp1 < 0) {
  387. c = n->left;
  388. } else {
  389. c = n->right;
  390. }
  391. if (!c) {
  392. return;
  393. }
  394. /* Next one left or right? If found or no child, we're done
  395. after one rotation. */
  396. cmp2 = (*sp->comp) (key, c->key);
  397. if ((cmp2 == 0) || ((cmp2 < 0) && (!c->left)) || ((cmp2 > 0) && (!c->right))) {
  398. if (cmp1 < 0) {
  399. rotate_left(&sp->root, n, c);
  400. } else {
  401. rotate_right(&sp->root, n, c);
  402. }
  403. return;
  404. }
  405. /* Now we have the four cases of double-rotation. */
  406. if ((cmp1 < 0) && (cmp2 < 0)) {
  407. rotate_left(&n->left, c, c->left);
  408. rotate_left(&sp->root, n, n->left);
  409. } else if ((cmp1 > 0) && (cmp2 > 0)) {
  410. rotate_right(&n->right, c, c->right);
  411. rotate_right(&sp->root, n, n->right);
  412. } else if ((cmp1 < 0) && (cmp2 > 0)) {
  413. rotate_right(&n->left, c, c->right);
  414. rotate_left(&sp->root, n, n->left);
  415. } else if ((cmp1 > 0) && (cmp2 < 0)) {
  416. rotate_left(&n->right, c, c->left);
  417. rotate_right(&sp->root, n, n->right);
  418. } else {
  419. /* nop shouldnothappen */
  420. }
  421. }
  422. while (1);
  423. return;
  424. }
  425. /* An allocator and deallocator based on xmalloc. */
  426. static void *splay_tree_xmalloc_allocate(unsigned int size, void *data)
  427. {
  428. void *ret = (void *)0;
  429. if (data) { /* not used */
  430. }
  431. ret = (void *)d4d__main->d4d__malloc(size);
  432. d4d__memzero(ret, size);
  433. return (ret);
  434. }
  435. static void splay_tree_xmalloc_deallocate(void *object, void *data)
  436. {
  437. if (object) {
  438. d4d__main->d4d__free(object);
  439. }
  440. if (data) { /* not used */
  441. }
  442. return;
  443. }
  444. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  445. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  446. values. Use xmalloc to allocate the splay tree structure, and any
  447. nodes added. */
  448. static splay_tree splay_tree_new(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn)
  449. {
  450. return (splay_tree_new_with_allocator(compare_fn, delete_key_fn, delete_value_fn, splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
  451. }
  452. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  453. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  454. values. */
  455. static splay_tree splay_tree_new_with_allocator(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn, splay_tree_allocate_fn allocate_fn,
  456. splay_tree_deallocate_fn deallocate_fn, void *allocate_data)
  457. {
  458. return splay_tree_new_typed_alloc(compare_fn, delete_key_fn, delete_value_fn, allocate_fn, allocate_fn, deallocate_fn, allocate_data);
  459. }
  460. /*
  461. @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
  462. (splay_tree_compare_fn @var{compare_fn}, @
  463. splay_tree_delete_key_fn @var{delete_key_fn}, @
  464. splay_tree_delete_value_fn @var{delete_value_fn}, @
  465. splay_tree_allocate_fn @var{tree_allocate_fn}, @
  466. splay_tree_allocate_fn @var{node_allocate_fn}, @
  467. splay_tree_deallocate_fn @var{deallocate_fn}, @
  468. void * @var{allocate_data})
  469. This function creates a splay tree that uses two different allocators
  470. @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
  471. tree itself and its nodes respectively. This is useful when variables of
  472. different types need to be allocated with different allocators.
  473. The splay tree will use @var{compare_fn} to compare nodes,
  474. @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
  475. deallocate values. Keys and values will be deallocated when the
  476. tree is deleted using splay_tree_delete or when a node is removed
  477. using splay_tree_remove. splay_tree_insert will release the previously
  478. inserted key and value using @var{delete_key_fn} and @var{delete_value_fn}
  479. if the inserted key is already found in the tree.
  480. @end deftypefn
  481. */
  482. static splay_tree splay_tree_new_typed_alloc(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn, splay_tree_allocate_fn tree_allocate_fn,
  483. splay_tree_allocate_fn node_allocate_fn, splay_tree_deallocate_fn deallocate_fn, void *allocate_data)
  484. {
  485. splay_tree sp = (splay_tree) (*tree_allocate_fn)
  486. (sizeof(struct splay_tree_s), allocate_data);
  487. sp->root = 0;
  488. sp->comp = compare_fn;
  489. sp->delete_key = delete_key_fn;
  490. sp->delete_value = delete_value_fn;
  491. sp->allocate = node_allocate_fn;
  492. sp->deallocate = deallocate_fn;
  493. sp->allocate_data = allocate_data;
  494. return sp;
  495. }
  496. /* Deallocate SP. */
  497. static void CN(splay_tree_delete) (splay_tree sp) {
  498. splay_tree_delete_helper(sp, sp->root);
  499. (*sp->deallocate) ((char *)sp, sp->allocate_data);
  500. return;
  501. }
  502. /* Insert a new node (associating KEY with DATA) into SP. If a
  503. previous node with the indicated KEY exists, its data is replaced
  504. with the new value. Returns the new node. */
  505. static splay_tree_node CN(splay_tree_insert) (splay_tree sp, splay_tree_key key, splay_tree_value value) {
  506. int comparison = 0;
  507. splay_tree_node node = (splay_tree_node) 0;
  508. splay_tree_splay(sp, key);
  509. if (sp->root) {
  510. comparison = (*sp->comp) (sp->root->key, key);
  511. }
  512. if (sp->root && (comparison == 0)) {
  513. /* If the root of the tree already has the indicated KEY, delete
  514. the old key and old value, and replace them with KEY and VALUE. */
  515. if (sp->delete_key) {
  516. (*sp->delete_key) (sp->root->key);
  517. }
  518. if (sp->delete_value) {
  519. (*sp->delete_value) (sp->root->value);
  520. }
  521. sp->root->key = key;
  522. sp->root->value = value;
  523. } else {
  524. /* Create a new node, and insert it at the root. */
  525. node = ((splay_tree_node)
  526. (*sp->allocate) (sizeof(struct splay_tree_node_s), sp->allocate_data));
  527. node->key = key;
  528. node->value = value;
  529. if (!sp->root) {
  530. node->left = (splay_tree_node) 0;
  531. node->right = (splay_tree_node) 0;
  532. } else if (comparison < 0) {
  533. node->left = sp->root;
  534. node->right = node->left->right;
  535. node->left->right = (splay_tree_node) 0;
  536. } else {
  537. node->right = sp->root;
  538. node->left = node->right->left;
  539. node->right->left = (splay_tree_node) 0;
  540. }
  541. sp->root = node;
  542. }
  543. return sp->root;
  544. }
  545. /* Remove KEY from SP. It is not an error if it did not exist. */
  546. static void CN(splay_tree_remove) (splay_tree sp, splay_tree_key key) {
  547. splay_tree_node left = (splay_tree_node) 0;
  548. splay_tree_node right = (splay_tree_node) 0;
  549. splay_tree_splay(sp, key);
  550. if (sp->root && ((*sp->comp) (sp->root->key, key) == 0)) {
  551. left = sp->root->left;
  552. right = sp->root->right;
  553. /* Delete the root node itself. */
  554. if (sp->delete_key) {
  555. (*sp->delete_key) (sp->root->key);
  556. }
  557. if (sp->delete_value) {
  558. (*sp->delete_value) (sp->root->value);
  559. }
  560. (*sp->deallocate) (sp->root, sp->allocate_data);
  561. /* One of the children is now the root. Doesn't matter much
  562. which, so long as we preserve the properties of the tree. */
  563. if (left) {
  564. sp->root = left;
  565. /* If there was a right child as well, hang it off the
  566. right-most leaf of the left child. */
  567. if (right) {
  568. while (left->right) {
  569. left = left->right;
  570. }
  571. left->right = right;
  572. }
  573. } else
  574. sp->root = right;
  575. }
  576. return;
  577. }
  578. /* Lookup KEY in SP, returning VALUE if present, and NULL
  579. otherwise. */
  580. static splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key key)
  581. {
  582. splay_tree_splay(sp, key);
  583. if (sp->root && ((*sp->comp) (sp->root->key, key) == 0)) {
  584. return sp->root;
  585. } else {
  586. return (splay_tree_node) 0;
  587. }
  588. }
  589. /* Return the node in SP with the greatest key. */
  590. static splay_tree_node splay_tree_max(splay_tree sp)
  591. {
  592. splay_tree_node n = sp->root;
  593. if (!n) {
  594. return (splay_tree_node) 0;
  595. }
  596. while (n->right) {
  597. n = n->right;
  598. }
  599. return n;
  600. }
  601. /* Return the node in SP with the smallest key. */
  602. static splay_tree_node CN(splay_tree_min) (splay_tree sp) {
  603. splay_tree_node n = (splay_tree_node) 0;
  604. if ((splay_tree) 0 == sp) {
  605. return (splay_tree_node) 0;
  606. }
  607. n = sp->root;
  608. if ((splay_tree_node) 0 == n) {
  609. return (splay_tree_node) 0;
  610. }
  611. /* now follow left */
  612. while (n->left) {
  613. n = n->left;
  614. }
  615. return n;
  616. }
  617. /* Return the immediate predecessor KEY, or NULL if there is no
  618. predecessor. KEY need not be present in the tree. */
  619. static splay_tree_node CN(splay_tree_predecessor) (splay_tree sp, splay_tree_key key) {
  620. int comparison = 0;
  621. splay_tree_node node = (splay_tree_node) 0;
  622. /* no tree, no crash. */
  623. if ((splay_tree) 0 == sp) {
  624. return (splay_tree_node) 0;
  625. }
  626. /* If the tree is empty, there is certainly no predecessor. */
  627. if (sp->root == (splay_tree_node) 0) {
  628. return (splay_tree_node) 0;
  629. }
  630. /* Splay the tree around KEY. That will leave either the KEY
  631. itself, its predecessor, or its successor at the root. */
  632. splay_tree_splay(sp, key);
  633. comparison = (*sp->comp) (sp->root->key, key);
  634. /* If the predecessor is at the root, just return it. */
  635. if (comparison < 0) {
  636. return sp->root;
  637. }
  638. /* Otherwise, find the rightmost element of the left subtree. */
  639. node = sp->root->left;
  640. if (node) {
  641. while (node->right) {
  642. node = node->right;
  643. }
  644. }
  645. return node;
  646. }
  647. /* Return the immediate successor KEY, or NULL if there is no
  648. successor. KEY need not be present in the tree. */
  649. static splay_tree_node splay_tree_successor(splay_tree sp, splay_tree_key key)
  650. {
  651. int comparison = 0;
  652. splay_tree_node node = (splay_tree_node) 0;
  653. /* If the tree is empty, there is certainly no successor. */
  654. if (!sp->root) {
  655. return (splay_tree_node) 0;
  656. }
  657. /* Splay the tree around KEY. That will leave either the KEY
  658. itself, its predecessor, or its successor at the root. */
  659. splay_tree_splay(sp, key);
  660. comparison = (*sp->comp) (sp->root->key, key);
  661. /* If the successor is at the root, just return it. */
  662. if (comparison > 0) {
  663. return sp->root;
  664. }
  665. /* Otherwise, find the leftmost element of the right subtree. */
  666. node = sp->root->right;
  667. if (node) {
  668. while (node->left) {
  669. node = node->left;
  670. }
  671. }
  672. return node;
  673. }
  674. /* Call FN, passing it the DATA, for every node in SP, following an
  675. in-order traversal. If FN every returns a non-zero value, the
  676. iteration ceases immediately, and the value is returned.
  677. Otherwise, this function returns 0. */
  678. /* slower other way does not use realloc() */
  679. static int splay_tree_foreach(splay_tree sp, splay_tree_foreach_fn fn, void *data)
  680. {
  681. splay_tree_node spn = (splay_tree_node) 0;
  682. splay_tree_key key = (splay_tree_key) 0;
  683. int val = 0;
  684. if ((splay_tree) 0 == sp) {
  685. /* no data */
  686. return (0);
  687. }
  688. if (!sp->root) {
  689. /* no data */
  690. return (0);
  691. }
  692. /* this is slow but will run with bigger splay tree */
  693. val = 0;
  694. spn = CN(splay_tree_min) (sp);
  695. while (spn) {
  696. key = (splay_tree_key) spn->key;
  697. val = (*fn) (spn, data);
  698. /* stop if callback routine returns <> 0 */
  699. if (val) {
  700. break;
  701. }
  702. spn = splay_tree_successor(sp, key);
  703. }
  704. return (val);
  705. }
  706. /* Splay-tree comparison function, treating the keys as ints. */
  707. static int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2)
  708. {
  709. if ((int)k1 < (int)k2) {
  710. return -1;
  711. } else if ((int)k1 > (int)k2) {
  712. return 1;
  713. } else {
  714. return 0;
  715. }
  716. }
  717. /* Splay-tree comparison function, treating the keys as pointers. */
  718. static int splay_tree_compare_pointers(splay_tree_key k1, splay_tree_key k2)
  719. {
  720. if ((char *)k1 < (char *)k2) {
  721. return -1;
  722. } else if ((char *)k1 > (char *)k2) {
  723. return 1;
  724. } else {
  725. return 0;
  726. }
  727. }
  728. /* Splay-tree comparison function, treating the keys as strings. strcmp() */
  729. static int splay_tree_compare_strings(splay_tree_key k1, splay_tree_key k2)
  730. {
  731. char *s1 = (char *)k1;
  732. char *s2 = (char *)k2;
  733. while (*s1 && (*s1 == *s2)) {
  734. s1++;
  735. s2++;
  736. }
  737. return (*s1 - *s2);
  738. }
  739. /* Splay-tree delete function, simply using free. */
  740. static void splay_tree_delete_pointers(splay_tree_value value)
  741. {
  742. if (value) {
  743. d4d__main->d4d__free((void *)value);
  744. }
  745. return;
  746. }
  747. /* end. */