58
58
#include <drizzled/tree.h>
59
59
#include <drizzled/internal/my_sys.h>
60
60
#include <drizzled/internal/m_string.h>
61
#include <drizzled/memory/root.h>
65
64
#define DEFAULT_ALLOC_SIZE 8192
66
65
#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,
88
void init_tree(TREE *tree, size_t default_alloc_size, uint32_t memory_limit,
89
uint32_t size, qsort_cmp2 compare, bool with_delete,
90
tree_element_free free_element, void *custom_arg)
72
* Tree class public methods
75
void Tree::init_tree(size_t default_alloc_size, uint32_t mem_limit,
76
uint32_t size, qsort_cmp2 compare_callback, bool free_with_tree,
77
tree_element_free free_callback, void *caller_arg)
92
79
if (default_alloc_size < DEFAULT_ALLOC_SIZE)
93
80
default_alloc_size= DEFAULT_ALLOC_SIZE;
94
81
default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
95
memset(&tree->null_element, 0, sizeof(tree->null_element));
96
tree->root= &tree->null_element;
97
tree->compare= compare;
98
tree->size_of_element= size > 0 ? (uint32_t) size : 0;
99
tree->memory_limit= memory_limit;
100
tree->free= free_element;
102
tree->elements_in_tree= 0;
103
tree->custom_arg = custom_arg;
104
tree->null_element.colour= BLACK;
105
tree->null_element.left=tree->null_element.right= 0;
82
memset(&this->null_element, 0, sizeof(this->null_element));
83
root= &this->null_element;
84
compare= compare_callback;
85
size_of_element= size > 0 ? (uint32_t) size : 0;
86
memory_limit= mem_limit;
90
custom_arg = caller_arg;
91
null_element.colour= BLACK;
92
null_element.left=this->null_element.right= 0;
108
95
(size <= sizeof(void*) || ((uint32_t) size & (sizeof(void*)-1))))
111
98
We know that the data doesn't have to be aligned (like if the key
112
99
contains a double), so we can store the data combined with the
115
tree->offset_to_key= sizeof(TREE_ELEMENT); /* Put key after element */
102
offset_to_key= sizeof(Tree_Element); /* Put key after element */
116
103
/* Fix allocation size so that we don't lose any memory */
117
default_alloc_size/= (sizeof(TREE_ELEMENT)+size);
104
default_alloc_size/= (sizeof(Tree_Element)+size);
118
105
if (!default_alloc_size)
119
106
default_alloc_size= 1;
120
default_alloc_size*= (sizeof(TREE_ELEMENT)+size);
107
default_alloc_size*= (sizeof(Tree_Element)+size);
124
tree->offset_to_key= 0; /* use key through pointer */
125
tree->size_of_element+= sizeof(void*);
127
if (! (tree->with_delete= with_delete))
129
tree->mem_root.init(default_alloc_size);
130
tree->mem_root.min_malloc= (sizeof(TREE_ELEMENT)+tree->size_of_element);
134
static void free_tree(TREE *tree, myf free_flags)
136
if (tree->root) /* If initialized */
138
if (tree->with_delete)
139
delete_tree_element(tree,tree->root);
144
if (tree->memory_limit)
145
(*tree->free)(NULL, free_init, tree->custom_arg);
146
delete_tree_element(tree,tree->root);
147
if (tree->memory_limit)
148
(*tree->free)(NULL, free_end, tree->custom_arg);
150
tree->mem_root.free_root(free_flags);
153
tree->root= &tree->null_element;
154
tree->elements_in_tree= 0;
158
void delete_tree(TREE* tree)
160
free_tree(tree, MYF(0)); /* free() mem_root if applicable */
163
void reset_tree(TREE* tree)
111
offset_to_key= 0; /* use key through pointer */
112
size_of_element+= sizeof(void*);
114
if (! (with_delete= free_with_tree))
116
mem_root.init(default_alloc_size);
117
mem_root.min_malloc= (sizeof(Tree_Element)+size_of_element);
121
void Tree::delete_tree()
123
free_tree(MYF(0)); /* free() mem_root if applicable */
126
void Tree::reset_tree()
165
128
/* do not free mem_root, just mark blocks as free */
166
free_tree(tree, MYF(memory::MARK_BLOCKS_FREE));
170
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
172
if (element != &tree->null_element)
174
delete_tree_element(tree,element->left);
176
(*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
177
delete_tree_element(tree,element->right);
178
if (tree->with_delete)
179
free((char*) element);
185
insert, search and delete of elements
187
The following should be true:
188
parent[0] = & parent[-1][0]->left ||
189
parent[0] = & parent[-1][0]->right
192
TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint32_t key_size,
129
free_tree(MYF(memory::MARK_BLOCKS_FREE));
132
Tree_Element* Tree::tree_insert(void* key, uint32_t key_size, void* caller_arg)
196
TREE_ELEMENT *element,***parent;
135
Tree_Element *element,***parent;
198
parent= tree->parents;
199
*parent = &tree->root; element= tree->root;
137
parent= this->parents;
138
*parent = &this->root; element= this->root;
202
if (element == &tree->null_element ||
203
(cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
141
if (element == &this->null_element ||
142
(cmp = (*compare)(caller_arg, element_key(element), key)) == 0)
212
150
*++parent = &element->left; element= element->left;
215
if (element == &tree->null_element)
153
if (element == &this->null_element)
217
size_t alloc_size= sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
218
tree->allocated+= alloc_size;
155
size_t alloc_size= sizeof(Tree_Element)+key_size+this->size_of_element;
156
this->allocated+= alloc_size;
220
if (tree->memory_limit && tree->elements_in_tree
221
&& tree->allocated > tree->memory_limit)
158
if (this->memory_limit && this->elements_in_tree
159
&& this->allocated > this->memory_limit)
224
return tree_insert(tree, key, key_size, custom_arg);
162
return tree_insert(key, key_size, caller_arg);
227
key_size+= tree->size_of_element;
228
if (tree->with_delete)
229
element= (TREE_ELEMENT *) malloc(alloc_size);
165
key_size+= this->size_of_element;
166
if (this->with_delete)
167
element= (Tree_Element *) malloc(alloc_size);
231
element= (TREE_ELEMENT *) tree->mem_root.alloc(alloc_size);
169
element= (Tree_Element *) this->mem_root.alloc(alloc_size);
232
170
**parent= element;
233
element->left= element->right= &tree->null_element;
234
if (!tree->offset_to_key)
171
element->left= element->right= &this->null_element;
172
if (!this->offset_to_key)
236
174
if (key_size == sizeof(void*)) /* no length, save pointer */
237
175
*((void**) (element+1))= key;
263
void *tree_search_key(TREE *tree, const void *key,
264
TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
265
enum ha_rkey_function flag, void *custom_arg)
267
TREE_ELEMENT *element= tree->root;
268
TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
269
TREE_ELEMENT **last_equal_element= NULL;
272
TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
275
*parents = &tree->null_element;
276
while (element != &tree->null_element)
282
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
286
case HA_READ_KEY_EXACT:
287
case HA_READ_KEY_OR_NEXT:
288
case HA_READ_BEFORE_KEY:
289
last_equal_element= parents;
292
case HA_READ_AFTER_KEY:
295
case HA_READ_PREFIX_LAST:
296
case HA_READ_PREFIX_LAST_OR_PREV:
297
last_equal_element= parents;
304
if (cmp < 0) /* element < key */
306
last_right_step_parent= parents;
307
element= element->right;
311
last_left_step_parent= parents;
312
element= element->left;
316
case HA_READ_KEY_EXACT:
317
case HA_READ_PREFIX_LAST:
318
*last_pos= last_equal_element;
320
case HA_READ_KEY_OR_NEXT:
321
*last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
323
case HA_READ_AFTER_KEY:
324
*last_pos= last_left_step_parent;
326
case HA_READ_PREFIX_LAST_OR_PREV:
327
*last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
329
case HA_READ_BEFORE_KEY:
330
*last_pos= last_right_step_parent;
336
return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
339
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
342
TREE_ELEMENT *x= **last_pos;
344
if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
346
x= ELEMENT_CHILD(x, r_offs);
348
while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
350
x= ELEMENT_CHILD(x, l_offs);
353
return ELEMENT_KEY(tree, x);
357
TREE_ELEMENT *y= *--*last_pos;
358
while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
363
return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
368
Expected that tree is fully balanced
369
(each path from root to leaf has the same length)
371
ha_rows tree_record_pos(TREE *tree, const void *key,
372
enum ha_rkey_function flag, void *custom_arg)
374
TREE_ELEMENT *element= tree->root;
376
double right= tree->elements_in_tree;
378
while (element != &tree->null_element)
382
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
386
case HA_READ_KEY_EXACT:
387
case HA_READ_BEFORE_KEY:
390
case HA_READ_AFTER_KEY:
397
if (cmp < 0) /* element < key */
399
element= element->right;
400
left= (left + right) / 2;
404
element= element->left;
405
right= (left + right) / 2;
410
case HA_READ_KEY_EXACT:
411
case HA_READ_BEFORE_KEY:
412
return (ha_rows) right;
413
case HA_READ_AFTER_KEY:
414
return (ha_rows) left;
420
int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
423
case left_root_right:
424
return tree_walk_left_root_right(tree,tree->root,action,argument);
425
case right_root_left:
426
return tree_walk_right_root_left(tree,tree->root,action,argument);
201
int Tree::tree_walk(tree_walk_action action, void *argument, TREE_WALK visit)
204
case left_root_right:
205
return tree_walk_left_root_right(root,action,argument);
206
case right_root_left:
207
return tree_walk_right_root_left(root,action,argument);
429
210
return 0; /* Keep gcc happy */
432
static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
214
* Tree class private methods
217
void Tree::free_tree(myf free_flags)
219
if (root) /* If initialized */
222
delete_tree_element(root);
228
(*free)(NULL, free_init, custom_arg);
229
delete_tree_element(root);
231
(*free)(NULL, free_end, custom_arg);
233
mem_root.free_root(free_flags);
241
void* Tree::element_key(Tree_Element* element)
243
return offset_to_key ? (void*)((unsigned char*) element + offset_to_key)
244
: *((void**)(element + 1));
247
void Tree::delete_tree_element(Tree_Element *element)
249
if (element != &null_element)
251
delete_tree_element(element->left);
253
(*free)(element_key(element), free_free, custom_arg);
254
delete_tree_element(element->right);
260
int Tree::tree_walk_left_root_right(Tree_Element *element, tree_walk_action action, void *argument)
435
263
if (element->left) /* Not null_element */
437
if ((error=tree_walk_left_root_right(tree,element->left,action,
265
if ((error=tree_walk_left_root_right(element->left,action,
438
266
argument)) == 0 &&
439
(error=(*action)(ELEMENT_KEY(tree,element),
442
error=tree_walk_left_root_right(tree,element->right,action,argument);
267
(error=(*action)(element_key(element), element->count, argument)) == 0)
268
error=tree_walk_left_root_right(element->right,action,argument);
449
static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
275
int Tree::tree_walk_right_root_left(Tree_Element *element, tree_walk_action action, void *argument)
452
278
if (element->right) /* Not null_element */
454
if ((error=tree_walk_right_root_left(tree,element->right,action,
280
if ((error=tree_walk_right_root_left(element->right,action,
455
281
argument)) == 0 &&
456
(error=(*action)(ELEMENT_KEY(tree,element),
282
(error=(*action)(element_key(element),
459
error=tree_walk_right_root_left(tree,element->left,action,argument);
285
error=tree_walk_right_root_left(element->left,action,argument);
467
/* Functions to fix up the tree after insert and delete */
469
static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
292
void Tree::left_rotate(Tree_Element **parent, Tree_Element *element)
474
leaf->right= y->left;
297
element->right= y->left;
479
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
302
void Tree::right_rotate(Tree_Element **parent, Tree_Element *element)
484
leaf->left= x->right;
307
element->left= x->right;
489
static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
312
void Tree::rb_insert(Tree_Element ***parent, Tree_Element *element)
491
TREE_ELEMENT *y,*par,*par2;
314
Tree_Element *y,*par,*par2;
494
while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
317
while (element != root && (par=parent[-1][0])->colour == RED)
496
319
if (par == (par2=parent[-2][0])->left)