1 /* Copyright (c) 2000, 2016, 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, version 2.0, 5 as published by the Free Software Foundation. 6 7 This program is also distributed with certain software (including 8 but not limited to OpenSSL) that is licensed under separate terms, 9 as designated in a particular file or component or in included license 10 documentation. The authors of MySQL hereby grant you an additional 11 permission to link the program and your derivative works with the 12 separately licensed software that they have included with MySQL. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License, version 2.0, for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 22 23 #ifndef _tree_h 24 #define _tree_h 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 #include "my_base.h" /* get 'enum ha_rkey_function' */ 30 #include "my_alloc.h" /* MEM_ROOT */ 31 32 /* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */ 33 #define MAX_TREE_HEIGHT 64 34 35 #define ELEMENT_KEY(tree,element)\ 36 (tree->offset_to_key ? (void*)((uchar*) element+tree->offset_to_key) :\ 37 *((void**) (element+1))) 38 39 #define tree_set_pointer(element,ptr) *((uchar **) (element+1))=((uchar*) (ptr)) 40 41 #define TREE_NO_DUPS 1 42 43 typedef enum { left_root_right, right_root_left } TREE_WALK; 44 typedef uint32 element_count; 45 typedef int (*tree_walk_action)(void *,element_count,void *); 46 47 typedef enum { free_init, free_free, free_end } TREE_FREE; 48 typedef void (*tree_element_free)(void*, TREE_FREE, const void *); 49 50 typedef struct st_tree_element { 51 struct st_tree_element *left,*right; 52 uint32 count:31, 53 colour:1; /* black is marked as 1 */ 54 } TREE_ELEMENT; 55 56 #define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs)) 57 58 typedef struct st_tree { 59 TREE_ELEMENT *root,null_element; 60 TREE_ELEMENT **parents[MAX_TREE_HEIGHT]; 61 uint offset_to_key,elements_in_tree,size_of_element; 62 ulong memory_limit, allocated; 63 qsort_cmp2 compare; 64 const void *custom_arg; 65 MEM_ROOT mem_root; 66 my_bool with_delete; 67 tree_element_free free; 68 uint flag; 69 } TREE; 70 71 /* Functions on whole tree */ 72 void init_tree(TREE *tree, size_t default_alloc_size, ulong memory_limit, 73 int size, qsort_cmp2 compare, my_bool with_delete, 74 tree_element_free free_element, const void *custom_arg); 75 void delete_tree(TREE*); 76 void reset_tree(TREE*); 77 /* similar to delete tree, except we do not my_free() blocks in mem_root 78 */ 79 #define is_tree_inited(tree) ((tree)->root != 0) 80 81 /* Functions on leafs */ 82 TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint key_size, 83 const void *custom_arg); 84 void *tree_search(TREE *tree, void *key, const void *custom_arg); 85 int tree_walk(TREE *tree,tree_walk_action action, 86 void *argument, TREE_WALK visit); 87 int tree_delete(TREE *tree, void *key, uint key_size, const void *custom_arg); 88 void *tree_search_key(TREE *tree, const void *key, 89 TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos, 90 enum ha_rkey_function flag, const void *custom_arg); 91 void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents, 92 TREE_ELEMENT ***last_pos, int child_offs); 93 void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs, 94 int r_offs); 95 ha_rows tree_record_pos(TREE *tree, const void *key, 96 enum ha_rkey_function search_flag, 97 const void *custom_arg); 98 99 #define TREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) + sizeof(void*)) 100 101 #ifdef __cplusplus 102 } 103 #endif 104 #endif 105