LCOV - code coverage report
Current view: top level - tommyds - tommytree.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 127 127 100.0 %
Date: 2026-03-15 15:58:19 Functions: 19 19 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015, Andrea Mazzoleni. All rights reserved.
       3             :  *
       4             :  * Redistribution and use in source and binary forms, with or without
       5             :  * modification, are permitted provided that the following conditions
       6             :  * are met:
       7             :  *
       8             :  * 1. Redistributions of source code must retain the above copyright
       9             :  *    notice, this list of conditions and the following disclaimer.
      10             :  *
      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             :  *
      15             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      16             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      17             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      18             :  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
      19             :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      20             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      21             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      22             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      23             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      24             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      25             :  * POSSIBILITY OF SUCH DAMAGE.
      26             :  */
      27             : 
      28             : #include "tommytree.h"
      29             : 
      30             : #include <assert.h> /* for assert */
      31             : 
      32             : /******************************************************************************/
      33             : /* tree */
      34             : 
      35        3543 : TOMMY_API void tommy_tree_init(tommy_tree* tree, tommy_compare_func* cmp)
      36             : {
      37        3543 :         tree->root = 0;
      38        3543 :         tree->count = 0;
      39        3543 :         tree->cmp = cmp;
      40        3543 : }
      41             : 
      42   119009252 : static tommy_ssize_t tommy_tree_delta(tommy_tree_node* root)
      43             : {
      44   119009252 :         tommy_ssize_t left_height = root->prev ? root->prev->index : 0;
      45   119009252 :         tommy_ssize_t right_height = root->next ? root->next->index : 0;
      46             : 
      47   119009252 :         return left_height - right_height;
      48             : }
      49             : 
      50             : /* AVL tree operations */
      51             : static tommy_tree_node* tommy_tree_balance(tommy_tree_node*);
      52             : 
      53     7951362 : static tommy_tree_node* tommy_tree_rotate_left(tommy_tree_node* root)
      54             : {
      55     7951362 :         tommy_tree_node* next = root->next;
      56             : 
      57     7951362 :         root->next = next->prev;
      58             : 
      59     7951362 :         next->prev = tommy_tree_balance(root);
      60             : 
      61     7951362 :         return tommy_tree_balance(next);
      62             : }
      63             : 
      64      184325 : static tommy_tree_node* tommy_tree_rotate_right(tommy_tree_node* root)
      65             : {
      66      184325 :         tommy_tree_node* prev = root->prev;
      67             : 
      68      184325 :         root->prev = prev->next;
      69             : 
      70      184325 :         prev->next = tommy_tree_balance(root);
      71             : 
      72      184325 :         return tommy_tree_balance(prev);
      73             : }
      74             : 
      75      178930 : static tommy_tree_node* tommy_tree_move_right(tommy_tree_node* root, tommy_tree_node* node)
      76             : {
      77      178930 :         if (!root)
      78       76640 :                 return node;
      79             : 
      80      102290 :         root->next = tommy_tree_move_right(root->next, node);
      81             : 
      82      102290 :         return tommy_tree_balance(root);
      83             : }
      84             : 
      85   111041265 : static tommy_tree_node* tommy_tree_balance(tommy_tree_node* root)
      86             : {
      87   111041265 :         tommy_ssize_t delta = tommy_tree_delta(root);
      88             : 
      89   111041265 :         if (delta < -1) {
      90     7828801 :                 if (tommy_tree_delta(root->next) > 0)
      91       45139 :                         root->next = tommy_tree_rotate_right(root->next);
      92     7828801 :                 return tommy_tree_rotate_left(root);
      93             :         }
      94             : 
      95   103212464 :         if (delta > 1) {
      96      139186 :                 if (tommy_tree_delta(root->prev) < 0)
      97      122561 :                         root->prev = tommy_tree_rotate_left(root->prev);
      98      139186 :                 return tommy_tree_rotate_right(root);
      99             :         }
     100             : 
     101             :         /* recompute key */
     102   103073278 :         root->index = 0;
     103             : 
     104   103073278 :         if (root->prev && root->prev->index > root->index)
     105    83313158 :                 root->index = root->prev->index;
     106             : 
     107   103073278 :         if (root->next && root->next->index > root->index)
     108    47473770 :                 root->index = root->next->index;
     109             : 
     110             :         /* count itself */
     111   103073278 :         root->index += 1;
     112             : 
     113   103073278 :         return root;
     114             : }
     115             : 
     116   101920055 : static tommy_tree_node* tommy_tree_insert_node(tommy_compare_func* cmp, tommy_tree_node* root, tommy_tree_node** let)
     117             : {
     118             :         int c;
     119             : 
     120   101920055 :         if (!root)
     121     7939460 :                 return *let;
     122             : 
     123    93980595 :         c = cmp((*let)->data, root->data);
     124             : 
     125    93980595 :         if (c < 0) {
     126     1719825 :                 root->prev = tommy_tree_insert_node(cmp, root->prev, let);
     127     1719825 :                 return tommy_tree_balance(root);
     128             :         }
     129             : 
     130    92260770 :         if (c > 0) {
     131    92260769 :                 root->next = tommy_tree_insert_node(cmp, root->next, let);
     132    92260769 :                 return tommy_tree_balance(root);
     133             :         }
     134             : 
     135             :         /* already present, set the return pointer */
     136           1 :         *let = root;
     137             : 
     138           1 :         return root;
     139             : }
     140             : 
     141     7939461 : TOMMY_API void* tommy_tree_insert(tommy_tree* tree, tommy_tree_node* node, void* data)
     142             : {
     143     7939461 :         tommy_tree_node* insert = node;
     144             : 
     145     7939461 :         insert->data = data;
     146     7939461 :         insert->prev = 0;
     147     7939461 :         insert->next = 0;
     148     7939461 :         insert->index = 0;
     149             : 
     150     7939461 :         tree->root = tommy_tree_insert_node(tree->cmp, tree->root, &insert);
     151             : 
     152     7939461 :         if (insert == node)
     153     7939460 :                 ++tree->count;
     154             : 
     155     7939461 :         return insert->data;
     156             : }
     157             : 
     158      763775 : static tommy_tree_node* tommy_tree_remove_node(tommy_compare_func* cmp, tommy_tree_node* root, void* data, tommy_tree_node** let)
     159             : {
     160             :         int c;
     161             : 
     162      763775 :         if (!root)
     163         128 :                 return 0;
     164             : 
     165      763647 :         c = cmp(data, root->data);
     166             : 
     167      763647 :         if (c < 0) {
     168      309129 :                 root->prev = tommy_tree_remove_node(cmp, root->prev, data, let);
     169      309129 :                 return tommy_tree_balance(root);
     170             :         }
     171             : 
     172      454518 :         if (c > 0) {
     173      377878 :                 root->next = tommy_tree_remove_node(cmp, root->next, data, let);
     174      377878 :                 return tommy_tree_balance(root);
     175             :         }
     176             : 
     177             :         /* found */
     178       76640 :         *let = root;
     179             : 
     180       76640 :         return tommy_tree_move_right(root->prev, root->next);
     181             : }
     182             : 
     183       76768 : TOMMY_API void* tommy_tree_remove(tommy_tree* tree, void* data)
     184             : {
     185       76768 :         tommy_tree_node* node = 0;
     186             : 
     187       76768 :         tree->root = tommy_tree_remove_node(tree->cmp, tree->root, data, &node);
     188             : 
     189       76768 :         if (!node)
     190         128 :                 return 0;
     191             : 
     192       76640 :         --tree->count;
     193             : 
     194       76640 :         return node->data;
     195             : }
     196             : 
     197   191538520 : static tommy_tree_node* tommy_tree_search_node(tommy_compare_func* cmp, tommy_tree_node* root, void* data)
     198             : {
     199             :         int c;
     200             : 
     201   191538520 :         if (!root)
     202     2133992 :                 return 0;
     203             : 
     204   189404528 :         c = cmp(data, root->data);
     205             : 
     206   189404528 :         if (c < 0)
     207    69779458 :                 return tommy_tree_search_node(cmp, root->prev, data);
     208             : 
     209   119625070 :         if (c > 0)
     210   104426160 :                 return tommy_tree_search_node(cmp, root->next, data);
     211             : 
     212    15198910 :         return root;
     213             : }
     214             : 
     215           2 : TOMMY_API void* tommy_tree_search(tommy_tree* tree, void* data)
     216             : {
     217           2 :         tommy_tree_node* node = tommy_tree_search_node(tree->cmp, tree->root, data);
     218             : 
     219           2 :         if (!node)
     220           1 :                 return 0;
     221             : 
     222           1 :         return node->data;
     223             : }
     224             : 
     225    17332900 : TOMMY_API void* tommy_tree_search_compare(tommy_tree* tree, tommy_compare_func* cmp, void* cmp_arg)
     226             : {
     227    17332900 :         tommy_tree_node* node = tommy_tree_search_node(cmp, tree->root, cmp_arg);
     228             : 
     229    17332900 :         if (!node)
     230     2133991 :                 return 0;
     231             : 
     232    15198909 :         return node->data;
     233             : }
     234             : 
     235         128 : TOMMY_API void* tommy_tree_remove_existing(tommy_tree* tree, tommy_tree_node* node)
     236             : {
     237         128 :         void* data = tommy_tree_remove(tree, node->data);
     238             : 
     239         128 :         assert(data != 0);
     240             : 
     241         128 :         return data;
     242             : }
     243             : 
     244     7773174 : static void tommy_tree_foreach_node(tommy_tree_node* root, tommy_foreach_func* func)
     245             : {
     246             :         tommy_tree_node* next;
     247             : 
     248     7773174 :         if (!root)
     249     3887443 :                 return;
     250             : 
     251     3885731 :         tommy_tree_foreach_node(root->prev, func);
     252             : 
     253             :         /* make a copy in case func is free() */
     254     3885731 :         next = root->next;
     255             : 
     256     3885731 :         func(root->data);
     257             : 
     258     3885731 :         tommy_tree_foreach_node(next, func);
     259             : }
     260             : 
     261        1712 : TOMMY_API void tommy_tree_foreach(tommy_tree* tree, tommy_foreach_func* func)
     262             : {
     263        1712 :         tommy_tree_foreach_node(tree->root, func);
     264        1712 : }
     265             : 
     266    28259877 : static void tommy_tree_foreach_arg_node(tommy_tree_node* root, tommy_foreach_arg_func* func, void* arg)
     267             : {
     268             :         tommy_tree_node* next;
     269             : 
     270    28259877 :         if (!root)
     271    14133155 :                 return;
     272             : 
     273    14126722 :         tommy_tree_foreach_arg_node(root->prev, func, arg);
     274             : 
     275             :         /* make a copy in case func is free() */
     276    14126722 :         next = root->next;
     277             : 
     278    14126722 :         func(arg, root->data);
     279             : 
     280    14126722 :         tommy_tree_foreach_arg_node(next, func, arg);
     281             : }
     282             : 
     283        6433 : TOMMY_API void tommy_tree_foreach_arg(tommy_tree* tree, tommy_foreach_arg_func* func, void* arg)
     284             : {
     285        6433 :         tommy_tree_foreach_arg_node(tree->root, func, arg);
     286        6433 : }
     287             : 
     288           1 : TOMMY_API tommy_size_t tommy_tree_memory_usage(tommy_tree* tree)
     289             : {
     290           1 :         return tommy_tree_count(tree) * sizeof(tommy_tree_node);
     291             : }
     292             : 

Generated by: LCOV version 1.0