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