my_tree.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. #ifndef _tree_h
  13. #define _tree_h
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include "my_base.h" /* get 'enum ha_rkey_function' */
  18. #include "my_alloc.h" /* MEM_ROOT */
  19. /* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
  20. #define MAX_TREE_HEIGHT 64
  21. #define ELEMENT_KEY(tree,element)\
  22. (tree->offset_to_key ? (void*)((uchar*) element+tree->offset_to_key) :\
  23. *((void**) (element+1)))
  24. #define tree_set_pointer(element,ptr) *((uchar **) (element+1))=((uchar*) (ptr))
  25. /*
  26. A tree with its flag set to TREE_ONLY_DUPS behaves differently on inserting
  27. an element that is not in the tree:
  28. the element is not added at all, but instead tree_insert() returns a special
  29. address TREE_ELEMENT_UNIQUE as an indication that the function has not failed
  30. due to lack of memory.
  31. */
  32. #define TREE_ELEMENT_UNIQUE ((TREE_ELEMENT *) 1)
  33. #define TREE_NO_DUPS 1
  34. #define TREE_ONLY_DUPS 2
  35. typedef enum { left_root_right, right_root_left } TREE_WALK;
  36. typedef uint32 element_count;
  37. typedef int (*tree_walk_action)(void *,element_count,void *);
  38. typedef enum { free_init, free_free, free_end } TREE_FREE;
  39. typedef void (*tree_element_free)(void*, TREE_FREE, void *);
  40. typedef struct st_tree_element {
  41. struct st_tree_element *left,*right;
  42. uint32 count:31,
  43. colour:1; /* black is marked as 1 */
  44. } TREE_ELEMENT;
  45. #define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))
  46. typedef struct st_tree {
  47. TREE_ELEMENT *root,null_element;
  48. TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
  49. uint offset_to_key,elements_in_tree,size_of_element;
  50. size_t memory_limit, allocated;
  51. qsort_cmp2 compare;
  52. void *custom_arg;
  53. MEM_ROOT mem_root;
  54. my_bool with_delete;
  55. tree_element_free free;
  56. myf my_flags;
  57. uint flag;
  58. } TREE;
  59. /* Functions on whole tree */
  60. void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit,
  61. int size, qsort_cmp2 compare,
  62. tree_element_free free_element, void *custom_arg,
  63. myf my_flags);
  64. void delete_tree(TREE*);
  65. void reset_tree(TREE*);
  66. /* similar to delete tree, except we do not my_free() blocks in mem_root */
  67. #define is_tree_inited(tree) ((tree)->root != 0)
  68. /* Functions on leafs */
  69. TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint key_size,
  70. void *custom_arg);
  71. void *tree_search(TREE *tree, void *key, void *custom_arg);
  72. int tree_walk(TREE *tree,tree_walk_action action,
  73. void *argument, TREE_WALK visit);
  74. int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg);
  75. void *tree_search_key(TREE *tree, const void *key,
  76. TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
  77. enum ha_rkey_function flag, void *custom_arg);
  78. void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
  79. TREE_ELEMENT ***last_pos, int child_offs);
  80. void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
  81. int r_offs);
  82. ha_rows tree_record_pos(TREE *tree, const void *key,
  83. enum ha_rkey_function search_flag, void *custom_arg);
  84. #define reset_free_element(tree) (tree)->free= 0
  85. #define TREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) + sizeof(void*))
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif