OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
avl.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  avl.h - Source code for the AVL-tree library.
4 
5  Copyright (C) 1998 Michael H. Buselli <cosine@cosine.org>
6  Copyright (C) 2000-2002 Wessel Dankers <wsl@nl.linux.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 
22  Augmented AVL-tree. Original by Michael H. Buselli <cosine@cosine.org>.
23 
24  Modified by Wessel Dankers <wsl@nl.linux.org> to add a bunch of bloat to
25  the sourcecode, change the interface and squash a few bugs.
26  Mail him if you find new bugs.
27 
28 *****************************************************************************/
29 
30 #ifndef _AVL_H
31 #define _AVL_H
32 
33 /* We need either depths, counts or both (the latter being the default) */
34 #if !defined(AVL_DEPTH) && !defined(AVL_COUNT)
35 #define AVL_DEPTH
36 #define AVL_COUNT
37 #endif
38 
39 /* User supplied function to compare two items like strcmp() does.
40  * For example: cmp(a,b) will return:
41  * -1 if a < b
42  * 0 if a = b
43  * 1 if a > b
44  */
45 typedef int (*avl_compare_t)(const void *, const void *);
46 
47 /* User supplied function to delete an item when a node is free()d.
48  * If NULL, the item is not free()d.
49  */
50 typedef void (*avl_freeitem_t)(void *);
51 
52 typedef struct avl_node_t {
53  struct avl_node_t *next;
54  struct avl_node_t *prev;
55  struct avl_node_t *parent;
56  struct avl_node_t *left;
57  struct avl_node_t *right;
58  void *item;
59 #ifdef AVL_COUNT
60  unsigned int count;
61 #endif
62 #ifdef AVL_DEPTH
63  unsigned char depth;
64 #endif
65 } avl_node_t;
66 
67 typedef struct avl_tree_t {
73 } avl_tree_t;
74 
75 /* Initializes a new tree for elements that will be ordered using
76  * the supplied strcmp()-like function.
77  * Returns the value of avltree (even if it's NULL).
78  * O(1) */
80 
81 /* Allocates and initializes a new tree for elements that will be
82  * ordered using the supplied strcmp()-like function.
83  * Returns NULL if memory could not be allocated.
84  * O(1) */
86 
87 /* Frees the entire tree efficiently. Nodes will be free()d.
88  * If the tree's freeitem is not NULL it will be invoked on every item.
89  * O(n) */
90 extern void avl_free_tree(avl_tree_t *);
91 
92 /* Reinitializes the tree structure for reuse. Nothing is free()d.
93  * Compare and freeitem functions are left alone.
94  * O(1) */
95 extern void avl_clear_tree(avl_tree_t *);
96 
97 /* Free()s all nodes in the tree but leaves the tree itself.
98  * If the tree's freeitem is not NULL it will be invoked on every item.
99  * O(n) */
100 extern void avl_free_nodes(avl_tree_t *);
101 
102 /* Initializes memory for use as a node. Returns NULL if avlnode is NULL.
103  * O(1) */
104 extern avl_node_t *avl_init_node(avl_node_t *avlnode, void *item);
105 
106 /* Insert an item into the tree and return the new node.
107  * Returns NULL and sets errno if memory for the new node could not be
108  * allocated or if the node is already in the tree (EEXIST).
109  * O(lg n) */
110 extern avl_node_t *avl_insert(avl_tree_t *, void *item);
111 
112 /* Insert a node into the tree and return it.
113  * Returns NULL if the node is already in the tree.
114  * O(lg n) */
116 
117 /* Insert a node in an empty tree. If avlnode is NULL, the tree will be
118  * cleared and ready for re-use.
119  * If the tree is not empty, the old nodes are left dangling.
120  * O(1) */
121 extern avl_node_t *avl_insert_top(avl_tree_t *, avl_node_t *avlnode);
122 
123 /* Insert a node before another node. Returns the new node.
124  * If old is NULL, the item is appended to the tree.
125  * O(lg n) */
126 //extern avl_node_t *avl_insert_before(avl_tree_t *, avl_node_t *old, avl_node_t *new);
128 
129 /* Insert a node after another node. Returns the new node.
130  * If old is NULL, the item is prepended to the tree.
131  * O(lg n) */
132 //extern avl_node_t *avl_insert_after(avl_tree_t *, avl_node_t *old, avl_node_t *new);
134 
135 /* Deletes a node from the tree. Returns immediately if the node is NULL.
136  * The item will not be free()d regardless of the tree's freeitem handler.
137  * This function comes in handy if you need to update the search key.
138  * O(lg n) */
139 extern void avl_unlink_node(avl_tree_t *, avl_node_t *);
140 
141 /* Deletes a node from the tree. Returns immediately if the node is NULL.
142  * If the tree's freeitem is not NULL, it is invoked on the item.
143  * If it is, returns the item.
144  * O(lg n) */
145 extern void *avl_delete_node(avl_tree_t *, avl_node_t *);
146 
147 /* Searches for an item in the tree and deletes it if found.
148  * If the tree's freeitem is not NULL, it is invoked on the item.
149  * If it is, returns the item.
150  * O(lg n) */
151 extern void *avl_delete(avl_tree_t *, const void *item);
152 
153 /* If exactly one node is moved in memory, this will fix the pointers
154  * in the tree that refer to it. It must be an exact shallow copy.
155  * Returns the pointer to the old position.
156  * O(1) */
157 //extern avl_node_t *avl_fixup_node(avl_tree_t *, avl_node_t *new);
159 
160 /* Searches for a node with the key closest (or equal) to the given item.
161  * If avlnode is not NULL, *avlnode will be set to the node found or NULL
162  * if the tree is empty. Return values:
163  * -1 if the returned node is smaller
164  * 0 if the returned node is equal or if the tree is empty
165  * 1 if the returned node is greater
166  * O(lg n) */
167 extern int avl_search_closest(const avl_tree_t *, const void *item, avl_node_t **avlnode);
168 
169 /* Searches for the item in the tree and returns a matching node if found
170  * or NULL if not.
171  * O(lg n) */
172 extern avl_node_t *avl_search(const avl_tree_t *, const void *item);
173 
174 #ifdef AVL_COUNT
175 /* Returns the number of nodes in the tree.
176  * O(1) */
177 extern unsigned int avl_count(const avl_tree_t *);
178 
179 /* Searches a node by its rank in the list. Counting starts at 0.
180  * Returns NULL if the index exceeds the number of nodes in the tree.
181  * O(lg n) */
182 extern avl_node_t *avl_at(const avl_tree_t *, unsigned int);
183 
184 /* Returns the rank of a node in the list. Counting starts at 0.
185  * O(lg n) */
186 extern unsigned int avl_index(const avl_node_t *);
187 #endif
188 
189 #endif
avl_tree_t * avl_alloc_tree(avl_compare_t cmp, avl_freeitem_t freeitem)
Definition: avl.cpp:189
void * avl_delete(avl_tree_t *avltree, const void *item)
Definition: avl.cpp:406
struct avl_tree_t avl_tree_t
avl_node_t * avl_at(const avl_tree_t *avltree, unsigned int index)
Definition: avl.cpp:97
struct avl_node_t * parent
Definition: avl.h:55
struct avl_node_t * prev
Definition: avl.h:54
unsigned char depth
Definition: avl.h:63
Definition: avl.h:52
avl_node_t * avl_fixup_node(avl_tree_t *avltree, avl_node_t *newnode)
Definition: avl.cpp:410
avl_node_t * avl_insert_before(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode)
Definition: avl.cpp:248
int avl_search_closest(const avl_tree_t *avltree, const void *item, avl_node_t **avlnode)
Definition: avl.cpp:134
void avl_unlink_node(avl_tree_t *avltree, avl_node_t *avlnode)
Definition: avl.cpp:340
avl_node_t * avl_insert_after(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode)
Definition: avl.cpp:274
int(* avl_compare_t)(const void *, const void *)
Definition: avl.h:45
avl_node_t * tail
Definition: avl.h:69
struct avl_node_t avl_node_t
avl_node_t * avl_insert(avl_tree_t *avltree, void *item)
Definition: avl.cpp:321
void avl_free_nodes(avl_tree_t *avltree)
Definition: avl.cpp:197
unsigned int avl_index(const avl_node_t *avlnode)
Definition: avl.cpp:118
avl_freeitem_t freeitem
Definition: avl.h:72
unsigned int avl_count(const avl_tree_t *avltree)
Definition: avl.cpp:93
struct avl_node_t * left
Definition: avl.h:56
void(* avl_freeitem_t)(void *)
Definition: avl.h:50
avl_node_t * avl_insert_top(avl_tree_t *avltree, avl_node_t *newnode)
Definition: avl.cpp:241
avl_node_t * avl_search(const avl_tree_t *avltree, const void *item)
Definition: avl.cpp:173
avl_compare_t cmp
Definition: avl.h:71
avl_tree_t * avl_init_tree(avl_tree_t *rc, avl_compare_t cmp, avl_freeitem_t freeitem)
Definition: avl.cpp:178
void * item
Definition: avl.h:58
void avl_free_tree(avl_tree_t *avltree)
Definition: avl.cpp:218
struct avl_node_t * next
Definition: avl.h:53
avl_node_t * avl_insert_node(avl_tree_t *avltree, avl_node_t *newnode)
Definition: avl.cpp:300
avl_node_t * head
Definition: avl.h:68
struct avl_node_t * right
Definition: avl.h:57
void avl_clear_tree(avl_tree_t *avltree)
Definition: avl.cpp:193
unsigned int count
Definition: avl.h:60
void * avl_delete_node(avl_tree_t *avltree, avl_node_t *avlnode)
Definition: avl.cpp:394
avl_node_t * top
Definition: avl.h:70
avl_node_t * avl_init_node(avl_node_t *newnode, void *item)
Definition: avl.cpp:233
Definition: avl.h:67