my_tree.h 3.43 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
9 10 11 12 13 14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
unknown's avatar
unknown committed
15 16 17 18 19 20 21

#ifndef _tree_h
#define _tree_h
#ifdef	__cplusplus
extern "C" {
#endif

22 23
#include "my_base.h"		/* get 'enum ha_rkey_function' */

unknown's avatar
unknown committed
24 25
/* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
#define MAX_TREE_HEIGHT	64
26

unknown's avatar
unknown committed
27
#define ELEMENT_KEY(tree,element)\
28
(tree->offset_to_key ? (void*)((uchar*) element+tree->offset_to_key) :\
unknown's avatar
unknown committed
29 30
			*((void**) (element+1)))

31
#define tree_set_pointer(element,ptr) *((uchar **) (element+1))=((uchar*) (ptr))
unknown's avatar
unknown committed
32

33
#define TREE_NO_DUPS 1
34
#define TREE_ONLY_DUPS 2
35

unknown's avatar
unknown committed
36 37 38 39
typedef enum { left_root_right, right_root_left } TREE_WALK;
typedef uint32 element_count;
typedef int (*tree_walk_action)(void *,element_count,void *);

unknown's avatar
unknown committed
40 41 42
typedef enum { free_init, free_free, free_end } TREE_FREE;
typedef void (*tree_element_free)(void*, TREE_FREE, void *);

unknown's avatar
unknown committed
43 44 45 46 47 48
typedef struct st_tree_element {
  struct st_tree_element *left,*right;
  uint32 count:31,
	 colour:1;			/* black is marked as 1 */
} TREE_ELEMENT;

49 50
#define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))

unknown's avatar
unknown committed
51 52
typedef struct st_tree {
  TREE_ELEMENT *root,null_element;
53
  TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
54
  uint offset_to_key,elements_in_tree,size_of_element;
55
  size_t memory_limit, allocated;
56
  qsort_cmp2 compare;
57
  void *custom_arg;
unknown's avatar
unknown committed
58 59
  MEM_ROOT mem_root;
  my_bool with_delete;
unknown's avatar
unknown committed
60
  tree_element_free free;
61
  uint flag;
unknown's avatar
unknown committed
62 63
} TREE;

64
	/* Functions on whole tree */
65
void init_tree(TREE *tree, size_t default_alloc_size, size_t memory_limit,
unknown's avatar
unknown committed
66 67
               int size, qsort_cmp2 compare, my_bool with_delete,
	       tree_element_free free_element, void *custom_arg);
unknown's avatar
unknown committed
68
void delete_tree(TREE*);
69
void reset_tree(TREE*);
70 71

  /* similar to delete tree, except we do not my_free() blocks in mem_root */
unknown's avatar
unknown committed
72
#define is_tree_inited(tree) ((tree)->root != 0)
unknown's avatar
unknown committed
73 74

	/* Functions on leafs */
75 76 77
TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint key_size, 
                          void *custom_arg);
void *tree_search(TREE *tree, void *key, void *custom_arg);
unknown's avatar
unknown committed
78 79
int tree_walk(TREE *tree,tree_walk_action action,
	      void *argument, TREE_WALK visit);
80
int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg);
81 82 83 84 85 86 87
void *tree_search_key(TREE *tree, const void *key, 
                      TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
                      enum ha_rkey_function flag, void *custom_arg);
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents, 
                        TREE_ELEMENT ***last_pos, int child_offs);
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs, 
                       int r_offs);
88
ha_rows tree_record_pos(TREE *tree, const void *key, 
89
                     enum ha_rkey_function search_flag, void *custom_arg);
90
#define reset_free_element(tree) (tree)->free= 0
unknown's avatar
unknown committed
91

92 93
#define TREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) + sizeof(void*))

unknown's avatar
unknown committed
94 95 96 97
#ifdef	__cplusplus
}
#endif
#endif