~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
56
#include "mysys/mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
57
#include <mystrings/m_string.h>
212.5.15 by Monty Taylor
Moved my_tree.
58
#include <mysys/my_tree.h>
1 by brian
clean slate
59
60
#define BLACK		1
61
#define RED		0
62
#define DEFAULT_ALLOC_SIZE 8192
63
#define DEFAULT_ALIGN_SIZE 8192
64
65
static void delete_tree_element(TREE *,TREE_ELEMENT *);
66
static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *,
67
				     tree_walk_action,void *);
68
static int tree_walk_right_root_left(TREE *,TREE_ELEMENT *,
69
				     tree_walk_action,void *);
70
static void left_rotate(TREE_ELEMENT **parent,TREE_ELEMENT *leaf);
71
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf);
72
static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
73
		      TREE_ELEMENT *leaf);
74
static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
75
76
298 by Brian Aker
ulong conversion.
77
void init_tree(TREE *tree, uint32_t default_alloc_size, uint32_t memory_limit,
1164 by Brian Aker
Small cleanup.
78
               uint32_t size, qsort_cmp2 compare, bool with_delete,
1 by brian
clean slate
79
	       tree_element_free free_element, void *custom_arg)
80
{
81
  if (default_alloc_size < DEFAULT_ALLOC_SIZE)
82
    default_alloc_size= DEFAULT_ALLOC_SIZE;
83
  default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
84
  memset(&tree->null_element, 0, sizeof(tree->null_element));
1 by brian
clean slate
85
  tree->root= &tree->null_element;
1164 by Brian Aker
Small cleanup.
86
  tree->compare= compare;
87
  tree->size_of_element= size > 0 ? (uint32_t) size : 0;
88
  tree->memory_limit= memory_limit;
89
  tree->free= free_element;
90
  tree->allocated= 0;
91
  tree->elements_in_tree= 0;
1 by brian
clean slate
92
  tree->custom_arg = custom_arg;
1164 by Brian Aker
Small cleanup.
93
  tree->null_element.colour= BLACK;
94
  tree->null_element.left=tree->null_element.right= 0;
1 by brian
clean slate
95
  tree->flag= 0;
1164 by Brian Aker
Small cleanup.
96
  if (!free_element &&
97
      (size <= sizeof(void*) || ((uint32_t) size & (sizeof(void*)-1))))
1 by brian
clean slate
98
  {
99
    /*
100
      We know that the data doesn't have to be aligned (like if the key
101
      contains a double), so we can store the data combined with the
102
      TREE_ELEMENT.
103
    */
1164 by Brian Aker
Small cleanup.
104
    tree->offset_to_key= sizeof(TREE_ELEMENT); /* Put key after element */
1 by brian
clean slate
105
    /* Fix allocation size so that we don't lose any memory */
1164 by Brian Aker
Small cleanup.
106
    default_alloc_size/= (sizeof(TREE_ELEMENT)+size);
1 by brian
clean slate
107
    if (!default_alloc_size)
1164 by Brian Aker
Small cleanup.
108
      default_alloc_size= 1;
109
    default_alloc_size*= (sizeof(TREE_ELEMENT)+size);
1 by brian
clean slate
110
  }
111
  else
112
  {
1164 by Brian Aker
Small cleanup.
113
    tree->offset_to_key= 0;		/* use key through pointer */
114
    tree->size_of_element+= sizeof(void*);
1 by brian
clean slate
115
  }
1164 by Brian Aker
Small cleanup.
116
  if (! (tree->with_delete= with_delete))
1 by brian
clean slate
117
  {
895 by Brian Aker
Completion (?) of uint conversion.
118
    init_alloc_root(&tree->mem_root, (uint32_t) default_alloc_size, 0);
1164 by Brian Aker
Small cleanup.
119
    tree->mem_root.min_malloc= (sizeof(TREE_ELEMENT)+tree->size_of_element);
1 by brian
clean slate
120
  }
121
}
122
123
static void free_tree(TREE *tree, myf free_flags)
124
{
125
  if (tree->root)				/* If initialized */
126
  {
127
    if (tree->with_delete)
128
      delete_tree_element(tree,tree->root);
129
    else
130
    {
131
      if (tree->free)
132
      {
133
        if (tree->memory_limit)
134
          (*tree->free)(NULL, free_init, tree->custom_arg);
135
	delete_tree_element(tree,tree->root);
136
        if (tree->memory_limit)
137
          (*tree->free)(NULL, free_end, tree->custom_arg);
138
      }
139
      free_root(&tree->mem_root, free_flags);
140
    }
141
  }
142
  tree->root= &tree->null_element;
1164 by Brian Aker
Small cleanup.
143
  tree->elements_in_tree= 0;
144
  tree->allocated= 0;
1 by brian
clean slate
145
}
146
147
void delete_tree(TREE* tree)
148
{
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
149
  free_tree(tree, MYF(0)); /* free() mem_root if applicable */
1 by brian
clean slate
150
}
151
152
void reset_tree(TREE* tree)
153
{
154
  /* do not free mem_root, just mark blocks as free */
155
  free_tree(tree, MYF(MY_MARK_BLOCKS_FREE));
156
}
157
158
159
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
160
{
161
  if (element != &tree->null_element)
162
  {
163
    delete_tree_element(tree,element->left);
164
    if (tree->free)
165
      (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
166
    delete_tree_element(tree,element->right);
167
    if (tree->with_delete)
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
168
      free((char*) element);
1 by brian
clean slate
169
  }
170
}
171
172
173
/*
174
  insert, search and delete of elements
175
176
  The following should be true:
177
    parent[0] = & parent[-1][0]->left ||
178
    parent[0] = & parent[-1][0]->right
179
*/
180
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
181
TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint32_t key_size,
1 by brian
clean slate
182
                          void* custom_arg)
183
{
184
  int cmp;
185
  TREE_ELEMENT *element,***parent;
186
187
  parent= tree->parents;
188
  *parent = &tree->root; element= tree->root;
189
  for (;;)
190
  {
191
    if (element == &tree->null_element ||
192
	(cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
193
                                key)) == 0)
194
      break;
195
    if (cmp < 0)
196
    {
197
      *++parent= &element->right; element= element->right;
198
    }
199
    else
200
    {
201
      *++parent = &element->left; element= element->left;
202
    }
203
  }
204
  if (element == &tree->null_element)
205
  {
1164 by Brian Aker
Small cleanup.
206
    size_t alloc_size= sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
207
    tree->allocated+= alloc_size;
1 by brian
clean slate
208
209
    if (tree->memory_limit && tree->elements_in_tree
210
                           && tree->allocated > tree->memory_limit)
211
    {
212
      reset_tree(tree);
213
      return tree_insert(tree, key, key_size, custom_arg);
214
    }
215
1164 by Brian Aker
Small cleanup.
216
    key_size+= tree->size_of_element;
1 by brian
clean slate
217
    if (tree->with_delete)
1164 by Brian Aker
Small cleanup.
218
      element= (TREE_ELEMENT *) malloc(alloc_size);
1 by brian
clean slate
219
    else
1164 by Brian Aker
Small cleanup.
220
      element= (TREE_ELEMENT *) alloc_root(&tree->mem_root,alloc_size);
1 by brian
clean slate
221
    if (!element)
222
      return(NULL);
1164 by Brian Aker
Small cleanup.
223
    **parent= element;
224
    element->left= element->right= &tree->null_element;
1 by brian
clean slate
225
    if (!tree->offset_to_key)
226
    {
227
      if (key_size == sizeof(void*))		 /* no length, save pointer */
1164 by Brian Aker
Small cleanup.
228
	*((void**) (element+1))= key;
1 by brian
clean slate
229
      else
230
      {
231
	*((void**) (element+1))= (void*) ((void **) (element+1)+1);
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
232
	memcpy(*((void **) (element+1)),key, key_size - sizeof(void*));
1 by brian
clean slate
233
      }
234
    }
235
    else
481 by Brian Aker
Remove all of uchar.
236
      memcpy((unsigned char*) element + tree->offset_to_key, key, key_size);
1164 by Brian Aker
Small cleanup.
237
    element->count= 1;			/* May give warning in purify */
1 by brian
clean slate
238
    tree->elements_in_tree++;
239
    rb_insert(tree,parent,element);	/* rebalance tree */
240
  }
241
  else
242
  {
243
    if (tree->flag & TREE_NO_DUPS)
244
      return(NULL);
245
    element->count++;
246
    /* Avoid a wrap over of the count. */
247
    if (! element->count)
248
      element->count--;
249
  }
1164 by Brian Aker
Small cleanup.
250
1 by brian
clean slate
251
  return element;
252
}
253
482 by Brian Aker
Remove uint.
254
int tree_delete(TREE *tree, void *key, uint32_t key_size, void *custom_arg)
1 by brian
clean slate
255
{
1164 by Brian Aker
Small cleanup.
256
  int remove_colour;
1 by brian
clean slate
257
  TREE_ELEMENT *element,***parent, ***org_parent, *nod;
258
  if (!tree->with_delete)
259
    return 1;					/* not allowed */
260
261
  parent= tree->parents;
262
  *parent= &tree->root; element= tree->root;
263
  for (;;)
264
  {
1164 by Brian Aker
Small cleanup.
265
    int cmp;
266
1 by brian
clean slate
267
    if (element == &tree->null_element)
268
      return 1;				/* Was not in tree */
269
    if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
270
                                key)) == 0)
271
      break;
272
    if (cmp < 0)
273
    {
274
      *++parent= &element->right; element= element->right;
275
    }
276
    else
277
    {
278
      *++parent = &element->left; element= element->left;
279
    }
280
  }
281
  if (element->left == &tree->null_element)
282
  {
1164 by Brian Aker
Small cleanup.
283
    (**parent)= element->right;
1 by brian
clean slate
284
    remove_colour= element->colour;
285
  }
286
  else if (element->right == &tree->null_element)
287
  {
1164 by Brian Aker
Small cleanup.
288
    (**parent)= element->left;
1 by brian
clean slate
289
    remove_colour= element->colour;
290
  }
291
  else
292
  {
293
    org_parent= parent;
294
    *++parent= &element->right; nod= element->right;
295
    while (nod->left != &tree->null_element)
296
    {
297
      *++parent= &nod->left; nod= nod->left;
298
    }
1164 by Brian Aker
Small cleanup.
299
    (**parent)= nod->right;		/* unlink nod from tree */
1 by brian
clean slate
300
    remove_colour= nod->colour;
1164 by Brian Aker
Small cleanup.
301
    org_parent[0][0]= nod;		/* put y in place of element */
1 by brian
clean slate
302
    org_parent[1]= &nod->right;
1164 by Brian Aker
Small cleanup.
303
    nod->left= element->left;
304
    nod->right= element->right;
305
    nod->colour= element->colour;
1 by brian
clean slate
306
  }
307
  if (remove_colour == BLACK)
308
    rb_delete_fixup(tree,parent);
309
  if (tree->free)
310
    (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
311
  tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size;
481 by Brian Aker
Remove all of uchar.
312
  free((unsigned char*) element);
1 by brian
clean slate
313
  tree->elements_in_tree--;
1164 by Brian Aker
Small cleanup.
314
1 by brian
clean slate
315
  return 0;
316
}
317
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
318
void *tree_search_key(TREE *tree, const void *key,
1 by brian
clean slate
319
                      TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
320
                      enum ha_rkey_function flag, void *custom_arg)
321
{
322
  TREE_ELEMENT *element= tree->root;
323
  TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
324
  TREE_ELEMENT **last_equal_element= NULL;
325
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
326
/*
1 by brian
clean slate
327
  TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
328
*/
329
330
  *parents = &tree->null_element;
331
  while (element != &tree->null_element)
332
  {
1164 by Brian Aker
Small cleanup.
333
    int cmp;
334
1 by brian
clean slate
335
    *++parents= element;
1164 by Brian Aker
Small cleanup.
336
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
337
    if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
1 by brian
clean slate
338
			       key)) == 0)
339
    {
340
      switch (flag) {
341
      case HA_READ_KEY_EXACT:
342
      case HA_READ_KEY_OR_NEXT:
343
      case HA_READ_BEFORE_KEY:
344
	last_equal_element= parents;
345
	cmp= 1;
346
	break;
347
      case HA_READ_AFTER_KEY:
348
	cmp= -1;
349
	break;
350
      case HA_READ_PREFIX_LAST:
351
      case HA_READ_PREFIX_LAST_OR_PREV:
352
	last_equal_element= parents;
353
	cmp= -1;
354
	break;
355
      default:
356
	return NULL;
357
      }
358
    }
359
    if (cmp < 0) /* element < key */
360
    {
361
      last_right_step_parent= parents;
362
      element= element->right;
363
    }
364
    else
365
    {
366
      last_left_step_parent= parents;
367
      element= element->left;
368
    }
369
  }
370
  switch (flag) {
371
  case HA_READ_KEY_EXACT:
372
  case HA_READ_PREFIX_LAST:
373
    *last_pos= last_equal_element;
374
    break;
375
  case HA_READ_KEY_OR_NEXT:
376
    *last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
377
    break;
378
  case HA_READ_AFTER_KEY:
379
    *last_pos= last_left_step_parent;
380
    break;
381
  case HA_READ_PREFIX_LAST_OR_PREV:
382
    *last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
383
    break;
384
  case HA_READ_BEFORE_KEY:
385
    *last_pos= last_right_step_parent;
386
    break;
387
  default:
388
    return NULL;
389
  }
1164 by Brian Aker
Small cleanup.
390
1 by brian
clean slate
391
  return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
392
}
393
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
394
/*
395
  Search first (the most left) or last (the most right) tree element
1 by brian
clean slate
396
*/
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
397
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
1 by brian
clean slate
398
		       TREE_ELEMENT ***last_pos, int child_offs)
399
{
400
  TREE_ELEMENT *element= tree->root;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
401
1 by brian
clean slate
402
  *parents= &tree->null_element;
403
  while (element != &tree->null_element)
404
  {
405
    *++parents= element;
406
    element= ELEMENT_CHILD(element, child_offs);
407
  }
408
  *last_pos= parents;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
409
  return **last_pos != &tree->null_element ?
1 by brian
clean slate
410
    ELEMENT_KEY(tree, **last_pos) : NULL;
411
}
412
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
413
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
1 by brian
clean slate
414
                       int r_offs)
415
{
416
  TREE_ELEMENT *x= **last_pos;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
417
1 by brian
clean slate
418
  if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
419
  {
420
    x= ELEMENT_CHILD(x, r_offs);
421
    *++*last_pos= x;
422
    while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
423
    {
424
      x= ELEMENT_CHILD(x, l_offs);
425
      *++*last_pos= x;
426
    }
427
    return ELEMENT_KEY(tree, x);
428
  }
429
  else
430
  {
431
    TREE_ELEMENT *y= *--*last_pos;
432
    while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
433
    {
434
      x= y;
435
      y= *--*last_pos;
436
    }
437
    return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
438
  }
439
}
440
441
/*
442
  Expected that tree is fully balanced
443
  (each path from root to leaf has the same length)
444
*/
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
445
ha_rows tree_record_pos(TREE *tree, const void *key,
1 by brian
clean slate
446
			enum ha_rkey_function flag, void *custom_arg)
447
{
448
  TREE_ELEMENT *element= tree->root;
449
  double left= 1;
450
  double right= tree->elements_in_tree;
451
452
  while (element != &tree->null_element)
453
  {
1164 by Brian Aker
Small cleanup.
454
    int cmp;
455
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
456
    if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
1 by brian
clean slate
457
			       key)) == 0)
458
    {
459
      switch (flag) {
460
      case HA_READ_KEY_EXACT:
461
      case HA_READ_BEFORE_KEY:
462
        cmp= 1;
463
        break;
464
      case HA_READ_AFTER_KEY:
465
        cmp= -1;
466
        break;
467
      default:
468
        return HA_POS_ERROR;
469
      }
470
    }
471
    if (cmp < 0) /* element < key */
472
    {
473
      element= element->right;
474
      left= (left + right) / 2;
475
    }
476
    else
477
    {
478
      element= element->left;
479
      right= (left + right) / 2;
480
    }
481
  }
1164 by Brian Aker
Small cleanup.
482
1 by brian
clean slate
483
  switch (flag) {
484
  case HA_READ_KEY_EXACT:
485
  case HA_READ_BEFORE_KEY:
486
    return (ha_rows) right;
487
  case HA_READ_AFTER_KEY:
488
    return (ha_rows) left;
489
  default:
490
    return HA_POS_ERROR;
491
  }
492
}
493
494
int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
495
{
496
  switch (visit) {
497
  case left_root_right:
498
    return tree_walk_left_root_right(tree,tree->root,action,argument);
499
  case right_root_left:
500
    return tree_walk_right_root_left(tree,tree->root,action,argument);
501
  }
1164 by Brian Aker
Small cleanup.
502
1 by brian
clean slate
503
  return 0;			/* Keep gcc happy */
504
}
505
506
static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
507
{
508
  int error;
509
  if (element->left)				/* Not null_element */
510
  {
511
    if ((error=tree_walk_left_root_right(tree,element->left,action,
512
					  argument)) == 0 &&
513
	(error=(*action)(ELEMENT_KEY(tree,element),
514
			  (element_count) element->count,
515
			  argument)) == 0)
516
      error=tree_walk_left_root_right(tree,element->right,action,argument);
517
    return error;
518
  }
1164 by Brian Aker
Small cleanup.
519
1 by brian
clean slate
520
  return 0;
521
}
522
523
static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
524
{
525
  int error;
526
  if (element->right)				/* Not null_element */
527
  {
528
    if ((error=tree_walk_right_root_left(tree,element->right,action,
529
					  argument)) == 0 &&
530
	(error=(*action)(ELEMENT_KEY(tree,element),
531
			  (element_count) element->count,
532
			  argument)) == 0)
533
     error=tree_walk_right_root_left(tree,element->left,action,argument);
534
    return error;
535
  }
1164 by Brian Aker
Small cleanup.
536
1 by brian
clean slate
537
  return 0;
538
}
539
540
1164 by Brian Aker
Small cleanup.
541
/* Functions to fix up the tree after insert and delete */
1 by brian
clean slate
542
543
static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
544
{
545
  TREE_ELEMENT *y;
546
1164 by Brian Aker
Small cleanup.
547
  y= leaf->right;
548
  leaf->right= y->left;
549
  parent[0]= y;
550
  y->left= leaf;
1 by brian
clean slate
551
}
552
553
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
554
{
555
  TREE_ELEMENT *x;
556
1164 by Brian Aker
Small cleanup.
557
  x= leaf->left;
558
  leaf->left= x->right;
559
  parent[0]= x;
560
  x->right= leaf;
1 by brian
clean slate
561
}
562
563
static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
564
{
565
  TREE_ELEMENT *y,*par,*par2;
566
567
  leaf->colour=RED;
568
  while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
569
  {
570
    if (par == (par2=parent[-2][0])->left)
571
    {
572
      y= par2->right;
573
      if (y->colour == RED)
574
      {
1164 by Brian Aker
Small cleanup.
575
	par->colour= BLACK;
576
	y->colour= BLACK;
577
	leaf= par2;
578
	parent-= 2;
579
	leaf->colour= RED;		/* And the loop continues */
1 by brian
clean slate
580
      }
581
      else
582
      {
583
	if (leaf == par->right)
584
	{
585
	  left_rotate(parent[-1],par);
1164 by Brian Aker
Small cleanup.
586
	  par= leaf;			/* leaf is now parent to old leaf */
1 by brian
clean slate
587
	}
1164 by Brian Aker
Small cleanup.
588
	par->colour= BLACK;
589
	par2->colour= RED;
1 by brian
clean slate
590
	right_rotate(parent[-2],par2);
591
	break;
592
      }
593
    }
594
    else
595
    {
596
      y= par2->left;
597
      if (y->colour == RED)
598
      {
1164 by Brian Aker
Small cleanup.
599
	par->colour= BLACK;
600
	y->colour= BLACK;
601
	leaf= par2;
602
	parent-= 2;
603
	leaf->colour= RED;		/* And the loop continues */
1 by brian
clean slate
604
      }
605
      else
606
      {
607
	if (leaf == par->left)
608
	{
609
	  right_rotate(parent[-1],par);
1164 by Brian Aker
Small cleanup.
610
	  par= leaf;
1 by brian
clean slate
611
	}
1164 by Brian Aker
Small cleanup.
612
	par->colour= BLACK;
613
	par2->colour= RED;
1 by brian
clean slate
614
	left_rotate(parent[-2],par2);
615
	break;
616
      }
617
    }
618
  }
619
  tree->root->colour=BLACK;
620
}
621
622
static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent)
623
{
624
  TREE_ELEMENT *x,*w,*par;
625
626
  x= **parent;
627
  while (x != tree->root && x->colour == BLACK)
628
  {
629
    if (x == (par=parent[-1][0])->left)
630
    {
1164 by Brian Aker
Small cleanup.
631
      w= par->right;
1 by brian
clean slate
632
      if (w->colour == RED)
633
      {
1164 by Brian Aker
Small cleanup.
634
	w->colour= BLACK;
635
	par->colour= RED;
1 by brian
clean slate
636
	left_rotate(parent[-1],par);
637
	parent[0]= &w->left;
638
	*++parent= &par->left;
1164 by Brian Aker
Small cleanup.
639
	w= par->right;
1 by brian
clean slate
640
      }
641
      if (w->left->colour == BLACK && w->right->colour == BLACK)
642
      {
1164 by Brian Aker
Small cleanup.
643
	w->colour= RED;
644
	x= par;
1 by brian
clean slate
645
	parent--;
646
      }
647
      else
648
      {
649
	if (w->right->colour == BLACK)
650
	{
1164 by Brian Aker
Small cleanup.
651
	  w->left->colour= BLACK;
652
	  w->colour= RED;
1 by brian
clean slate
653
	  right_rotate(&par->right,w);
1164 by Brian Aker
Small cleanup.
654
	  w= par->right;
1 by brian
clean slate
655
	}
1164 by Brian Aker
Small cleanup.
656
	w->colour= par->colour;
657
	par->colour= BLACK;
658
	w->right->colour= BLACK;
1 by brian
clean slate
659
	left_rotate(parent[-1],par);
1164 by Brian Aker
Small cleanup.
660
	x= tree->root;
1 by brian
clean slate
661
	break;
662
      }
663
    }
664
    else
665
    {
666
      w=par->left;
667
      if (w->colour == RED)
668
      {
1164 by Brian Aker
Small cleanup.
669
	w->colour= BLACK;
670
	par->colour= RED;
1 by brian
clean slate
671
	right_rotate(parent[-1],par);
672
	parent[0]= &w->right;
673
	*++parent= &par->right;
1164 by Brian Aker
Small cleanup.
674
	w= par->left;
1 by brian
clean slate
675
      }
676
      if (w->right->colour == BLACK && w->left->colour == BLACK)
677
      {
1164 by Brian Aker
Small cleanup.
678
	w->colour= RED;
679
	x= par;
1 by brian
clean slate
680
	parent--;
681
      }
682
      else
683
      {
684
	if (w->left->colour == BLACK)
685
	{
1164 by Brian Aker
Small cleanup.
686
	  w->right->colour= BLACK;
687
	  w->colour= RED;
1 by brian
clean slate
688
	  left_rotate(&par->left,w);
1164 by Brian Aker
Small cleanup.
689
	  w= par->left;
1 by brian
clean slate
690
	}
1164 by Brian Aker
Small cleanup.
691
	w->colour= par->colour;
692
	par->colour= BLACK;
693
	w->left->colour= BLACK;
1 by brian
clean slate
694
	right_rotate(parent[-1],par);
1164 by Brian Aker
Small cleanup.
695
	x= tree->root;
1 by brian
clean slate
696
	break;
697
      }
698
    }
699
  }
1164 by Brian Aker
Small cleanup.
700
  x->colour= BLACK;
1 by brian
clean slate
701
}