LCOV - code coverage report
Current view: top level - src - tree.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 312 312 100.0 %
Date: 2026-11-26 02:32:31 Functions: 23 23 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 310 450 68.9 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2011-2025 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #include <zix/tree.h>
       5                 :            : 
       6                 :            : #include <zix/allocator.h>
       7                 :            : #include <zix/attributes.h>
       8                 :            : #include <zix/status.h>
       9                 :            : 
      10                 :            : #include <assert.h>
      11                 :            : 
      12                 :            : typedef struct ZixTreeNodeImpl ZixTreeNode;
      13                 :            : 
      14                 :            : struct ZixTreeImpl {
      15                 :            :   ZixAllocator*      allocator;
      16                 :            :   ZixTreeNode*       root;
      17                 :            :   ZixTreeDestroyFunc destroy;
      18                 :            :   const void*        destroy_user_data;
      19                 :            :   ZixTreeCompareFunc cmp;
      20                 :            :   void*              cmp_data;
      21                 :            :   size_t             size;
      22                 :            :   bool               allow_duplicates;
      23                 :            : };
      24                 :            : 
      25                 :            : struct ZixTreeNodeImpl {
      26                 :            :   void*                   data;
      27                 :            :   struct ZixTreeNodeImpl* left;
      28                 :            :   struct ZixTreeNodeImpl* right;
      29                 :            :   struct ZixTreeNodeImpl* parent;
      30                 :            :   int                     balance;
      31                 :            : };
      32                 :            : 
      33                 :            : #define MIN(a, b) (((a) < (b)) ? (a) : (b))
      34                 :            : #define MAX(a, b) (((a) > (b)) ? (a) : (b))
      35                 :            : 
      36                 :            : #ifndef NDEBUG
      37                 :            : #  define ASSERT_BALANCE(n)                             \
      38                 :            :     assert(!(((n)->balance < -2 || (n)->balance > 2) || \
      39                 :            :              ((n)->balance < 0 && !(n)->left) ||        \
      40                 :            :              ((n)->balance > 0 && !(n)->right) ||       \
      41                 :            :              ((n)->balance != 0 && !(n)->left && !(n)->right)))
      42                 :            : #else
      43                 :            : #  define ASSERT_BALANCE(n)
      44                 :            : #endif
      45                 :            : 
      46                 :            : static void
      47                 :     601659 : zix_tree_noop_destroy(void* const ptr, const void* const user_data)
      48                 :            : {
      49                 :            :   (void)ptr;
      50                 :            :   (void)user_data;
      51                 :     601659 : }
      52                 :            : 
      53                 :            : ZixTree*
      54                 :        114 : zix_tree_new(ZixAllocator* const allocator,
      55                 :            :              bool                allow_duplicates,
      56                 :            :              ZixTreeCompareFunc  cmp,
      57                 :            :              void*               cmp_data,
      58                 :            :              ZixTreeDestroyFunc  destroy,
      59                 :            :              const void*         destroy_user_data)
      60                 :            : {
      61                 :        114 :   ZixTree* t = (ZixTree*)zix_malloc(allocator, sizeof(ZixTree));
      62                 :            : 
      63         [ +  + ]:        114 :   if (t) {
      64                 :        111 :     t->allocator         = allocator;
      65                 :        111 :     t->root              = NULL;
      66         [ -  + ]:        111 :     t->destroy           = destroy ? destroy : zix_tree_noop_destroy;
      67                 :        111 :     t->destroy_user_data = destroy_user_data;
      68                 :        111 :     t->cmp               = cmp;
      69                 :        111 :     t->cmp_data          = cmp_data;
      70                 :        111 :     t->size              = 0;
      71                 :        111 :     t->allow_duplicates  = allow_duplicates;
      72                 :            :   }
      73                 :            : 
      74                 :        114 :   return t;
      75                 :            : }
      76                 :            : 
      77                 :            : static void
      78                 :     601725 : zix_tree_free_rec(ZixTree* t, ZixTreeNode* n)
      79                 :            : {
      80         [ +  + ]:     601725 :   if (n) {
      81                 :     300807 :     zix_tree_free_rec(t, n->left);
      82                 :     300807 :     zix_tree_free_rec(t, n->right);
      83                 :     300807 :     t->destroy(n->data, t->destroy_user_data);
      84                 :            : 
      85                 :     300807 :     zix_free(t->allocator, n);
      86                 :            :   }
      87                 :     601725 : }
      88                 :            : 
      89                 :            : void
      90                 :        210 : zix_tree_free(ZixTree* t)
      91                 :            : {
      92         [ +  + ]:        210 :   if (t) {
      93                 :        111 :     zix_tree_free_rec(t, t->root);
      94                 :        111 :     zix_free(t->allocator, t);
      95                 :            :   }
      96                 :        210 : }
      97                 :            : 
      98                 :            : ZIX_REALTIME size_t
      99                 :        132 : zix_tree_size(const ZixTree* t)
     100                 :            : {
     101                 :        132 :   return t->size;
     102                 :            : }
     103                 :            : 
     104                 :            : static void
     105                 :     749642 : rotate(ZixTreeNode* p, ZixTreeNode* q)
     106                 :            : {
     107         [ -  + ]:     749642 :   assert(q->parent == p);
     108   [ +  +  -  + ]:     749642 :   assert(p->left == q || p->right == q);
     109                 :            : 
     110                 :     749642 :   q->parent = p->parent;
     111         [ +  + ]:     749642 :   if (q->parent) {
     112         [ +  + ]:     749035 :     if (q->parent->left == p) {
     113                 :     301053 :       q->parent->left = q;
     114                 :            :     } else {
     115                 :     447982 :       q->parent->right = q;
     116                 :            :     }
     117                 :            :   }
     118                 :            : 
     119         [ +  + ]:     749642 :   if (p->right == q) {
     120                 :            :     // Rotate left
     121                 :     498195 :     p->right = q->left;
     122                 :     498195 :     q->left  = p;
     123         [ +  + ]:     498195 :     if (p->right) {
     124                 :     297600 :       p->right->parent = p;
     125                 :            :     }
     126                 :            :   } else {
     127                 :            :     // Rotate right
     128         [ -  + ]:     251447 :     assert(p->left == q);
     129                 :     251447 :     p->left  = q->right;
     130                 :     251447 :     q->right = p;
     131         [ +  + ]:     251447 :     if (p->left) {
     132                 :     150324 :       p->left->parent = p;
     133                 :            :     }
     134                 :            :   }
     135                 :            : 
     136                 :     749642 :   p->parent = q;
     137                 :     749642 : }
     138                 :            : 
     139                 :            : /**
     140                 :            :  * Rotate left about `p`.
     141                 :            :  *
     142                 :            :  *    p              q
     143                 :            :  *   / \            / \
     144                 :            :  *  A   q    =>    p   C
     145                 :            :  *     / \        / \
     146                 :            :  *    B   C      A   B
     147                 :            :  */
     148                 :            : static ZixTreeNode*
     149                 :     496920 : rotate_left(ZixTreeNode* p, int* height_change)
     150                 :            : {
     151                 :     496920 :   ZixTreeNode* const q = p->right;
     152         [ +  + ]:     496920 :   *height_change       = (q->balance == 0) ? 0 : -1;
     153                 :            : 
     154         [ -  + ]:     496920 :   assert(p->balance == 2);
     155   [ +  +  -  + ]:     496920 :   assert(q->balance == 0 || q->balance == 1);
     156                 :            : 
     157                 :     496920 :   rotate(p, q);
     158                 :            : 
     159                 :            :   // p->balance -= 1 + MAX(0, q->balance);
     160                 :            :   // q->balance -= 1 - MIN(0, p->balance);
     161                 :     496920 :   --q->balance;
     162                 :     496920 :   p->balance = -q->balance;
     163                 :            : 
     164   [ +  -  +  -  :     496920 :   ASSERT_BALANCE(p);
          -  +  -  -  +  
          +  +  -  +  +  
             +  +  -  + ]
     165   [ +  -  +  -  :     496920 :   ASSERT_BALANCE(q);
          +  +  +  -  -  
          +  -  -  +  +  
             -  +  -  - ]
     166                 :     496920 :   return q;
     167                 :            : }
     168                 :            : 
     169                 :            : /**
     170                 :            :  * Rotate right about `p`.
     171                 :            :  *
     172                 :            :  *      p          q
     173                 :            :  *     / \        / \
     174                 :            :  *    q   C  =>  A   p
     175                 :            :  *   / \            / \
     176                 :            :  *  A   B          B   C
     177                 :            :  *
     178                 :            :  */
     179                 :            : static ZixTreeNode*
     180                 :     250172 : rotate_right(ZixTreeNode* p, int* height_change)
     181                 :            : {
     182                 :     250172 :   ZixTreeNode* const q = p->left;
     183         [ +  + ]:     250172 :   *height_change       = (q->balance == 0) ? 0 : -1;
     184                 :            : 
     185         [ -  + ]:     250172 :   assert(p->balance == -2);
     186   [ +  +  -  + ]:     250172 :   assert(q->balance == 0 || q->balance == -1);
     187                 :            : 
     188                 :     250172 :   rotate(p, q);
     189                 :            : 
     190                 :            :   // p->balance += 1 - MIN(0, q->balance);
     191                 :            :   // q->balance += 1 + MAX(0, p->balance);
     192                 :     250172 :   ++q->balance;
     193                 :     250172 :   p->balance = -q->balance;
     194                 :            : 
     195   [ +  -  +  -  :     250172 :   ASSERT_BALANCE(p);
          +  +  +  -  -  
          +  -  -  +  +  
             -  +  -  - ]
     196   [ +  -  +  -  :     250172 :   ASSERT_BALANCE(q);
          -  +  -  -  +  
          +  +  -  +  +  
             -  +  -  - ]
     197                 :     250172 :   return q;
     198                 :            : }
     199                 :            : 
     200                 :            : /**
     201                 :            :  * Rotate left about `p->left` then right about `p`.
     202                 :            :  *
     203                 :            :  *      p             r
     204                 :            :  *     / \           / \
     205                 :            :  *    q   D  =>    q     p
     206                 :            :  *   / \          / \   / \
     207                 :            :  *  A   r        A   B C   D
     208                 :            :  *     / \
     209                 :            :  *    B   C
     210                 :            :  *
     211                 :            :  */
     212                 :            : static ZixTreeNode*
     213                 :       1158 : rotate_left_right(ZixTreeNode* p, int* height_change)
     214                 :            : {
     215                 :       1158 :   ZixTreeNode* const q = p->left;
     216                 :       1158 :   ZixTreeNode* const r = q->right;
     217                 :            : 
     218         [ -  + ]:       1158 :   assert(p->balance == -2);
     219         [ -  + ]:       1158 :   assert(q->balance == 1);
     220   [ +  +  +  +  :       1158 :   assert(r->balance == -1 || r->balance == 0 || r->balance == 1);
                   -  + ]
     221                 :            : 
     222                 :       1158 :   rotate(q, r);
     223                 :       1158 :   rotate(p, r);
     224                 :            : 
     225                 :       1158 :   q->balance -= 1 + MAX(0, r->balance);
     226         [ +  - ]:       1158 :   p->balance += 1 - MIN(MIN(0, r->balance) - 1, r->balance + q->balance);
     227                 :            :   // r->balance += MAX(0, p->balance) + MIN(0, q->balance);
     228                 :            : 
     229                 :            :   // p->balance = (p->left && p->right) ? -MIN(r->balance, 0) : 0;
     230                 :            :   // q->balance = - MAX(r->balance, 0);
     231                 :       1158 :   r->balance = 0;
     232                 :            : 
     233                 :       1158 :   *height_change = -1;
     234                 :            : 
     235   [ +  -  +  -  :       1158 :   ASSERT_BALANCE(p);
          -  +  -  -  +  
          +  +  -  +  +  
             -  +  -  - ]
     236   [ +  -  +  -  :       1158 :   ASSERT_BALANCE(q);
          +  +  +  -  -  
          +  -  -  +  +  
             -  +  -  - ]
     237   [ +  -  +  -  :       1158 :   ASSERT_BALANCE(r);
          -  +  -  -  -  
          +  -  -  -  +  
             -  -  -  - ]
     238                 :       1158 :   return r;
     239                 :            : }
     240                 :            : 
     241                 :            : /**
     242                 :            :  * Rotate right about `p->right` then right about `p`.
     243                 :            :  *
     244                 :            :  *    p               r
     245                 :            :  *   / \             / \
     246                 :            :  *  A   q    =>    p     q
     247                 :            :  *     / \        / \   / \
     248                 :            :  *    r   D      A   B C   D
     249                 :            :  *   / \
     250                 :            :  *  B   C
     251                 :            :  *
     252                 :            :  */
     253                 :            : static ZixTreeNode*
     254                 :        117 : rotate_right_left(ZixTreeNode* p, int* height_change)
     255                 :            : {
     256                 :        117 :   ZixTreeNode* const q = p->right;
     257                 :        117 :   ZixTreeNode* const r = q->left;
     258                 :            : 
     259         [ -  + ]:        117 :   assert(p->balance == 2);
     260         [ -  + ]:        117 :   assert(q->balance == -1);
     261   [ +  +  +  +  :        117 :   assert(r->balance == -1 || r->balance == 0 || r->balance == 1);
                   -  + ]
     262                 :            : 
     263                 :        117 :   rotate(q, r);
     264                 :        117 :   rotate(p, r);
     265                 :            : 
     266                 :        117 :   q->balance += 1 - MIN(0, r->balance);
     267         [ +  - ]:        117 :   p->balance -= 1 + MAX(MAX(0, r->balance) + 1, r->balance + q->balance);
     268                 :            :   // r->balance += MAX(0, q->balance) + MIN(0, p->balance);
     269                 :            : 
     270                 :            :   // p->balance = (p->left && p->right) ? -MAX(r->balance, 0) : 0;
     271                 :            :   // q->balance = - MIN(r->balance, 0);
     272                 :        117 :   r->balance = 0;
     273                 :            :   // assert(r->balance == 0);
     274                 :            : 
     275                 :        117 :   *height_change = -1;
     276                 :            : 
     277   [ +  -  +  -  :        117 :   ASSERT_BALANCE(p);
          +  +  +  -  -  
          +  -  -  +  +  
             -  +  -  - ]
     278   [ +  -  +  -  :        117 :   ASSERT_BALANCE(q);
          -  +  -  -  +  
          +  +  -  +  +  
             +  +  -  + ]
     279   [ +  -  +  -  :        117 :   ASSERT_BALANCE(r);
          -  +  -  -  -  
          +  -  -  -  +  
             -  -  -  - ]
     280                 :        117 :   return r;
     281                 :            : }
     282                 :            : 
     283                 :            : static ZixTreeNode*
     284                 :     897944 : zix_tree_rebalance(ZixTree* t, ZixTreeNode* node, int* height_change)
     285                 :            : {
     286                 :     897944 :   *height_change = 0;
     287                 :            : 
     288                 :     897944 :   const bool is_root = !node->parent;
     289   [ +  +  -  +  :     897944 :   assert((is_root && t->root == node) || (!is_root && t->root != node));
             +  -  -  + ]
     290                 :            : 
     291                 :     897944 :   ZixTreeNode* replacement = node;
     292         [ +  + ]:     897944 :   if (node->balance == -2) {
     293         [ -  + ]:     251330 :     assert(node->left);
     294                 :            :     // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
     295         [ +  + ]:     251330 :     if (node->left->balance == 1) {
     296                 :       1158 :       replacement = rotate_left_right(node, height_change);
     297                 :            :     } else {
     298                 :     250172 :       replacement = rotate_right(node, height_change);
     299                 :            :     }
     300         [ +  + ]:     646614 :   } else if (node->balance == 2) {
     301         [ -  + ]:     497037 :     assert(node->right);
     302                 :            :     // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
     303         [ +  + ]:     497037 :     if (node->right->balance == -1) {
     304                 :        117 :       replacement = rotate_right_left(node, height_change);
     305                 :            :     } else {
     306                 :     496920 :       replacement = rotate_left(node, height_change);
     307                 :            :     }
     308                 :            :   }
     309         [ +  + ]:     897944 :   if (is_root) {
     310         [ -  + ]:        629 :     assert(!replacement->parent);
     311                 :        629 :     t->root = replacement;
     312                 :            :   }
     313                 :            : 
     314                 :     897944 :   return replacement;
     315                 :            : }
     316                 :            : 
     317                 :            : ZixStatus
     318                 :     601758 : zix_tree_insert(ZixTree* t, void* e, ZixTreeIter** ti)
     319                 :            : {
     320                 :     601758 :   int          cmp = 0;
     321                 :     601758 :   ZixTreeNode* p   = NULL;
     322                 :            : 
     323                 :            :   // Find the parent p of e
     324         [ +  + ]:   10034734 :   for (ZixTreeNode* n = t->root; n;) {
     325                 :    9432979 :     p   = n;
     326                 :    9432979 :     cmp = t->cmp(e, n->data, t->cmp_data);
     327         [ +  + ]:    9432979 :     if (cmp < 0) {
     328                 :    3843118 :       n = n->left;
     329   [ +  +  +  + ]:    5589861 :     } else if (cmp > 0 || t->allow_duplicates) {
     330                 :    5589858 :       n = n->right;
     331                 :            :     } else {
     332         [ +  - ]:          3 :       if (ti) {
     333                 :          3 :         *ti = n;
     334                 :            :       }
     335                 :          3 :       return ZIX_STATUS_EXISTS;
     336                 :            :     }
     337                 :            :   }
     338                 :            : 
     339                 :            :   // Allocate a new node n
     340                 :            :   ZixTreeNode* const n =
     341                 :     601755 :     (ZixTreeNode*)zix_calloc(t->allocator, 1, sizeof(ZixTreeNode));
     342         [ +  + ]:     601755 :   if (!n) {
     343                 :         96 :     return ZIX_STATUS_NO_MEM;
     344                 :            :   }
     345                 :            : 
     346                 :     601659 :   n->data = e;
     347         [ +  - ]:     601659 :   if (ti) {
     348                 :     601659 :     *ti = n;
     349                 :            :   }
     350                 :            : 
     351                 :     601659 :   bool p_height_increased = false;
     352                 :            : 
     353                 :            :   // Make p the parent of n
     354                 :     601659 :   n->parent = p;
     355         [ +  + ]:     601659 :   if (!p) {
     356                 :        165 :     t->root = n;
     357                 :            :   } else {
     358         [ +  + ]:     601494 :     if (cmp < 0) {
     359         [ -  + ]:     201298 :       assert(!p->left);
     360   [ +  +  -  + ]:     201298 :       assert(p->balance == 0 || p->balance == 1);
     361                 :     201298 :       p->left = n;
     362                 :     201298 :       --p->balance;
     363                 :     201298 :       p_height_increased = !p->right;
     364                 :            :     } else {
     365         [ -  + ]:     400196 :       assert(!p->right);
     366   [ +  +  -  + ]:     400196 :       assert(p->balance == 0 || p->balance == -1);
     367                 :     400196 :       p->right = n;
     368                 :     400196 :       ++p->balance;
     369                 :     400196 :       p_height_increased = !p->left;
     370                 :            :     }
     371                 :            :   }
     372                 :            : 
     373                 :            :   // Rebalance if necessary (at most 1 rotation)
     374   [ +  +  +  +  :     601659 :   assert(!p || p->balance == -1 || p->balance == 0 || p->balance == 1);
             +  +  -  + ]
     375   [ +  +  +  + ]:     601659 :   if (p && p_height_increased) {
     376                 :     601394 :     int height_change = 0;
     377   [ +  -  +  + ]:    1200998 :     for (ZixTreeNode* i = p; i && i->parent; i = i->parent) {
     378         [ +  + ]:    1200450 :       i->parent->balance += (i == i->parent->left) ? -1 : 1;
     379                 :            : 
     380   [ +  +  +  + ]:    1200450 :       if (i->parent->balance == -2 || i->parent->balance == 2) {
     381                 :     599004 :         zix_tree_rebalance(t, i->parent, &height_change);
     382                 :     599004 :         break;
     383                 :            :       }
     384                 :            : 
     385         [ +  + ]:     601446 :       if (i->parent->balance == 0) {
     386                 :       1842 :         break;
     387                 :            :       }
     388                 :            :     }
     389                 :            :   }
     390                 :            : 
     391                 :     601659 :   ++t->size;
     392                 :            : 
     393                 :     601659 :   return ZIX_STATUS_SUCCESS;
     394                 :            : }
     395                 :            : 
     396                 :            : ZixStatus
     397                 :     300852 : zix_tree_remove(ZixTree* t, ZixTreeIter* ti)
     398                 :            : {
     399                 :     300852 :   ZixTreeNode* const n          = ti;
     400                 :     300852 :   ZixTreeNode**      pp         = NULL;      // parent pointer
     401                 :     300852 :   ZixTreeNode*       to_balance = n->parent; // lowest node to balance
     402                 :     300852 :   int                d_balance  = 0;         // delta(balance) for n->parent
     403                 :            : 
     404   [ +  +  +  +  :     300852 :   if ((n == t->root) && !n->left && !n->right) {
                   +  + ]
     405                 :         60 :     t->root = NULL;
     406                 :         60 :     t->destroy(n->data, t->destroy_user_data);
     407                 :         60 :     zix_free(t->allocator, n);
     408                 :         60 :     --t->size;
     409         [ -  + ]:         60 :     assert(!t->size);
     410                 :         60 :     return ZIX_STATUS_SUCCESS;
     411                 :            :   }
     412                 :            : 
     413                 :            :   // Set pp to the parent pointer to n, if applicable
     414         [ +  + ]:     300792 :   if (n->parent) {
     415   [ +  +  -  + ]:     299730 :     assert(n->parent->left == n || n->parent->right == n);
     416         [ +  + ]:     299730 :     if (n->parent->left == n) { // n is left child
     417                 :     149732 :       pp        = &n->parent->left;
     418                 :     149732 :       d_balance = 1;
     419                 :            :     } else { // n is right child
     420         [ -  + ]:     149998 :       assert(n->parent->right == n);
     421                 :     149998 :       pp        = &n->parent->right;
     422                 :     149998 :       d_balance = -1;
     423                 :            :     }
     424                 :            :   }
     425                 :            : 
     426   [ +  +  -  + ]:     300792 :   assert(!pp || *pp == n);
     427                 :            : 
     428                 :     300792 :   int height_change = 0;
     429   [ +  +  +  + ]:     300792 :   if (!n->left && !n->right) {
     430                 :            :     // n is a leaf, just remove it
     431         [ +  - ]:     100477 :     if (pp) {
     432                 :     100477 :       *pp           = NULL;
     433                 :     100477 :       to_balance    = n->parent;
     434   [ +  +  +  + ]:     100477 :       height_change = (!n->parent->left && !n->parent->right) ? -1 : 0;
     435                 :            :     }
     436                 :            : 
     437         [ +  + ]:     200315 :   } else if (!n->left) {
     438                 :            :     // Replace n with right (only) child
     439         [ +  + ]:      50368 :     if (pp) {
     440                 :      50314 :       *pp        = n->right;
     441                 :      50314 :       to_balance = n->parent;
     442                 :            :     } else {
     443                 :         54 :       t->root = n->right;
     444                 :            :     }
     445                 :      50368 :     n->right->parent = n->parent;
     446                 :      50368 :     height_change    = -1;
     447                 :            : 
     448         [ +  + ]:     149947 :   } else if (!n->right) {
     449                 :            :     // Replace n with left (only) child
     450         [ +  + ]:      50036 :     if (pp) {
     451                 :      50031 :       *pp        = n->left;
     452                 :      50031 :       to_balance = n->parent;
     453                 :            :     } else {
     454                 :          5 :       t->root = n->left;
     455                 :            :     }
     456                 :      50036 :     n->left->parent = n->parent;
     457                 :      50036 :     height_change   = -1;
     458                 :            : 
     459                 :            :   } else {
     460                 :            :     // Replace n with in-order successor (leftmost child of right subtree)
     461                 :      99911 :     ZixTreeNode* replace = n->right;
     462         [ +  + ]:     770789 :     while (replace->left) {
     463         [ -  + ]:     670878 :       assert(replace->left->parent == replace);
     464                 :     670878 :       replace = replace->left;
     465                 :            :     }
     466                 :            : 
     467                 :            :     // Remove replace from parent (replace_p)
     468         [ +  + ]:      99911 :     if (replace->parent->left == replace) {
     469         [ +  + ]:      99801 :       height_change         = replace->parent->right ? 0 : -1;
     470                 :      99801 :       d_balance             = 1;
     471                 :      99801 :       to_balance            = replace->parent;
     472                 :      99801 :       replace->parent->left = replace->right;
     473                 :            :     } else {
     474         [ -  + ]:        110 :       assert(replace->parent == n);
     475         [ +  - ]:        110 :       height_change          = replace->parent->left ? 0 : -1;
     476                 :        110 :       d_balance              = -1;
     477                 :        110 :       to_balance             = replace->parent;
     478                 :        110 :       replace->parent->right = replace->right;
     479                 :            :     }
     480                 :            : 
     481         [ +  + ]:      99911 :     if (to_balance == n) {
     482                 :        110 :       to_balance = replace;
     483                 :            :     }
     484                 :            : 
     485         [ +  + ]:      99911 :     if (replace->right) {
     486                 :      49858 :       replace->right->parent = replace->parent;
     487                 :            :     }
     488                 :            : 
     489                 :      99911 :     replace->balance = n->balance;
     490                 :            : 
     491                 :            :     // Swap node to delete with replace
     492         [ +  + ]:      99911 :     if (pp) {
     493                 :      98908 :       *pp = replace;
     494                 :            :     } else {
     495         [ -  + ]:       1003 :       assert(t->root == n);
     496                 :       1003 :       t->root = replace;
     497                 :            :     }
     498                 :            : 
     499         [ -  + ]:      99911 :     assert(n->left);
     500                 :            : 
     501                 :      99911 :     replace->parent = n->parent;
     502                 :      99911 :     replace->left   = n->left;
     503                 :      99911 :     n->left->parent = replace;
     504                 :      99911 :     replace->right  = n->right;
     505         [ +  + ]:      99911 :     if (n->right) {
     506                 :      99854 :       n->right->parent = replace;
     507                 :            :     }
     508                 :            : 
     509   [ +  +  +  +  :      99911 :     assert(!replace->parent || replace->parent->left == replace ||
                   -  + ]
     510                 :            :            replace->parent->right == replace);
     511                 :            :   }
     512                 :            : 
     513                 :            :   // Rebalance starting at to_balance upwards
     514         [ +  + ]:     599732 :   for (ZixTreeNode* i = to_balance; i; i = i->parent) {
     515                 :     599454 :     i->balance += d_balance;
     516   [ +  +  +  +  :     599454 :     if (d_balance == 0 || i->balance == -1 || i->balance == 1) {
                   +  + ]
     517                 :            :       break;
     518                 :            :     }
     519                 :            : 
     520         [ -  + ]:     298940 :     assert(i != n);
     521                 :     298940 :     i = zix_tree_rebalance(t, i, &height_change);
     522         [ +  + ]:     298940 :     if (i->balance == 0) {
     523                 :     150204 :       height_change = -1;
     524                 :            :     }
     525                 :            : 
     526         [ +  + ]:     298940 :     if (i->parent) {
     527         [ +  + ]:     298721 :       if (i == i->parent->left) {
     528                 :     197525 :         d_balance = height_change * -1;
     529                 :            :       } else {
     530         [ -  + ]:     101196 :         assert(i == i->parent->right);
     531                 :     101196 :         d_balance = height_change;
     532                 :            :       }
     533                 :            :     }
     534                 :            :   }
     535                 :            : 
     536                 :     300792 :   t->destroy(n->data, t->destroy_user_data);
     537                 :     300792 :   zix_free(t->allocator, n);
     538                 :            : 
     539                 :     300792 :   --t->size;
     540                 :     300792 :   return ZIX_STATUS_SUCCESS;
     541                 :            : }
     542                 :            : 
     543                 :            : ZixStatus
     544                 :     601704 : zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti)
     545                 :            : {
     546                 :     601704 :   ZixTreeNode* n = t->root;
     547         [ +  - ]:    6703519 :   while (n) {
     548                 :    6703519 :     const int cmp = t->cmp(e, n->data, t->cmp_data);
     549         [ +  + ]:    6703519 :     if (cmp == 0) {
     550                 :     601704 :       break;
     551                 :            :     }
     552                 :            : 
     553         [ +  + ]:    6101815 :     if (cmp < 0) {
     554                 :    3073671 :       n = n->left;
     555                 :            :     } else {
     556                 :    3028144 :       n = n->right;
     557                 :            :     }
     558                 :            :   }
     559                 :            : 
     560                 :     601704 :   *ti = n;
     561         [ +  - ]:     601704 :   return n ? ZIX_STATUS_SUCCESS : ZIX_STATUS_NOT_FOUND;
     562                 :            : }
     563                 :            : 
     564                 :            : ZIX_REALTIME void*
     565                 :    1504215 : zix_tree_get(const ZixTreeIter* ti)
     566                 :            : {
     567         [ +  - ]:    1504215 :   return ti ? ti->data : NULL;
     568                 :            : }
     569                 :            : 
     570                 :            : ZIX_NONBLOCKING ZixTreeIter*
     571                 :        171 : zix_tree_begin(ZixTree* t)
     572                 :            : {
     573         [ +  + ]:        171 :   if (!t->root) {
     574                 :        111 :     return NULL;
     575                 :            :   }
     576                 :            : 
     577                 :         60 :   ZixTreeNode* n = t->root;
     578         [ +  + ]:        273 :   while (n->left) {
     579                 :        213 :     n = n->left;
     580                 :            :   }
     581                 :            : 
     582                 :         60 :   return n;
     583                 :            : }
     584                 :            : 
     585                 :            : ZIX_REALTIME ZixTreeIter*
     586                 :        111 : zix_tree_end(ZixTree* ZIX_UNUSED(t))
     587                 :            : {
     588                 :        111 :   return NULL;
     589                 :            : }
     590                 :            : 
     591                 :            : ZIX_NONBLOCKING ZixTreeIter*
     592                 :        171 : zix_tree_rbegin(ZixTree* t)
     593                 :            : {
     594         [ +  + ]:        171 :   if (!t->root) {
     595                 :        111 :     return NULL;
     596                 :            :   }
     597                 :            : 
     598                 :         60 :   ZixTreeNode* n = t->root;
     599         [ +  + ]:        321 :   while (n->right) {
     600                 :        261 :     n = n->right;
     601                 :            :   }
     602                 :            : 
     603                 :         60 :   return n;
     604                 :            : }
     605                 :            : 
     606                 :            : ZIX_REALTIME ZixTreeIter*
     607                 :        111 : zix_tree_rend(ZixTree* ZIX_UNUSED(t))
     608                 :            : {
     609                 :        111 :   return NULL;
     610                 :            : }
     611                 :            : 
     612                 :            : ZIX_REALTIME bool
     613                 :     300912 : zix_tree_iter_is_end(const ZixTreeIter* i)
     614                 :            : {
     615                 :     300912 :   return !i;
     616                 :            : }
     617                 :            : 
     618                 :            : ZIX_REALTIME bool
     619                 :     300912 : zix_tree_iter_is_rend(const ZixTreeIter* i)
     620                 :            : {
     621                 :     300912 :   return !i;
     622                 :            : }
     623                 :            : 
     624                 :            : ZIX_NONBLOCKING ZixTreeIter*
     625                 :     300855 : zix_tree_iter_next(ZixTreeIter* i)
     626                 :            : {
     627         [ +  + ]:     300855 :   if (!i) {
     628                 :          3 :     return NULL;
     629                 :            :   }
     630                 :            : 
     631         [ +  + ]:     300852 :   if (i->right) {
     632                 :     150438 :     i = i->right;
     633         [ +  + ]:     300579 :     while (i->left) {
     634                 :     150141 :       i = i->left;
     635                 :            :     }
     636                 :            :   } else {
     637   [ +  +  +  + ]:     300852 :     while (i->parent && i->parent->right == i) { // i is a right child
     638                 :     150438 :       i = i->parent;
     639                 :            :     }
     640                 :            : 
     641                 :     150414 :     i = i->parent;
     642                 :            :   }
     643                 :            : 
     644                 :     300852 :   return i;
     645                 :            : }
     646                 :            : 
     647                 :            : ZIX_NONBLOCKING ZixTreeIter*
     648                 :     300855 : zix_tree_iter_prev(ZixTreeIter* i)
     649                 :            : {
     650         [ +  + ]:     300855 :   if (!i) {
     651                 :          3 :     return NULL;
     652                 :            :   }
     653                 :            : 
     654         [ +  + ]:     300852 :   if (i->left) {
     655                 :     150354 :     i = i->left;
     656         [ +  + ]:     300531 :     while (i->right) {
     657                 :     150177 :       i = i->right;
     658                 :            :     }
     659                 :            : 
     660                 :            :   } else {
     661   [ +  +  +  + ]:     300852 :     while (i->parent && i->parent->left == i) { // i is a left child
     662                 :     150354 :       i = i->parent;
     663                 :            :     }
     664                 :            : 
     665                 :     150498 :     i = i->parent;
     666                 :            :   }
     667                 :            : 
     668                 :     300852 :   return i;
     669                 :            : }

Generated by: LCOV version 1.16