pavlrc.c 22 KB

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