1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* Copyright (C) 2000 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/*
Code for handling red-black (balanced) binary trees.
key in tree is allocated according to following:
1) If size < 0 then tree will not allocate keys and only a pointer to
each key is saved in tree.
compare and search functions uses and returns key-pointer
2) If size == 0 then there are two options:
- key_size != 0 to tree_insert: The key will be stored in the tree.
- key_size == 0 to tree_insert: A pointer to the key is stored.
compare and search functions uses and returns key-pointer.
3) if key_size is given to init_tree then each node will continue the
key and calls to insert_key may increase length of key.
if key_size > sizeof(pointer) and key_size is a multiple of 8 (double
align) then key will be put on a 8 aligned address. Else
the key will be on address (element+1). This is transparent for user
compare and search functions uses a pointer to given key-argument.
- If you use a free function for tree-elements and you are freeing
the element itself, you should use key_size = 0 to init_tree and
tree_search
The actual key in TREE_ELEMENT is saved as a pointer or after the
TREE_ELEMENT struct.
If one uses only pointers in tree one can use tree_set_pointer() to
change address of data.
Implemented by monty.
*/
/*
NOTE:
tree->compare function should be ALWAYS called as
(*compare)(custom_arg, element_key(element), key)
and NOT the other way around, as
(*compare)(custom_arg, key, element_key(element))
*/
#include <config.h>
#include <drizzled/tree.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/internal/m_string.h>
#define BLACK 1
#define RED 0
#define DEFAULT_ALLOC_SIZE 8192
#define DEFAULT_ALIGN_SIZE 8192
namespace drizzled
{
/**
* Tree class public methods
*/
void Tree::init_tree(size_t default_alloc_size, uint32_t mem_limit,
uint32_t size, qsort_cmp2 compare_callback, bool free_with_tree,
tree_element_free free_callback, void *caller_arg)
{
if (default_alloc_size < DEFAULT_ALLOC_SIZE)
default_alloc_size= DEFAULT_ALLOC_SIZE;
default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
memset(&this->null_element, 0, sizeof(this->null_element));
root= &this->null_element;
compare= compare_callback;
size_of_element= size > 0 ? (uint32_t) size : 0;
memory_limit= mem_limit;
free= free_callback;
allocated= 0;
elements_in_tree= 0;
custom_arg = caller_arg;
null_element.colour= BLACK;
null_element.left=this->null_element.right= 0;
flag= 0;
if (!free_callback &&
(size <= sizeof(void*) || ((uint32_t) size & (sizeof(void*)-1))))
{
/*
We know that the data doesn't have to be aligned (like if the key
contains a double), so we can store the data combined with the
Tree_Element.
*/
offset_to_key= sizeof(Tree_Element); /* Put key after element */
/* Fix allocation size so that we don't lose any memory */
default_alloc_size/= (sizeof(Tree_Element)+size);
if (!default_alloc_size)
default_alloc_size= 1;
default_alloc_size*= (sizeof(Tree_Element)+size);
}
else
{
offset_to_key= 0; /* use key through pointer */
size_of_element+= sizeof(void*);
}
if (! (with_delete= free_with_tree))
{
mem_root.init(default_alloc_size);
mem_root.min_malloc= (sizeof(Tree_Element)+size_of_element);
}
}
void Tree::delete_tree()
{
free_tree(MYF(0)); /* free() mem_root if applicable */
}
void Tree::reset_tree()
{
/* do not free mem_root, just mark blocks as free */
free_tree(MYF(memory::MARK_BLOCKS_FREE));
}
Tree_Element* Tree::tree_insert(void* key, uint32_t key_size, void* caller_arg)
{
int cmp;
Tree_Element *element,***parent;
parent= this->parents;
*parent = &this->root; element= this->root;
for (;;)
{
if (element == &this->null_element ||
(cmp = (*compare)(caller_arg, element_key(element), key)) == 0)
break;
if (cmp < 0)
{
*++parent= &element->right; element= element->right;
}
else
{
*++parent = &element->left; element= element->left;
}
}
if (element == &this->null_element)
{
size_t alloc_size= sizeof(Tree_Element)+key_size+this->size_of_element;
this->allocated+= alloc_size;
if (this->memory_limit && this->elements_in_tree
&& this->allocated > this->memory_limit)
{
reset_tree();
return tree_insert(key, key_size, caller_arg);
}
key_size+= this->size_of_element;
if (this->with_delete)
element= (Tree_Element *) malloc(alloc_size);
else
element= (Tree_Element *) this->mem_root.alloc(alloc_size);
**parent= element;
element->left= element->right= &this->null_element;
if (!this->offset_to_key)
{
if (key_size == sizeof(void*)) /* no length, save pointer */
*((void**) (element+1))= key;
else
{
*((void**) (element+1))= (void*) ((void **) (element+1)+1);
memcpy(*((void **) (element+1)),key, key_size - sizeof(void*));
}
}
else
memcpy((unsigned char*) element + this->offset_to_key, key, key_size);
element->count= 1; /* May give warning in purify */
this->elements_in_tree++;
rb_insert(parent,element); /* rebalance tree */
}
else
{
if (this->flag & TREE_NO_DUPS)
return(NULL);
element->count++;
/* Avoid a wrap over of the count. */
if (! element->count)
element->count--;
}
return element;
}
int Tree::tree_walk(tree_walk_action action, void *argument, TREE_WALK visit)
{
switch (visit) {
case left_root_right:
return tree_walk_left_root_right(root,action,argument);
case right_root_left:
return tree_walk_right_root_left(root,action,argument);
}
return 0; /* Keep gcc happy */
}
/**
* Tree class private methods
*/
void Tree::free_tree(myf free_flags)
{
if (root) /* If initialized */
{
if (with_delete)
delete_tree_element(root);
else
{
if (free)
{
if (memory_limit)
(*free)(NULL, free_init, custom_arg);
delete_tree_element(root);
if (memory_limit)
(*free)(NULL, free_end, custom_arg);
}
mem_root.free_root(free_flags);
}
}
root= &null_element;
elements_in_tree= 0;
allocated= 0;
}
void* Tree::element_key(Tree_Element* element)
{
return offset_to_key ? (void*)((unsigned char*) element + offset_to_key)
: *((void**)(element + 1));
}
void Tree::delete_tree_element(Tree_Element *element)
{
if (element != &null_element)
{
delete_tree_element(element->left);
if (free)
(*free)(element_key(element), free_free, custom_arg);
delete_tree_element(element->right);
if (with_delete)
delete element;
}
}
int Tree::tree_walk_left_root_right(Tree_Element *element, tree_walk_action action, void *argument)
{
int error;
if (element->left) /* Not null_element */
{
if ((error=tree_walk_left_root_right(element->left,action,
argument)) == 0 &&
(error=(*action)(element_key(element), element->count, argument)) == 0)
error=tree_walk_left_root_right(element->right,action,argument);
return error;
}
return 0;
}
int Tree::tree_walk_right_root_left(Tree_Element *element, tree_walk_action action, void *argument)
{
int error;
if (element->right) /* Not null_element */
{
if ((error=tree_walk_right_root_left(element->right,action,
argument)) == 0 &&
(error=(*action)(element_key(element),
element->count,
argument)) == 0)
error=tree_walk_right_root_left(element->left,action,argument);
return error;
}
return 0;
}
void Tree::left_rotate(Tree_Element **parent, Tree_Element *element)
{
Tree_Element *y;
y= element->right;
element->right= y->left;
parent[0]= y;
y->left= element;
}
void Tree::right_rotate(Tree_Element **parent, Tree_Element *element)
{
Tree_Element *x;
x= element->left;
element->left= x->right;
parent[0]= x;
x->right= element;
}
void Tree::rb_insert(Tree_Element ***parent, Tree_Element *element)
{
Tree_Element *y,*par,*par2;
element->colour=RED;
while (element != root && (par=parent[-1][0])->colour == RED)
{
if (par == (par2=parent[-2][0])->left)
{
y= par2->right;
if (y->colour == RED)
{
par->colour= BLACK;
y->colour= BLACK;
element= par2;
parent-= 2;
element->colour= RED; /* And the loop continues */
}
else
{
if (element == par->right)
{
left_rotate(parent[-1],par);
par= element; /* element is now parent to old element */
}
par->colour= BLACK;
par2->colour= RED;
right_rotate(parent[-2],par2);
break;
}
}
else
{
y= par2->left;
if (y->colour == RED)
{
par->colour= BLACK;
y->colour= BLACK;
element= par2;
parent-= 2;
element->colour= RED; /* And the loop continues */
}
else
{
if (element == par->left)
{
right_rotate(parent[-1],par);
par= element;
}
par->colour= BLACK;
par2->colour= RED;
left_rotate(parent[-2],par2);
break;
}
}
}
root->colour=BLACK;
}
} /* namespace drizzled */
|