OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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 */
45typedef 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 */
50typedef void (*avl_freeitem_t)(void *);
51
52typedef struct avl_node_t {
58 void *item;
59#ifdef AVL_COUNT
60 unsigned int count;
61#endif
62#ifdef AVL_DEPTH
63 unsigned char depth;
64#endif
66
67typedef struct 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) */
90extern 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) */
95extern 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) */
100extern 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) */
104extern 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) */
110extern 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) */
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) */
139extern 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) */
145extern 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) */
151extern 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) */
167extern 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) */
172extern 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) */
177extern 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) */
182extern 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) */
186extern unsigned int avl_index(const avl_node_t *);
187#endif
188
189#endif
void avl_free_tree(avl_tree_t *)
Definition: avl.cpp:218
avl_node_t * avl_insert(avl_tree_t *, void *item)
Definition: avl.cpp:321
avl_node_t * avl_search(const avl_tree_t *, const void *item)
Definition: avl.cpp:173
void(* avl_freeitem_t)(void *)
Definition: avl.h:50
int(* avl_compare_t)(const void *, const void *)
Definition: avl.h:45
unsigned int avl_count(const avl_tree_t *)
Definition: avl.cpp:93
avl_node_t * avl_at(const avl_tree_t *, unsigned int)
Definition: avl.cpp:97
struct avl_tree_t avl_tree_t
avl_node_t * avl_insert_after(avl_tree_t *, avl_node_t *old, avl_node_t *n)
Definition: avl.cpp:274
avl_tree_t * avl_alloc_tree(avl_compare_t, avl_freeitem_t)
Definition: avl.cpp:189
void * avl_delete(avl_tree_t *, const void *item)
Definition: avl.cpp:406
struct avl_node_t avl_node_t
unsigned int avl_index(const avl_node_t *)
Definition: avl.cpp:118
avl_node_t * avl_fixup_node(avl_tree_t *, avl_node_t *n)
Definition: avl.cpp:410
void avl_free_nodes(avl_tree_t *)
Definition: avl.cpp:197
void avl_clear_tree(avl_tree_t *)
Definition: avl.cpp:193
avl_tree_t * avl_init_tree(avl_tree_t *avltree, avl_compare_t, avl_freeitem_t)
Definition: avl.cpp:178
avl_node_t * avl_insert_top(avl_tree_t *, avl_node_t *avlnode)
Definition: avl.cpp:241
void avl_unlink_node(avl_tree_t *, avl_node_t *)
Definition: avl.cpp:340
int avl_search_closest(const avl_tree_t *, const void *item, avl_node_t **avlnode)
Definition: avl.cpp:134
avl_node_t * avl_init_node(avl_node_t *avlnode, void *item)
Definition: avl.cpp:233
void * avl_delete_node(avl_tree_t *, avl_node_t *)
Definition: avl.cpp:394
avl_node_t * avl_insert_node(avl_tree_t *, avl_node_t *)
Definition: avl.cpp:300
avl_node_t * avl_insert_before(avl_tree_t *, avl_node_t *old, avl_node_t *n)
Definition: avl.cpp:248
Definition: avl.h:52
struct avl_node_t * left
Definition: avl.h:56
struct avl_node_t * parent
Definition: avl.h:55
struct avl_node_t * prev
Definition: avl.h:54
struct avl_node_t * next
Definition: avl.h:53
struct avl_node_t * right
Definition: avl.h:57
unsigned int count
Definition: avl.h:60
void * item
Definition: avl.h:58
unsigned char depth
Definition: avl.h:63
Definition: avl.h:67
avl_node_t * tail
Definition: avl.h:69
avl_node_t * top
Definition: avl.h:70
avl_compare_t cmp
Definition: avl.h:71
avl_freeitem_t freeitem
Definition: avl.h:72
avl_node_t * head
Definition: avl.h:68