BAVL.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /**
  2. * @file BAVL.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * AVL tree.
  32. */
  33. #ifndef BADVPN_STRUCTURE_BAVL_H
  34. #define BADVPN_STRUCTURE_BAVL_H
  35. //#define BAVL_DEBUG
  36. #include <stdint.h>
  37. #include <stddef.h>
  38. #include <misc/debug.h>
  39. /**
  40. * Handler function called by tree algorithms to compare two values.
  41. * For any two values, the comparator must always return the same result.
  42. * The <= relation defined by the comparator must be a total order.
  43. * Values are obtained like that:
  44. * - The value of a node in the tree, or a node that is being inserted is:
  45. * (uint8_t *)node + offset.
  46. * - The value being looked up is the same as given to the lookup function.
  47. *
  48. * @param user as in {@link BAVL_Init}
  49. * @param val1 first value
  50. * @param val2 second value
  51. * @return -1 if val1 < val2, 0 if val1 = val2, 1 if val1 > val2
  52. */
  53. typedef int (*BAVL_comparator) (void *user, void *val1, void *val2);
  54. struct BAVLNode;
  55. /**
  56. * AVL tree.
  57. */
  58. typedef struct {
  59. int offset;
  60. BAVL_comparator comparator;
  61. void *user;
  62. struct BAVLNode *root;
  63. } BAVL;
  64. /**
  65. * AVL tree node.
  66. */
  67. typedef struct BAVLNode {
  68. struct BAVLNode *parent;
  69. struct BAVLNode *link[2];
  70. int8_t balance;
  71. #ifdef BAVL_COUNT
  72. uint64_t count;
  73. #endif
  74. } BAVLNode;
  75. /**
  76. * Initializes the tree.
  77. *
  78. * @param o tree to initialize
  79. * @param offset offset of a value from its node
  80. * @param comparator value comparator handler function
  81. * @param user value to pass to comparator
  82. */
  83. static void BAVL_Init (BAVL *o, int offset, BAVL_comparator comparator, void *user);
  84. /**
  85. * Inserts a node into the tree.
  86. * Must not be called from comparator.
  87. *
  88. * @param o the tree
  89. * @param node uninitialized node to insert. Must have a valid value (its value
  90. * may be passed to the comparator during insertion).
  91. * @param ref if not NULL, will return (regardless if insertion succeeded):
  92. * - the greatest node lesser than the inserted value, or (not in order)
  93. * - the smallest node greater than the inserted value, or
  94. * - NULL meaning there were no nodes in the tree.
  95. * @param 1 on success, 0 if an element with an equal value is already in the tree
  96. */
  97. static int BAVL_Insert (BAVL *o, BAVLNode *node, BAVLNode **ref);
  98. /**
  99. * Removes a node from the tree.
  100. * Must not be called from comparator.
  101. *
  102. * @param o the tree
  103. * @param node node to remove
  104. */
  105. static void BAVL_Remove (BAVL *o, BAVLNode *node);
  106. /**
  107. * Checks if the tree is empty.
  108. * Must not be called from comparator.
  109. *
  110. * @param o the tree
  111. * @return 1 if empty, 0 if not
  112. */
  113. static int BAVL_IsEmpty (const BAVL *o);
  114. /**
  115. * Looks for a value in the tree.
  116. * Must not be called from comparator.
  117. *
  118. * @param o the tree
  119. * @param val value to look for
  120. * @return If a node is in the thee with an equal value, that node.
  121. * Else if the tree is not empty:
  122. * - the greatest node lesser than the given value, or (not in order)
  123. * - the smallest node greater than the given value.
  124. * NULL if the tree is empty.
  125. */
  126. static BAVLNode * BAVL_Lookup (const BAVL *o, void *val);
  127. /**
  128. * Looks for a value in the tree.
  129. * Must not be called from comparator.
  130. *
  131. * @param o the tree
  132. * @param val value to look for
  133. * @return If a node is in the thee with an equal value, that node.
  134. * Else NULL.
  135. */
  136. static BAVLNode * BAVL_LookupExact (const BAVL *o, void *val);
  137. /**
  138. * Returns the smallest node in the tree, or NULL if the tree is empty.
  139. *
  140. * @param o the tree
  141. * @return smallest node or NULL
  142. */
  143. static BAVLNode * BAVL_GetFirst (const BAVL *o);
  144. /**
  145. * Returns the greatest node in the tree, or NULL if the tree is empty.
  146. *
  147. * @param o the tree
  148. * @return greatest node or NULL
  149. */
  150. static BAVLNode * BAVL_GetLast (const BAVL *o);
  151. /**
  152. * Returns the node that follows the given node, or NULL if it's the
  153. * last node.
  154. *
  155. * @param o the tree
  156. * @param n node
  157. * @return next node, or NULL
  158. */
  159. static BAVLNode * BAVL_GetNext (const BAVL *o, BAVLNode *n);
  160. /**
  161. * Returns the node that precedes the given node, or NULL if it's the
  162. * first node.
  163. *
  164. * @param o the tree
  165. * @param n node
  166. * @return previous node, or NULL
  167. */
  168. static BAVLNode * BAVL_GetPrev (const BAVL *o, BAVLNode *n);
  169. #ifdef BAVL_COUNT
  170. static uint64_t BAVL_Count (const BAVL *o);
  171. static uint64_t BAVL_IndexOf (const BAVL *o, BAVLNode *n);
  172. static BAVLNode * BAVL_GetAt (const BAVL *o, uint64_t index);
  173. #endif
  174. static void BAVL_Verify (BAVL *o);
  175. #define BAVL_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
  176. #define BAVL_OPTNEG(_a, _neg) ((_neg) ? -(_a) : (_a))
  177. static void * _BAVL_node_value (const BAVL *o, BAVLNode *n)
  178. {
  179. return ((uint8_t *)n + o->offset);
  180. }
  181. static int _BAVL_compare_values (const BAVL *o, void *v1, void *v2)
  182. {
  183. int res = o->comparator(o->user, v1, v2);
  184. ASSERT(res == -1 || res == 0 || res == 1)
  185. return res;
  186. }
  187. static int _BAVL_compare_nodes (BAVL *o, BAVLNode *n1, BAVLNode *n2)
  188. {
  189. return _BAVL_compare_values(o, _BAVL_node_value(o, n1), _BAVL_node_value(o, n2));
  190. }
  191. #ifdef BAVL_AUTO_VERIFY
  192. #define BAVL_ASSERT(_h) BAVL_Verify((_h));
  193. #else
  194. #define BAVL_ASSERT(_h)
  195. #endif
  196. static int _BAVL_assert_recurser (BAVL *o, BAVLNode *n)
  197. {
  198. ASSERT_FORCE(n->balance >= -1)
  199. ASSERT_FORCE(n->balance <= 1)
  200. int height_left = 0;
  201. int height_right = 0;
  202. #ifdef BAVL_COUNT
  203. uint64_t count_left = 0;
  204. uint64_t count_right = 0;
  205. #endif
  206. // check left subtree
  207. if (n->link[0]) {
  208. // check parent link
  209. ASSERT_FORCE(n->link[0]->parent == n)
  210. // check binary search tree
  211. ASSERT_FORCE(_BAVL_compare_nodes(o, n->link[0], n) == -1)
  212. // recursively calculate height
  213. height_left = _BAVL_assert_recurser(o, n->link[0]);
  214. #ifdef BAVL_COUNT
  215. count_left = n->link[0]->count;
  216. #endif
  217. }
  218. // check right subtree
  219. if (n->link[1]) {
  220. // check parent link
  221. ASSERT_FORCE(n->link[1]->parent == n)
  222. // check binary search tree
  223. ASSERT_FORCE(_BAVL_compare_nodes(o, n->link[1], n) == 1)
  224. // recursively calculate height
  225. height_right = _BAVL_assert_recurser(o, n->link[1]);
  226. #ifdef BAVL_COUNT
  227. count_right = n->link[1]->count;
  228. #endif
  229. }
  230. // check balance factor
  231. ASSERT_FORCE(n->balance == height_right - height_left)
  232. #ifdef BAVL_COUNT
  233. // check count
  234. ASSERT_FORCE(n->count == 1 + count_left + count_right)
  235. #endif
  236. return (BAVL_MAX(height_left, height_right) + 1);
  237. }
  238. #ifdef BAVL_COUNT
  239. static void _BAVL_update_count_from_children (BAVLNode *n)
  240. {
  241. n->count = 1 + (n->link[0] ? n->link[0]->count : 0) + (n->link[1] ? n->link[1]->count : 0);
  242. }
  243. #endif
  244. static void _BAVL_rotate (BAVL *tree, BAVLNode *r, uint8_t dir)
  245. {
  246. BAVLNode *nr = r->link[!dir];
  247. r->link[!dir] = nr->link[dir];
  248. if (r->link[!dir]) {
  249. r->link[!dir]->parent = r;
  250. }
  251. nr->link[dir] = r;
  252. nr->parent = r->parent;
  253. if (nr->parent) {
  254. nr->parent->link[r == r->parent->link[1]] = nr;
  255. } else {
  256. tree->root = nr;
  257. }
  258. r->parent = nr;
  259. #ifdef BAVL_COUNT
  260. // update counts
  261. _BAVL_update_count_from_children(r); // first r!
  262. _BAVL_update_count_from_children(nr);
  263. #endif
  264. }
  265. static BAVLNode * _BAVL_subtree_max (BAVLNode *n)
  266. {
  267. ASSERT(n)
  268. while (n->link[1]) {
  269. n = n->link[1];
  270. }
  271. return n;
  272. }
  273. static void _BAVL_replace_subtree (BAVL *tree, BAVLNode *dest, BAVLNode *n)
  274. {
  275. ASSERT(dest)
  276. if (dest->parent) {
  277. dest->parent->link[dest == dest->parent->link[1]] = n;
  278. } else {
  279. tree->root = n;
  280. }
  281. if (n) {
  282. n->parent = dest->parent;
  283. }
  284. #ifdef BAVL_COUNT
  285. // update counts
  286. for (BAVLNode *c = dest->parent; c; c = c->parent) {
  287. ASSERT(c->count >= dest->count)
  288. c->count -= dest->count;
  289. if (n) {
  290. ASSERT(n->count <= UINT64_MAX - c->count)
  291. c->count += n->count;
  292. }
  293. }
  294. #endif
  295. }
  296. static void _BAVL_swap_nodes (BAVL *tree, BAVLNode *n1, BAVLNode *n2)
  297. {
  298. if (n2->parent == n1 || n1->parent == n2) {
  299. // when the nodes are directly connected we need special handling
  300. // make sure n1 is above n2
  301. if (n1->parent == n2) {
  302. BAVLNode *t = n1;
  303. n1 = n2;
  304. n2 = t;
  305. }
  306. uint8_t side = (n2 == n1->link[1]);
  307. BAVLNode *c = n1->link[!side];
  308. if (n1->link[0] = n2->link[0]) {
  309. n1->link[0]->parent = n1;
  310. }
  311. if (n1->link[1] = n2->link[1]) {
  312. n1->link[1]->parent = n1;
  313. }
  314. if (n2->parent = n1->parent) {
  315. n2->parent->link[n1 == n1->parent->link[1]] = n2;
  316. } else {
  317. tree->root = n2;
  318. }
  319. n2->link[side] = n1;
  320. n1->parent = n2;
  321. if (n2->link[!side] = c) {
  322. c->parent = n2;
  323. }
  324. } else {
  325. BAVLNode *temp;
  326. // swap parents
  327. temp = n1->parent;
  328. if (n1->parent = n2->parent) {
  329. n1->parent->link[n2 == n2->parent->link[1]] = n1;
  330. } else {
  331. tree->root = n1;
  332. }
  333. if (n2->parent = temp) {
  334. n2->parent->link[n1 == temp->link[1]] = n2;
  335. } else {
  336. tree->root = n2;
  337. }
  338. // swap left children
  339. temp = n1->link[0];
  340. if (n1->link[0] = n2->link[0]) {
  341. n1->link[0]->parent = n1;
  342. }
  343. if (n2->link[0] = temp) {
  344. n2->link[0]->parent = n2;
  345. }
  346. // swap right children
  347. temp = n1->link[1];
  348. if (n1->link[1] = n2->link[1]) {
  349. n1->link[1]->parent = n1;
  350. }
  351. if (n2->link[1] = temp) {
  352. n2->link[1]->parent = n2;
  353. }
  354. }
  355. // swap balance factors
  356. int8_t b = n1->balance;
  357. n1->balance = n2->balance;
  358. n2->balance = b;
  359. #ifdef BAVL_COUNT
  360. // swap counts
  361. uint64_t c = n1->count;
  362. n1->count = n2->count;
  363. n2->count = c;
  364. #endif
  365. }
  366. static void _BAVL_rebalance (BAVL *o, BAVLNode *node, uint8_t side, int8_t deltac)
  367. {
  368. ASSERT(side == 0 || side == 1)
  369. ASSERT(deltac >= -1 && deltac <= 1)
  370. ASSERT(node->balance >= -1 && node->balance <= 1)
  371. // if no subtree changed its height, no more rebalancing is needed
  372. if (deltac == 0) {
  373. return;
  374. }
  375. // calculate how much our height changed
  376. int8_t delta = BAVL_MAX(deltac, BAVL_OPTNEG(node->balance, side)) - BAVL_MAX(0, BAVL_OPTNEG(node->balance, side));
  377. ASSERT(delta >= -1 && delta <= 1)
  378. // update our balance factor
  379. node->balance -= BAVL_OPTNEG(deltac, side);
  380. BAVLNode *child;
  381. BAVLNode *gchild;
  382. // perform transformations if the balance factor is wrong
  383. if (node->balance == 2 || node->balance == -2) {
  384. uint8_t bside;
  385. int8_t bsidef;
  386. if (node->balance == 2) {
  387. bside = 1;
  388. bsidef = 1;
  389. } else {
  390. bside = 0;
  391. bsidef = -1;
  392. }
  393. ASSERT(node->link[bside])
  394. child = node->link[bside];
  395. switch (child->balance * bsidef) {
  396. case 1:
  397. _BAVL_rotate(o, node, !bside);
  398. node->balance = 0;
  399. child->balance = 0;
  400. node = child;
  401. delta -= 1;
  402. break;
  403. case 0:
  404. _BAVL_rotate(o, node, !bside);
  405. node->balance = 1 * bsidef;
  406. child->balance = -1 * bsidef;
  407. node = child;
  408. break;
  409. case -1:
  410. ASSERT(child->link[!bside])
  411. gchild = child->link[!bside];
  412. _BAVL_rotate(o, child, bside);
  413. _BAVL_rotate(o, node, !bside);
  414. node->balance = -BAVL_MAX(0, gchild->balance * bsidef) * bsidef;
  415. child->balance = BAVL_MAX(0, -gchild->balance * bsidef) * bsidef;
  416. gchild->balance = 0;
  417. node = gchild;
  418. delta -= 1;
  419. break;
  420. default:
  421. ASSERT(0);
  422. }
  423. }
  424. ASSERT(delta >= -1 && delta <= 1)
  425. // Transformations above preserve this. Proof:
  426. // - if a child subtree gained 1 height and rebalancing was needed,
  427. // it was the heavier subtree. Then delta was was originally 1, because
  428. // the heaviest subtree gained one height. If the transformation reduces
  429. // delta by one, it becomes 0.
  430. // - if a child subtree lost 1 height and rebalancing was needed, it
  431. // was the lighter subtree. Then delta was originally 0, because
  432. // the height of the heaviest subtree was unchanged. If the transformation
  433. // reduces delta by one, it becomes -1.
  434. if (node->parent) {
  435. _BAVL_rebalance(o, node->parent, node == node->parent->link[1], delta);
  436. }
  437. }
  438. void BAVL_Init (BAVL *o, int offset, BAVL_comparator comparator, void *user)
  439. {
  440. o->offset = offset;
  441. o->comparator = comparator;
  442. o->user = user;
  443. o->root = NULL;
  444. BAVL_ASSERT(o)
  445. }
  446. int BAVL_Insert (BAVL *o, BAVLNode *node, BAVLNode **ref)
  447. {
  448. // insert to root?
  449. if (!o->root) {
  450. o->root = node;
  451. node->parent = NULL;
  452. node->link[0] = NULL;
  453. node->link[1] = NULL;
  454. node->balance = 0;
  455. #ifdef BAVL_COUNT
  456. node->count = 1;
  457. #endif
  458. BAVL_ASSERT(o)
  459. if (ref) {
  460. *ref = NULL;
  461. }
  462. return 1;
  463. }
  464. // find node to insert to
  465. BAVLNode *c = o->root;
  466. int side;
  467. while (1) {
  468. // compare
  469. int comp = _BAVL_compare_nodes(o, node, c);
  470. // have we found a node that compares equal?
  471. if (comp == 0) {
  472. if (ref) {
  473. *ref = c;
  474. }
  475. return 0;
  476. }
  477. side = (comp == 1);
  478. // have we reached a leaf?
  479. if (!c->link[side]) {
  480. break;
  481. }
  482. c = c->link[side];
  483. }
  484. // insert
  485. c->link[side] = node;
  486. node->parent = c;
  487. node->link[0] = NULL;
  488. node->link[1] = NULL;
  489. node->balance = 0;
  490. #ifdef BAVL_COUNT
  491. node->count = 1;
  492. #endif
  493. #ifdef BAVL_COUNT
  494. // update counts
  495. for (BAVLNode *p = c; p; p = p->parent) {
  496. ASSERT(p->count < UINT64_MAX)
  497. p->count++;
  498. }
  499. #endif
  500. // rebalance
  501. _BAVL_rebalance(o, c, side, 1);
  502. BAVL_ASSERT(o)
  503. if (ref) {
  504. *ref = c;
  505. }
  506. return 1;
  507. }
  508. void BAVL_Remove (BAVL *o, BAVLNode *node)
  509. {
  510. // if we have both subtrees, swap the node and the largest node
  511. // in the left subtree, so we have at most one subtree
  512. if (node->link[0] && node->link[1]) {
  513. // find the largest node in the left subtree
  514. BAVLNode *max = _BAVL_subtree_max(node->link[0]);
  515. // swap the nodes
  516. _BAVL_swap_nodes(o, node, max);
  517. }
  518. // have at most one child now
  519. ASSERT(!(node->link[0] && node->link[1]))
  520. BAVLNode *parent = node->parent;
  521. BAVLNode *child = (node->link[0] ? node->link[0] : node->link[1]);
  522. if (parent) {
  523. // remember on which side node is
  524. int side = (node == parent->link[1]);
  525. // replace node with child
  526. _BAVL_replace_subtree(o, node, child);
  527. // rebalance
  528. _BAVL_rebalance(o, parent, side, -1);
  529. } else {
  530. // replace node with child
  531. _BAVL_replace_subtree(o, node, child);
  532. }
  533. BAVL_ASSERT(o)
  534. }
  535. int BAVL_IsEmpty (const BAVL *o)
  536. {
  537. return (!o->root);
  538. }
  539. BAVLNode * BAVL_Lookup (const BAVL *o, void *val)
  540. {
  541. if (!o->root) {
  542. return NULL;
  543. }
  544. BAVLNode *c = o->root;
  545. while (1) {
  546. // compare
  547. int comp = _BAVL_compare_values(o, val, _BAVL_node_value(o, c));
  548. // have we found a node that compares equal?
  549. if (comp == 0) {
  550. return c;
  551. }
  552. int side = (comp == 1);
  553. // have we reached a leaf?
  554. if (!c->link[side]) {
  555. return c;
  556. }
  557. c = c->link[side];
  558. }
  559. }
  560. BAVLNode * BAVL_LookupExact (const BAVL *o, void *val)
  561. {
  562. if (!o->root) {
  563. return NULL;
  564. }
  565. BAVLNode *c = o->root;
  566. while (1) {
  567. // compare
  568. int comp = _BAVL_compare_values(o, val, _BAVL_node_value(o, c));
  569. // have we found a node that compares equal?
  570. if (comp == 0) {
  571. return c;
  572. }
  573. int side = (comp == 1);
  574. // have we reached a leaf?
  575. if (!c->link[side]) {
  576. return NULL;
  577. }
  578. c = c->link[side];
  579. }
  580. }
  581. BAVLNode * BAVL_GetFirst (const BAVL *o)
  582. {
  583. if (!o->root) {
  584. return NULL;
  585. }
  586. BAVLNode *n = o->root;
  587. while (n->link[0]) {
  588. n = n->link[0];
  589. }
  590. return n;
  591. }
  592. BAVLNode * BAVL_GetLast (const BAVL *o)
  593. {
  594. if (!o->root) {
  595. return NULL;
  596. }
  597. BAVLNode *n = o->root;
  598. while (n->link[1]) {
  599. n = n->link[1];
  600. }
  601. return n;
  602. }
  603. BAVLNode * BAVL_GetNext (const BAVL *o, BAVLNode *n)
  604. {
  605. if (n->link[1]) {
  606. n = n->link[1];
  607. while (n->link[0]) {
  608. n = n->link[0];
  609. }
  610. } else {
  611. while (n->parent && n == n->parent->link[1]) {
  612. n = n->parent;
  613. }
  614. n = n->parent;
  615. }
  616. return n;
  617. }
  618. BAVLNode * BAVL_GetPrev (const BAVL *o, BAVLNode *n)
  619. {
  620. if (n->link[0]) {
  621. n = n->link[0];
  622. while (n->link[1]) {
  623. n = n->link[1];
  624. }
  625. } else {
  626. while (n->parent && n == n->parent->link[0]) {
  627. n = n->parent;
  628. }
  629. n = n->parent;
  630. }
  631. return n;
  632. }
  633. #ifdef BAVL_COUNT
  634. static uint64_t BAVL_Count (const BAVL *o)
  635. {
  636. return (o->root ? o->root->count : 0);
  637. }
  638. static uint64_t BAVL_IndexOf (const BAVL *o, BAVLNode *n)
  639. {
  640. uint64_t index = (n->link[0] ? n->link[0]->count : 0);
  641. for (BAVLNode *c = n; c->parent; c = c->parent) {
  642. if (c == c->parent->link[1]) {
  643. ASSERT(c->parent->count > c->count)
  644. ASSERT(c->parent->count - c->count <= UINT64_MAX - index)
  645. index += c->parent->count - c->count;
  646. }
  647. }
  648. return index;
  649. }
  650. static BAVLNode * BAVL_GetAt (const BAVL *o, uint64_t index)
  651. {
  652. if (index >= BAVL_Count(o)) {
  653. return NULL;
  654. }
  655. BAVLNode *c = o->root;
  656. while (1) {
  657. ASSERT(c)
  658. ASSERT(index < c->count)
  659. uint64_t left_count = (c->link[0] ? c->link[0]->count : 0);
  660. if (index == left_count) {
  661. return c;
  662. }
  663. if (index < left_count) {
  664. c = c->link[0];
  665. } else {
  666. c = c->link[1];
  667. index -= left_count + 1;
  668. }
  669. }
  670. }
  671. #endif
  672. static void BAVL_Verify (BAVL *o)
  673. {
  674. if (o->root) {
  675. ASSERT(!o->root->parent)
  676. _BAVL_assert_recurser(o, o->root);
  677. }
  678. }
  679. #endif