~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/tree.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 18:48:58 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717184858-2mbouxl8xi41gcge
Removed DBUG from CSV and Blackhole storage engines

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Code for handling red-black (balanced) binary trees.
 
18
  key in tree is allocated accrding to following:
 
19
 
 
20
  1) If size < 0 then tree will not allocate keys and only a pointer to
 
21
     each key is saved in tree.
 
22
     compare and search functions uses and returns key-pointer
 
23
 
 
24
  2) If size == 0 then there are two options:
 
25
       - key_size != 0 to tree_insert: The key will be stored in the tree.
 
26
       - key_size == 0 to tree_insert:  A pointer to the key is stored.
 
27
     compare and search functions uses and returns key-pointer.
 
28
 
 
29
  3) if key_size is given to init_tree then each node will continue the
 
30
     key and calls to insert_key may increase length of key.
 
31
     if key_size > sizeof(pointer) and key_size is a multiple of 8 (double
 
32
     allign) then key will be put on a 8 alligned adress. Else
 
33
     the key will be on adress (element+1). This is transparent for user
 
34
     compare and search functions uses a pointer to given key-argument.
 
35
 
 
36
  - If you use a free function for tree-elements and you are freeing
 
37
    the element itself, you should use key_size = 0 to init_tree and
 
38
    tree_search
 
39
 
 
40
  The actual key in TREE_ELEMENT is saved as a pointer or after the
 
41
  TREE_ELEMENT struct.
 
42
  If one uses only pointers in tree one can use tree_set_pointer() to
 
43
  change address of data.
 
44
 
 
45
  Implemented by monty.
 
46
*/
 
47
 
 
48
/*
 
49
  NOTE:
 
50
  tree->compare function should be ALWAYS called as
 
51
    (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element), key)
 
52
  and not other way around, as
 
53
    (*tree->compare)(custom_arg, key, ELEMENT_KEY(tree,element))
 
54
*/
 
55
 
 
56
#include "mysys_priv.h"
 
57
#include <m_string.h>
 
58
#include <my_tree.h>
 
59
#include "my_base.h"
 
60
 
 
61
#define BLACK           1
 
62
#define RED             0
 
63
#define DEFAULT_ALLOC_SIZE 8192
 
64
#define DEFAULT_ALIGN_SIZE 8192
 
65
 
 
66
static void delete_tree_element(TREE *,TREE_ELEMENT *);
 
67
static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *,
 
68
                                     tree_walk_action,void *);
 
69
static int tree_walk_right_root_left(TREE *,TREE_ELEMENT *,
 
70
                                     tree_walk_action,void *);
 
71
static void left_rotate(TREE_ELEMENT **parent,TREE_ELEMENT *leaf);
 
72
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf);
 
73
static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
 
74
                      TREE_ELEMENT *leaf);
 
75
static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
 
76
 
 
77
 
 
78
        /* The actuall code for handling binary trees */
 
79
 
 
80
#ifndef DBUG_OFF
 
81
static int test_rb_tree(TREE_ELEMENT *element);
 
82
#endif
 
83
 
 
84
void init_tree(TREE *tree, ulong default_alloc_size, ulong memory_limit,
 
85
               int size, qsort_cmp2 compare, bool with_delete,
 
86
               tree_element_free free_element, void *custom_arg)
 
87
{
 
88
  DBUG_ENTER("init_tree");
 
89
  DBUG_PRINT("enter",("tree: 0x%lx  size: %d", (long) tree, size));
 
90
 
 
91
  if (default_alloc_size < DEFAULT_ALLOC_SIZE)
 
92
    default_alloc_size= DEFAULT_ALLOC_SIZE;
 
93
  default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
 
94
  bzero((uchar*) &tree->null_element,sizeof(tree->null_element));
 
95
  tree->root= &tree->null_element;
 
96
  tree->compare=compare;
 
97
  tree->size_of_element=size > 0 ? (uint) size : 0;
 
98
  tree->memory_limit=memory_limit;
 
99
  tree->free=free_element;
 
100
  tree->allocated=0;
 
101
  tree->elements_in_tree=0;
 
102
  tree->custom_arg = custom_arg;
 
103
  tree->null_element.colour=BLACK;
 
104
  tree->null_element.left=tree->null_element.right=0;
 
105
  tree->flag= 0;
 
106
  if (!free_element && size >= 0 &&
 
107
      ((uint) size <= sizeof(void*) || ((uint) size & (sizeof(void*)-1))))
 
108
  {
 
109
    /*
 
110
      We know that the data doesn't have to be aligned (like if the key
 
111
      contains a double), so we can store the data combined with the
 
112
      TREE_ELEMENT.
 
113
    */
 
114
    tree->offset_to_key=sizeof(TREE_ELEMENT); /* Put key after element */
 
115
    /* Fix allocation size so that we don't lose any memory */
 
116
    default_alloc_size/=(sizeof(TREE_ELEMENT)+size);
 
117
    if (!default_alloc_size)
 
118
      default_alloc_size=1;
 
119
    default_alloc_size*=(sizeof(TREE_ELEMENT)+size);
 
120
  }
 
121
  else
 
122
  {
 
123
    tree->offset_to_key=0;              /* use key through pointer */
 
124
    tree->size_of_element+=sizeof(void*);
 
125
  }
 
126
  if (!(tree->with_delete=with_delete))
 
127
  {
 
128
    init_alloc_root(&tree->mem_root, (uint) default_alloc_size, 0);
 
129
    tree->mem_root.min_malloc=(sizeof(TREE_ELEMENT)+tree->size_of_element);
 
130
  }
 
131
  DBUG_VOID_RETURN;
 
132
}
 
133
 
 
134
static void free_tree(TREE *tree, myf free_flags)
 
135
{
 
136
  DBUG_ENTER("free_tree");
 
137
  DBUG_PRINT("enter",("tree: 0x%lx", (long) tree));
 
138
 
 
139
  if (tree->root)                               /* If initialized */
 
140
  {
 
141
    if (tree->with_delete)
 
142
      delete_tree_element(tree,tree->root);
 
143
    else
 
144
    {
 
145
      if (tree->free)
 
146
      {
 
147
        if (tree->memory_limit)
 
148
          (*tree->free)(NULL, free_init, tree->custom_arg);
 
149
        delete_tree_element(tree,tree->root);
 
150
        if (tree->memory_limit)
 
151
          (*tree->free)(NULL, free_end, tree->custom_arg);
 
152
      }
 
153
      free_root(&tree->mem_root, free_flags);
 
154
    }
 
155
  }
 
156
  tree->root= &tree->null_element;
 
157
  tree->elements_in_tree=0;
 
158
  tree->allocated=0;
 
159
 
 
160
  DBUG_VOID_RETURN;
 
161
}
 
162
 
 
163
void delete_tree(TREE* tree)
 
164
{
 
165
  free_tree(tree, MYF(0)); /* my_free() mem_root if applicable */
 
166
}
 
167
 
 
168
void reset_tree(TREE* tree)
 
169
{
 
170
  /* do not free mem_root, just mark blocks as free */
 
171
  free_tree(tree, MYF(MY_MARK_BLOCKS_FREE));
 
172
}
 
173
 
 
174
 
 
175
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
 
176
{
 
177
  if (element != &tree->null_element)
 
178
  {
 
179
    delete_tree_element(tree,element->left);
 
180
    if (tree->free)
 
181
      (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
 
182
    delete_tree_element(tree,element->right);
 
183
    if (tree->with_delete)
 
184
      my_free((char*) element,MYF(0));
 
185
  }
 
186
}
 
187
 
 
188
 
 
189
/*
 
190
  insert, search and delete of elements
 
191
 
 
192
  The following should be true:
 
193
    parent[0] = & parent[-1][0]->left ||
 
194
    parent[0] = & parent[-1][0]->right
 
195
*/
 
196
 
 
197
TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size, 
 
198
                          void* custom_arg)
 
199
{
 
200
  int cmp;
 
201
  TREE_ELEMENT *element,***parent;
 
202
 
 
203
  parent= tree->parents;
 
204
  *parent = &tree->root; element= tree->root;
 
205
  for (;;)
 
206
  {
 
207
    if (element == &tree->null_element ||
 
208
        (cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
 
209
                                key)) == 0)
 
210
      break;
 
211
    if (cmp < 0)
 
212
    {
 
213
      *++parent= &element->right; element= element->right;
 
214
    }
 
215
    else
 
216
    {
 
217
      *++parent = &element->left; element= element->left;
 
218
    }
 
219
  }
 
220
  if (element == &tree->null_element)
 
221
  {
 
222
    uint alloc_size=sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
 
223
    tree->allocated+=alloc_size;
 
224
 
 
225
    if (tree->memory_limit && tree->elements_in_tree
 
226
                           && tree->allocated > tree->memory_limit)
 
227
    {
 
228
      reset_tree(tree);
 
229
      return tree_insert(tree, key, key_size, custom_arg);
 
230
    }
 
231
 
 
232
    key_size+=tree->size_of_element;
 
233
    if (tree->with_delete)
 
234
      element=(TREE_ELEMENT *) my_malloc(alloc_size, MYF(MY_WME));
 
235
    else
 
236
      element=(TREE_ELEMENT *) alloc_root(&tree->mem_root,alloc_size);
 
237
    if (!element)
 
238
      return(NULL);
 
239
    **parent=element;
 
240
    element->left=element->right= &tree->null_element;
 
241
    if (!tree->offset_to_key)
 
242
    {
 
243
      if (key_size == sizeof(void*))             /* no length, save pointer */
 
244
        *((void**) (element+1))=key;
 
245
      else
 
246
      {
 
247
        *((void**) (element+1))= (void*) ((void **) (element+1)+1);
 
248
        memcpy((uchar*) *((void **) (element+1)),key,
 
249
               (size_t) (key_size-sizeof(void*)));
 
250
      }
 
251
    }
 
252
    else
 
253
      memcpy((uchar*) element+tree->offset_to_key,key,(size_t) key_size);
 
254
    element->count=1;                   /* May give warning in purify */
 
255
    tree->elements_in_tree++;
 
256
    rb_insert(tree,parent,element);     /* rebalance tree */
 
257
  }
 
258
  else
 
259
  {
 
260
    if (tree->flag & TREE_NO_DUPS)
 
261
      return(NULL);
 
262
    element->count++;
 
263
    /* Avoid a wrap over of the count. */
 
264
    if (! element->count)
 
265
      element->count--;
 
266
  }
 
267
  DBUG_EXECUTE("check_tree", test_rb_tree(tree->root););
 
268
  return element;
 
269
}
 
270
 
 
271
int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg)
 
272
{
 
273
  int cmp,remove_colour;
 
274
  TREE_ELEMENT *element,***parent, ***org_parent, *nod;
 
275
  if (!tree->with_delete)
 
276
    return 1;                                   /* not allowed */
 
277
 
 
278
  parent= tree->parents;
 
279
  *parent= &tree->root; element= tree->root;
 
280
  for (;;)
 
281
  {
 
282
    if (element == &tree->null_element)
 
283
      return 1;                         /* Was not in tree */
 
284
    if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
 
285
                                key)) == 0)
 
286
      break;
 
287
    if (cmp < 0)
 
288
    {
 
289
      *++parent= &element->right; element= element->right;
 
290
    }
 
291
    else
 
292
    {
 
293
      *++parent = &element->left; element= element->left;
 
294
    }
 
295
  }
 
296
  if (element->left == &tree->null_element)
 
297
  {
 
298
    (**parent)=element->right;
 
299
    remove_colour= element->colour;
 
300
  }
 
301
  else if (element->right == &tree->null_element)
 
302
  {
 
303
    (**parent)=element->left;
 
304
    remove_colour= element->colour;
 
305
  }
 
306
  else
 
307
  {
 
308
    org_parent= parent;
 
309
    *++parent= &element->right; nod= element->right;
 
310
    while (nod->left != &tree->null_element)
 
311
    {
 
312
      *++parent= &nod->left; nod= nod->left;
 
313
    }
 
314
    (**parent)=nod->right;              /* unlink nod from tree */
 
315
    remove_colour= nod->colour;
 
316
    org_parent[0][0]=nod;               /* put y in place of element */
 
317
    org_parent[1]= &nod->right;
 
318
    nod->left=element->left;
 
319
    nod->right=element->right;
 
320
    nod->colour=element->colour;
 
321
  }
 
322
  if (remove_colour == BLACK)
 
323
    rb_delete_fixup(tree,parent);
 
324
  if (tree->free)
 
325
    (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
 
326
  tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size;
 
327
  my_free((uchar*) element,MYF(0));
 
328
  tree->elements_in_tree--;
 
329
  return 0;
 
330
}
 
331
 
 
332
 
 
333
void *tree_search(TREE *tree, void *key, void *custom_arg)
 
334
{
 
335
  int cmp;
 
336
  TREE_ELEMENT *element=tree->root;
 
337
 
 
338
  for (;;)
 
339
  {
 
340
    if (element == &tree->null_element)
 
341
      return (void*) 0;
 
342
    if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
 
343
                                key)) == 0)
 
344
      return ELEMENT_KEY(tree,element);
 
345
    if (cmp < 0)
 
346
      element=element->right;
 
347
    else
 
348
      element=element->left;
 
349
  }
 
350
}
 
351
 
 
352
void *tree_search_key(TREE *tree, const void *key, 
 
353
                      TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
 
354
                      enum ha_rkey_function flag, void *custom_arg)
 
355
{
 
356
  int cmp;
 
357
  TREE_ELEMENT *element= tree->root;
 
358
  TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
 
359
  TREE_ELEMENT **last_equal_element= NULL;
 
360
 
 
361
/* 
 
362
  TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
 
363
*/
 
364
 
 
365
  *parents = &tree->null_element;
 
366
  while (element != &tree->null_element)
 
367
  {
 
368
    *++parents= element;
 
369
    if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element), 
 
370
                               key)) == 0)
 
371
    {
 
372
      switch (flag) {
 
373
      case HA_READ_KEY_EXACT:
 
374
      case HA_READ_KEY_OR_NEXT:
 
375
      case HA_READ_BEFORE_KEY:
 
376
        last_equal_element= parents;
 
377
        cmp= 1;
 
378
        break;
 
379
      case HA_READ_AFTER_KEY:
 
380
        cmp= -1;
 
381
        break;
 
382
      case HA_READ_PREFIX_LAST:
 
383
      case HA_READ_PREFIX_LAST_OR_PREV:
 
384
        last_equal_element= parents;
 
385
        cmp= -1;
 
386
        break;
 
387
      default:
 
388
        return NULL;
 
389
      }
 
390
    }
 
391
    if (cmp < 0) /* element < key */
 
392
    {
 
393
      last_right_step_parent= parents;
 
394
      element= element->right;
 
395
    }
 
396
    else
 
397
    {
 
398
      last_left_step_parent= parents;
 
399
      element= element->left;
 
400
    }
 
401
  }
 
402
  switch (flag) {
 
403
  case HA_READ_KEY_EXACT:
 
404
  case HA_READ_PREFIX_LAST:
 
405
    *last_pos= last_equal_element;
 
406
    break;
 
407
  case HA_READ_KEY_OR_NEXT:
 
408
    *last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
 
409
    break;
 
410
  case HA_READ_AFTER_KEY:
 
411
    *last_pos= last_left_step_parent;
 
412
    break;
 
413
  case HA_READ_PREFIX_LAST_OR_PREV:
 
414
    *last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
 
415
    break;
 
416
  case HA_READ_BEFORE_KEY:
 
417
    *last_pos= last_right_step_parent;
 
418
    break;
 
419
  default:
 
420
    return NULL;
 
421
  }
 
422
  return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
 
423
}
 
424
 
 
425
/* 
 
426
  Search first (the most left) or last (the most right) tree element 
 
427
*/
 
428
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents, 
 
429
                       TREE_ELEMENT ***last_pos, int child_offs)
 
430
{
 
431
  TREE_ELEMENT *element= tree->root;
 
432
  
 
433
  *parents= &tree->null_element;
 
434
  while (element != &tree->null_element)
 
435
  {
 
436
    *++parents= element;
 
437
    element= ELEMENT_CHILD(element, child_offs);
 
438
  }
 
439
  *last_pos= parents;
 
440
  return **last_pos != &tree->null_element ? 
 
441
    ELEMENT_KEY(tree, **last_pos) : NULL;
 
442
}
 
443
 
 
444
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs, 
 
445
                       int r_offs)
 
446
{
 
447
  TREE_ELEMENT *x= **last_pos;
 
448
  
 
449
  if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
 
450
  {
 
451
    x= ELEMENT_CHILD(x, r_offs);
 
452
    *++*last_pos= x;
 
453
    while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
 
454
    {
 
455
      x= ELEMENT_CHILD(x, l_offs);
 
456
      *++*last_pos= x;
 
457
    }
 
458
    return ELEMENT_KEY(tree, x);
 
459
  }
 
460
  else
 
461
  {
 
462
    TREE_ELEMENT *y= *--*last_pos;
 
463
    while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
 
464
    {
 
465
      x= y;
 
466
      y= *--*last_pos;
 
467
    }
 
468
    return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
 
469
  }
 
470
}
 
471
 
 
472
/*
 
473
  Expected that tree is fully balanced
 
474
  (each path from root to leaf has the same length)
 
475
*/
 
476
ha_rows tree_record_pos(TREE *tree, const void *key, 
 
477
                        enum ha_rkey_function flag, void *custom_arg)
 
478
{
 
479
  int cmp;
 
480
  TREE_ELEMENT *element= tree->root;
 
481
  double left= 1;
 
482
  double right= tree->elements_in_tree;
 
483
 
 
484
  while (element != &tree->null_element)
 
485
  {
 
486
    if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element), 
 
487
                               key)) == 0)
 
488
    {
 
489
      switch (flag) {
 
490
      case HA_READ_KEY_EXACT:
 
491
      case HA_READ_BEFORE_KEY:
 
492
        cmp= 1;
 
493
        break;
 
494
      case HA_READ_AFTER_KEY:
 
495
        cmp= -1;
 
496
        break;
 
497
      default:
 
498
        return HA_POS_ERROR;
 
499
      }
 
500
    }
 
501
    if (cmp < 0) /* element < key */
 
502
    {
 
503
      element= element->right;
 
504
      left= (left + right) / 2;
 
505
    }
 
506
    else
 
507
    {
 
508
      element= element->left;
 
509
      right= (left + right) / 2;
 
510
    }
 
511
  }
 
512
  switch (flag) {
 
513
  case HA_READ_KEY_EXACT:
 
514
  case HA_READ_BEFORE_KEY:
 
515
    return (ha_rows) right;
 
516
  case HA_READ_AFTER_KEY:
 
517
    return (ha_rows) left;
 
518
  default:
 
519
    return HA_POS_ERROR;
 
520
  }
 
521
}
 
522
 
 
523
int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
 
524
{
 
525
  switch (visit) {
 
526
  case left_root_right:
 
527
    return tree_walk_left_root_right(tree,tree->root,action,argument);
 
528
  case right_root_left:
 
529
    return tree_walk_right_root_left(tree,tree->root,action,argument);
 
530
  }
 
531
  return 0;                     /* Keep gcc happy */
 
532
}
 
533
 
 
534
static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
 
535
{
 
536
  int error;
 
537
  if (element->left)                            /* Not null_element */
 
538
  {
 
539
    if ((error=tree_walk_left_root_right(tree,element->left,action,
 
540
                                          argument)) == 0 &&
 
541
        (error=(*action)(ELEMENT_KEY(tree,element),
 
542
                          (element_count) element->count,
 
543
                          argument)) == 0)
 
544
      error=tree_walk_left_root_right(tree,element->right,action,argument);
 
545
    return error;
 
546
  }
 
547
  return 0;
 
548
}
 
549
 
 
550
static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
 
551
{
 
552
  int error;
 
553
  if (element->right)                           /* Not null_element */
 
554
  {
 
555
    if ((error=tree_walk_right_root_left(tree,element->right,action,
 
556
                                          argument)) == 0 &&
 
557
        (error=(*action)(ELEMENT_KEY(tree,element),
 
558
                          (element_count) element->count,
 
559
                          argument)) == 0)
 
560
     error=tree_walk_right_root_left(tree,element->left,action,argument);
 
561
    return error;
 
562
  }
 
563
  return 0;
 
564
}
 
565
 
 
566
 
 
567
        /* Functions to fix up the tree after insert and delete */
 
568
 
 
569
static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
 
570
{
 
571
  TREE_ELEMENT *y;
 
572
 
 
573
  y=leaf->right;
 
574
  leaf->right=y->left;
 
575
  parent[0]=y;
 
576
  y->left=leaf;
 
577
}
 
578
 
 
579
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
 
580
{
 
581
  TREE_ELEMENT *x;
 
582
 
 
583
  x=leaf->left;
 
584
  leaf->left=x->right;
 
585
  parent[0]=x;
 
586
  x->right=leaf;
 
587
}
 
588
 
 
589
static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
 
590
{
 
591
  TREE_ELEMENT *y,*par,*par2;
 
592
 
 
593
  leaf->colour=RED;
 
594
  while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
 
595
  {
 
596
    if (par == (par2=parent[-2][0])->left)
 
597
    {
 
598
      y= par2->right;
 
599
      if (y->colour == RED)
 
600
      {
 
601
        par->colour=BLACK;
 
602
        y->colour=BLACK;
 
603
        leaf=par2;
 
604
        parent-=2;
 
605
        leaf->colour=RED;               /* And the loop continues */
 
606
      }
 
607
      else
 
608
      {
 
609
        if (leaf == par->right)
 
610
        {
 
611
          left_rotate(parent[-1],par);
 
612
          par=leaf;                     /* leaf is now parent to old leaf */
 
613
        }
 
614
        par->colour=BLACK;
 
615
        par2->colour=RED;
 
616
        right_rotate(parent[-2],par2);
 
617
        break;
 
618
      }
 
619
    }
 
620
    else
 
621
    {
 
622
      y= par2->left;
 
623
      if (y->colour == RED)
 
624
      {
 
625
        par->colour=BLACK;
 
626
        y->colour=BLACK;
 
627
        leaf=par2;
 
628
        parent-=2;
 
629
        leaf->colour=RED;               /* And the loop continues */
 
630
      }
 
631
      else
 
632
      {
 
633
        if (leaf == par->left)
 
634
        {
 
635
          right_rotate(parent[-1],par);
 
636
          par=leaf;
 
637
        }
 
638
        par->colour=BLACK;
 
639
        par2->colour=RED;
 
640
        left_rotate(parent[-2],par2);
 
641
        break;
 
642
      }
 
643
    }
 
644
  }
 
645
  tree->root->colour=BLACK;
 
646
}
 
647
 
 
648
static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent)
 
649
{
 
650
  TREE_ELEMENT *x,*w,*par;
 
651
 
 
652
  x= **parent;
 
653
  while (x != tree->root && x->colour == BLACK)
 
654
  {
 
655
    if (x == (par=parent[-1][0])->left)
 
656
    {
 
657
      w=par->right;
 
658
      if (w->colour == RED)
 
659
      {
 
660
        w->colour=BLACK;
 
661
        par->colour=RED;
 
662
        left_rotate(parent[-1],par);
 
663
        parent[0]= &w->left;
 
664
        *++parent= &par->left;
 
665
        w=par->right;
 
666
      }
 
667
      if (w->left->colour == BLACK && w->right->colour == BLACK)
 
668
      {
 
669
        w->colour=RED;
 
670
        x=par;
 
671
        parent--;
 
672
      }
 
673
      else
 
674
      {
 
675
        if (w->right->colour == BLACK)
 
676
        {
 
677
          w->left->colour=BLACK;
 
678
          w->colour=RED;
 
679
          right_rotate(&par->right,w);
 
680
          w=par->right;
 
681
        }
 
682
        w->colour=par->colour;
 
683
        par->colour=BLACK;
 
684
        w->right->colour=BLACK;
 
685
        left_rotate(parent[-1],par);
 
686
        x=tree->root;
 
687
        break;
 
688
      }
 
689
    }
 
690
    else
 
691
    {
 
692
      w=par->left;
 
693
      if (w->colour == RED)
 
694
      {
 
695
        w->colour=BLACK;
 
696
        par->colour=RED;
 
697
        right_rotate(parent[-1],par);
 
698
        parent[0]= &w->right;
 
699
        *++parent= &par->right;
 
700
        w=par->left;
 
701
      }
 
702
      if (w->right->colour == BLACK && w->left->colour == BLACK)
 
703
      {
 
704
        w->colour=RED;
 
705
        x=par;
 
706
        parent--;
 
707
      }
 
708
      else
 
709
      {
 
710
        if (w->left->colour == BLACK)
 
711
        {
 
712
          w->right->colour=BLACK;
 
713
          w->colour=RED;
 
714
          left_rotate(&par->left,w);
 
715
          w=par->left;
 
716
        }
 
717
        w->colour=par->colour;
 
718
        par->colour=BLACK;
 
719
        w->left->colour=BLACK;
 
720
        right_rotate(parent[-1],par);
 
721
        x=tree->root;
 
722
        break;
 
723
      }
 
724
    }
 
725
  }
 
726
  x->colour=BLACK;
 
727
}
 
728
 
 
729
#ifndef DBUG_OFF
 
730
 
 
731
        /* Test that the proporties for a red-black tree holds */
 
732
 
 
733
static int test_rb_tree(TREE_ELEMENT *element)
 
734
{
 
735
  int count_l,count_r;
 
736
 
 
737
  if (!element->left)
 
738
    return 0;                           /* Found end of tree */
 
739
  if (element->colour == RED &&
 
740
      (element->left->colour == RED || element->right->colour == RED))
 
741
  {
 
742
    printf("Wrong tree: Found two red in a row\n");
 
743
    return -1;
 
744
  }
 
745
  count_l=test_rb_tree(element->left);
 
746
  count_r=test_rb_tree(element->right);
 
747
  if (count_l >= 0 && count_r >= 0)
 
748
  {
 
749
    if (count_l == count_r)
 
750
      return count_l+(element->colour == BLACK);
 
751
    printf("Wrong tree: Incorrect black-count: %d - %d\n",count_l,count_r);
 
752
  }
 
753
  return -1;
 
754
}
 
755
#endif