LCOV - code coverage report
Current view: top level - lib/util - rbtree.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 201 234 85.9 %
Date: 2024-05-31 13:13:24 Functions: 10 13 76.9 %

          Line data    Source code
       1             : /*
       2             :   Red Black Trees
       3             :   (C) 1999  Andrea Arcangeli <andrea@suse.de>
       4             :   (C) 2002  David Woodhouse <dwmw2@infradead.org>
       5             :   
       6             :   This program is free software; you can redistribute it and/or modify
       7             :   it under the terms of the GNU General Public License as published by
       8             :   the Free Software Foundation; either version 2 of the License, or
       9             :   (at your option) any later version.
      10             : 
      11             :   This program is distributed in the hope that it will be useful,
      12             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :   GNU General Public License for more details.
      15             : 
      16             :   You should have received a copy of the GNU General Public License
      17             :   along with this program; if not, write to the Free Software
      18             :   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      19             : 
      20             :   linux/lib/rbtree.c
      21             : */
      22             : 
      23             : #include "replace.h"
      24             : #include "rbtree.h"
      25             : #include "fault.h"
      26             : 
      27             : #define RB_RED          0
      28             : #define RB_BLACK        1
      29             : 
      30             : #define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
      31             : #define rb_color(r)   ((r)->rb_parent_color & 1)
      32             : #define rb_is_red(r)   (!rb_color(r))
      33             : #define rb_is_black(r) rb_color(r)
      34             : #define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
      35             : #define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)
      36             : 
      37     5308766 : static void rb_set_parent(struct rb_node *rb, struct rb_node *p)
      38             : {
      39     5308766 :         rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
      40     5253398 : }
      41       35550 : static void rb_set_color(struct rb_node *rb, int color)
      42             : {
      43       35550 :         rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
      44       35241 : }
      45             : 
      46             : #define RB_EMPTY_ROOT(root)     ((root)->rb_node == NULL)
      47             : #define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
      48             : #define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
      49             : 
      50     1225393 : static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
      51             : {
      52     1225393 :         struct rb_node *right = node->rb_right;
      53     1225393 :         struct rb_node *parent = rb_parent(node);
      54             : 
      55     1225393 :         if ((node->rb_right = right->rb_left))
      56      291950 :                 rb_set_parent(right->rb_left, node);
      57     1225393 :         right->rb_left = node;
      58             : 
      59     1225393 :         rb_set_parent(right, parent);
      60             : 
      61     1225393 :         if (parent)
      62             :         {
      63     1070128 :                 if (node == parent->rb_left)
      64      677269 :                         parent->rb_left = right;
      65             :                 else
      66      392859 :                         parent->rb_right = right;
      67             :         }
      68             :         else
      69      155265 :                 root->rb_node = right;
      70     1225393 :         rb_set_parent(node, right);
      71     1225393 : }
      72             : 
      73      992530 : static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
      74             : {
      75      992530 :         struct rb_node *left = node->rb_left;
      76      992530 :         struct rb_node *parent = rb_parent(node);
      77             : 
      78      992530 :         if ((node->rb_left = left->rb_right))
      79      223130 :                 rb_set_parent(left->rb_right, node);
      80      992530 :         left->rb_right = node;
      81             : 
      82      992530 :         rb_set_parent(left, parent);
      83             : 
      84      992530 :         if (parent)
      85             :         {
      86      917400 :                 if (node == parent->rb_right)
      87      634995 :                         parent->rb_right = left;
      88             :                 else
      89      282405 :                         parent->rb_left = left;
      90             :         }
      91             :         else
      92       75130 :                 root->rb_node = left;
      93      992530 :         rb_set_parent(node, left);
      94      992530 : }
      95             : 
      96     4333261 : void rb_insert_color(struct rb_node *node, struct rb_root *root)
      97             : {
      98       48768 :         struct rb_node *parent, *gparent;
      99             : 
     100     7281110 :         while ((parent = rb_parent(node)) && rb_is_red(parent))
     101             :         {
     102     2947849 :                 gparent = rb_parent(parent);
     103             : 
     104     2947849 :                 if (parent == gparent->rb_left)
     105             :                 {
     106             :                         {
     107     1313263 :                                 register struct rb_node *uncle = gparent->rb_right;
     108     1313263 :                                 if (uncle && rb_is_red(uncle))
     109             :                                 {
     110      617813 :                                         rb_set_black(uncle);
     111      617813 :                                         rb_set_black(parent);
     112      617813 :                                         rb_set_red(gparent);
     113      617813 :                                         node = gparent;
     114      617813 :                                         continue;
     115             :                                 }
     116             :                         }
     117             : 
     118      695450 :                         if (parent->rb_right == node)
     119             :                         {
     120        4083 :                                 register struct rb_node *tmp;
     121      416896 :                                 __rb_rotate_left(parent, root);
     122      416896 :                                 tmp = parent;
     123      416896 :                                 parent = node;
     124      416896 :                                 node = tmp;
     125             :                         }
     126             : 
     127      695450 :                         rb_set_black(parent);
     128      695450 :                         rb_set_red(gparent);
     129      695450 :                         __rb_rotate_right(gparent, root);
     130             :                 } else {
     131             :                         {
     132     1634586 :                                 register struct rb_node *uncle = gparent->rb_left;
     133     1634586 :                                 if (uncle && rb_is_red(uncle))
     134             :                                 {
     135      865238 :                                         rb_set_black(uncle);
     136      865238 :                                         rb_set_black(parent);
     137      865238 :                                         rb_set_red(gparent);
     138      865238 :                                         node = gparent;
     139      865238 :                                         continue;
     140             :                                 }
     141             :                         }
     142             : 
     143      769348 :                         if (parent->rb_left == node)
     144             :                         {
     145        4848 :                                 register struct rb_node *tmp;
     146      261790 :                                 __rb_rotate_right(parent, root);
     147      261790 :                                 tmp = parent;
     148      261790 :                                 parent = node;
     149      261790 :                                 node = tmp;
     150             :                         }
     151             : 
     152      769348 :                         rb_set_black(parent);
     153      769348 :                         rb_set_red(gparent);
     154      769348 :                         __rb_rotate_left(gparent, root);
     155             :                 }
     156             :         }
     157             : 
     158     4333261 :         rb_set_black(root->rb_node);
     159     4333261 : }
     160             : 
     161      235556 : static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
     162             :                              struct rb_root *root)
     163             : {
     164        2573 :         struct rb_node *other;
     165             : 
     166      319717 :         while ((!node || rb_is_black(node)) && node != root->rb_node)
     167             :         {
     168      119711 :                 if (parent->rb_left == node)
     169             :                 {
     170       64957 :                         other = parent->rb_right;
     171       64957 :                         if (other == NULL) {
     172             :                                 /* we should never get here */
     173           0 :                                 smb_panic("corrupted rb tree");
     174             :                                 /* satisfy static checkers */
     175             :                                 return;
     176             :                         }
     177       64957 :                         if (rb_is_red(other))
     178             :                         {
     179       11774 :                                 rb_set_black(other);
     180       11774 :                                 rb_set_red(parent);
     181       11774 :                                 __rb_rotate_left(parent, root);
     182       11774 :                                 other = parent->rb_right;
     183             :                         }
     184       64957 :                         if ((!other->rb_left || rb_is_black(other->rb_left)) &&
     185       51037 :                             (!other->rb_right || rb_is_black(other->rb_right)))
     186             :                         {
     187       45928 :                                 rb_set_red(other);
     188       45928 :                                 node = parent;
     189       45928 :                                 parent = rb_parent(node);
     190             :                         }
     191             :                         else
     192             :                         {
     193       19029 :                                 if (!other->rb_right || rb_is_black(other->rb_right))
     194             :                                 {
     195          66 :                                         struct rb_node *o_left;
     196        9328 :                                         if ((o_left = other->rb_left))
     197        9328 :                                                 rb_set_black(o_left);
     198        9328 :                                         rb_set_red(other);
     199        9328 :                                         __rb_rotate_right(other, root);
     200        9328 :                                         other = parent->rb_right;
     201             :                                 }
     202       19029 :                                 rb_set_color(other, rb_color(parent));
     203       19029 :                                 rb_set_black(parent);
     204       19029 :                                 if (other->rb_right)
     205       19029 :                                         rb_set_black(other->rb_right);
     206       19029 :                                 __rb_rotate_left(parent, root);
     207       19029 :                                 node = root->rb_node;
     208       19029 :                                 break;
     209             :                         }
     210             :                 }
     211             :                 else
     212             :                 {
     213       54754 :                         other = parent->rb_left;
     214       54754 :                         if (rb_is_red(other))
     215             :                         {
     216        9441 :                                 rb_set_black(other);
     217        9441 :                                 rb_set_red(parent);
     218        9441 :                                 __rb_rotate_right(parent, root);
     219        9441 :                                 other = parent->rb_left;
     220             :                         }
     221       54754 :                         if ((!other->rb_left || rb_is_black(other->rb_left)) &&
     222       46579 :                             (!other->rb_right || rb_is_black(other->rb_right)))
     223             :                         {
     224       38233 :                                 rb_set_red(other);
     225       38233 :                                 node = parent;
     226       38233 :                                 parent = rb_parent(node);
     227             :                         }
     228             :                         else
     229             :                         {
     230       16521 :                                 if (!other->rb_left || rb_is_black(other->rb_left))
     231             :                                 {
     232          63 :                                         register struct rb_node *o_right;
     233        8346 :                                         if ((o_right = other->rb_right))
     234        8346 :                                                 rb_set_black(o_right);
     235        8346 :                                         rb_set_red(other);
     236        8346 :                                         __rb_rotate_left(other, root);
     237        8346 :                                         other = parent->rb_left;
     238             :                                 }
     239       16521 :                                 rb_set_color(other, rb_color(parent));
     240       16521 :                                 rb_set_black(parent);
     241       16521 :                                 if (other->rb_left)
     242       16521 :                                         rb_set_black(other->rb_left);
     243       16521 :                                 __rb_rotate_right(parent, root);
     244       16521 :                                 node = root->rb_node;
     245       16521 :                                 break;
     246             :                         }
     247             :                 }
     248             :         }
     249      235556 :         if (node)
     250      159038 :                 rb_set_black(node);
     251             : }
     252             : 
     253      783319 : void rb_erase(struct rb_node *node, struct rb_root *root)
     254             : {
     255        4330 :         struct rb_node *child, *parent;
     256        4330 :         int color;
     257             : 
     258      783319 :         if (!node->rb_left)
     259      559605 :                 child = node->rb_right;
     260      223714 :         else if (!node->rb_right)
     261       26189 :                 child = node->rb_left;
     262             :         else
     263             :         {
     264      196849 :                 struct rb_node *old = node, *left;
     265             : 
     266      196849 :                 node = node->rb_right;
     267      340963 :                 while ((left = node->rb_left) != NULL)
     268      143211 :                         node = left;
     269      197333 :                 child = node->rb_right;
     270      197333 :                 parent = rb_parent(node);
     271      197333 :                 color = rb_color(node);
     272             : 
     273      197333 :                 if (child)
     274       21015 :                         rb_set_parent(child, parent);
     275      197333 :                 if (parent == old) {
     276      119054 :                         parent->rb_right = child;
     277      119054 :                         parent = node;
     278             :                 } else
     279       78279 :                         parent->rb_left = child;
     280             : 
     281      197333 :                 node->rb_parent_color = old->rb_parent_color;
     282      197333 :                 node->rb_right = old->rb_right;
     283      197333 :                 node->rb_left = old->rb_left;
     284             : 
     285      197333 :                 if (rb_parent(old))
     286             :                 {
     287      188703 :                         if (rb_parent(old)->rb_left == old)
     288       63619 :                                 rb_parent(old)->rb_left = node;
     289             :                         else
     290      125084 :                                 rb_parent(old)->rb_right = node;
     291             :                 } else
     292        8630 :                         root->rb_node = node;
     293             : 
     294      197333 :                 rb_set_parent(old->rb_left, node);
     295      197333 :                 if (old->rb_right)
     296       86276 :                         rb_set_parent(old->rb_right, node);
     297      197333 :                 goto color;
     298             :         }
     299             : 
     300      585986 :         parent = rb_parent(node);
     301      585986 :         color = rb_color(node);
     302             : 
     303      585986 :         if (child)
     304       53216 :                 rb_set_parent(child, parent);
     305      585986 :         if (parent)
     306             :         {
     307      508650 :                 if (parent->rb_left == node)
     308       75653 :                         parent->rb_left = child;
     309             :                 else
     310      432997 :                         parent->rb_right = child;
     311             :         }
     312             :         else
     313       77336 :                 root->rb_node = child;
     314             : 
     315      783319 :  color:
     316      783319 :         if (color == RB_BLACK)
     317      235556 :                 __rb_erase_color(child, parent, root);
     318      783319 : }
     319             : 
     320             : /*
     321             :  * This function returns the first node (in sort order) of the tree.
     322             :  */
     323           0 : struct rb_node *rb_first(struct rb_root *root)
     324             : {
     325           0 :         struct rb_node  *n;
     326             : 
     327           0 :         n = root->rb_node;
     328           0 :         if (!n)
     329           0 :                 return NULL;
     330           0 :         while (n->rb_left)
     331           0 :                 n = n->rb_left;
     332           0 :         return n;
     333             : }
     334             : 
     335           0 : struct rb_node *rb_last(struct rb_root *root)
     336             : {
     337           0 :         struct rb_node  *n;
     338             : 
     339           0 :         n = root->rb_node;
     340           0 :         if (!n)
     341           0 :                 return NULL;
     342           0 :         while (n->rb_right)
     343           0 :                 n = n->rb_right;
     344           0 :         return n;
     345             : }
     346             : 
     347       42897 : struct rb_node *rb_next(struct rb_node *node)
     348             : {
     349           1 :         struct rb_node *parent;
     350             : 
     351       42897 :         if (rb_parent(node) == node)
     352           0 :                 return NULL;
     353             : 
     354             :         /* If we have a right-hand child, go down and then left as far
     355             :            as we can. */
     356       42897 :         if (node->rb_right) {
     357       20508 :                 node = node->rb_right; 
     358       20522 :                 while (node->rb_left)
     359          14 :                         node=node->rb_left;
     360       20508 :                 return node;
     361             :         }
     362             : 
     363             :         /* No right-hand children.  Everything down and left is
     364             :            smaller than us, so any 'next' node must be in the general
     365             :            direction of our parent. Go up the tree; any time the
     366             :            ancestor is a right-hand child of its parent, keep going
     367             :            up. First time it's a left-hand child of its parent, said
     368             :            parent is our 'next' node. */
     369       45197 :         while ((parent = rb_parent(node)) && node == parent->rb_right)
     370       22808 :                 node = parent;
     371             : 
     372       22388 :         return parent;
     373             : }
     374             : 
     375       22340 : struct rb_node *rb_prev(struct rb_node *node)
     376             : {
     377           1 :         struct rb_node *parent;
     378             : 
     379       22340 :         if (rb_parent(node) == node)
     380           0 :                 return NULL;
     381             : 
     382             :         /* If we have a left-hand child, go down and then right as far
     383             :            as we can. */
     384       22340 :         if (node->rb_left) {
     385         152 :                 node = node->rb_left; 
     386         152 :                 while (node->rb_right)
     387           0 :                         node=node->rb_right;
     388         152 :                 return node;
     389             :         }
     390             : 
     391             :         /* No left-hand children. Go up till we find an ancestor which
     392             :            is a right-hand child of its parent */
     393       22239 :         while ((parent = rb_parent(node)) && node == parent->rb_left)
     394          51 :                 node = parent;
     395             : 
     396       22187 :         return parent;
     397             : }
     398             : 
     399           0 : void rb_replace_node(struct rb_node *victim, struct rb_node *new_node,
     400             :                      struct rb_root *root)
     401             : {
     402           0 :         struct rb_node *parent = rb_parent(victim);
     403             : 
     404             :         /* Set the surrounding nodes to point to the replacement */
     405           0 :         if (parent) {
     406           0 :                 if (victim == parent->rb_left)
     407           0 :                         parent->rb_left = new_node;
     408             :                 else
     409           0 :                         parent->rb_right = new_node;
     410             :         } else {
     411           0 :                 root->rb_node = new_node;
     412             :         }
     413           0 :         if (victim->rb_left)
     414           0 :                 rb_set_parent(victim->rb_left, new_node);
     415           0 :         if (victim->rb_right)
     416           0 :                 rb_set_parent(victim->rb_right, new_node);
     417             : 
     418             :         /* Copy the pointers/colour from the victim to the replacement */
     419           0 :         *new_node = *victim;
     420           0 : }
     421             : 
     422     4333261 : void rb_link_node(struct rb_node * node, struct rb_node * parent,
     423             :                   struct rb_node ** rb_link)
     424             : {
     425     4333261 :         node->rb_parent_color = (unsigned long )parent;
     426     4333261 :         node->rb_left = node->rb_right = NULL;
     427             : 
     428     4333261 :         *rb_link = node;
     429     4333261 : }

Generated by: LCOV version 1.14