~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
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 as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#ifndef _tree_h
17
#define _tree_h
18
#ifdef	__cplusplus
19
extern "C" {
20
#endif
21
212.5.39 by Monty Taylor
Phew. Moved my_base and my_global.
22
#include <drizzled/base.h>		/* get 'enum ha_rkey_function' */
612.2.13 by Monty Taylor
Work on removing global.h from headers that should be installed.
23
#include <mysys/my_sys.h>
1 by brian
clean slate
24
25
/* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
26
#define MAX_TREE_HEIGHT	64
27
28
#define ELEMENT_KEY(tree,element)\
481 by Brian Aker
Remove all of uchar.
29
(tree->offset_to_key ? (void*)((unsigned char*) element+tree->offset_to_key) :\
1 by brian
clean slate
30
			*((void**) (element+1)))
31
481 by Brian Aker
Remove all of uchar.
32
#define tree_set_pointer(element,ptr) *((unsigned char **) (element+1))=((unsigned char*) (ptr))
1 by brian
clean slate
33
34
#define TREE_NO_DUPS 1
35
36
typedef enum { left_root_right, right_root_left } TREE_WALK;
205 by Brian Aker
uint32 -> uin32_t
37
typedef uint32_t element_count;
1 by brian
clean slate
38
typedef int (*tree_walk_action)(void *,element_count,void *);
39
40
typedef enum { free_init, free_free, free_end } TREE_FREE;
41
typedef void (*tree_element_free)(void*, TREE_FREE, void *);
42
43
typedef struct st_tree_element {
44
  struct st_tree_element *left,*right;
205 by Brian Aker
uint32 -> uin32_t
45
  uint32_t count:31,
1 by brian
clean slate
46
	 colour:1;			/* black is marked as 1 */
47
} TREE_ELEMENT;
48
49
#define ELEMENT_CHILD(element, offs) (*(TREE_ELEMENT**)((char*)element + offs))
50
51
typedef struct st_tree {
52
  TREE_ELEMENT *root,null_element;
53
  TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
1164 by Brian Aker
Small cleanup.
54
  uint32_t offset_to_key, elements_in_tree, size_of_element;
55
  size_t memory_limit;
56
  size_t allocated;
1 by brian
clean slate
57
  qsort_cmp2 compare;
58
  void *custom_arg;
59
  MEM_ROOT mem_root;
146 by Brian Aker
my_bool cleanup.
60
  bool with_delete;
1 by brian
clean slate
61
  tree_element_free free;
482 by Brian Aker
Remove uint.
62
  uint32_t flag;
1 by brian
clean slate
63
} TREE;
64
1164 by Brian Aker
Small cleanup.
65
/* Functions on whole tree */
298 by Brian Aker
ulong conversion.
66
void init_tree(TREE *tree, uint32_t default_alloc_size, uint32_t memory_limit,
1164 by Brian Aker
Small cleanup.
67
               uint32_t size, qsort_cmp2 compare, bool with_delete,
1 by brian
clean slate
68
	       tree_element_free free_element, void *custom_arg);
69
void delete_tree(TREE*);
70
void reset_tree(TREE*);
1164 by Brian Aker
Small cleanup.
71
72
/* 
73
  similar to delete tree, except we do not free() blocks in mem_root
74
*/
1 by brian
clean slate
75
#define is_tree_inited(tree) ((tree)->root != 0)
76
1164 by Brian Aker
Small cleanup.
77
/* Functions on leafs */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
78
TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint32_t key_size,
1 by brian
clean slate
79
                          void *custom_arg);
80
void *tree_search(TREE *tree, void *key, void *custom_arg);
81
int tree_walk(TREE *tree,tree_walk_action action,
82
	      void *argument, TREE_WALK visit);
482 by Brian Aker
Remove uint.
83
int tree_delete(TREE *tree, void *key, uint32_t key_size, void *custom_arg);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
84
void *tree_search_key(TREE *tree, const void *key,
1 by brian
clean slate
85
                      TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
86
                      enum ha_rkey_function flag, void *custom_arg);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
87
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
1 by brian
clean slate
88
                        TREE_ELEMENT ***last_pos, int child_offs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
89
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
1 by brian
clean slate
90
                       int r_offs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
91
ha_rows tree_record_pos(TREE *tree, const void *key,
1164 by Brian Aker
Small cleanup.
92
                        enum ha_rkey_function search_flag, void *custom_arg);
1 by brian
clean slate
93
94
#define TREE_ELEMENT_EXTRA_SIZE (sizeof(TREE_ELEMENT) + sizeof(void*))
95
96
#ifdef	__cplusplus
97
}
98
#endif
99
#endif