MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_tree.h
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 #ifndef _tree_h
17 #define _tree_h
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "my_base.h" /* get 'enum ha_rkey_function' */
23 #include "my_alloc.h" /* MEM_ROOT */
24 
25 /* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
26 #define MAX_TREE_HEIGHT 64
27 
28 #define ELEMENT_KEY(tree,element)\
29 (tree->offset_to_key ? (void*)((uchar*) element+tree->offset_to_key) :\
30  *((void**) (element+1)))
31 
32 #define tree_set_pointer(element,ptr) *((uchar **) (element+1))=((uchar*) (ptr))
33 
34 #define TREE_NO_DUPS 1
35 
36 typedef enum { left_root_right, right_root_left } TREE_WALK;
37 typedef uint32 element_count;
38 typedef int (*tree_walk_action)(void *,element_count,void *);
39 
40 typedef enum { free_init, free_free, free_end } TREE_FREE;
41 typedef void (*tree_element_free)(void*, TREE_FREE, const void *);
42 
43 typedef struct st_tree_element {
44  struct st_tree_element *left,*right;
45  uint32 count:31,
46  colour:1; /* black is marked as 1 */
47 } TREE_ELEMENT;
48 
49 #define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))
50 
51 typedef struct st_tree {
52  TREE_ELEMENT *root,null_element;
53  TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
54  uint offset_to_key,elements_in_tree,size_of_element;
55  ulong memory_limit, allocated;
56  qsort_cmp2 compare;
57  const void *custom_arg;
58  MEM_ROOT mem_root;
59  my_bool with_delete;
60  tree_element_free free;
61  uint flag;
62 } TREE;
63 
64  /* Functions on whole tree */
65 void init_tree(TREE *tree, ulong default_alloc_size, ulong memory_limit,
66  int size, qsort_cmp2 compare, my_bool with_delete,
67  tree_element_free free_element, const void *custom_arg);
68 void delete_tree(TREE*);
69 void reset_tree(TREE*);
70  /* similar to delete tree, except we do not my_free() blocks in mem_root
71  */
72 #define is_tree_inited(tree) ((tree)->root != 0)
73 
74  /* Functions on leafs */
75 TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint key_size,
76  const void *custom_arg);
77 void *tree_search(TREE *tree, void *key, const void *custom_arg);
78 int tree_walk(TREE *tree,tree_walk_action action,
79  void *argument, TREE_WALK visit);
80 int tree_delete(TREE *tree, void *key, uint key_size, const void *custom_arg);
81 void *tree_search_key(TREE *tree, const void *key,
82  TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
83  enum ha_rkey_function flag, const void *custom_arg);
84 void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
85  TREE_ELEMENT ***last_pos, int child_offs);
86 void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
87  int r_offs);
88 ha_rows tree_record_pos(TREE *tree, const void *key,
89  enum ha_rkey_function search_flag,
90  const void *custom_arg);
91 
92 #define TREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) + sizeof(void*))
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 #endif