my_tree.h 3.45 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
unknown's avatar
unknown committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
10 11 12 13 14 15
   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
16 17 18 19 20 21 22

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

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

25
#define MAX_TREE_HEIGHT	40	/* = max 1048576 leafs in tree */
unknown's avatar
unknown committed
26 27 28 29 30 31
#define ELEMENT_KEY(tree,element)\
(tree->offset_to_key ? (void*)((byte*) element+tree->offset_to_key) :\
			*((void**) (element+1)))

#define tree_set_pointer(element,ptr) *((byte **) (element+1))=((byte*) (ptr))

32 33
#define TREE_NO_DUPS 1

unknown's avatar
unknown committed
34 35 36 37
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
38 39 40
typedef enum { free_init, free_free, free_end } TREE_FREE;
typedef void (*tree_element_free)(void*, TREE_FREE, void *);

unknown's avatar
unknown committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54
#ifdef MSDOS
typedef struct st_tree_element {
  struct st_tree_element *left,*right;
  unsigned long count;
  uchar    colour;			/* black is marked as 1 */
} TREE_ELEMENT;
#else
typedef struct st_tree_element {
  struct st_tree_element *left,*right;
  uint32 count:31,
	 colour:1;			/* black is marked as 1 */
} TREE_ELEMENT;
#endif /* MSDOS */

55 56
#define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))

unknown's avatar
unknown committed
57 58
typedef struct st_tree {
  TREE_ELEMENT *root,null_element;
59
  TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
unknown's avatar
unknown committed
60
  uint offset_to_key,elements_in_tree,size_of_element,memory_limit,allocated;
61
  qsort_cmp2 compare;
62
  void *custom_arg;
unknown's avatar
unknown committed
63 64
  MEM_ROOT mem_root;
  my_bool with_delete;
unknown's avatar
unknown committed
65
  tree_element_free free;
66
  uint flag;
unknown's avatar
unknown committed
67 68
} TREE;

69
	/* Functions on whole tree */
unknown's avatar
unknown committed
70 71 72
void init_tree(TREE *tree, uint default_alloc_size, uint memory_limit,
               int size, qsort_cmp2 compare, my_bool with_delete,
	       tree_element_free free_element, void *custom_arg);
unknown's avatar
unknown committed
73
void delete_tree(TREE*);
74 75 76
void reset_tree(TREE*);
  /* similar to delete tree, except we do not my_free() blocks in mem_root
   */
unknown's avatar
unknown committed
77
#define is_tree_inited(tree) ((tree)->root != 0)
unknown's avatar
unknown committed
78 79

	/* Functions on leafs */
80 81 82
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
83 84
int tree_walk(TREE *tree,tree_walk_action action,
	      void *argument, TREE_WALK visit);
85
int tree_delete(TREE *tree, void *key, void *custom_arg);
unknown's avatar
unknown committed
86

87 88 89 90 91 92 93 94 95
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);
uint tree_record_pos(TREE *tree, const void *key, 
                     enum ha_rkey_function search_flag, void *custom_arg);
unknown's avatar
unknown committed
96 97 98 99
#ifdef	__cplusplus
}
#endif
#endif