1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
17
Code for handling red-black (balanced) binary trees.
18
key in tree is allocated accrding to following:
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
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.
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.
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
40
The actual key in TREE_ELEMENT is saved as a pointer or after the
42
If one uses only pointers in tree one can use tree_set_pointer() to
43
change address of data.
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))
58
#include "drizzled/tree.h"
59
#include "drizzled/internal/my_sys.h"
60
#include "drizzled/internal/m_string.h"
61
#include "drizzled/memory/root.h"
65
#define DEFAULT_ALLOC_SIZE 8192
66
#define DEFAULT_ALIGN_SIZE 8192
68
#define ELEMENT_KEY(tree,element)\
69
(tree->offset_to_key ? (void*)((unsigned char*) element+tree->offset_to_key) :\
70
*((void**) (element+1)))
71
#define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))
77
static void delete_tree_element(TREE *,TREE_ELEMENT *);
78
static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *,
79
tree_walk_action,void *);
80
static int tree_walk_right_root_left(TREE *,TREE_ELEMENT *,
81
tree_walk_action,void *);
82
static void left_rotate(TREE_ELEMENT **parent,TREE_ELEMENT *leaf);
83
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf);
84
static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
86
static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
89
void init_tree(TREE *tree, size_t default_alloc_size, uint32_t memory_limit,
90
uint32_t size, qsort_cmp2 compare, bool with_delete,
91
tree_element_free free_element, void *custom_arg)
93
if (default_alloc_size < DEFAULT_ALLOC_SIZE)
94
default_alloc_size= DEFAULT_ALLOC_SIZE;
95
default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
96
memset(&tree->null_element, 0, sizeof(tree->null_element));
97
tree->root= &tree->null_element;
98
tree->compare= compare;
99
tree->size_of_element= size > 0 ? (uint32_t) size : 0;
100
tree->memory_limit= memory_limit;
101
tree->free= free_element;
103
tree->elements_in_tree= 0;
104
tree->custom_arg = custom_arg;
105
tree->null_element.colour= BLACK;
106
tree->null_element.left=tree->null_element.right= 0;
109
(size <= sizeof(void*) || ((uint32_t) size & (sizeof(void*)-1))))
112
We know that the data doesn't have to be aligned (like if the key
113
contains a double), so we can store the data combined with the
116
tree->offset_to_key= sizeof(TREE_ELEMENT); /* Put key after element */
117
/* Fix allocation size so that we don't lose any memory */
118
default_alloc_size/= (sizeof(TREE_ELEMENT)+size);
119
if (!default_alloc_size)
120
default_alloc_size= 1;
121
default_alloc_size*= (sizeof(TREE_ELEMENT)+size);
125
tree->offset_to_key= 0; /* use key through pointer */
126
tree->size_of_element+= sizeof(void*);
128
if (! (tree->with_delete= with_delete))
130
init_alloc_root(&tree->mem_root, default_alloc_size);
131
tree->mem_root.min_malloc= (sizeof(TREE_ELEMENT)+tree->size_of_element);
135
static void free_tree(TREE *tree, myf free_flags)
137
if (tree->root) /* If initialized */
139
if (tree->with_delete)
140
delete_tree_element(tree,tree->root);
145
if (tree->memory_limit)
146
(*tree->free)(NULL, free_init, tree->custom_arg);
147
delete_tree_element(tree,tree->root);
148
if (tree->memory_limit)
149
(*tree->free)(NULL, free_end, tree->custom_arg);
151
free_root(&tree->mem_root, free_flags);
154
tree->root= &tree->null_element;
155
tree->elements_in_tree= 0;
159
void delete_tree(TREE* tree)
161
free_tree(tree, MYF(0)); /* free() mem_root if applicable */
164
void reset_tree(TREE* tree)
166
/* do not free mem_root, just mark blocks as free */
167
free_tree(tree, MYF(memory::MARK_BLOCKS_FREE));
171
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
173
if (element != &tree->null_element)
175
delete_tree_element(tree,element->left);
177
(*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
178
delete_tree_element(tree,element->right);
179
if (tree->with_delete)
180
free((char*) element);
186
insert, search and delete of elements
188
The following should be true:
189
parent[0] = & parent[-1][0]->left ||
190
parent[0] = & parent[-1][0]->right
193
TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint32_t key_size,
197
TREE_ELEMENT *element,***parent;
199
parent= tree->parents;
200
*parent = &tree->root; element= tree->root;
203
if (element == &tree->null_element ||
204
(cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
209
*++parent= &element->right; element= element->right;
213
*++parent = &element->left; element= element->left;
216
if (element == &tree->null_element)
218
size_t alloc_size= sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
219
tree->allocated+= alloc_size;
221
if (tree->memory_limit && tree->elements_in_tree
222
&& tree->allocated > tree->memory_limit)
225
return tree_insert(tree, key, key_size, custom_arg);
228
key_size+= tree->size_of_element;
229
if (tree->with_delete)
230
element= (TREE_ELEMENT *) malloc(alloc_size);
232
element= (TREE_ELEMENT *) alloc_root(&tree->mem_root,alloc_size);
236
element->left= element->right= &tree->null_element;
237
if (!tree->offset_to_key)
239
if (key_size == sizeof(void*)) /* no length, save pointer */
240
*((void**) (element+1))= key;
243
*((void**) (element+1))= (void*) ((void **) (element+1)+1);
244
memcpy(*((void **) (element+1)),key, key_size - sizeof(void*));
248
memcpy((unsigned char*) element + tree->offset_to_key, key, key_size);
249
element->count= 1; /* May give warning in purify */
250
tree->elements_in_tree++;
251
rb_insert(tree,parent,element); /* rebalance tree */
255
if (tree->flag & TREE_NO_DUPS)
258
/* Avoid a wrap over of the count. */
259
if (! element->count)
266
int tree_delete(TREE *tree, void *key, uint32_t key_size, void *custom_arg)
269
TREE_ELEMENT *element,***parent, ***org_parent, *nod;
270
if (!tree->with_delete)
271
return 1; /* not allowed */
273
parent= tree->parents;
274
*parent= &tree->root; element= tree->root;
279
if (element == &tree->null_element)
280
return 1; /* Was not in tree */
281
if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
286
*++parent= &element->right; element= element->right;
290
*++parent = &element->left; element= element->left;
293
if (element->left == &tree->null_element)
295
(**parent)= element->right;
296
remove_colour= element->colour;
298
else if (element->right == &tree->null_element)
300
(**parent)= element->left;
301
remove_colour= element->colour;
306
*++parent= &element->right; nod= element->right;
307
while (nod->left != &tree->null_element)
309
*++parent= &nod->left; nod= nod->left;
311
(**parent)= nod->right; /* unlink nod from tree */
312
remove_colour= nod->colour;
313
org_parent[0][0]= nod; /* put y in place of element */
314
org_parent[1]= &nod->right;
315
nod->left= element->left;
316
nod->right= element->right;
317
nod->colour= element->colour;
319
if (remove_colour == BLACK)
320
rb_delete_fixup(tree,parent);
322
(*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
323
tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size;
324
free((unsigned char*) element);
325
tree->elements_in_tree--;
330
void *tree_search_key(TREE *tree, const void *key,
331
TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
332
enum ha_rkey_function flag, void *custom_arg)
334
TREE_ELEMENT *element= tree->root;
335
TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
336
TREE_ELEMENT **last_equal_element= NULL;
339
TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
342
*parents = &tree->null_element;
343
while (element != &tree->null_element)
349
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
353
case HA_READ_KEY_EXACT:
354
case HA_READ_KEY_OR_NEXT:
355
case HA_READ_BEFORE_KEY:
356
last_equal_element= parents;
359
case HA_READ_AFTER_KEY:
362
case HA_READ_PREFIX_LAST:
363
case HA_READ_PREFIX_LAST_OR_PREV:
364
last_equal_element= parents;
371
if (cmp < 0) /* element < key */
373
last_right_step_parent= parents;
374
element= element->right;
378
last_left_step_parent= parents;
379
element= element->left;
383
case HA_READ_KEY_EXACT:
384
case HA_READ_PREFIX_LAST:
385
*last_pos= last_equal_element;
387
case HA_READ_KEY_OR_NEXT:
388
*last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
390
case HA_READ_AFTER_KEY:
391
*last_pos= last_left_step_parent;
393
case HA_READ_PREFIX_LAST_OR_PREV:
394
*last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
396
case HA_READ_BEFORE_KEY:
397
*last_pos= last_right_step_parent;
403
return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
407
Search first (the most left) or last (the most right) tree element
409
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
410
TREE_ELEMENT ***last_pos, int child_offs)
412
TREE_ELEMENT *element= tree->root;
414
*parents= &tree->null_element;
415
while (element != &tree->null_element)
418
element= ELEMENT_CHILD(element, child_offs);
421
return **last_pos != &tree->null_element ?
422
ELEMENT_KEY(tree, **last_pos) : NULL;
425
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
428
TREE_ELEMENT *x= **last_pos;
430
if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
432
x= ELEMENT_CHILD(x, r_offs);
434
while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
436
x= ELEMENT_CHILD(x, l_offs);
439
return ELEMENT_KEY(tree, x);
443
TREE_ELEMENT *y= *--*last_pos;
444
while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
449
return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
454
Expected that tree is fully balanced
455
(each path from root to leaf has the same length)
457
ha_rows tree_record_pos(TREE *tree, const void *key,
458
enum ha_rkey_function flag, void *custom_arg)
460
TREE_ELEMENT *element= tree->root;
462
double right= tree->elements_in_tree;
464
while (element != &tree->null_element)
468
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
472
case HA_READ_KEY_EXACT:
473
case HA_READ_BEFORE_KEY:
476
case HA_READ_AFTER_KEY:
483
if (cmp < 0) /* element < key */
485
element= element->right;
486
left= (left + right) / 2;
490
element= element->left;
491
right= (left + right) / 2;
496
case HA_READ_KEY_EXACT:
497
case HA_READ_BEFORE_KEY:
498
return (ha_rows) right;
499
case HA_READ_AFTER_KEY:
500
return (ha_rows) left;
506
int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
509
case left_root_right:
510
return tree_walk_left_root_right(tree,tree->root,action,argument);
511
case right_root_left:
512
return tree_walk_right_root_left(tree,tree->root,action,argument);
515
return 0; /* Keep gcc happy */
518
static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
521
if (element->left) /* Not null_element */
523
if ((error=tree_walk_left_root_right(tree,element->left,action,
525
(error=(*action)(ELEMENT_KEY(tree,element),
528
error=tree_walk_left_root_right(tree,element->right,action,argument);
535
static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
538
if (element->right) /* Not null_element */
540
if ((error=tree_walk_right_root_left(tree,element->right,action,
542
(error=(*action)(ELEMENT_KEY(tree,element),
545
error=tree_walk_right_root_left(tree,element->left,action,argument);
553
/* Functions to fix up the tree after insert and delete */
555
static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
560
leaf->right= y->left;
565
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
570
leaf->left= x->right;
575
static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
577
TREE_ELEMENT *y,*par,*par2;
580
while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
582
if (par == (par2=parent[-2][0])->left)
585
if (y->colour == RED)
591
leaf->colour= RED; /* And the loop continues */
595
if (leaf == par->right)
597
left_rotate(parent[-1],par);
598
par= leaf; /* leaf is now parent to old leaf */
602
right_rotate(parent[-2],par2);
609
if (y->colour == RED)
615
leaf->colour= RED; /* And the loop continues */
619
if (leaf == par->left)
621
right_rotate(parent[-1],par);
626
left_rotate(parent[-2],par2);
631
tree->root->colour=BLACK;
634
static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent)
636
TREE_ELEMENT *x,*w,*par;
639
while (x != tree->root && x->colour == BLACK)
641
if (x == (par=parent[-1][0])->left)
644
if (w->colour == RED)
648
left_rotate(parent[-1],par);
650
*++parent= &par->left;
653
if (w->left->colour == BLACK && w->right->colour == BLACK)
661
if (w->right->colour == BLACK)
663
w->left->colour= BLACK;
665
right_rotate(&par->right,w);
668
w->colour= par->colour;
670
w->right->colour= BLACK;
671
left_rotate(parent[-1],par);
679
if (w->colour == RED)
683
right_rotate(parent[-1],par);
684
parent[0]= &w->right;
685
*++parent= &par->right;
688
if (w->right->colour == BLACK && w->left->colour == BLACK)
696
if (w->left->colour == BLACK)
698
w->right->colour= BLACK;
700
left_rotate(&par->left,w);
703
w->colour= par->colour;
705
w->left->colour= BLACK;
706
right_rotate(parent[-1],par);
715
} /* namespace drizzled */