avl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /* Produced by texiweb from libavl.w on 2002/02/09 at 01:45. */
  2. /* libavl - library for manipulation of binary trees.
  3. Copyright (C) 1998-2002 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. The author may be contacted at <blp@gnu.org> on the Internet, or
  17. as Ben Pfaff, 12167 Airport Rd, DeWitt MI 48820, USA through more
  18. mundane means.
  19. */
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "avl.h"
  25. /* Creates and returns a new table
  26. with comparison function |compare| using parameter |param|
  27. and memory allocator |allocator|.
  28. Returns |NULL| if memory allocation failed. */
  29. struct avl_table *avl_create(avl_comparison_func * compare, void *param,
  30. struct libavl_allocator *allocator)
  31. {
  32. struct avl_table *tree;
  33. assert(compare != NULL);
  34. if (allocator == NULL)
  35. allocator = &avl_allocator_default;
  36. tree = allocator->libavl_malloc(allocator, sizeof *tree);
  37. if (tree == NULL)
  38. return NULL;
  39. tree->avl_root = NULL;
  40. tree->avl_compare = compare;
  41. tree->avl_param = param;
  42. tree->avl_alloc = allocator;
  43. tree->avl_count = 0;
  44. tree->avl_generation = 0;
  45. return tree;
  46. }
  47. /* Search |tree| for an item matching |item|, and return it if found.
  48. Otherwise return |NULL|. */
  49. void *avl_find(const struct avl_table *tree, const void *item)
  50. {
  51. const struct avl_node *p;
  52. assert(tree != NULL && item != NULL);
  53. for (p = tree->avl_root; p != NULL;) {
  54. int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param);
  55. if (cmp < 0)
  56. p = p->avl_link[0];
  57. else if (cmp > 0)
  58. p = p->avl_link[1];
  59. else /* |cmp == 0| */
  60. return p->avl_data;
  61. }
  62. return NULL;
  63. }
  64. /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
  65. If a duplicate item is found in the tree,
  66. returns a pointer to the duplicate without inserting |item|.
  67. Returns |NULL| in case of memory allocation failure. */
  68. void **avl_probe(struct avl_table *tree, void *item)
  69. {
  70. struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
  71. struct avl_node *p, *q; /* Iterator, and parent. */
  72. struct avl_node *n; /* Newly inserted node. */
  73. struct avl_node *w; /* New root of rebalanced subtree. */
  74. int dir; /* Direction to descend. */
  75. unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
  76. int k = 0; /* Number of cached results. */
  77. assert(tree != NULL && item != NULL);
  78. z = (struct avl_node *)&tree->avl_root;
  79. y = tree->avl_root;
  80. dir = 0;
  81. for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir]) {
  82. int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param);
  83. if (cmp == 0)
  84. return &p->avl_data;
  85. if (p->avl_balance != 0)
  86. z = q, y = p, k = 0;
  87. da[k++] = dir = cmp > 0;
  88. }
  89. n = q->avl_link[dir] =
  90. tree->avl_alloc->libavl_malloc(tree->avl_alloc, sizeof *n);
  91. if (n == NULL)
  92. return NULL;
  93. tree->avl_count++;
  94. n->avl_data = item;
  95. n->avl_link[0] = n->avl_link[1] = NULL;
  96. n->avl_balance = 0;
  97. if (y == NULL)
  98. return &n->avl_data;
  99. for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
  100. if (da[k] == 0)
  101. p->avl_balance--;
  102. else
  103. p->avl_balance++;
  104. if (y->avl_balance == -2) {
  105. struct avl_node *x = y->avl_link[0];
  106. if (x->avl_balance == -1) {
  107. w = x;
  108. y->avl_link[0] = x->avl_link[1];
  109. x->avl_link[1] = y;
  110. x->avl_balance = y->avl_balance = 0;
  111. }
  112. else {
  113. assert(x->avl_balance == +1);
  114. w = x->avl_link[1];
  115. x->avl_link[1] = w->avl_link[0];
  116. w->avl_link[0] = x;
  117. y->avl_link[0] = w->avl_link[1];
  118. w->avl_link[1] = y;
  119. if (w->avl_balance == -1)
  120. x->avl_balance = 0, y->avl_balance = +1;
  121. else if (w->avl_balance == 0)
  122. x->avl_balance = y->avl_balance = 0;
  123. else /* |w->avl_balance == +1| */
  124. x->avl_balance = -1, y->avl_balance = 0;
  125. w->avl_balance = 0;
  126. }
  127. }
  128. else if (y->avl_balance == +2) {
  129. struct avl_node *x = y->avl_link[1];
  130. if (x->avl_balance == +1) {
  131. w = x;
  132. y->avl_link[1] = x->avl_link[0];
  133. x->avl_link[0] = y;
  134. x->avl_balance = y->avl_balance = 0;
  135. }
  136. else {
  137. assert(x->avl_balance == -1);
  138. w = x->avl_link[0];
  139. x->avl_link[0] = w->avl_link[1];
  140. w->avl_link[1] = x;
  141. y->avl_link[1] = w->avl_link[0];
  142. w->avl_link[0] = y;
  143. if (w->avl_balance == +1)
  144. x->avl_balance = 0, y->avl_balance = -1;
  145. else if (w->avl_balance == 0)
  146. x->avl_balance = y->avl_balance = 0;
  147. else /* |w->avl_balance == -1| */
  148. x->avl_balance = +1, y->avl_balance = 0;
  149. w->avl_balance = 0;
  150. }
  151. }
  152. else
  153. return &n->avl_data;
  154. z->avl_link[y != z->avl_link[0]] = w;
  155. tree->avl_generation++;
  156. return &n->avl_data;
  157. }
  158. /* Inserts |item| into |table|.
  159. Returns |NULL| if |item| was successfully inserted
  160. or if a memory allocation error occurred.
  161. Otherwise, returns the duplicate item. */
  162. void *avl_insert(struct avl_table *table, void *item)
  163. {
  164. void **p = avl_probe(table, item);
  165. return p == NULL || *p == item ? NULL : *p;
  166. }
  167. /* Inserts |item| into |table|, replacing any duplicate item.
  168. Returns |NULL| if |item| was inserted without replacing a duplicate,
  169. or if a memory allocation error occurred.
  170. Otherwise, returns the item that was replaced. */
  171. void *avl_replace(struct avl_table *table, void *item)
  172. {
  173. void **p = avl_probe(table, item);
  174. if (p == NULL || *p == item)
  175. return NULL;
  176. else {
  177. void *r = *p;
  178. *p = item;
  179. return r;
  180. }
  181. }
  182. /* Deletes from |tree| and returns an item matching |item|.
  183. Returns a null pointer if no matching item found. */
  184. void *avl_delete(struct avl_table *tree, const void *item)
  185. {
  186. /* Stack of nodes. */
  187. struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
  188. unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
  189. int k; /* Stack pointer. */
  190. struct avl_node *p; /* Traverses tree to find node to delete. */
  191. int cmp; /* Result of comparison between |item| and |p|. */
  192. assert(tree != NULL && item != NULL);
  193. k = 0;
  194. p = (struct avl_node *)&tree->avl_root;
  195. for (cmp = -1; cmp != 0;
  196. cmp = tree->avl_compare(item, p->avl_data, tree->avl_param)) {
  197. int dir = cmp > 0;
  198. pa[k] = p;
  199. da[k++] = dir;
  200. p = p->avl_link[dir];
  201. if (p == NULL)
  202. return NULL;
  203. }
  204. item = p->avl_data;
  205. if (p->avl_link[1] == NULL)
  206. pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
  207. else {
  208. struct avl_node *r = p->avl_link[1];
  209. if (r->avl_link[0] == NULL) {
  210. r->avl_link[0] = p->avl_link[0];
  211. r->avl_balance = p->avl_balance;
  212. pa[k - 1]->avl_link[da[k - 1]] = r;
  213. da[k] = 1;
  214. pa[k++] = r;
  215. }
  216. else {
  217. struct avl_node *s;
  218. int j = k++;
  219. for (;;) {
  220. da[k] = 0;
  221. pa[k++] = r;
  222. s = r->avl_link[0];
  223. if (s->avl_link[0] == NULL)
  224. break;
  225. r = s;
  226. }
  227. s->avl_link[0] = p->avl_link[0];
  228. r->avl_link[0] = s->avl_link[1];
  229. s->avl_link[1] = p->avl_link[1];
  230. s->avl_balance = p->avl_balance;
  231. pa[j - 1]->avl_link[da[j - 1]] = s;
  232. da[j] = 1;
  233. pa[j] = s;
  234. }
  235. }
  236. tree->avl_alloc->libavl_free(tree->avl_alloc, p);
  237. assert(k > 0);
  238. while (--k > 0) {
  239. struct avl_node *y = pa[k];
  240. if (da[k] == 0) {
  241. y->avl_balance++;
  242. if (y->avl_balance == +1)
  243. break;
  244. else if (y->avl_balance == +2) {
  245. struct avl_node *x = y->avl_link[1];
  246. if (x->avl_balance == -1) {
  247. struct avl_node *w;
  248. assert(x->avl_balance == -1);
  249. w = x->avl_link[0];
  250. x->avl_link[0] = w->avl_link[1];
  251. w->avl_link[1] = x;
  252. y->avl_link[1] = w->avl_link[0];
  253. w->avl_link[0] = y;
  254. if (w->avl_balance == +1)
  255. x->avl_balance = 0, y->avl_balance = -1;
  256. else if (w->avl_balance == 0)
  257. x->avl_balance = y->avl_balance = 0;
  258. else /* |w->avl_balance == -1| */
  259. x->avl_balance = +1, y->avl_balance = 0;
  260. w->avl_balance = 0;
  261. pa[k - 1]->avl_link[da[k - 1]] = w;
  262. }
  263. else {
  264. y->avl_link[1] = x->avl_link[0];
  265. x->avl_link[0] = y;
  266. pa[k - 1]->avl_link[da[k - 1]] = x;
  267. if (x->avl_balance == 0) {
  268. x->avl_balance = -1;
  269. y->avl_balance = +1;
  270. break;
  271. }
  272. else
  273. x->avl_balance = y->avl_balance = 0;
  274. }
  275. }
  276. }
  277. else {
  278. y->avl_balance--;
  279. if (y->avl_balance == -1)
  280. break;
  281. else if (y->avl_balance == -2) {
  282. struct avl_node *x = y->avl_link[0];
  283. if (x->avl_balance == +1) {
  284. struct avl_node *w;
  285. assert(x->avl_balance == +1);
  286. w = x->avl_link[1];
  287. x->avl_link[1] = w->avl_link[0];
  288. w->avl_link[0] = x;
  289. y->avl_link[0] = w->avl_link[1];
  290. w->avl_link[1] = y;
  291. if (w->avl_balance == -1)
  292. x->avl_balance = 0, y->avl_balance = +1;
  293. else if (w->avl_balance == 0)
  294. x->avl_balance = y->avl_balance = 0;
  295. else /* |w->avl_balance == +1| */
  296. x->avl_balance = -1, y->avl_balance = 0;
  297. w->avl_balance = 0;
  298. pa[k - 1]->avl_link[da[k - 1]] = w;
  299. }
  300. else {
  301. y->avl_link[0] = x->avl_link[1];
  302. x->avl_link[1] = y;
  303. pa[k - 1]->avl_link[da[k - 1]] = x;
  304. if (x->avl_balance == 0) {
  305. x->avl_balance = +1;
  306. y->avl_balance = -1;
  307. break;
  308. }
  309. else
  310. x->avl_balance = y->avl_balance = 0;
  311. }
  312. }
  313. }
  314. }
  315. tree->avl_count--;
  316. tree->avl_generation++;
  317. return (void *)item;
  318. }
  319. /* Refreshes the stack of parent pointers in |trav|
  320. and updates its generation number. */
  321. static void trav_refresh(struct avl_traverser *trav)
  322. {
  323. assert(trav != NULL);
  324. trav->avl_generation = trav->avl_table->avl_generation;
  325. if (trav->avl_node != NULL) {
  326. avl_comparison_func *cmp = trav->avl_table->avl_compare;
  327. void *param = trav->avl_table->avl_param;
  328. struct avl_node *node = trav->avl_node;
  329. struct avl_node *i;
  330. trav->avl_height = 0;
  331. for (i = trav->avl_table->avl_root; i != node;) {
  332. assert(trav->avl_height < AVL_MAX_HEIGHT);
  333. assert(i != NULL);
  334. trav->avl_stack[trav->avl_height++] = i;
  335. i = i->avl_link[cmp(node->avl_data, i->avl_data, param) > 0];
  336. }
  337. }
  338. }
  339. /* Initializes |trav| for use with |tree|
  340. and selects the null node. */
  341. void avl_t_init(struct avl_traverser *trav, struct avl_table *tree)
  342. {
  343. trav->avl_table = tree;
  344. trav->avl_node = NULL;
  345. trav->avl_height = 0;
  346. trav->avl_generation = tree->avl_generation;
  347. }
  348. /* Initializes |trav| for |tree|
  349. and selects and returns a pointer to its least-valued item.
  350. Returns |NULL| if |tree| contains no nodes. */
  351. void *avl_t_first(struct avl_traverser *trav, struct avl_table *tree)
  352. {
  353. struct avl_node *x;
  354. assert(tree != NULL && trav != NULL);
  355. trav->avl_table = tree;
  356. trav->avl_height = 0;
  357. trav->avl_generation = tree->avl_generation;
  358. x = tree->avl_root;
  359. if (x != NULL)
  360. while (x->avl_link[0] != NULL) {
  361. assert(trav->avl_height < AVL_MAX_HEIGHT);
  362. trav->avl_stack[trav->avl_height++] = x;
  363. x = x->avl_link[0];
  364. }
  365. trav->avl_node = x;
  366. return x != NULL ? x->avl_data : NULL;
  367. }
  368. /* Initializes |trav| for |tree|
  369. and selects and returns a pointer to its greatest-valued item.
  370. Returns |NULL| if |tree| contains no nodes. */
  371. void *avl_t_last(struct avl_traverser *trav, struct avl_table *tree)
  372. {
  373. struct avl_node *x;
  374. assert(tree != NULL && trav != NULL);
  375. trav->avl_table = tree;
  376. trav->avl_height = 0;
  377. trav->avl_generation = tree->avl_generation;
  378. x = tree->avl_root;
  379. if (x != NULL)
  380. while (x->avl_link[1] != NULL) {
  381. assert(trav->avl_height < AVL_MAX_HEIGHT);
  382. trav->avl_stack[trav->avl_height++] = x;
  383. x = x->avl_link[1];
  384. }
  385. trav->avl_node = x;
  386. return x != NULL ? x->avl_data : NULL;
  387. }
  388. /* Searches for |item| in |tree|.
  389. If found, initializes |trav| to the item found and returns the item
  390. as well.
  391. If there is no matching item, initializes |trav| to the null item
  392. and returns |NULL|. */
  393. void *avl_t_find(struct avl_traverser *trav, struct avl_table *tree,
  394. void *item)
  395. {
  396. struct avl_node *p, *q;
  397. assert(trav != NULL && tree != NULL && item != NULL);
  398. trav->avl_table = tree;
  399. trav->avl_height = 0;
  400. trav->avl_generation = tree->avl_generation;
  401. for (p = tree->avl_root; p != NULL; p = q) {
  402. int cmp = tree->avl_compare(item, p->avl_data, tree->avl_param);
  403. if (cmp < 0)
  404. q = p->avl_link[0];
  405. else if (cmp > 0)
  406. q = p->avl_link[1];
  407. else { /* |cmp == 0| */
  408. trav->avl_node = p;
  409. return p->avl_data;
  410. }
  411. assert(trav->avl_height < AVL_MAX_HEIGHT);
  412. trav->avl_stack[trav->avl_height++] = p;
  413. }
  414. trav->avl_height = 0;
  415. trav->avl_node = NULL;
  416. return NULL;
  417. }
  418. /* Attempts to insert |item| into |tree|.
  419. If |item| is inserted successfully, it is returned and |trav| is
  420. initialized to its location.
  421. If a duplicate is found, it is returned and |trav| is initialized to
  422. its location. No replacement of the item occurs.
  423. If a memory allocation failure occurs, |NULL| is returned and |trav|
  424. is initialized to the null item. */
  425. void *avl_t_insert(struct avl_traverser *trav, struct avl_table *tree,
  426. void *item)
  427. {
  428. void **p;
  429. assert(trav != NULL && tree != NULL && item != NULL);
  430. p = avl_probe(tree, item);
  431. if (p != NULL) {
  432. trav->avl_table = tree;
  433. trav->avl_node = ((struct avl_node *)
  434. ((char *)p - offsetof(struct avl_node, avl_data)));
  435. trav->avl_generation = tree->avl_generation - 1;
  436. return *p;
  437. }
  438. else {
  439. avl_t_init(trav, tree);
  440. return NULL;
  441. }
  442. }
  443. /* Initializes |trav| to have the same current node as |src|. */
  444. void *avl_t_copy(struct avl_traverser *trav, const struct avl_traverser *src)
  445. {
  446. assert(trav != NULL && src != NULL);
  447. if (trav != src) {
  448. trav->avl_table = src->avl_table;
  449. trav->avl_node = src->avl_node;
  450. trav->avl_generation = src->avl_generation;
  451. if (trav->avl_generation == trav->avl_table->avl_generation) {
  452. trav->avl_height = src->avl_height;
  453. memcpy(trav->avl_stack, (const void *)src->avl_stack,
  454. sizeof *trav->avl_stack * trav->avl_height);
  455. }
  456. }
  457. return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
  458. }
  459. /* Returns the next data item in inorder
  460. within the tree being traversed with |trav|,
  461. or if there are no more data items returns |NULL|. */
  462. void *avl_t_next(struct avl_traverser *trav)
  463. {
  464. struct avl_node *x;
  465. assert(trav != NULL);
  466. if (trav->avl_generation != trav->avl_table->avl_generation)
  467. trav_refresh(trav);
  468. x = trav->avl_node;
  469. if (x == NULL) {
  470. return avl_t_first(trav, trav->avl_table);
  471. }
  472. else if (x->avl_link[1] != NULL) {
  473. assert(trav->avl_height < AVL_MAX_HEIGHT);
  474. trav->avl_stack[trav->avl_height++] = x;
  475. x = x->avl_link[1];
  476. while (x->avl_link[0] != NULL) {
  477. assert(trav->avl_height < AVL_MAX_HEIGHT);
  478. trav->avl_stack[trav->avl_height++] = x;
  479. x = x->avl_link[0];
  480. }
  481. }
  482. else {
  483. struct avl_node *y;
  484. do {
  485. if (trav->avl_height == 0) {
  486. trav->avl_node = NULL;
  487. return NULL;
  488. }
  489. y = x;
  490. x = trav->avl_stack[--trav->avl_height];
  491. }
  492. while (y == x->avl_link[1]);
  493. }
  494. trav->avl_node = x;
  495. return x->avl_data;
  496. }
  497. /* Returns the previous data item in inorder
  498. within the tree being traversed with |trav|,
  499. or if there are no more data items returns |NULL|. */
  500. void *avl_t_prev(struct avl_traverser *trav)
  501. {
  502. struct avl_node *x;
  503. assert(trav != NULL);
  504. if (trav->avl_generation != trav->avl_table->avl_generation)
  505. trav_refresh(trav);
  506. x = trav->avl_node;
  507. if (x == NULL) {
  508. return avl_t_last(trav, trav->avl_table);
  509. }
  510. else if (x->avl_link[0] != NULL) {
  511. assert(trav->avl_height < AVL_MAX_HEIGHT);
  512. trav->avl_stack[trav->avl_height++] = x;
  513. x = x->avl_link[0];
  514. while (x->avl_link[1] != NULL) {
  515. assert(trav->avl_height < AVL_MAX_HEIGHT);
  516. trav->avl_stack[trav->avl_height++] = x;
  517. x = x->avl_link[1];
  518. }
  519. }
  520. else {
  521. struct avl_node *y;
  522. do {
  523. if (trav->avl_height == 0) {
  524. trav->avl_node = NULL;
  525. return NULL;
  526. }
  527. y = x;
  528. x = trav->avl_stack[--trav->avl_height];
  529. }
  530. while (y == x->avl_link[0]);
  531. }
  532. trav->avl_node = x;
  533. return x->avl_data;
  534. }
  535. /* Returns |trav|'s current item. */
  536. void *avl_t_cur(struct avl_traverser *trav)
  537. {
  538. assert(trav != NULL);
  539. return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
  540. }
  541. /* Replaces the current item in |trav| by |new| and returns the item replaced.
  542. |trav| must not have the null item selected.
  543. The new item must not upset the ordering of the tree. */
  544. void *avl_t_replace(struct avl_traverser *trav, void *new)
  545. {
  546. struct avl_node *old;
  547. assert(trav != NULL && trav->avl_node != NULL && new != NULL);
  548. old = trav->avl_node->avl_data;
  549. trav->avl_node->avl_data = new;
  550. return old;
  551. }
  552. static void
  553. copy_error_recovery(struct avl_node **stack, int height,
  554. struct avl_table *new, avl_item_func * destroy)
  555. {
  556. assert(stack != NULL && height >= 0 && new != NULL);
  557. for (; height > 2; height -= 2)
  558. stack[height - 1]->avl_link[1] = NULL;
  559. avl_destroy(new, destroy);
  560. }
  561. /* Copies |org| to a newly created tree, which is returned.
  562. If |copy != NULL|, each data item in |org| is first passed to |copy|,
  563. and the return values are inserted into the tree,
  564. with |NULL| return values taken as indications of failure.
  565. On failure, destroys the partially created new tree,
  566. applying |destroy|, if non-null, to each item in the new tree so far,
  567. and returns |NULL|.
  568. If |allocator != NULL|, it is used for allocation in the new tree.
  569. Otherwise, the same allocator used for |org| is used. */
  570. struct avl_table *avl_copy(const struct avl_table *org, avl_copy_func * copy,
  571. avl_item_func * destroy,
  572. struct libavl_allocator *allocator)
  573. {
  574. struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
  575. int height = 0;
  576. struct avl_table *new;
  577. const struct avl_node *x;
  578. struct avl_node *y;
  579. assert(org != NULL);
  580. new = avl_create(org->avl_compare, org->avl_param,
  581. allocator != NULL ? allocator : org->avl_alloc);
  582. if (new == NULL)
  583. return NULL;
  584. new->avl_count = org->avl_count;
  585. if (new->avl_count == 0)
  586. return new;
  587. x = (const struct avl_node *)&org->avl_root;
  588. y = (struct avl_node *)&new->avl_root;
  589. for (;;) {
  590. while (x->avl_link[0] != NULL) {
  591. assert(height < 2 * (AVL_MAX_HEIGHT + 1));
  592. y->avl_link[0] =
  593. new->avl_alloc->libavl_malloc(new->avl_alloc,
  594. sizeof *y->avl_link[0]);
  595. if (y->avl_link[0] == NULL) {
  596. if (y != (struct avl_node *)&new->avl_root) {
  597. y->avl_data = NULL;
  598. y->avl_link[1] = NULL;
  599. }
  600. copy_error_recovery(stack, height, new, destroy);
  601. return NULL;
  602. }
  603. stack[height++] = (struct avl_node *)x;
  604. stack[height++] = y;
  605. x = x->avl_link[0];
  606. y = y->avl_link[0];
  607. }
  608. y->avl_link[0] = NULL;
  609. for (;;) {
  610. y->avl_balance = x->avl_balance;
  611. if (copy == NULL)
  612. y->avl_data = x->avl_data;
  613. else {
  614. y->avl_data = copy(x->avl_data, org->avl_param);
  615. if (y->avl_data == NULL) {
  616. y->avl_link[1] = NULL;
  617. copy_error_recovery(stack, height, new, destroy);
  618. return NULL;
  619. }
  620. }
  621. if (x->avl_link[1] != NULL) {
  622. y->avl_link[1] =
  623. new->avl_alloc->libavl_malloc(new->avl_alloc,
  624. sizeof *y->avl_link[1]);
  625. if (y->avl_link[1] == NULL) {
  626. copy_error_recovery(stack, height, new, destroy);
  627. return NULL;
  628. }
  629. x = x->avl_link[1];
  630. y = y->avl_link[1];
  631. break;
  632. }
  633. else
  634. y->avl_link[1] = NULL;
  635. if (height <= 2)
  636. return new;
  637. y = stack[--height];
  638. x = stack[--height];
  639. }
  640. }
  641. }
  642. /* Frees storage allocated for |tree|.
  643. If |destroy != NULL|, applies it to each data item in inorder. */
  644. void avl_destroy(struct avl_table *tree, avl_item_func * destroy)
  645. {
  646. struct avl_node *p, *q;
  647. assert(tree != NULL);
  648. for (p = tree->avl_root; p != NULL; p = q)
  649. if (p->avl_link[0] == NULL) {
  650. q = p->avl_link[1];
  651. if (destroy != NULL && p->avl_data != NULL)
  652. destroy(p->avl_data, tree->avl_param);
  653. tree->avl_alloc->libavl_free(tree->avl_alloc, p);
  654. }
  655. else {
  656. q = p->avl_link[0];
  657. p->avl_link[0] = q->avl_link[1];
  658. q->avl_link[1] = p;
  659. }
  660. tree->avl_alloc->libavl_free(tree->avl_alloc, tree);
  661. }
  662. /* Allocates |size| bytes of space using |malloc()|.
  663. Returns a null pointer if allocation fails. */
  664. void *avl_malloc(struct libavl_allocator *allocator, size_t size)
  665. {
  666. assert(allocator != NULL && size > 0);
  667. return malloc(size);
  668. }
  669. /* Frees |block|. */
  670. void avl_free(struct libavl_allocator *allocator, void *block)
  671. {
  672. assert(allocator != NULL && block != NULL);
  673. free(block);
  674. }
  675. /* Default memory allocator that uses |malloc()| and |free()|. */
  676. struct libavl_allocator avl_allocator_default = {
  677. avl_malloc,
  678. avl_free
  679. };
  680. /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
  681. void (avl_assert_insert) (struct avl_table * table, void *item)
  682. {
  683. void **p = avl_probe(table, item);
  684. assert(p != NULL && *p == item);
  685. }
  686. /* Asserts that |avl_delete()| really removes |item| from |table|,
  687. and returns the removed item. */
  688. void *(avl_assert_delete) (struct avl_table * table, void *item)
  689. {
  690. void *p = avl_delete(table, item);
  691. assert(p != NULL);
  692. return p;
  693. }