my_tree.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #ifndef _tree_h
  18. #define _tree_h
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define MAX_TREE_HIGHT 40 /* = max 1048576 leafs in tree */
  23. #define ELEMENT_KEY(tree,element)
  24. (tree->offset_to_key ? (void*)((byte*) element+tree->offset_to_key) :
  25. *((void**) (element+1)))
  26. #define tree_set_pointer(element,ptr) *((byte **) (element+1))=((byte*) (ptr))
  27. typedef enum { left_root_right, right_root_left } TREE_WALK;
  28. typedef uint32 element_count;
  29. typedef int (*tree_walk_action)(void *,element_count,void *);
  30. #ifdef MSDOS
  31. typedef struct st_tree_element {
  32.   struct st_tree_element *left,*right;
  33.   unsigned long count;
  34.   uchar    colour; /* black is marked as 1 */
  35. } TREE_ELEMENT;
  36. #else
  37. typedef struct st_tree_element {
  38.   struct st_tree_element *left,*right;
  39.   uint32 count:31,
  40.  colour:1; /* black is marked as 1 */
  41. } TREE_ELEMENT;
  42. #endif /* MSDOS */
  43. typedef struct st_tree {
  44.   TREE_ELEMENT *root,null_element;
  45.   TREE_ELEMENT **parents[MAX_TREE_HIGHT];
  46.   uint offset_to_key,elements_in_tree,size_of_element;
  47.   qsort_cmp compare;
  48.   MEM_ROOT mem_root;
  49.   my_bool with_delete;
  50.   void (*free)(void *);
  51. } TREE;
  52. /* Functions on hole tree */
  53. void init_tree(TREE *tree,uint default_alloc_size, int element_size,
  54.        qsort_cmp compare, my_bool with_delete,
  55.        void (*free_element)(void*));
  56. void delete_tree(TREE*);
  57. #define is_tree_inited(tree) ((tree)->root != 0)
  58. /* Functions on leafs */
  59. TREE_ELEMENT *tree_insert(TREE *tree,void *key,uint key_size);
  60. void *tree_search(TREE *tree,void *key);
  61. int tree_walk(TREE *tree,tree_walk_action action,
  62.       void *argument, TREE_WALK visit);
  63. int tree_delete(TREE *tree,void *key);
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67. #endif