Commit 6cc19078 authored by Sergei Golubchik's avatar Sergei Golubchik

cleanup: make TREE copyable

move per-object TREE::null_element to be one global
static null_element.
parent bed1ede1
...@@ -59,7 +59,7 @@ typedef struct st_tree_element { ...@@ -59,7 +59,7 @@ typedef struct st_tree_element {
#define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs)) #define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))
typedef struct st_tree { typedef struct st_tree {
TREE_ELEMENT *root,null_element; TREE_ELEMENT *root;
TREE_ELEMENT **parents[MAX_TREE_HEIGHT]; TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
uint offset_to_key,elements_in_tree,size_of_element; uint offset_to_key,elements_in_tree,size_of_element;
size_t memory_limit, allocated; size_t memory_limit, allocated;
......
...@@ -77,6 +77,7 @@ static void rb_insert(TREE *tree,TREE_ELEMENT ***parent, ...@@ -77,6 +77,7 @@ static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
TREE_ELEMENT *leaf); TREE_ELEMENT *leaf);
static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent); static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
static TREE_ELEMENT null_element= { NULL, NULL, 0, BLACK };
/* The actual code for handling binary trees */ /* The actual code for handling binary trees */
...@@ -95,8 +96,7 @@ void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit, ...@@ -95,8 +96,7 @@ void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit,
if (default_alloc_size < DEFAULT_ALLOC_SIZE) if (default_alloc_size < DEFAULT_ALLOC_SIZE)
default_alloc_size= DEFAULT_ALLOC_SIZE; default_alloc_size= DEFAULT_ALLOC_SIZE;
default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE); default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
bzero((uchar*) &tree->null_element,sizeof(tree->null_element)); tree->root= &null_element;
tree->root= &tree->null_element;
tree->compare=compare; tree->compare=compare;
tree->size_of_element= size > 0 ? (uint) size : 0; tree->size_of_element= size > 0 ? (uint) size : 0;
tree->memory_limit=memory_limit; tree->memory_limit=memory_limit;
...@@ -104,8 +104,6 @@ void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit, ...@@ -104,8 +104,6 @@ void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit,
tree->allocated=0; tree->allocated=0;
tree->elements_in_tree=0; tree->elements_in_tree=0;
tree->custom_arg = custom_arg; tree->custom_arg = custom_arg;
tree->null_element.colour=BLACK;
tree->null_element.left=tree->null_element.right=0;
tree->my_flags= my_flags; tree->my_flags= my_flags;
tree->flag= 0; tree->flag= 0;
if (!free_element && size >= 0 && if (!free_element && size >= 0 &&
...@@ -158,7 +156,7 @@ static void free_tree(TREE *tree, myf free_flags) ...@@ -158,7 +156,7 @@ static void free_tree(TREE *tree, myf free_flags)
free_root(&tree->mem_root, free_flags); free_root(&tree->mem_root, free_flags);
} }
} }
tree->root= &tree->null_element; tree->root= &null_element;
tree->elements_in_tree=0; tree->elements_in_tree=0;
tree->allocated=0; tree->allocated=0;
...@@ -179,7 +177,7 @@ void reset_tree(TREE* tree) ...@@ -179,7 +177,7 @@ void reset_tree(TREE* tree)
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element) static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
{ {
if (element != &tree->null_element) if (element != &null_element)
{ {
delete_tree_element(tree,element->left); delete_tree_element(tree,element->left);
if (tree->free) if (tree->free)
...@@ -209,7 +207,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size, ...@@ -209,7 +207,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size,
*parent = &tree->root; element= tree->root; *parent = &tree->root; element= tree->root;
for (;;) for (;;)
{ {
if (element == &tree->null_element || if (element == &null_element ||
(cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element), (cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
key)) == 0) key)) == 0)
break; break;
...@@ -222,7 +220,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size, ...@@ -222,7 +220,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size,
*++parent = &element->left; element= element->left; *++parent = &element->left; element= element->left;
} }
} }
if (element == &tree->null_element) if (element == &null_element)
{ {
uint alloc_size; uint alloc_size;
if (tree->flag & TREE_ONLY_DUPS) if (tree->flag & TREE_ONLY_DUPS)
...@@ -246,7 +244,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size, ...@@ -246,7 +244,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size,
if (!element) if (!element)
return(NULL); return(NULL);
**parent=element; **parent=element;
element->left=element->right= &tree->null_element; element->left=element->right= &null_element;
if (!tree->offset_to_key) if (!tree->offset_to_key)
{ {
if (key_size == sizeof(void*)) /* no length, save pointer */ if (key_size == sizeof(void*)) /* no length, save pointer */
...@@ -288,7 +286,7 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg) ...@@ -288,7 +286,7 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg)
*parent= &tree->root; element= tree->root; *parent= &tree->root; element= tree->root;
for (;;) for (;;)
{ {
if (element == &tree->null_element) if (element == &null_element)
return 1; /* Was not in tree */ return 1; /* Was not in tree */
if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element), if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
key)) == 0) key)) == 0)
...@@ -302,12 +300,12 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg) ...@@ -302,12 +300,12 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg)
*++parent = &element->left; element= element->left; *++parent = &element->left; element= element->left;
} }
} }
if (element->left == &tree->null_element) if (element->left == &null_element)
{ {
(**parent)=element->right; (**parent)=element->right;
remove_colour= element->colour; remove_colour= element->colour;
} }
else if (element->right == &tree->null_element) else if (element->right == &null_element)
{ {
(**parent)=element->left; (**parent)=element->left;
remove_colour= element->colour; remove_colour= element->colour;
...@@ -316,7 +314,7 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg) ...@@ -316,7 +314,7 @@ int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg)
{ {
org_parent= parent; org_parent= parent;
*++parent= &element->right; nod= element->right; *++parent= &element->right; nod= element->right;
while (nod->left != &tree->null_element) while (nod->left != &null_element)
{ {
*++parent= &nod->left; nod= nod->left; *++parent= &nod->left; nod= nod->left;
} }
...@@ -346,7 +344,7 @@ void *tree_search(TREE *tree, void *key, void *custom_arg) ...@@ -346,7 +344,7 @@ void *tree_search(TREE *tree, void *key, void *custom_arg)
for (;;) for (;;)
{ {
if (element == &tree->null_element) if (element == &null_element)
return (void*) 0; return (void*) 0;
if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element), if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
key)) == 0) key)) == 0)
...@@ -371,8 +369,8 @@ void *tree_search_key(TREE *tree, const void *key, ...@@ -371,8 +369,8 @@ void *tree_search_key(TREE *tree, const void *key,
TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed. TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
*/ */
*parents = &tree->null_element; *parents = &null_element;
while (element != &tree->null_element) while (element != &null_element)
{ {
*++parents= element; *++parents= element;
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element), if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
...@@ -443,14 +441,14 @@ void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents, ...@@ -443,14 +441,14 @@ void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
{ {
TREE_ELEMENT *element= tree->root; TREE_ELEMENT *element= tree->root;
*parents= &tree->null_element; *parents= &null_element;
while (element != &tree->null_element) while (element != &null_element)
{ {
*++parents= element; *++parents= element;
element= ELEMENT_CHILD(element, child_offs); element= ELEMENT_CHILD(element, child_offs);
} }
*last_pos= parents; *last_pos= parents;
return **last_pos != &tree->null_element ? return **last_pos != &null_element ?
ELEMENT_KEY(tree, **last_pos) : NULL; ELEMENT_KEY(tree, **last_pos) : NULL;
} }
...@@ -459,11 +457,11 @@ void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs, ...@@ -459,11 +457,11 @@ void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
{ {
TREE_ELEMENT *x= **last_pos; TREE_ELEMENT *x= **last_pos;
if (ELEMENT_CHILD(x, r_offs) != &tree->null_element) if (ELEMENT_CHILD(x, r_offs) != &null_element)
{ {
x= ELEMENT_CHILD(x, r_offs); x= ELEMENT_CHILD(x, r_offs);
*++*last_pos= x; *++*last_pos= x;
while (ELEMENT_CHILD(x, l_offs) != &tree->null_element) while (ELEMENT_CHILD(x, l_offs) != &null_element)
{ {
x= ELEMENT_CHILD(x, l_offs); x= ELEMENT_CHILD(x, l_offs);
*++*last_pos= x; *++*last_pos= x;
...@@ -473,12 +471,12 @@ void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs, ...@@ -473,12 +471,12 @@ void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
else else
{ {
TREE_ELEMENT *y= *--*last_pos; TREE_ELEMENT *y= *--*last_pos;
while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs)) while (y != &null_element && x == ELEMENT_CHILD(y, r_offs))
{ {
x= y; x= y;
y= *--*last_pos; y= *--*last_pos;
} }
return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y); return y == &null_element ? NULL : ELEMENT_KEY(tree, y);
} }
} }
...@@ -494,7 +492,7 @@ ha_rows tree_record_pos(TREE *tree, const void *key, ...@@ -494,7 +492,7 @@ ha_rows tree_record_pos(TREE *tree, const void *key,
double left= 1; double left= 1;
double right= tree->elements_in_tree; double right= tree->elements_in_tree;
while (element != &tree->null_element) while (element != &null_element)
{ {
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element), if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
key)) == 0) key)) == 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment