- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmy_tree.h
111 lines (89 loc) · 4.2 KB
/
my_tree.h
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
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 */
#ifndef_tree_h
#define_tree_h
/**
@file include/my_tree.h
*/
#include<stddef.h>
#include<sys/types.h>
#include"my_alloc.h"/* MEM_ROOT */
#include"my_base.h"/* get 'enum ha_rkey_function' */
#include"my_inttypes.h"
#include"my_sys.h"/* qsort2_cmp */
/* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
#defineMAX_TREE_HEIGHT 64
#defineELEMENT_KEY(tree, element) \
(tree->offset_to_key ? (void *)((uchar *)element + tree->offset_to_key) \
: *((void **)(element + 1)))
#definetree_set_pointer(element, ptr) \
*((uchar **)(element + 1)) = ((uchar *)(ptr))
#defineTREE_NO_DUPS 1
typedefenum { left_root_right, right_root_left } TREE_WALK;
typedefuint32element_count;
typedefint (*tree_walk_action)(void*, element_count, void*);
typedefenum { free_init, free_free, free_end } TREE_FREE;
typedefvoid (*tree_element_free)(void*, TREE_FREE, constvoid*);
structTREE_ELEMENT {
TREE_ELEMENT() : count(0), colour(0) {}
TREE_ELEMENT*left{nullptr}, *right{nullptr};
uint32count : 31, colour : 1; /* black is marked as 1 */
};
#defineELEMENT_CHILD(element, offs) \
(*(TREE_ELEMENT**)((char*)element+offs))
structTREE {
TREE_ELEMENT*root{nullptr}, null_element;
TREE_ELEMENT**parents[MAX_TREE_HEIGHT]{nullptr};
uintoffset_to_key{0}, elements_in_tree{0}, size_of_element{0};
ulongmemory_limit{0}, allocated{0};
qsort2_cmpcompare{nullptr};
constvoid*custom_arg{nullptr};
MEM_ROOTmem_root;
boolwith_delete{false};
tree_element_freefree{nullptr};
uintflag{0};
};
/* Functions on whole tree */
voidinit_tree(TREE*tree, ulongmemory_limit, intelement_size,
qsort2_cmpcompare, boolwith_delete,
tree_element_freefree_element, constvoid*custom_arg);
voiddelete_tree(TREE*);
voidreset_tree(TREE*);
/* similar to delete tree, except we do not my_free() blocks in mem_root
*/
inlineboolis_tree_inited(TREE*tree) { returntree->root!= nullptr; }
/* Functions on leafs */
TREE_ELEMENT*tree_insert(TREE*tree, void*key, uintkey_size,
constvoid*custom_arg);
void*tree_search(TREE*tree, void*key, constvoid*custom_arg);
inttree_walk(TREE*tree, tree_walk_actionaction, void*argument,
TREE_WALKvisit);
inttree_delete(TREE*tree, void*key, uintkey_size, constvoid*custom_arg);
void*tree_search_key(TREE*tree, constvoid*key, TREE_ELEMENT**parents,
TREE_ELEMENT***last_pos, enumha_rkey_functionflag,
constvoid*custom_arg);
void*tree_search_edge(TREE*tree, TREE_ELEMENT**parents,
TREE_ELEMENT***last_pos, intchild_offs);
void*tree_search_next(TREE*tree, TREE_ELEMENT***last_pos, intl_offs,
intr_offs);
ha_rowstree_record_pos(TREE*tree, constvoid*key,
enumha_rkey_functionsearch_flag,
constvoid*custom_arg);
#defineTREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) +sizeof(void*))
#endif