acl  3.5.3.0
acl_avl.h
浏览该文件的文档.
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef ACL_AVL_H
27 #define ACL_AVL_H
28 
29 /*
30  * This is a private header file. Applications should not directly include
31  * this file.
32  */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include "acl_define.h"
39 #include <sys/types.h>
40 #include "avl_impl.h"
41 
42 /*
43  * This is a generic implemenatation of AVL trees for use in the Solaris kernel.
44  * The interfaces provide an efficient way of implementing an ordered set of
45  * data structures.
46  *
47  * AVL trees provide an alternative to using an ordered linked list. Using AVL
48  * trees will usually be faster, however they requires more storage. An ordered
49  * linked list in general requires 2 pointers in each data structure. The
50  * AVL tree implementation uses 3 pointers. The following chart gives the
51  * approximate performance of operations with the different approaches:
52  *
53  * Operation Link List AVL tree
54  * --------- -------- --------
55  * lookup O(n) O(log(n))
56  *
57  * insert 1 node constant constant
58  *
59  * delete 1 node constant between constant and O(log(n))
60  *
61  * delete all nodes O(n) O(n)
62  *
63  * visit the next
64  * or prev node constant between constant and O(log(n))
65  *
66  *
67  * The data structure nodes are anchored at an "avl_tree_t" (the equivalent
68  * of a list header) and the individual nodes will have a field of
69  * type "avl_node_t" (corresponding to list pointers).
70  *
71  * The type "avl_index_t" is used to indicate a position in the list for
72  * certain calls.
73  *
74  * The usage scenario is generally:
75  *
76  * 1. Create the list/tree with: avl_create()
77  *
78  * followed by any mixture of:
79  *
80  * 2a. Insert nodes with: avl_add(), or avl_find() and avl_insert()
81  *
82  * 2b. Visited elements with:
83  * avl_first() - returns the lowest valued node
84  * avl_last() - returns the highest valued node
85  * AVL_NEXT() - given a node go to next higher one
86  * AVL_PREV() - given a node go to previous lower one
87  *
88  * 2c. Find the node with the closest value either less than or greater
89  * than a given value with avl_nearest().
90  *
91  * 2d. Remove individual nodes from the list/tree with avl_remove().
92  *
93  * and finally when the list is being destroyed
94  *
95  * 3. Use avl_destroy_nodes() to quickly process/free up any remaining nodes.
96  * Note that once you use avl_destroy_nodes(), you can no longer
97  * use any routine except avl_destroy_nodes() and avl_destoy().
98  *
99  * 4. Use avl_destroy() to destroy the AVL tree itself.
100  *
101  * Any locking for multiple thread access is up to the user to provide, just
102  * as is needed for any linked list implementation.
103  */
104 
105 /*
106  * Type used for the root of the AVL tree.
107  */
108 typedef struct avl_tree avl_tree_t;
109 
110 /*
111  * The data nodes in the AVL tree must have a field of this type.
112  */
113 typedef struct avl_node avl_node_t;
114 
115 /*
116  * An opaque type used to locate a position in the tree where a node
117  * would be inserted.
118  */
119 #ifdef MS_VC6
120 typedef unsigned int uintptr_t;
121 typedef uintptr_t avl_index_t;
122 #elif defined(BORLAND_CB)
123 typedef unsigned int uintptr_t;
124 typedef uintptr_t avl_index_t;
125 #else
127 #endif
128 
129 /*
130  * Direction constants used for avl_nearest().
131  */
132 #define AVL_BEFORE (0)
133 #define AVL_AFTER (1)
134 
135 
136 /*
137  * Prototypes
138  *
139  * Where not otherwise mentioned, "void *" arguments are a pointer to the
140  * user data structure which must contain a field of type avl_node_t.
141  *
142  * Also assume the user data structures looks like:
143  * stuct my_type {
144  * ...
145  * avl_node_t my_link;
146  * ...
147  * };
148  */
149 
150 /*
151  * Initialize an AVL tree. Arguments are:
152  *
153  * tree - the tree to be initialized
154  * compar - function to compare two nodes, it must return exactly: -1, 0, or +1
155  * -1 for <, 0 for ==, and +1 for >
156  * size - the value of sizeof(struct my_type)
157  * offset - the value of OFFSETOF(struct my_type, my_link)
158  */
159 ACL_API void avl_create(avl_tree_t *tree,
160  int (*compar) (const void *, const void *), size_t size, size_t offset);
161 
162 
163 /*
164  * Find a node with a matching value in the tree. Returns the matching node
165  * found. If not found, it returns NULL and then if "where" is not NULL it sets
166  * "where" for use with avl_insert() or avl_nearest().
167  *
168  * node - node that has the value being looked for
169  * where - position for use with avl_nearest() or avl_insert(), may be NULL
170  */
171 ACL_API void *avl_find(avl_tree_t *tree, void *node, avl_index_t *where);
172 
173 /*
174  * Insert a node into the tree.
175  *
176  * node - the node to insert
177  * where - position as returned from avl_find()
178  */
179 ACL_API void avl_insert(avl_tree_t *tree, void *node, avl_index_t where);
180 
181 /*
182  * Insert "new_data" in "tree" in the given "direction" either after
183  * or before the data "here".
184  *
185  * This might be usefull for avl clients caching recently accessed
186  * data to avoid doing avl_find() again for insertion.
187  *
188  * new_data - new data to insert
189  * here - existing node in "tree"
190  * direction - either AVL_AFTER or AVL_BEFORE the data "here".
191  */
192 ACL_API void avl_insert_here(avl_tree_t *tree, void *new_data, void *here,
193  int direction);
194 
195 
196 /*
197  * Return the first or last valued node in the tree. Will return NULL
198  * if the tree is empty.
199  *
200  */
201 ACL_API void *avl_first(avl_tree_t *tree);
202 ACL_API void *avl_last(avl_tree_t *tree);
203 
204 
205 /*
206  * Return the next or previous valued node in the tree.
207  * AVL_NEXT() will return NULL if at the last node.
208  * AVL_PREV() will return NULL if at the first node.
209  *
210  * node - the node from which the next or previous node is found
211  */
212 #define AVL_NEXT(tree, node) avl_walk(tree, node, AVL_AFTER)
213 #define AVL_PREV(tree, node) avl_walk(tree, node, AVL_BEFORE)
214 
215 
216 /*
217  * Find the node with the nearest value either greater or less than
218  * the value from a previous avl_find(). Returns the node or NULL if
219  * there isn't a matching one.
220  *
221  * where - position as returned from avl_find()
222  * direction - either AVL_BEFORE or AVL_AFTER
223  *
224  * EXAMPLE get the greatest node that is less than a given value:
225  *
226  * avl_tree_t *tree;
227  * struct my_data look_for_value = {....};
228  * struct my_data *node;
229  * struct my_data *less;
230  * avl_index_t where;
231  *
232  * node = avl_find(tree, &look_for_value, &where);
233  * if (node != NULL)
234  * less = AVL_PREV(tree, node);
235  * else
236  * less = avl_nearest(tree, where, AVL_BEFORE);
237  */
238 ACL_API void *avl_nearest(avl_tree_t *tree, avl_index_t where, int direction);
239 
240 
241 /*
242  * Add a single node to the tree.
243  * The node must not be in the tree, and it must not
244  * compare equal to any other node already in the tree.
245  *
246  * node - the node to add
247  */
248 ACL_API void avl_add(avl_tree_t *tree, void *node);
249 
250 
251 /*
252  * Remove a single node from the tree. The node must be in the tree.
253  *
254  * node - the node to remove
255  */
256 ACL_API void avl_remove(avl_tree_t *tree, void *node);
257 
258 /*
259  * Reinsert a node only if its order has changed relative to its nearest
260  * neighbors. To optimize performance avl_update_lt() checks only the previous
261  * node and avl_update_gt() checks only the next node. Use avl_update_lt() and
262  * avl_update_gt() only if you know the direction in which the order of the
263  * node may change.
264  */
265 ACL_API acl_boolean_t avl_update(avl_tree_t *, void *);
266 ACL_API acl_boolean_t avl_update_lt(avl_tree_t *, void *);
267 ACL_API acl_boolean_t avl_update_gt(avl_tree_t *, void *);
268 
269 /*
270  * Return the number of nodes in the tree
271  */
272 ACL_API ulong_t avl_numnodes(avl_tree_t *tree);
273 
274 /*
275  * Return B_TRUE if there are zero nodes in the tree, B_FALSE otherwise.
276  */
277 ACL_API acl_boolean_t avl_is_empty(avl_tree_t *tree);
278 
279 /*
280  * Used to destroy any remaining nodes in a tree. The cookie argument should
281  * be initialized to NULL before the first call. Returns a node that has been
282  * removed from the tree and may be free()'d. Returns NULL when the tree is
283  * empty.
284  *
285  * Once you call avl_destroy_nodes(), you can only continuing calling it and
286  * finally avl_destroy(). No other AVL routines will be valid.
287  *
288  * cookie - a "void *" used to save state between calls to avl_destroy_nodes()
289  *
290  * EXAMPLE:
291  * avl_tree_t *tree;
292  * struct my_data *node;
293  * void *cookie;
294  *
295  * cookie = NULL;
296  * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
297  * free(node);
298  * avl_destroy(tree);
299  */
300 ACL_API void *avl_destroy_nodes(avl_tree_t *tree, void **cookie);
301 
302 
303 /*
304  * Final destroy of an AVL tree. Arguments are:
305  *
306  * tree - the empty tree to destroy
307  */
308 ACL_API void avl_destroy(avl_tree_t *tree);
309 
310 #ifdef __cplusplus
311 }
312 #endif
313 
314 #endif /* _AVL_H */
uintptr_t avl_index_t
Definition: acl_avl.h:126
ACL_API void * avl_nearest(avl_tree_t *tree, avl_index_t where, int direction)
ACL_API acl_boolean_t avl_update_lt(avl_tree_t *, void *)
ACL_API void * avl_last(avl_tree_t *tree)
ACL_API acl_boolean_t avl_update_gt(avl_tree_t *, void *)
unsigned int ulong_t
Definition: avl_impl.h:59
ACL_API void avl_destroy(avl_tree_t *tree)
ACL_API void avl_create(avl_tree_t *tree, int(*compar)(const void *, const void *), size_t size, size_t offset)
ACL_API void avl_insert(avl_tree_t *tree, void *node, avl_index_t where)
ACL_API void * avl_find(avl_tree_t *tree, void *node, avl_index_t *where)
ACL_API acl_boolean_t avl_is_empty(avl_tree_t *tree)
char acl_boolean_t
Definition: avl_impl.h:61
ACL_API ulong_t avl_numnodes(avl_tree_t *tree)
ACL_API void avl_add(avl_tree_t *tree, void *node)
unsigned int uintptr_t
Definition: avl_impl.h:54
ACL_API void * avl_first(avl_tree_t *tree)
ACL_API void avl_remove(avl_tree_t *tree, void *node)
ACL_API acl_boolean_t avl_update(avl_tree_t *, void *)
ACL_API void avl_insert_here(avl_tree_t *tree, void *new_data, void *here, int direction)
ACL_API void * avl_destroy_nodes(avl_tree_t *tree, void **cookie)