BAVL.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. #define BAVL_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
  175. #define BAVL_OPTNEG(_a, _neg) ((_neg) ? -(_a) : (_a))
  176. static void * _BAVL_node_value (const BAVL *o, BAVLNode *n)
  177. {
  178. return ((uint8_t *)n + o->offset);
  179. }
  180. static int _BAVL_compare_values (const BAVL *o, void *v1, void *v2)
  181. {
  182. int res = o->comparator(o->user, v1, v2);
  183. ASSERT(res == -1 || res == 0 || res == 1)
  184. return res;
  185. }
  186. static int _BAVL_compare_nodes (BAVL *o, BAVLNode *n1, BAVLNode *n2)
  187. {
  188. return _BAVL_compare_values(o, _BAVL_node_value(o, n1), _BAVL_node_value(o, n2));
  189. }
  190. #ifdef BAVL_DEBUG
  191. #define BAVL_ASSERT(_h) _BAVL_assert(_h);
  192. #else
  193. #define BAVL_ASSERT(_h)
  194. #endif
  195. #ifdef BAVL_DEBUG
  196. static int _BAVL_assert_recurser (BAVL *o, BAVLNode *n)
  197. {
  198. ASSERT(n->balance >= -1 && n->balance <= 1)
  199. int height_left = 0;
  200. int height_right = 0;
  201. #ifdef BAVL_COUNT
  202. uint64_t count_left = 0;
  203. uint64_t count_right = 0;
  204. #endif
  205. // check left subtree
  206. if (n->link[0]) {
  207. // check parent link
  208. ASSERT(n->link[0]->parent == n)
  209. // check binary search tree
  210. ASSERT(_BAVL_compare_nodes(o, n->link[0], n) == -1)
  211. // recursively calculate height
  212. height_left = _BAVL_assert_recurser(o, n->link[0]);
  213. #ifdef BAVL_COUNT
  214. count_left = n->link[0]->count;
  215. #endif
  216. }
  217. // check right subtree
  218. if (n->link[1]) {
  219. // check parent link
  220. ASSERT(n->link[1]->parent == n)
  221. // check binary search tree
  222. ASSERT(_BAVL_compare_nodes(o, n->link[1], n) == 1)
  223. // recursively calculate height
  224. height_right = _BAVL_assert_recurser(o, n->link[1]);
  225. #ifdef BAVL_COUNT
  226. count_right = n->link[1]->count;
  227. #endif
  228. }
  229. // check balance factor
  230. ASSERT(n->balance == height_right - height_left)
  231. #ifdef BAVL_COUNT
  232. // check count
  233. ASSERT(n->count == 1 + count_left + count_right)
  234. #endif
  235. return (BAVL_MAX(height_left, height_right) + 1);
  236. }
  237. static void _BAVL_assert (BAVL *o)
  238. {
  239. if (o->root) {
  240. ASSERT(!o->root->parent)
  241. _BAVL_assert_recurser(o, o->root);
  242. }
  243. }
  244. #endif
  245. #ifdef BAVL_COUNT
  246. static void _BAVL_update_count_from_children (BAVLNode *n)
  247. {
  248. n->count = 1 + (n->link[0] ? n->link[0]->count : 0) + (n->link[1] ? n->link[1]->count : 0);
  249. }
  250. #endif
  251. static void _BAVL_rotate (BAVL *tree, BAVLNode *r, uint8_t dir)
  252. {
  253. BAVLNode *nr = r->link[!dir];
  254. r->link[!dir] = nr->link[dir];
  255. if (r->link[!dir]) {
  256. r->link[!dir]->parent = r;
  257. }
  258. nr->link[dir] = r;
  259. nr->parent = r->parent;
  260. if (nr->parent) {
  261. nr->parent->link[r == r->parent->link[1]] = nr;
  262. } else {
  263. tree->root = nr;
  264. }
  265. r->parent = nr;
  266. #ifdef BAVL_COUNT
  267. // update counts
  268. _BAVL_update_count_from_children(r); // first r!
  269. _BAVL_update_count_from_children(nr);
  270. #endif
  271. }
  272. static BAVLNode * _BAVL_subtree_max (BAVLNode *n)
  273. {
  274. ASSERT(n)
  275. while (n->link[1]) {
  276. n = n->link[1];
  277. }
  278. return n;
  279. }
  280. static void _BAVL_replace_subtree (BAVL *tree, BAVLNode *dest, BAVLNode *n)
  281. {
  282. ASSERT(dest)
  283. if (dest->parent) {
  284. dest->parent->link[dest == dest->parent->link[1]] = n;
  285. } else {
  286. tree->root = n;
  287. }
  288. if (n) {
  289. n->parent = dest->parent;
  290. }
  291. #ifdef BAVL_COUNT
  292. // update counts
  293. for (BAVLNode *c = dest->parent; c; c = c->parent) {
  294. ASSERT(c->count >= dest->count)
  295. c->count -= dest->count;
  296. if (n) {
  297. ASSERT(n->count <= UINT64_MAX - c->count)
  298. c->count += n->count;
  299. }
  300. }
  301. #endif
  302. }
  303. static void _BAVL_swap_nodes (BAVL *tree, BAVLNode *n1, BAVLNode *n2)
  304. {
  305. if (n2->parent == n1 || n1->parent == n2) {
  306. // when the nodes are directly connected we need special handling
  307. // make sure n1 is above n2
  308. if (n1->parent == n2) {
  309. BAVLNode *t = n1;
  310. n1 = n2;
  311. n2 = t;
  312. }
  313. uint8_t side = (n2 == n1->link[1]);
  314. BAVLNode *c = n1->link[!side];
  315. if (n1->link[0] = n2->link[0]) {
  316. n1->link[0]->parent = n1;
  317. }
  318. if (n1->link[1] = n2->link[1]) {
  319. n1->link[1]->parent = n1;
  320. }
  321. if (n2->parent = n1->parent) {
  322. n2->parent->link[n1 == n1->parent->link[1]] = n2;
  323. } else {
  324. tree->root = n2;
  325. }
  326. n2->link[side] = n1;
  327. n1->parent = n2;
  328. if (n2->link[!side] = c) {
  329. c->parent = n2;
  330. }
  331. } else {
  332. BAVLNode *temp;
  333. // swap parents
  334. temp = n1->parent;
  335. if (n1->parent = n2->parent) {
  336. n1->parent->link[n2 == n2->parent->link[1]] = n1;
  337. } else {
  338. tree->root = n1;
  339. }
  340. if (n2->parent = temp) {
  341. n2->parent->link[n1 == temp->link[1]] = n2;
  342. } else {
  343. tree->root = n2;
  344. }
  345. // swap left children
  346. temp = n1->link[0];
  347. if (n1->link[0] = n2->link[0]) {
  348. n1->link[0]->parent = n1;
  349. }
  350. if (n2->link[0] = temp) {
  351. n2->link[0]->parent = n2;
  352. }
  353. // swap right children
  354. temp = n1->link[1];
  355. if (n1->link[1] = n2->link[1]) {
  356. n1->link[1]->parent = n1;
  357. }
  358. if (n2->link[1] = temp) {
  359. n2->link[1]->parent = n2;
  360. }
  361. }
  362. // swap balance factors
  363. int8_t b = n1->balance;
  364. n1->balance = n2->balance;
  365. n2->balance = b;
  366. #ifdef BAVL_COUNT
  367. // swap counts
  368. uint64_t c = n1->count;
  369. n1->count = n2->count;
  370. n2->count = c;
  371. #endif
  372. }
  373. static void _BAVL_rebalance (BAVL *o, BAVLNode *node, uint8_t side, int8_t deltac)
  374. {
  375. ASSERT(side == 0 || side == 1)
  376. ASSERT(deltac >= -1 && deltac <= 1)
  377. ASSERT(node->balance >= -1 && node->balance <= 1)
  378. // if no subtree changed its height, no more rebalancing is needed
  379. if (deltac == 0) {
  380. return;
  381. }
  382. // calculate how much our height changed
  383. int8_t delta = BAVL_MAX(deltac, BAVL_OPTNEG(node->balance, side)) - BAVL_MAX(0, BAVL_OPTNEG(node->balance, side));
  384. ASSERT(delta >= -1 && delta <= 1)
  385. // update our balance factor
  386. node->balance -= BAVL_OPTNEG(deltac, side);
  387. BAVLNode *child;
  388. BAVLNode *gchild;
  389. // perform transformations if the balance factor is wrong
  390. if (node->balance == 2 || node->balance == -2) {
  391. uint8_t bside;
  392. int8_t bsidef;
  393. if (node->balance == 2) {
  394. bside = 1;
  395. bsidef = 1;
  396. } else {
  397. bside = 0;
  398. bsidef = -1;
  399. }
  400. ASSERT(node->link[bside])
  401. child = node->link[bside];
  402. switch (child->balance * bsidef) {
  403. case 1:
  404. _BAVL_rotate(o, node, !bside);
  405. node->balance = 0;
  406. child->balance = 0;
  407. node = child;
  408. delta -= 1;
  409. break;
  410. case 0:
  411. _BAVL_rotate(o, node, !bside);
  412. node->balance = 1 * bsidef;
  413. child->balance = -1 * bsidef;
  414. node = child;
  415. break;
  416. case -1:
  417. ASSERT(child->link[!bside])
  418. gchild = child->link[!bside];
  419. _BAVL_rotate(o, child, bside);
  420. _BAVL_rotate(o, node, !bside);
  421. node->balance = -BAVL_MAX(0, gchild->balance * bsidef) * bsidef;
  422. child->balance = BAVL_MAX(0, -gchild->balance * bsidef) * bsidef;
  423. gchild->balance = 0;
  424. node = gchild;
  425. delta -= 1;
  426. break;
  427. default:
  428. ASSERT(0);
  429. }
  430. }
  431. ASSERT(delta >= -1 && delta <= 1)
  432. // Transformations above preserve this. Proof:
  433. // - if a child subtree gained 1 height and rebalancing was needed,
  434. // it was the heavier subtree. Then delta was was originally 1, because
  435. // the heaviest subtree gained one height. If the transformation reduces
  436. // delta by one, it becomes 0.
  437. // - if a child subtree lost 1 height and rebalancing was needed, it
  438. // was the lighter subtree. Then delta was originally 0, because
  439. // the height of the heaviest subtree was unchanged. If the transformation
  440. // reduces delta by one, it becomes -1.
  441. if (node->parent) {
  442. _BAVL_rebalance(o, node->parent, node == node->parent->link[1], delta);
  443. }
  444. }
  445. void BAVL_Init (BAVL *o, int offset, BAVL_comparator comparator, void *user)
  446. {
  447. o->offset = offset;
  448. o->comparator = comparator;
  449. o->user = user;
  450. o->root = NULL;
  451. BAVL_ASSERT(o)
  452. }
  453. int BAVL_Insert (BAVL *o, BAVLNode *node, BAVLNode **ref)
  454. {
  455. // insert to root?
  456. if (!o->root) {
  457. o->root = node;
  458. node->parent = NULL;
  459. node->link[0] = NULL;
  460. node->link[1] = NULL;
  461. node->balance = 0;
  462. #ifdef BAVL_COUNT
  463. node->count = 1;
  464. #endif
  465. BAVL_ASSERT(o)
  466. if (ref) {
  467. *ref = NULL;
  468. }
  469. return 1;
  470. }
  471. // find node to insert to
  472. BAVLNode *c = o->root;
  473. int side;
  474. while (1) {
  475. // compare
  476. int comp = _BAVL_compare_nodes(o, node, c);
  477. // have we found a node that compares equal?
  478. if (comp == 0) {
  479. if (ref) {
  480. *ref = c;
  481. }
  482. return 0;
  483. }
  484. side = (comp == 1);
  485. // have we reached a leaf?
  486. if (!c->link[side]) {
  487. break;
  488. }
  489. c = c->link[side];
  490. }
  491. // insert
  492. c->link[side] = node;
  493. node->parent = c;
  494. node->link[0] = NULL;
  495. node->link[1] = NULL;
  496. node->balance = 0;
  497. #ifdef BAVL_COUNT
  498. node->count = 1;
  499. #endif
  500. #ifdef BAVL_COUNT
  501. // update counts
  502. for (BAVLNode *p = c; p; p = p->parent) {
  503. ASSERT(p->count < UINT64_MAX)
  504. p->count++;
  505. }
  506. #endif
  507. // rebalance
  508. _BAVL_rebalance(o, c, side, 1);
  509. BAVL_ASSERT(o)
  510. if (ref) {
  511. *ref = c;
  512. }
  513. return 1;
  514. }
  515. void BAVL_Remove (BAVL *o, BAVLNode *node)
  516. {
  517. // if we have both subtrees, swap the node and the largest node
  518. // in the left subtree, so we have at most one subtree
  519. if (node->link[0] && node->link[1]) {
  520. // find the largest node in the left subtree
  521. BAVLNode *max = _BAVL_subtree_max(node->link[0]);
  522. // swap the nodes
  523. _BAVL_swap_nodes(o, node, max);
  524. }
  525. // have at most one child now
  526. ASSERT(!(node->link[0] && node->link[1]))
  527. BAVLNode *parent = node->parent;
  528. BAVLNode *child = (node->link[0] ? node->link[0] : node->link[1]);
  529. if (parent) {
  530. // remember on which side node is
  531. int side = (node == parent->link[1]);
  532. // replace node with child
  533. _BAVL_replace_subtree(o, node, child);
  534. // rebalance
  535. _BAVL_rebalance(o, parent, side, -1);
  536. } else {
  537. // replace node with child
  538. _BAVL_replace_subtree(o, node, child);
  539. }
  540. BAVL_ASSERT(o)
  541. }
  542. int BAVL_IsEmpty (const BAVL *o)
  543. {
  544. return (!o->root);
  545. }
  546. BAVLNode * BAVL_Lookup (const BAVL *o, void *val)
  547. {
  548. if (!o->root) {
  549. return NULL;
  550. }
  551. BAVLNode *c = o->root;
  552. while (1) {
  553. // compare
  554. int comp = _BAVL_compare_values(o, val, _BAVL_node_value(o, c));
  555. // have we found a node that compares equal?
  556. if (comp == 0) {
  557. return c;
  558. }
  559. int side = (comp == 1);
  560. // have we reached a leaf?
  561. if (!c->link[side]) {
  562. return c;
  563. }
  564. c = c->link[side];
  565. }
  566. }
  567. BAVLNode * BAVL_LookupExact (const BAVL *o, void *val)
  568. {
  569. if (!o->root) {
  570. return NULL;
  571. }
  572. BAVLNode *c = o->root;
  573. while (1) {
  574. // compare
  575. int comp = _BAVL_compare_values(o, val, _BAVL_node_value(o, c));
  576. // have we found a node that compares equal?
  577. if (comp == 0) {
  578. return c;
  579. }
  580. int side = (comp == 1);
  581. // have we reached a leaf?
  582. if (!c->link[side]) {
  583. return NULL;
  584. }
  585. c = c->link[side];
  586. }
  587. }
  588. BAVLNode * BAVL_GetFirst (const BAVL *o)
  589. {
  590. if (!o->root) {
  591. return NULL;
  592. }
  593. BAVLNode *n = o->root;
  594. while (n->link[0]) {
  595. n = n->link[0];
  596. }
  597. return n;
  598. }
  599. BAVLNode * BAVL_GetLast (const BAVL *o)
  600. {
  601. if (!o->root) {
  602. return NULL;
  603. }
  604. BAVLNode *n = o->root;
  605. while (n->link[1]) {
  606. n = n->link[1];
  607. }
  608. return n;
  609. }
  610. BAVLNode * BAVL_GetNext (const BAVL *o, BAVLNode *n)
  611. {
  612. if (n->link[1]) {
  613. n = n->link[1];
  614. while (n->link[0]) {
  615. n = n->link[0];
  616. }
  617. } else {
  618. while (n->parent && n == n->parent->link[1]) {
  619. n = n->parent;
  620. }
  621. n = n->parent;
  622. }
  623. return n;
  624. }
  625. BAVLNode * BAVL_GetPrev (const BAVL *o, BAVLNode *n)
  626. {
  627. if (n->link[0]) {
  628. n = n->link[0];
  629. while (n->link[1]) {
  630. n = n->link[1];
  631. }
  632. } else {
  633. while (n->parent && n == n->parent->link[0]) {
  634. n = n->parent;
  635. }
  636. n = n->parent;
  637. }
  638. return n;
  639. }
  640. #ifdef BAVL_COUNT
  641. static uint64_t BAVL_Count (const BAVL *o)
  642. {
  643. return (o->root ? o->root->count : 0);
  644. }
  645. static uint64_t BAVL_IndexOf (const BAVL *o, BAVLNode *n)
  646. {
  647. uint64_t index = (n->link[0] ? n->link[0]->count : 0);
  648. for (BAVLNode *c = n; c->parent; c = c->parent) {
  649. if (c == c->parent->link[1]) {
  650. ASSERT(c->parent->count > c->count)
  651. ASSERT(c->parent->count - c->count <= UINT64_MAX - index)
  652. index += c->parent->count - c->count;
  653. }
  654. }
  655. return index;
  656. }
  657. static BAVLNode * BAVL_GetAt (const BAVL *o, uint64_t index)
  658. {
  659. if (index >= BAVL_Count(o)) {
  660. return NULL;
  661. }
  662. BAVLNode *c = o->root;
  663. while (1) {
  664. ASSERT(c)
  665. ASSERT(index < c->count)
  666. uint64_t left_count = (c->link[0] ? c->link[0]->count : 0);
  667. if (index == left_count) {
  668. return c;
  669. }
  670. if (index < left_count) {
  671. c = c->link[0];
  672. } else {
  673. c = c->link[1];
  674. index -= left_count + 1;
  675. }
  676. }
  677. }
  678. #endif
  679. #endif