Coverage Report

Created: 2026-06-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/lib/util/rbtree.c
Line
Count
Source
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
0
#define RB_BLACK  1
29
30
0
#define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
31
0
#define rb_color(r)   ((r)->rb_parent_color & 1)
32
0
#define rb_is_red(r)   (!rb_color(r))
33
0
#define rb_is_black(r) rb_color(r)
34
0
#define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
35
0
#define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)
36
37
static void rb_set_parent(struct rb_node *rb, struct rb_node *p)
38
0
{
39
0
  rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
40
0
}
41
static void rb_set_color(struct rb_node *rb, int color)
42
0
{
43
0
  rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
44
0
}
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
static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
51
0
{
52
0
  struct rb_node *right = node->rb_right;
53
0
  struct rb_node *parent = rb_parent(node);
54
55
0
  if ((node->rb_right = right->rb_left))
56
0
    rb_set_parent(right->rb_left, node);
57
0
  right->rb_left = node;
58
59
0
  rb_set_parent(right, parent);
60
61
0
  if (parent)
62
0
  {
63
0
    if (node == parent->rb_left)
64
0
      parent->rb_left = right;
65
0
    else
66
0
      parent->rb_right = right;
67
0
  }
68
0
  else
69
0
    root->rb_node = right;
70
0
  rb_set_parent(node, right);
71
0
}
72
73
static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
74
0
{
75
0
  struct rb_node *left = node->rb_left;
76
0
  struct rb_node *parent = rb_parent(node);
77
78
0
  if ((node->rb_left = left->rb_right))
79
0
    rb_set_parent(left->rb_right, node);
80
0
  left->rb_right = node;
81
82
0
  rb_set_parent(left, parent);
83
84
0
  if (parent)
85
0
  {
86
0
    if (node == parent->rb_right)
87
0
      parent->rb_right = left;
88
0
    else
89
0
      parent->rb_left = left;
90
0
  }
91
0
  else
92
0
    root->rb_node = left;
93
0
  rb_set_parent(node, left);
94
0
}
95
96
void rb_insert_color(struct rb_node *node, struct rb_root *root)
97
0
{
98
0
  struct rb_node *parent, *gparent;
99
100
0
  while ((parent = rb_parent(node)) && rb_is_red(parent))
101
0
  {
102
0
    gparent = rb_parent(parent);
103
104
0
    if (parent == gparent->rb_left)
105
0
    {
106
0
      {
107
0
        register struct rb_node *uncle = gparent->rb_right;
108
0
        if (uncle && rb_is_red(uncle))
109
0
        {
110
0
          rb_set_black(uncle);
111
0
          rb_set_black(parent);
112
0
          rb_set_red(gparent);
113
0
          node = gparent;
114
0
          continue;
115
0
        }
116
0
      }
117
118
0
      if (parent->rb_right == node)
119
0
      {
120
0
        register struct rb_node *tmp;
121
0
        __rb_rotate_left(parent, root);
122
0
        tmp = parent;
123
0
        parent = node;
124
0
        node = tmp;
125
0
      }
126
127
0
      rb_set_black(parent);
128
0
      rb_set_red(gparent);
129
0
      __rb_rotate_right(gparent, root);
130
0
    } else {
131
0
      {
132
0
        register struct rb_node *uncle = gparent->rb_left;
133
0
        if (uncle && rb_is_red(uncle))
134
0
        {
135
0
          rb_set_black(uncle);
136
0
          rb_set_black(parent);
137
0
          rb_set_red(gparent);
138
0
          node = gparent;
139
0
          continue;
140
0
        }
141
0
      }
142
143
0
      if (parent->rb_left == node)
144
0
      {
145
0
        register struct rb_node *tmp;
146
0
        __rb_rotate_right(parent, root);
147
0
        tmp = parent;
148
0
        parent = node;
149
0
        node = tmp;
150
0
      }
151
152
0
      rb_set_black(parent);
153
0
      rb_set_red(gparent);
154
0
      __rb_rotate_left(gparent, root);
155
0
    }
156
0
  }
157
158
0
  rb_set_black(root->rb_node);
159
0
}
160
161
static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
162
           struct rb_root *root)
163
0
{
164
0
  struct rb_node *other;
165
166
0
  while ((!node || rb_is_black(node)) && node != root->rb_node)
167
0
  {
168
0
    if (parent->rb_left == node)
169
0
    {
170
0
      other = parent->rb_right;
171
0
      if (other == NULL) {
172
        /* we should never get here */
173
0
        smb_panic("corrupted rb tree");
174
        /* satisfy static checkers */
175
0
        return;
176
0
      }
177
0
      if (rb_is_red(other))
178
0
      {
179
0
        rb_set_black(other);
180
0
        rb_set_red(parent);
181
0
        __rb_rotate_left(parent, root);
182
0
        other = parent->rb_right;
183
0
      }
184
0
      if ((!other->rb_left || rb_is_black(other->rb_left)) &&
185
0
          (!other->rb_right || rb_is_black(other->rb_right)))
186
0
      {
187
0
        rb_set_red(other);
188
0
        node = parent;
189
0
        parent = rb_parent(node);
190
0
      }
191
0
      else
192
0
      {
193
0
        if (!other->rb_right || rb_is_black(other->rb_right))
194
0
        {
195
0
          struct rb_node *o_left;
196
0
          if ((o_left = other->rb_left))
197
0
            rb_set_black(o_left);
198
0
          rb_set_red(other);
199
0
          __rb_rotate_right(other, root);
200
0
          other = parent->rb_right;
201
0
        }
202
0
        rb_set_color(other, rb_color(parent));
203
0
        rb_set_black(parent);
204
0
        if (other->rb_right)
205
0
          rb_set_black(other->rb_right);
206
0
        __rb_rotate_left(parent, root);
207
0
        node = root->rb_node;
208
0
        break;
209
0
      }
210
0
    }
211
0
    else
212
0
    {
213
0
      other = parent->rb_left;
214
0
      if (rb_is_red(other))
215
0
      {
216
0
        rb_set_black(other);
217
0
        rb_set_red(parent);
218
0
        __rb_rotate_right(parent, root);
219
0
        other = parent->rb_left;
220
0
      }
221
0
      if ((!other->rb_left || rb_is_black(other->rb_left)) &&
222
0
          (!other->rb_right || rb_is_black(other->rb_right)))
223
0
      {
224
0
        rb_set_red(other);
225
0
        node = parent;
226
0
        parent = rb_parent(node);
227
0
      }
228
0
      else
229
0
      {
230
0
        if (!other->rb_left || rb_is_black(other->rb_left))
231
0
        {
232
0
          register struct rb_node *o_right;
233
0
          if ((o_right = other->rb_right))
234
0
            rb_set_black(o_right);
235
0
          rb_set_red(other);
236
0
          __rb_rotate_left(other, root);
237
0
          other = parent->rb_left;
238
0
        }
239
0
        rb_set_color(other, rb_color(parent));
240
0
        rb_set_black(parent);
241
0
        if (other->rb_left)
242
0
          rb_set_black(other->rb_left);
243
0
        __rb_rotate_right(parent, root);
244
0
        node = root->rb_node;
245
0
        break;
246
0
      }
247
0
    }
248
0
  }
249
0
  if (node)
250
0
    rb_set_black(node);
251
0
}
252
253
void rb_erase(struct rb_node *node, struct rb_root *root)
254
0
{
255
0
  struct rb_node *child, *parent;
256
0
  int color;
257
258
0
  if (!node->rb_left)
259
0
    child = node->rb_right;
260
0
  else if (!node->rb_right)
261
0
    child = node->rb_left;
262
0
  else
263
0
  {
264
0
    struct rb_node *old = node, *left;
265
266
0
    node = node->rb_right;
267
0
    while ((left = node->rb_left) != NULL)
268
0
      node = left;
269
0
    child = node->rb_right;
270
0
    parent = rb_parent(node);
271
0
    color = rb_color(node);
272
273
0
    if (child)
274
0
      rb_set_parent(child, parent);
275
0
    if (parent == old) {
276
0
      parent->rb_right = child;
277
0
      parent = node;
278
0
    } else
279
0
      parent->rb_left = child;
280
281
0
    node->rb_parent_color = old->rb_parent_color;
282
0
    node->rb_right = old->rb_right;
283
0
    node->rb_left = old->rb_left;
284
285
0
    if (rb_parent(old))
286
0
    {
287
0
      if (rb_parent(old)->rb_left == old)
288
0
        rb_parent(old)->rb_left = node;
289
0
      else
290
0
        rb_parent(old)->rb_right = node;
291
0
    } else
292
0
      root->rb_node = node;
293
294
0
    rb_set_parent(old->rb_left, node);
295
0
    if (old->rb_right)
296
0
      rb_set_parent(old->rb_right, node);
297
0
    goto color;
298
0
  }
299
300
0
  parent = rb_parent(node);
301
0
  color = rb_color(node);
302
303
0
  if (child)
304
0
    rb_set_parent(child, parent);
305
0
  if (parent)
306
0
  {
307
0
    if (parent->rb_left == node)
308
0
      parent->rb_left = child;
309
0
    else
310
0
      parent->rb_right = child;
311
0
  }
312
0
  else
313
0
    root->rb_node = child;
314
315
0
 color:
316
0
  if (color == RB_BLACK)
317
0
    __rb_erase_color(child, parent, root);
318
0
}
319
320
/*
321
 * This function returns the first node (in sort order) of the tree.
322
 */
323
struct rb_node *rb_first(struct rb_root *root)
324
0
{
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
0
}
334
335
struct rb_node *rb_last(struct rb_root *root)
336
0
{
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
0
}
346
347
struct rb_node *rb_next(struct rb_node *node)
348
0
{
349
0
  struct rb_node *parent;
350
351
0
  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
0
  if (node->rb_right) {
357
0
    node = node->rb_right; 
358
0
    while (node->rb_left)
359
0
      node=node->rb_left;
360
0
    return node;
361
0
  }
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
0
  while ((parent = rb_parent(node)) && node == parent->rb_right)
370
0
    node = parent;
371
372
0
  return parent;
373
0
}
374
375
struct rb_node *rb_prev(struct rb_node *node)
376
0
{
377
0
  struct rb_node *parent;
378
379
0
  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
0
  if (node->rb_left) {
385
0
    node = node->rb_left; 
386
0
    while (node->rb_right)
387
0
      node=node->rb_right;
388
0
    return node;
389
0
  }
390
391
  /* No left-hand children. Go up till we find an ancestor which
392
     is a right-hand child of its parent */
393
0
  while ((parent = rb_parent(node)) && node == parent->rb_left)
394
0
    node = parent;
395
396
0
  return parent;
397
0
}
398
399
void rb_replace_node(struct rb_node *victim, struct rb_node *new_node,
400
         struct rb_root *root)
401
0
{
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
0
    else
409
0
      parent->rb_right = new_node;
410
0
  } else {
411
0
    root->rb_node = new_node;
412
0
  }
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
void rb_link_node(struct rb_node * node, struct rb_node * parent,
423
      struct rb_node ** rb_link)
424
0
{
425
0
  node->rb_parent_color = (unsigned long )parent;
426
0
  node->rb_left = node->rb_right = NULL;
427
428
0
  *rb_link = node;
429
0
}