~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/tree.h

  • Committer: Mark Atwood
  • Date: 2011-12-27 16:09:18 UTC
  • mfrom: (2461.2.1 trunk)
  • Revision ID: me@mark.atwood.name-20111227160918-ai7t6ka9s9xlmlbv
resolve and merge lp:~ivolucien/drizzle/trunk-bug-621861

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include <unistd.h>
19
19
 
20
 
#include <drizzled/base.h>              /* get 'enum ha_rkey_function' */
 
20
#include <drizzled/base.h>              // for 'enum ha_rkey_function'
 
21
#include <drizzled/memory/root.h>
21
22
#include <drizzled/qsort_cmp.h>
22
 
#include <drizzled/memory/root.h>
23
23
 
24
24
namespace drizzled
25
25
{
26
26
 
27
 
/* Worst case tree is half full. This gives use 2^(MAX_TREE_HEIGHT/2) leafs */
 
27
// Worst case tree is half full. This gives us 2^(MAX_TREE_HEIGHT/2) leaves
28
28
static const int MAX_TREE_HEIGHT= 64;
29
29
 
30
30
static const int TREE_NO_DUPS= 1;
35
35
typedef enum { free_init, free_free, free_end } TREE_FREE;
36
36
typedef void (*tree_element_free)(void*, TREE_FREE, void *);
37
37
 
38
 
typedef struct st_tree_element {
39
 
  struct st_tree_element *left,*right;
40
 
  uint32_t count:31,
41
 
         colour:1;                      /* black is marked as 1 */
42
 
} TREE_ELEMENT;
43
 
 
44
 
static const int TREE_ELEMENT_EXTRA_SIZE= (sizeof(TREE_ELEMENT) + sizeof(void*));
45
 
 
46
 
 
47
 
typedef struct st_tree {
48
 
  TREE_ELEMENT *root,null_element;
49
 
  TREE_ELEMENT **parents[MAX_TREE_HEIGHT];
50
 
  uint32_t offset_to_key, elements_in_tree, size_of_element;
51
 
  size_t memory_limit;
52
 
  size_t allocated;
53
 
  qsort_cmp2 compare;
54
 
  void *custom_arg;
55
 
  memory::Root mem_root;
56
 
  bool with_delete;
57
 
  tree_element_free free;
58
 
  uint32_t flag;
59
 
} TREE;
60
 
 
61
 
/* Functions on whole tree */
62
 
void init_tree(TREE *tree, size_t default_alloc_size, uint32_t memory_limit,
63
 
               uint32_t size, qsort_cmp2 compare, bool with_delete,
64
 
               tree_element_free free_element, void *custom_arg);
65
 
void delete_tree(TREE*);
66
 
void reset_tree(TREE*);
67
 
 
68
 
/* 
69
 
  similar to delete tree, except we do not free() blocks in mem_root
70
 
*/
71
 
#define is_tree_inited(tree) ((tree)->root != 0)
72
 
 
73
 
/* Functions on leafs */
74
 
TREE_ELEMENT *tree_insert(TREE *tree,void *key, uint32_t key_size,
75
 
                          void *custom_arg);
76
 
int tree_walk(TREE *tree,tree_walk_action action,
77
 
              void *argument, TREE_WALK visit);
78
 
int tree_delete(TREE *tree, void *key, uint32_t key_size, void *custom_arg);
79
 
void *tree_search_key(TREE *tree, const void *key,
80
 
                      TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
81
 
                      enum ha_rkey_function flag, void *custom_arg);
82
 
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
83
 
                        TREE_ELEMENT ***last_pos, int child_offs);
84
 
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
85
 
                       int r_offs);
86
 
ha_rows tree_record_pos(TREE *tree, const void *key,
87
 
                        enum ha_rkey_function search_flag, void *custom_arg);
 
38
 
 
39
class Tree_Element
 
40
{
 
41
public:
 
42
        Tree_Element *left,*right;
 
43
        uint32_t count:31,
 
44
                         colour:1;                      /* black is marked as 1 */
 
45
};
 
46
 
 
47
static const int TREE_ELEMENT_EXTRA_SIZE= (sizeof(Tree_Element) + sizeof(void*));
 
48
 
 
49
/**
 
50
 * red-black binary tree class
 
51
 *
 
52
 * NOTE: unused search code removed 11/2011
 
53
 */
 
54
class Tree
 
55
{
 
56
private:
 
57
        Tree_Element *root, null_element;
 
58
        void *custom_arg;
 
59
        Tree_Element **parents[MAX_TREE_HEIGHT];
 
60
        uint32_t offset_to_key, elements_in_tree, size_of_element;
 
61
        size_t memory_limit;
 
62
        size_t allocated;
 
63
        qsort_cmp2 compare;
 
64
        memory::Root mem_root;
 
65
        bool with_delete;
 
66
        tree_element_free free;
 
67
        uint32_t flag;
 
68
 
 
69
public:
 
70
        void* getCustomArg() {
 
71
                return custom_arg;
 
72
        }
 
73
        Tree_Element* getRoot() {
 
74
                return root;
 
75
        }
 
76
        void setRoot(Tree_Element* root_arg) {
 
77
                root = root_arg;
 
78
        }
 
79
        uint32_t getElementsInTree() {
 
80
                return elements_in_tree;
 
81
        }
 
82
        // tree methods
 
83
        void init_tree(size_t default_alloc_size, uint32_t memory_limit,
 
84
                                   uint32_t size, qsort_cmp2 compare, bool with_delete,
 
85
                                   tree_element_free free_element, void *custom_arg);
 
86
        bool is_inited()
 
87
        {
 
88
                return this->root != 0;
 
89
        }
 
90
        void delete_tree();
 
91
        void reset_tree();
 
92
 
 
93
        // element methods
 
94
        Tree_Element *tree_insert(void *key, uint32_t key_size, void *custom_arg);
 
95
        int tree_walk(tree_walk_action action, void *argument, TREE_WALK visit);
 
96
 
 
97
private:
 
98
        void free_tree(myf free_flags);
 
99
 
 
100
        void* element_key(Tree_Element* element);
 
101
        void delete_tree_element(Tree_Element* element);
 
102
        int tree_walk_left_root_right(Tree_Element* element, tree_walk_action action, void* argument);
 
103
        int tree_walk_right_root_left(Tree_Element* element, tree_walk_action action, void* argument);
 
104
 
 
105
        void left_rotate(Tree_Element **parent,Tree_Element *element);
 
106
        void right_rotate(Tree_Element **parent, Tree_Element *element);
 
107
        void rb_insert(Tree_Element ***parent, Tree_Element *element);
 
108
};
88
109
 
89
110
} /* namespace drizzled */
90
111