MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tree.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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  ft_boolean_search.c (at least) relies on that.
56 */
57 
58 #include "mysys_priv.h"
59 #include <m_string.h>
60 #include <my_tree.h>
61 #include "my_base.h"
62 
63 #define BLACK 1
64 #define RED 0
65 #define DEFAULT_ALLOC_SIZE 8192
66 #define DEFAULT_ALIGN_SIZE 8192
67 
68 static void delete_tree_element(TREE *,TREE_ELEMENT *);
69 static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *,
70  tree_walk_action,void *);
71 static int tree_walk_right_root_left(TREE *,TREE_ELEMENT *,
72  tree_walk_action,void *);
73 static void left_rotate(TREE_ELEMENT **parent,TREE_ELEMENT *leaf);
74 static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf);
75 static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
76  TREE_ELEMENT *leaf);
77 static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
78 
79 
80  /* The actuall code for handling binary trees */
81 
82 #ifndef DBUG_OFF
83 static int test_rb_tree(TREE_ELEMENT *element);
84 #endif
85 
86 void init_tree(TREE *tree, ulong default_alloc_size, ulong memory_limit,
87  int size, qsort_cmp2 compare, my_bool with_delete,
88  tree_element_free free_element, const void *custom_arg)
89 {
90  DBUG_ENTER("init_tree");
91  DBUG_PRINT("enter",("tree: 0x%lx size: %d", (long) tree, size));
92 
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 ? (uint) size : 0;
100  tree->memory_limit=memory_limit;
101  tree->free=free_element;
102  tree->allocated=0;
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;
107  tree->flag= 0;
108  if (!free_element && size >= 0 &&
109  ((uint) size <= sizeof(void*) || ((uint) size & (sizeof(void*)-1))))
110  {
111  /*
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
114  TREE_ELEMENT.
115  */
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);
122  }
123  else
124  {
125  tree->offset_to_key=0; /* use key through pointer */
126  tree->size_of_element+=sizeof(void*);
127  }
128  if (!(tree->with_delete=with_delete))
129  {
130  init_alloc_root(&tree->mem_root, (uint) default_alloc_size, 0);
131  tree->mem_root.min_malloc=(sizeof(TREE_ELEMENT)+tree->size_of_element);
132  }
133  DBUG_VOID_RETURN;
134 }
135 
136 static void free_tree(TREE *tree, myf free_flags)
137 {
138  DBUG_ENTER("free_tree");
139  DBUG_PRINT("enter",("tree: 0x%lx", (long) tree));
140 
141  if (tree->root) /* If initialized */
142  {
143  if (tree->with_delete)
144  delete_tree_element(tree,tree->root);
145  else
146  {
147  if (tree->free)
148  {
149  if (tree->memory_limit)
150  (*tree->free)(NULL, free_init, tree->custom_arg);
151  delete_tree_element(tree,tree->root);
152  if (tree->memory_limit)
153  (*tree->free)(NULL, free_end, tree->custom_arg);
154  }
155  free_root(&tree->mem_root, free_flags);
156  }
157  }
158  tree->root= &tree->null_element;
159  tree->elements_in_tree=0;
160  tree->allocated=0;
161 
162  DBUG_VOID_RETURN;
163 }
164 
165 void delete_tree(TREE* tree)
166 {
167  free_tree(tree, MYF(0)); /* my_free() mem_root if applicable */
168 }
169 
170 void reset_tree(TREE* tree)
171 {
172  /* do not free mem_root, just mark blocks as free */
173  free_tree(tree, MYF(MY_MARK_BLOCKS_FREE));
174 }
175 
176 
177 static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
178 {
179  if (element != &tree->null_element)
180  {
181  delete_tree_element(tree,element->left);
182  if (tree->free)
183  (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
184  delete_tree_element(tree,element->right);
185  if (tree->with_delete)
186  my_free(element);
187  }
188 }
189 
190 
191 /*
192  insert, search and delete of elements
193 
194  The following should be true:
195  parent[0] = & parent[-1][0]->left ||
196  parent[0] = & parent[-1][0]->right
197 */
198 
199 TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size,
200  const void* custom_arg)
201 {
202  int cmp;
203  TREE_ELEMENT *element,***parent;
204 
205  parent= tree->parents;
206  *parent = &tree->root; element= tree->root;
207  for (;;)
208  {
209  if (element == &tree->null_element ||
210  (cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
211  key)) == 0)
212  break;
213  if (cmp < 0)
214  {
215  *++parent= &element->right; element= element->right;
216  }
217  else
218  {
219  *++parent = &element->left; element= element->left;
220  }
221  }
222  if (element == &tree->null_element)
223  {
224  uint alloc_size=sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
225  tree->allocated+=alloc_size;
226 
227  if (tree->memory_limit && tree->elements_in_tree
228  && tree->allocated > tree->memory_limit)
229  {
230  reset_tree(tree);
231  return tree_insert(tree, key, key_size, custom_arg);
232  }
233 
234  key_size+=tree->size_of_element;
235  if (tree->with_delete)
236  element=(TREE_ELEMENT *) my_malloc(alloc_size, MYF(MY_WME));
237  else
238  element=(TREE_ELEMENT *) alloc_root(&tree->mem_root,alloc_size);
239  if (!element)
240  return(NULL);
241  **parent=element;
242  element->left=element->right= &tree->null_element;
243  if (!tree->offset_to_key)
244  {
245  if (key_size == sizeof(void*)) /* no length, save pointer */
246  *((void**) (element+1))=key;
247  else
248  {
249  *((void**) (element+1))= (void*) ((void **) (element+1)+1);
250  memcpy((uchar*) *((void **) (element+1)),key,
251  (size_t) (key_size-sizeof(void*)));
252  }
253  }
254  else
255  memcpy((uchar*) element+tree->offset_to_key,key,(size_t) key_size);
256  element->count=1; /* May give warning in purify */
257  tree->elements_in_tree++;
258  rb_insert(tree,parent,element); /* rebalance tree */
259  }
260  else
261  {
262  if (tree->flag & TREE_NO_DUPS)
263  return(NULL);
264  element->count++;
265  /* Avoid a wrap over of the count. */
266  if (! element->count)
267  element->count--;
268  }
269  DBUG_EXECUTE("check_tree", test_rb_tree(tree->root););
270  return element;
271 }
272 
273 int tree_delete(TREE *tree, void *key, uint key_size, const void *custom_arg)
274 {
275  int cmp,remove_colour;
276  TREE_ELEMENT *element,***parent, ***org_parent, *nod;
277  if (!tree->with_delete)
278  return 1; /* not allowed */
279 
280  parent= tree->parents;
281  *parent= &tree->root; element= tree->root;
282  for (;;)
283  {
284  if (element == &tree->null_element)
285  return 1; /* Was not in tree */
286  if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
287  key)) == 0)
288  break;
289  if (cmp < 0)
290  {
291  *++parent= &element->right; element= element->right;
292  }
293  else
294  {
295  *++parent = &element->left; element= element->left;
296  }
297  }
298  if (element->left == &tree->null_element)
299  {
300  (**parent)=element->right;
301  remove_colour= element->colour;
302  }
303  else if (element->right == &tree->null_element)
304  {
305  (**parent)=element->left;
306  remove_colour= element->colour;
307  }
308  else
309  {
310  org_parent= parent;
311  *++parent= &element->right; nod= element->right;
312  while (nod->left != &tree->null_element)
313  {
314  *++parent= &nod->left; nod= nod->left;
315  }
316  (**parent)=nod->right; /* unlink nod from tree */
317  remove_colour= nod->colour;
318  org_parent[0][0]=nod; /* put y in place of element */
319  org_parent[1]= &nod->right;
320  nod->left=element->left;
321  nod->right=element->right;
322  nod->colour=element->colour;
323  }
324  if (remove_colour == BLACK)
325  rb_delete_fixup(tree,parent);
326  if (tree->free)
327  (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
328  tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size;
329  my_free(element);
330  tree->elements_in_tree--;
331  return 0;
332 }
333 
334 
335 void *tree_search(TREE *tree, void *key, const void *custom_arg)
336 {
337  int cmp;
338  TREE_ELEMENT *element=tree->root;
339 
340  for (;;)
341  {
342  if (element == &tree->null_element)
343  return (void*) 0;
344  if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
345  key)) == 0)
346  return ELEMENT_KEY(tree,element);
347  if (cmp < 0)
348  element=element->right;
349  else
350  element=element->left;
351  }
352 }
353 
354 void *tree_search_key(TREE *tree, const void *key,
355  TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
356  enum ha_rkey_function flag, const void *custom_arg)
357 {
358  int cmp;
359  TREE_ELEMENT *element= tree->root;
360  TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
361  TREE_ELEMENT **last_equal_element= NULL;
362 
363 /*
364  TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
365 */
366 
367  *parents = &tree->null_element;
368  while (element != &tree->null_element)
369  {
370  *++parents= element;
371  if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
372  key)) == 0)
373  {
374  switch (flag) {
375  case HA_READ_KEY_EXACT:
376  case HA_READ_KEY_OR_NEXT:
377  case HA_READ_BEFORE_KEY:
378  last_equal_element= parents;
379  cmp= 1;
380  break;
381  case HA_READ_AFTER_KEY:
382  cmp= -1;
383  break;
384  case HA_READ_PREFIX_LAST:
385  case HA_READ_PREFIX_LAST_OR_PREV:
386  last_equal_element= parents;
387  cmp= -1;
388  break;
389  default:
390  return NULL;
391  }
392  }
393  if (cmp < 0) /* element < key */
394  {
395  last_right_step_parent= parents;
396  element= element->right;
397  }
398  else
399  {
400  last_left_step_parent= parents;
401  element= element->left;
402  }
403  }
404  switch (flag) {
405  case HA_READ_KEY_EXACT:
406  case HA_READ_PREFIX_LAST:
407  *last_pos= last_equal_element;
408  break;
409  case HA_READ_KEY_OR_NEXT:
410  *last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
411  break;
412  case HA_READ_AFTER_KEY:
413  *last_pos= last_left_step_parent;
414  break;
415  case HA_READ_PREFIX_LAST_OR_PREV:
416  *last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
417  break;
418  case HA_READ_BEFORE_KEY:
419  *last_pos= last_right_step_parent;
420  break;
421  default:
422  return NULL;
423  }
424  return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
425 }
426 
427 /*
428  Search first (the most left) or last (the most right) tree element
429 */
430 void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
431  TREE_ELEMENT ***last_pos, int child_offs)
432 {
433  TREE_ELEMENT *element= tree->root;
434 
435  *parents= &tree->null_element;
436  while (element != &tree->null_element)
437  {
438  *++parents= element;
439  element= ELEMENT_CHILD(element, child_offs);
440  }
441  *last_pos= parents;
442  return **last_pos != &tree->null_element ?
443  ELEMENT_KEY(tree, **last_pos) : NULL;
444 }
445 
446 void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
447  int r_offs)
448 {
449  TREE_ELEMENT *x= **last_pos;
450 
451  if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
452  {
453  x= ELEMENT_CHILD(x, r_offs);
454  *++*last_pos= x;
455  while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
456  {
457  x= ELEMENT_CHILD(x, l_offs);
458  *++*last_pos= x;
459  }
460  return ELEMENT_KEY(tree, x);
461  }
462  else
463  {
464  TREE_ELEMENT *y= *--*last_pos;
465  while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
466  {
467  x= y;
468  y= *--*last_pos;
469  }
470  return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
471  }
472 }
473 
474 /*
475  Expected that tree is fully balanced
476  (each path from root to leaf has the same length)
477 */
478 ha_rows tree_record_pos(TREE *tree, const void *key,
479  enum ha_rkey_function flag, const void *custom_arg)
480 {
481  int cmp;
482  TREE_ELEMENT *element= tree->root;
483  double left= 1;
484  double right= tree->elements_in_tree;
485 
486  while (element != &tree->null_element)
487  {
488  if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
489  key)) == 0)
490  {
491  switch (flag) {
492  case HA_READ_KEY_EXACT:
493  case HA_READ_BEFORE_KEY:
494  cmp= 1;
495  break;
496  case HA_READ_AFTER_KEY:
497  cmp= -1;
498  break;
499  default:
500  return HA_POS_ERROR;
501  }
502  }
503  if (cmp < 0) /* element < key */
504  {
505  element= element->right;
506  left= (left + right) / 2;
507  }
508  else
509  {
510  element= element->left;
511  right= (left + right) / 2;
512  }
513  }
514  switch (flag) {
515  case HA_READ_KEY_EXACT:
516  case HA_READ_BEFORE_KEY:
517  return (ha_rows) right;
518  case HA_READ_AFTER_KEY:
519  return (ha_rows) left;
520  default:
521  return HA_POS_ERROR;
522  }
523 }
524 
525 int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
526 {
527  switch (visit) {
528  case left_root_right:
529  return tree_walk_left_root_right(tree,tree->root,action,argument);
530  case right_root_left:
531  return tree_walk_right_root_left(tree,tree->root,action,argument);
532  }
533  return 0; /* Keep gcc happy */
534 }
535 
536 static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
537 {
538  int error;
539  if (element->left) /* Not null_element */
540  {
541  if ((error=tree_walk_left_root_right(tree,element->left,action,
542  argument)) == 0 &&
543  (error=(*action)(ELEMENT_KEY(tree,element),
544  (element_count) element->count,
545  argument)) == 0)
546  error=tree_walk_left_root_right(tree,element->right,action,argument);
547  return error;
548  }
549  return 0;
550 }
551 
552 static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
553 {
554  int error;
555  if (element->right) /* Not null_element */
556  {
557  if ((error=tree_walk_right_root_left(tree,element->right,action,
558  argument)) == 0 &&
559  (error=(*action)(ELEMENT_KEY(tree,element),
560  (element_count) element->count,
561  argument)) == 0)
562  error=tree_walk_right_root_left(tree,element->left,action,argument);
563  return error;
564  }
565  return 0;
566 }
567 
568 
569  /* Functions to fix up the tree after insert and delete */
570 
571 static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
572 {
573  TREE_ELEMENT *y;
574 
575  y=leaf->right;
576  leaf->right=y->left;
577  parent[0]=y;
578  y->left=leaf;
579 }
580 
581 static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
582 {
583  TREE_ELEMENT *x;
584 
585  x=leaf->left;
586  leaf->left=x->right;
587  parent[0]=x;
588  x->right=leaf;
589 }
590 
591 static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
592 {
593  TREE_ELEMENT *y,*par,*par2;
594 
595  leaf->colour=RED;
596  while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
597  {
598  if (par == (par2=parent[-2][0])->left)
599  {
600  y= par2->right;
601  if (y->colour == RED)
602  {
603  par->colour=BLACK;
604  y->colour=BLACK;
605  leaf=par2;
606  parent-=2;
607  leaf->colour=RED; /* And the loop continues */
608  }
609  else
610  {
611  if (leaf == par->right)
612  {
613  left_rotate(parent[-1],par);
614  par=leaf; /* leaf is now parent to old leaf */
615  }
616  par->colour=BLACK;
617  par2->colour=RED;
618  right_rotate(parent[-2],par2);
619  break;
620  }
621  }
622  else
623  {
624  y= par2->left;
625  if (y->colour == RED)
626  {
627  par->colour=BLACK;
628  y->colour=BLACK;
629  leaf=par2;
630  parent-=2;
631  leaf->colour=RED; /* And the loop continues */
632  }
633  else
634  {
635  if (leaf == par->left)
636  {
637  right_rotate(parent[-1],par);
638  par=leaf;
639  }
640  par->colour=BLACK;
641  par2->colour=RED;
642  left_rotate(parent[-2],par2);
643  break;
644  }
645  }
646  }
647  tree->root->colour=BLACK;
648 }
649 
650 static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent)
651 {
652  TREE_ELEMENT *x,*w,*par;
653 
654  x= **parent;
655  while (x != tree->root && x->colour == BLACK)
656  {
657  if (x == (par=parent[-1][0])->left)
658  {
659  w=par->right;
660  if (w->colour == RED)
661  {
662  w->colour=BLACK;
663  par->colour=RED;
664  left_rotate(parent[-1],par);
665  parent[0]= &w->left;
666  *++parent= &par->left;
667  w=par->right;
668  }
669  if (w->left->colour == BLACK && w->right->colour == BLACK)
670  {
671  w->colour=RED;
672  x=par;
673  parent--;
674  }
675  else
676  {
677  if (w->right->colour == BLACK)
678  {
679  w->left->colour=BLACK;
680  w->colour=RED;
681  right_rotate(&par->right,w);
682  w=par->right;
683  }
684  w->colour=par->colour;
685  par->colour=BLACK;
686  w->right->colour=BLACK;
687  left_rotate(parent[-1],par);
688  x=tree->root;
689  break;
690  }
691  }
692  else
693  {
694  w=par->left;
695  if (w->colour == RED)
696  {
697  w->colour=BLACK;
698  par->colour=RED;
699  right_rotate(parent[-1],par);
700  parent[0]= &w->right;
701  *++parent= &par->right;
702  w=par->left;
703  }
704  if (w->right->colour == BLACK && w->left->colour == BLACK)
705  {
706  w->colour=RED;
707  x=par;
708  parent--;
709  }
710  else
711  {
712  if (w->left->colour == BLACK)
713  {
714  w->right->colour=BLACK;
715  w->colour=RED;
716  left_rotate(&par->left,w);
717  w=par->left;
718  }
719  w->colour=par->colour;
720  par->colour=BLACK;
721  w->left->colour=BLACK;
722  right_rotate(parent[-1],par);
723  x=tree->root;
724  break;
725  }
726  }
727  }
728  x->colour=BLACK;
729 }
730 
731 #ifndef DBUG_OFF
732 
733  /* Test that the proporties for a red-black tree holds */
734 
735 static int test_rb_tree(TREE_ELEMENT *element)
736 {
737  int count_l,count_r;
738 
739  if (!element->left)
740  return 0; /* Found end of tree */
741  if (element->colour == RED &&
742  (element->left->colour == RED || element->right->colour == RED))
743  {
744  printf("Wrong tree: Found two red in a row\n");
745  return -1;
746  }
747  count_l=test_rb_tree(element->left);
748  count_r=test_rb_tree(element->right);
749  if (count_l >= 0 && count_r >= 0)
750  {
751  if (count_l == count_r)
752  return count_l+(element->colour == BLACK);
753  printf("Wrong tree: Incorrect black-count: %d - %d\n",count_l,count_r);
754  }
755  return -1;
756 }
757 #endif