avl.c 21 KB

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