avl.c 21 KB

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