Merged trunk and subtype.
[cacao.git] / src / toolbox / avl.h
1 /* src/toolbox/avl.h - AVL tree implementation
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _AVL_H
27 #define _AVL_H
28
29 #include "config.h"
30
31 #include "vm/types.h"
32
33 #include "threads/mutex.hpp"
34
35 #include "vm/global.h"
36
37
38 /* define direction in an AVL node ********************************************/
39
40 #define AVL_LEFT     0
41 #define AVL_RIGHT    1
42
43
44 /* tree comparator prototype **************************************************/
45
46 typedef s4 avl_comparator(const void *treenode, const void *node);
47
48
49 /* forward typedefs ***********************************************************/
50
51 typedef struct avl_tree_t avl_tree_t;
52 typedef struct avl_node_t avl_node_t;
53
54
55 /* avl_tree_t *****************************************************************/
56
57 struct avl_tree_t {
58         Mutex*          mutex;              ///< Mutex to lock the tree.
59         avl_node_t     *root;               /* pointer to root node               */
60         avl_comparator *comparator;         /* pointer to comparison function     */
61         s4              entries;            /* contains number of entries         */
62 };
63
64
65 /* avl_node_t *****************************************************************/
66
67 struct avl_node_t {
68         void       *data;                   /* pointer to data structure          */
69         s4          balance;                /* the range of the field is -2...2   */
70         avl_node_t *childs[2];              /* pointers to the child nodes        */
71 };
72
73
74 /* function prototypes ********************************************************/
75
76 avl_tree_t *avl_create(avl_comparator *comparator);
77 bool        avl_insert(avl_tree_t *tree, void *data);
78 void       *avl_find(avl_tree_t *tree, void *data);
79
80 #if !defined(NDEBUG)
81 void        avl_dump(avl_node_t* node, s4 indent);
82 #endif
83
84 #endif /* _AVL_H */
85
86
87 /*
88  * These are local overrides for various environment variables in Emacs.
89  * Please do not remove this and leave it at the end of the file, where
90  * Emacs will automagically detect them.
91  * ---------------------------------------------------------------------
92  * Local variables:
93  * mode: c
94  * indent-tabs-mode: t
95  * c-basic-offset: 4
96  * tab-width: 4
97  * End:
98  */