BAVL.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /**
  2. * @file BAVL.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * AVL tree.
  25. */
  26. #ifndef BADVPN_STRUCTURE_BAVL_H
  27. #define BADVPN_STRUCTURE_BAVL_H
  28. //#define BAVL_DEBUG
  29. #include <stdint.h>
  30. #include <stddef.h>
  31. #include <misc/debug.h>
  32. /**
  33. * Handler function called by tree algorithms to compare two values.
  34. * For any two values, the comparator must always return the same result.
  35. * The <= relation defined by the comparator must be a total order.
  36. * Values are obtained like that:
  37. * - The value of a node in the tree, or a node that is being inserted is:
  38. * (uint8_t *)node + offset.
  39. * - The value being looked up is the same as given to the lookup function.
  40. *
  41. * @param user as in {@link BAVL_Init}
  42. * @param val1 first value
  43. * @param val2 second value
  44. * @return -1 if val1 < val2, 0 if val1 = val2, 1 if val1 > val2
  45. */
  46. typedef int (*BAVL_comparator) (void *user, void *val1, void *val2);
  47. struct BAVLNode;
  48. /**
  49. * AVL tree.
  50. */
  51. typedef struct {
  52. int offset;
  53. BAVL_comparator comparator;
  54. void *user;
  55. struct BAVLNode *root;
  56. } BAVL;
  57. /**
  58. * AVL tree node.
  59. */
  60. typedef struct BAVLNode {
  61. struct BAVLNode *parent;
  62. struct BAVLNode *link[2];
  63. int8_t balance;
  64. #ifdef BAVL_COUNT
  65. uint64_t count;
  66. #endif
  67. } BAVLNode;
  68. /**
  69. * Initializes the tree.
  70. *
  71. * @param o tree to initialize
  72. * @param offset offset of a value from its node
  73. * @param comparator value comparator handler function
  74. * @param user value to pass to comparator
  75. */
  76. static void BAVL_Init (BAVL *o, int offset, BAVL_comparator comparator, void *user);
  77. /**
  78. * Inserts a node into the tree.
  79. * Must not be called from comparator.
  80. *
  81. * @param o the tree
  82. * @param node uninitialized node to insert. Must have a valid value (its value
  83. * may be passed to the comparator during insertion).
  84. * @param ref if not NULL, will return (regardless if insertion succeeded):
  85. * - the greatest node lesser than the inserted value, or (not in order)
  86. * - the smallest node greater than the inserted value, or
  87. * - NULL meaning there were no nodes in the tree.
  88. * @param 1 on success, 0 if an element with an equal value is already in the tree
  89. */
  90. static int BAVL_Insert (BAVL *o, BAVLNode *node, BAVLNode **ref);
  91. /**
  92. * Removes a node from the tree.
  93. * Must not be called from comparator.
  94. *
  95. * @param o the tree
  96. * @param node node to remove
  97. */
  98. static void BAVL_Remove (BAVL *o, BAVLNode *node);
  99. /**
  100. * Checks if the tree is empty.
  101. * Must not be called from comparator.
  102. *
  103. * @param o the tree
  104. * @return 1 if empty, 0 if not
  105. */
  106. static int BAVL_IsEmpty (const BAVL *o);
  107. /**
  108. * Looks for a value in the tree.
  109. * Must not be called from comparator.
  110. *
  111. * @param o the tree
  112. * @param val value to look for
  113. * @return If a node is in the thee with an equal value, that node.
  114. * Else if the tree is not empty:
  115. * - the greatest node lesser than the given value, or (not in order)
  116. * - the smallest node greater than the given value.
  117. * NULL if the tree is empty.
  118. */
  119. static BAVLNode * BAVL_Lookup (const BAVL *o, void *val);
  120. /**
  121. * Looks for a value in the tree.
  122. * Must not be called from comparator.
  123. *
  124. * @param o the tree
  125. * @param val value to look for
  126. * @return If a node is in the thee with an equal value, that node.
  127. * Else NULL.
  128. */
  129. static BAVLNode * BAVL_LookupExact (const BAVL *o, void *val);
  130. /**
  131. * Returns the smallest node in the tree, or NULL if the tree is empty.
  132. *
  133. * @param o the tree
  134. * @return smallest node or NULL
  135. */
  136. static BAVLNode * BAVL_GetFirst (const BAVL *o);
  137. /**
  138. * Returns the greatest node in the tree, or NULL if the tree is empty.
  139. *
  140. * @param o the tree
  141. * @return greatest node or NULL
  142. */
  143. static BAVLNode * BAVL_GetLast (const BAVL *o);
  144. /**
  145. * Returns the node that follows the given node, or NULL if it's the
  146. * last node.
  147. *
  148. * @param o the tree
  149. * @param n node
  150. * @return next node, or NULL
  151. */
  152. static BAVLNode * BAVL_GetNext (const BAVL *o, BAVLNode *n);
  153. /**
  154. * Returns the node that precedes the given node, or NULL if it's the
  155. * first node.
  156. *
  157. * @param o the tree
  158. * @param n node
  159. * @return previous node, or NULL
  160. */
  161. static BAVLNode * BAVL_GetPrev (const BAVL *o, BAVLNode *n);
  162. #ifdef BAVL_COUNT
  163. static uint64_t BAVL_Count (const BAVL *o);
  164. static uint64_t BAVL_IndexOf (const BAVL *o, BAVLNode *n);
  165. static BAVLNode * BAVL_GetAt (const BAVL *o, uint64_t index);
  166. #endif
  167. #define BAVL_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
  168. #define BAVL_OPTNEG(_a, _neg) ((_neg) ? -(_a) : (_a))
  169. static void * _BAVL_node_value (const BAVL *o, BAVLNode *n)
  170. {
  171. return ((uint8_t *)n + o->offset);
  172. }
  173. static int _BAVL_compare_values (const BAVL *o, void *v1, void *v2)
  174. {
  175. int res = o->comparator(o->user, v1, v2);
  176. ASSERT(res == -1 || res == 0 || res == 1)
  177. return res;
  178. }
  179. static int _BAVL_compare_nodes (BAVL *o, BAVLNode *n1, BAVLNode *n2)
  180. {
  181. return _BAVL_compare_values(o, _BAVL_node_value(o, n1), _BAVL_node_value(o, n2));
  182. }
  183. #ifdef BAVL_DEBUG
  184. #define BAVL_ASSERT(_h) _BAVL_assert(_h);
  185. #else
  186. #define BAVL_ASSERT(_h)
  187. #endif
  188. #ifdef BAVL_DEBUG
  189. static int _BAVL_assert_recurser (BAVL *o, BAVLNode *n)
  190. {
  191. ASSERT(n->balance >= -1 && n->balance <= 1)
  192. int height_left = 0;
  193. int height_right = 0;
  194. #ifdef BAVL_COUNT
  195. uint64_t count_left = 0;
  196. uint64_t count_right = 0;
  197. #endif
  198. // check left subtree
  199. if (n->link[0]) {
  200. // check parent link
  201. ASSERT(n->link[0]->parent == n)
  202. // check binary search tree
  203. ASSERT(_BAVL_compare_nodes(o, n->link[0], n) == -1)
  204. // recursively calculate height
  205. height_left = _BAVL_assert_recurser(o, n->link[0]);
  206. #ifdef BAVL_COUNT
  207. count_left = n->link[0]->count;
  208. #endif
  209. }
  210. // check right subtree
  211. if (n->link[1]) {
  212. // check parent link
  213. ASSERT(n->link[1]->parent == n)
  214. // check binary search tree
  215. ASSERT(_BAVL_compare_nodes(o, n->link[1], n) == 1)
  216. // recursively calculate height
  217. height_right = _BAVL_assert_recurser(o, n->link[1]);
  218. #ifdef BAVL_COUNT
  219. count_right = n->link[1]->count;
  220. #endif
  221. }
  222. // check balance factor
  223. ASSERT(n->balance == height_right - height_left)
  224. #ifdef BAVL_COUNT
  225. // check count
  226. ASSERT(n->count == 1 + count_left + count_right)
  227. #endif
  228. return (BAVL_MAX(height_left, height_right) + 1);
  229. }
  230. static void _BAVL_assert (BAVL *o)
  231. {
  232. if (o->root) {
  233. ASSERT(!o->root->parent)
  234. _BAVL_assert_recurser(o, o->root);
  235. }
  236. }
  237. #endif
  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. #endif