1*8404SAndrew.W.Wilson@sun.com /* 2*8404SAndrew.W.Wilson@sun.com * CDDL HEADER START 3*8404SAndrew.W.Wilson@sun.com * 4*8404SAndrew.W.Wilson@sun.com * The contents of this file are subject to the terms of the 5*8404SAndrew.W.Wilson@sun.com * Common Development and Distribution License (the "License"). 6*8404SAndrew.W.Wilson@sun.com * You may not use this file except in compliance with the License. 7*8404SAndrew.W.Wilson@sun.com * 8*8404SAndrew.W.Wilson@sun.com * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*8404SAndrew.W.Wilson@sun.com * or http://www.opensolaris.org/os/licensing. 10*8404SAndrew.W.Wilson@sun.com * See the License for the specific language governing permissions 11*8404SAndrew.W.Wilson@sun.com * and limitations under the License. 12*8404SAndrew.W.Wilson@sun.com * 13*8404SAndrew.W.Wilson@sun.com * When distributing Covered Code, include this CDDL HEADER in each 14*8404SAndrew.W.Wilson@sun.com * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*8404SAndrew.W.Wilson@sun.com * If applicable, add the following below this CDDL HEADER, with the 16*8404SAndrew.W.Wilson@sun.com * fields enclosed by brackets "[]" replaced with your own identifying 17*8404SAndrew.W.Wilson@sun.com * information: Portions Copyright [yyyy] [name of copyright owner] 18*8404SAndrew.W.Wilson@sun.com * 19*8404SAndrew.W.Wilson@sun.com * CDDL HEADER END 20*8404SAndrew.W.Wilson@sun.com */ 21*8404SAndrew.W.Wilson@sun.com /* 22*8404SAndrew.W.Wilson@sun.com * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23*8404SAndrew.W.Wilson@sun.com * Use is subject to license terms. 24*8404SAndrew.W.Wilson@sun.com */ 25*8404SAndrew.W.Wilson@sun.com 26*8404SAndrew.W.Wilson@sun.com #ifndef FB_AVL_H 27*8404SAndrew.W.Wilson@sun.com #define FB_AVL_H 28*8404SAndrew.W.Wilson@sun.com 29*8404SAndrew.W.Wilson@sun.com /* 30*8404SAndrew.W.Wilson@sun.com * derived from Solaris' sys/avl.h and sys/avl_impl.h 31*8404SAndrew.W.Wilson@sun.com */ 32*8404SAndrew.W.Wilson@sun.com 33*8404SAndrew.W.Wilson@sun.com #ifdef __cplusplus 34*8404SAndrew.W.Wilson@sun.com extern "C" { 35*8404SAndrew.W.Wilson@sun.com #endif 36*8404SAndrew.W.Wilson@sun.com 37*8404SAndrew.W.Wilson@sun.com #include <sys/types.h> 38*8404SAndrew.W.Wilson@sun.com 39*8404SAndrew.W.Wilson@sun.com /* 40*8404SAndrew.W.Wilson@sun.com * generic AVL tree implementation for FileBench use 41*8404SAndrew.W.Wilson@sun.com * 42*8404SAndrew.W.Wilson@sun.com * The interfaces provide an efficient way of implementing an ordered set of 43*8404SAndrew.W.Wilson@sun.com * data structures. 44*8404SAndrew.W.Wilson@sun.com * 45*8404SAndrew.W.Wilson@sun.com * AVL trees provide an alternative to using an ordered linked list. Using AVL 46*8404SAndrew.W.Wilson@sun.com * trees will usually be faster, however they requires more storage. An ordered 47*8404SAndrew.W.Wilson@sun.com * linked list in general requires 2 pointers in each data structure. The 48*8404SAndrew.W.Wilson@sun.com * AVL tree implementation uses 3 pointers. The following chart gives the 49*8404SAndrew.W.Wilson@sun.com * approximate performance of operations with the different approaches: 50*8404SAndrew.W.Wilson@sun.com * 51*8404SAndrew.W.Wilson@sun.com * Operation Link List AVL tree 52*8404SAndrew.W.Wilson@sun.com * --------- -------- -------- 53*8404SAndrew.W.Wilson@sun.com * lookup O(n) O(log(n)) 54*8404SAndrew.W.Wilson@sun.com * 55*8404SAndrew.W.Wilson@sun.com * insert 1 node constant constant 56*8404SAndrew.W.Wilson@sun.com * 57*8404SAndrew.W.Wilson@sun.com * delete 1 node constant between constant and O(log(n)) 58*8404SAndrew.W.Wilson@sun.com * 59*8404SAndrew.W.Wilson@sun.com * delete all nodes O(n) O(n) 60*8404SAndrew.W.Wilson@sun.com * 61*8404SAndrew.W.Wilson@sun.com * visit the next 62*8404SAndrew.W.Wilson@sun.com * or prev node constant between constant and O(log(n)) 63*8404SAndrew.W.Wilson@sun.com * 64*8404SAndrew.W.Wilson@sun.com * 65*8404SAndrew.W.Wilson@sun.com * There are 5 pieces of information stored for each node in an AVL tree 66*8404SAndrew.W.Wilson@sun.com * 67*8404SAndrew.W.Wilson@sun.com * pointer to less than child 68*8404SAndrew.W.Wilson@sun.com * pointer to greater than child 69*8404SAndrew.W.Wilson@sun.com * a pointer to the parent of this node 70*8404SAndrew.W.Wilson@sun.com * an indication [0/1] of which child I am of my parent 71*8404SAndrew.W.Wilson@sun.com * a "balance" (-1, 0, +1) indicating which child tree is taller 72*8404SAndrew.W.Wilson@sun.com * 73*8404SAndrew.W.Wilson@sun.com * Since they only need 3 bits, the last two fields are packed into the 74*8404SAndrew.W.Wilson@sun.com * bottom bits of the parent pointer on 64 bit machines to save on space. 75*8404SAndrew.W.Wilson@sun.com */ 76*8404SAndrew.W.Wilson@sun.com 77*8404SAndrew.W.Wilson@sun.com #ifndef _LP64 78*8404SAndrew.W.Wilson@sun.com 79*8404SAndrew.W.Wilson@sun.com struct avl_node { 80*8404SAndrew.W.Wilson@sun.com struct avl_node *avl_child[2]; /* left/right children */ 81*8404SAndrew.W.Wilson@sun.com struct avl_node *avl_parent; /* this node's parent */ 82*8404SAndrew.W.Wilson@sun.com unsigned short avl_child_index; /* my index in parent's avl_child[] */ 83*8404SAndrew.W.Wilson@sun.com short avl_balance; /* balance value: -1, 0, +1 */ 84*8404SAndrew.W.Wilson@sun.com }; 85*8404SAndrew.W.Wilson@sun.com 86*8404SAndrew.W.Wilson@sun.com #define AVL_XPARENT(n) ((n)->avl_parent) 87*8404SAndrew.W.Wilson@sun.com #define AVL_SETPARENT(n, p) ((n)->avl_parent = (p)) 88*8404SAndrew.W.Wilson@sun.com 89*8404SAndrew.W.Wilson@sun.com #define AVL_XCHILD(n) ((n)->avl_child_index) 90*8404SAndrew.W.Wilson@sun.com #define AVL_SETCHILD(n, c) ((n)->avl_child_index = (unsigned short)(c)) 91*8404SAndrew.W.Wilson@sun.com 92*8404SAndrew.W.Wilson@sun.com #define AVL_XBALANCE(n) ((n)->avl_balance) 93*8404SAndrew.W.Wilson@sun.com #define AVL_SETBALANCE(n, b) ((n)->avl_balance = (short)(b)) 94*8404SAndrew.W.Wilson@sun.com 95*8404SAndrew.W.Wilson@sun.com #else /* _LP64 */ 96*8404SAndrew.W.Wilson@sun.com 97*8404SAndrew.W.Wilson@sun.com /* 98*8404SAndrew.W.Wilson@sun.com * for 64 bit machines, avl_pcb contains parent pointer, balance and child_index 99*8404SAndrew.W.Wilson@sun.com * values packed in the following manner: 100*8404SAndrew.W.Wilson@sun.com * 101*8404SAndrew.W.Wilson@sun.com * |63 3| 2 |1 0 | 102*8404SAndrew.W.Wilson@sun.com * |-------------------------------------|-----------------|-------------| 103*8404SAndrew.W.Wilson@sun.com * | avl_parent hi order bits | avl_child_index | avl_balance | 104*8404SAndrew.W.Wilson@sun.com * | | | + 1 | 105*8404SAndrew.W.Wilson@sun.com * |-------------------------------------|-----------------|-------------| 106*8404SAndrew.W.Wilson@sun.com * 107*8404SAndrew.W.Wilson@sun.com */ 108*8404SAndrew.W.Wilson@sun.com struct avl_node { 109*8404SAndrew.W.Wilson@sun.com struct avl_node *avl_child[2]; /* left/right children nodes */ 110*8404SAndrew.W.Wilson@sun.com uintptr_t avl_pcb; /* parent, child_index, balance */ 111*8404SAndrew.W.Wilson@sun.com }; 112*8404SAndrew.W.Wilson@sun.com 113*8404SAndrew.W.Wilson@sun.com /* 114*8404SAndrew.W.Wilson@sun.com * macros to extract/set fields in avl_pcb 115*8404SAndrew.W.Wilson@sun.com * 116*8404SAndrew.W.Wilson@sun.com * pointer to the parent of the current node is the high order bits 117*8404SAndrew.W.Wilson@sun.com */ 118*8404SAndrew.W.Wilson@sun.com #define AVL_XPARENT(n) ((struct avl_node *)((n)->avl_pcb & ~7)) 119*8404SAndrew.W.Wilson@sun.com #define AVL_SETPARENT(n, p) \ 120*8404SAndrew.W.Wilson@sun.com ((n)->avl_pcb = (((n)->avl_pcb & 7) | (uintptr_t)(p))) 121*8404SAndrew.W.Wilson@sun.com 122*8404SAndrew.W.Wilson@sun.com /* 123*8404SAndrew.W.Wilson@sun.com * index of this node in its parent's avl_child[]: bit #2 124*8404SAndrew.W.Wilson@sun.com */ 125*8404SAndrew.W.Wilson@sun.com #define AVL_XCHILD(n) (((n)->avl_pcb >> 2) & 1) 126*8404SAndrew.W.Wilson@sun.com #define AVL_SETCHILD(n, c) \ 127*8404SAndrew.W.Wilson@sun.com ((n)->avl_pcb = (uintptr_t)(((n)->avl_pcb & ~4) | ((c) << 2))) 128*8404SAndrew.W.Wilson@sun.com 129*8404SAndrew.W.Wilson@sun.com /* 130*8404SAndrew.W.Wilson@sun.com * balance indication for a node, lowest 2 bits. A valid balance is 131*8404SAndrew.W.Wilson@sun.com * -1, 0, or +1, and is encoded by adding 1 to the value to get the 132*8404SAndrew.W.Wilson@sun.com * unsigned values of 0, 1, 2. 133*8404SAndrew.W.Wilson@sun.com */ 134*8404SAndrew.W.Wilson@sun.com #define AVL_XBALANCE(n) ((int)(((n)->avl_pcb & 3) - 1)) 135*8404SAndrew.W.Wilson@sun.com #define AVL_SETBALANCE(n, b) \ 136*8404SAndrew.W.Wilson@sun.com ((n)->avl_pcb = (uintptr_t)((((n)->avl_pcb & ~3) | ((b) + 1)))) 137*8404SAndrew.W.Wilson@sun.com 138*8404SAndrew.W.Wilson@sun.com #endif /* _LP64 */ 139*8404SAndrew.W.Wilson@sun.com 140*8404SAndrew.W.Wilson@sun.com 141*8404SAndrew.W.Wilson@sun.com 142*8404SAndrew.W.Wilson@sun.com /* 143*8404SAndrew.W.Wilson@sun.com * switch between a node and data pointer for a given tree 144*8404SAndrew.W.Wilson@sun.com * the value of "o" is tree->avl_offset 145*8404SAndrew.W.Wilson@sun.com */ 146*8404SAndrew.W.Wilson@sun.com #define AVL_NODE2DATA(n, o) ((void *)((uintptr_t)(n) - (o))) 147*8404SAndrew.W.Wilson@sun.com #define AVL_DATA2NODE(d, o) ((struct avl_node *)((uintptr_t)(d) + (o))) 148*8404SAndrew.W.Wilson@sun.com 149*8404SAndrew.W.Wilson@sun.com 150*8404SAndrew.W.Wilson@sun.com 151*8404SAndrew.W.Wilson@sun.com /* 152*8404SAndrew.W.Wilson@sun.com * macros used to create/access an avl_index_t 153*8404SAndrew.W.Wilson@sun.com */ 154*8404SAndrew.W.Wilson@sun.com #define AVL_INDEX2NODE(x) ((avl_node_t *)((x) & ~1)) 155*8404SAndrew.W.Wilson@sun.com #define AVL_INDEX2CHILD(x) ((x) & 1) 156*8404SAndrew.W.Wilson@sun.com #define AVL_MKINDEX(n, c) ((avl_index_t)(n) | (c)) 157*8404SAndrew.W.Wilson@sun.com 158*8404SAndrew.W.Wilson@sun.com 159*8404SAndrew.W.Wilson@sun.com /* 160*8404SAndrew.W.Wilson@sun.com * The tree structure. The fields avl_root, avl_compar, and avl_offset come 161*8404SAndrew.W.Wilson@sun.com * first since they are needed for avl_find(). We want them to fit into 162*8404SAndrew.W.Wilson@sun.com * a single 64 byte cache line to make avl_find() as fast as possible. 163*8404SAndrew.W.Wilson@sun.com */ 164*8404SAndrew.W.Wilson@sun.com struct avl_tree { 165*8404SAndrew.W.Wilson@sun.com struct avl_node *avl_root; /* root node in tree */ 166*8404SAndrew.W.Wilson@sun.com int (*avl_compar)(const void *, const void *); 167*8404SAndrew.W.Wilson@sun.com size_t avl_offset; /* offsetof(type, avl_link_t field) */ 168*8404SAndrew.W.Wilson@sun.com ulong_t avl_numnodes; /* number of nodes in the tree */ 169*8404SAndrew.W.Wilson@sun.com size_t avl_size; /* sizeof user type struct */ 170*8404SAndrew.W.Wilson@sun.com }; 171*8404SAndrew.W.Wilson@sun.com 172*8404SAndrew.W.Wilson@sun.com 173*8404SAndrew.W.Wilson@sun.com /* 174*8404SAndrew.W.Wilson@sun.com * This will only by used via AVL_NEXT() or AVL_PREV() 175*8404SAndrew.W.Wilson@sun.com */ 176*8404SAndrew.W.Wilson@sun.com extern void *avl_walk(struct avl_tree *, void *, int); 177*8404SAndrew.W.Wilson@sun.com 178*8404SAndrew.W.Wilson@sun.com 179*8404SAndrew.W.Wilson@sun.com /* 180*8404SAndrew.W.Wilson@sun.com * The data structure nodes are anchored at an "avl_tree_t" (the equivalent 181*8404SAndrew.W.Wilson@sun.com * of a list header) and the individual nodes will have a field of 182*8404SAndrew.W.Wilson@sun.com * type "avl_node_t" (corresponding to list pointers). 183*8404SAndrew.W.Wilson@sun.com * 184*8404SAndrew.W.Wilson@sun.com * The type "avl_index_t" is used to indicate a position in the list for 185*8404SAndrew.W.Wilson@sun.com * certain calls. 186*8404SAndrew.W.Wilson@sun.com * 187*8404SAndrew.W.Wilson@sun.com * The usage scenario is generally: 188*8404SAndrew.W.Wilson@sun.com * 189*8404SAndrew.W.Wilson@sun.com * 1. Create the list/tree with: avl_create() 190*8404SAndrew.W.Wilson@sun.com * 191*8404SAndrew.W.Wilson@sun.com * followed by any mixture of: 192*8404SAndrew.W.Wilson@sun.com * 193*8404SAndrew.W.Wilson@sun.com * 2a. Insert nodes with: avl_add(), or avl_find() and avl_insert() 194*8404SAndrew.W.Wilson@sun.com * 195*8404SAndrew.W.Wilson@sun.com * 2b. Visited elements with: 196*8404SAndrew.W.Wilson@sun.com * avl_first() - returns the lowest valued node 197*8404SAndrew.W.Wilson@sun.com * avl_last() - returns the highest valued node 198*8404SAndrew.W.Wilson@sun.com * AVL_NEXT() - given a node go to next higher one 199*8404SAndrew.W.Wilson@sun.com * AVL_PREV() - given a node go to previous lower one 200*8404SAndrew.W.Wilson@sun.com * 201*8404SAndrew.W.Wilson@sun.com * 2c. Find the node with the closest value either less than or greater 202*8404SAndrew.W.Wilson@sun.com * than a given value with avl_nearest(). 203*8404SAndrew.W.Wilson@sun.com * 204*8404SAndrew.W.Wilson@sun.com * 2d. Remove individual nodes from the list/tree with avl_remove(). 205*8404SAndrew.W.Wilson@sun.com * 206*8404SAndrew.W.Wilson@sun.com * and finally when the list is being destroyed 207*8404SAndrew.W.Wilson@sun.com * 208*8404SAndrew.W.Wilson@sun.com * 3. Use avl_destroy_nodes() to quickly process/free up any remaining nodes. 209*8404SAndrew.W.Wilson@sun.com * Note that once you use avl_destroy_nodes(), you can no longer 210*8404SAndrew.W.Wilson@sun.com * use any routine except avl_destroy_nodes() and avl_destoy(). 211*8404SAndrew.W.Wilson@sun.com * 212*8404SAndrew.W.Wilson@sun.com * 4. Use avl_destroy() to destroy the AVL tree itself. 213*8404SAndrew.W.Wilson@sun.com * 214*8404SAndrew.W.Wilson@sun.com * Any locking for multiple thread access is up to the user to provide, just 215*8404SAndrew.W.Wilson@sun.com * as is needed for any linked list implementation. 216*8404SAndrew.W.Wilson@sun.com */ 217*8404SAndrew.W.Wilson@sun.com 218*8404SAndrew.W.Wilson@sun.com 219*8404SAndrew.W.Wilson@sun.com /* 220*8404SAndrew.W.Wilson@sun.com * Type used for the root of the AVL tree. 221*8404SAndrew.W.Wilson@sun.com */ 222*8404SAndrew.W.Wilson@sun.com typedef struct avl_tree avl_tree_t; 223*8404SAndrew.W.Wilson@sun.com 224*8404SAndrew.W.Wilson@sun.com /* 225*8404SAndrew.W.Wilson@sun.com * The data nodes in the AVL tree must have a field of this type. 226*8404SAndrew.W.Wilson@sun.com */ 227*8404SAndrew.W.Wilson@sun.com typedef struct avl_node avl_node_t; 228*8404SAndrew.W.Wilson@sun.com 229*8404SAndrew.W.Wilson@sun.com /* 230*8404SAndrew.W.Wilson@sun.com * An opaque type used to locate a position in the tree where a node 231*8404SAndrew.W.Wilson@sun.com * would be inserted. 232*8404SAndrew.W.Wilson@sun.com */ 233*8404SAndrew.W.Wilson@sun.com typedef uintptr_t avl_index_t; 234*8404SAndrew.W.Wilson@sun.com 235*8404SAndrew.W.Wilson@sun.com 236*8404SAndrew.W.Wilson@sun.com /* 237*8404SAndrew.W.Wilson@sun.com * Direction constants used for avl_nearest(). 238*8404SAndrew.W.Wilson@sun.com */ 239*8404SAndrew.W.Wilson@sun.com #define AVL_BEFORE (0) 240*8404SAndrew.W.Wilson@sun.com #define AVL_AFTER (1) 241*8404SAndrew.W.Wilson@sun.com 242*8404SAndrew.W.Wilson@sun.com 243*8404SAndrew.W.Wilson@sun.com /* 244*8404SAndrew.W.Wilson@sun.com * Prototypes 245*8404SAndrew.W.Wilson@sun.com * 246*8404SAndrew.W.Wilson@sun.com * Where not otherwise mentioned, "void *" arguments are a pointer to the 247*8404SAndrew.W.Wilson@sun.com * user data structure which must contain a field of type avl_node_t. 248*8404SAndrew.W.Wilson@sun.com * 249*8404SAndrew.W.Wilson@sun.com * Also assume the user data structures looks like: 250*8404SAndrew.W.Wilson@sun.com * stuct my_type { 251*8404SAndrew.W.Wilson@sun.com * ... 252*8404SAndrew.W.Wilson@sun.com * avl_node_t my_link; 253*8404SAndrew.W.Wilson@sun.com * ... 254*8404SAndrew.W.Wilson@sun.com * }; 255*8404SAndrew.W.Wilson@sun.com */ 256*8404SAndrew.W.Wilson@sun.com 257*8404SAndrew.W.Wilson@sun.com /* 258*8404SAndrew.W.Wilson@sun.com * Initialize an AVL tree. Arguments are: 259*8404SAndrew.W.Wilson@sun.com * 260*8404SAndrew.W.Wilson@sun.com * tree - the tree to be initialized 261*8404SAndrew.W.Wilson@sun.com * compar - function to compare two nodes, it must return exactly: -1, 0, or +1 262*8404SAndrew.W.Wilson@sun.com * -1 for <, 0 for ==, and +1 for > 263*8404SAndrew.W.Wilson@sun.com * size - the value of sizeof(struct my_type) 264*8404SAndrew.W.Wilson@sun.com * offset - the value of OFFSETOF(struct my_type, my_link) 265*8404SAndrew.W.Wilson@sun.com */ 266*8404SAndrew.W.Wilson@sun.com extern void avl_create(avl_tree_t *tree, 267*8404SAndrew.W.Wilson@sun.com int (*compar) (const void *, const void *), size_t size, size_t offset); 268*8404SAndrew.W.Wilson@sun.com 269*8404SAndrew.W.Wilson@sun.com 270*8404SAndrew.W.Wilson@sun.com /* 271*8404SAndrew.W.Wilson@sun.com * Find a node with a matching value in the tree. Returns the matching node 272*8404SAndrew.W.Wilson@sun.com * found. If not found, it returns NULL and then if "where" is not NULL it sets 273*8404SAndrew.W.Wilson@sun.com * "where" for use with avl_insert() or avl_nearest(). 274*8404SAndrew.W.Wilson@sun.com * 275*8404SAndrew.W.Wilson@sun.com * node - node that has the value being looked for 276*8404SAndrew.W.Wilson@sun.com * where - position for use with avl_nearest() or avl_insert(), may be NULL 277*8404SAndrew.W.Wilson@sun.com */ 278*8404SAndrew.W.Wilson@sun.com extern void *avl_find(avl_tree_t *tree, void *node, avl_index_t *where); 279*8404SAndrew.W.Wilson@sun.com 280*8404SAndrew.W.Wilson@sun.com /* 281*8404SAndrew.W.Wilson@sun.com * Insert a node into the tree. 282*8404SAndrew.W.Wilson@sun.com * 283*8404SAndrew.W.Wilson@sun.com * node - the node to insert 284*8404SAndrew.W.Wilson@sun.com * where - position as returned from avl_find() 285*8404SAndrew.W.Wilson@sun.com */ 286*8404SAndrew.W.Wilson@sun.com extern void avl_insert(avl_tree_t *tree, void *node, avl_index_t where); 287*8404SAndrew.W.Wilson@sun.com 288*8404SAndrew.W.Wilson@sun.com /* 289*8404SAndrew.W.Wilson@sun.com * Insert "new_data" in "tree" in the given "direction" either after 290*8404SAndrew.W.Wilson@sun.com * or before the data "here". 291*8404SAndrew.W.Wilson@sun.com * 292*8404SAndrew.W.Wilson@sun.com * This might be usefull for avl clients caching recently accessed 293*8404SAndrew.W.Wilson@sun.com * data to avoid doing avl_find() again for insertion. 294*8404SAndrew.W.Wilson@sun.com * 295*8404SAndrew.W.Wilson@sun.com * new_data - new data to insert 296*8404SAndrew.W.Wilson@sun.com * here - existing node in "tree" 297*8404SAndrew.W.Wilson@sun.com * direction - either AVL_AFTER or AVL_BEFORE the data "here". 298*8404SAndrew.W.Wilson@sun.com */ 299*8404SAndrew.W.Wilson@sun.com extern void avl_insert_here(avl_tree_t *tree, void *new_data, void *here, 300*8404SAndrew.W.Wilson@sun.com int direction); 301*8404SAndrew.W.Wilson@sun.com 302*8404SAndrew.W.Wilson@sun.com 303*8404SAndrew.W.Wilson@sun.com /* 304*8404SAndrew.W.Wilson@sun.com * Return the first or last valued node in the tree. Will return NULL 305*8404SAndrew.W.Wilson@sun.com * if the tree is empty. 306*8404SAndrew.W.Wilson@sun.com * 307*8404SAndrew.W.Wilson@sun.com */ 308*8404SAndrew.W.Wilson@sun.com extern void *avl_first(avl_tree_t *tree); 309*8404SAndrew.W.Wilson@sun.com extern void *avl_last(avl_tree_t *tree); 310*8404SAndrew.W.Wilson@sun.com 311*8404SAndrew.W.Wilson@sun.com 312*8404SAndrew.W.Wilson@sun.com /* 313*8404SAndrew.W.Wilson@sun.com * Return the next or previous valued node in the tree. 314*8404SAndrew.W.Wilson@sun.com * AVL_NEXT() will return NULL if at the last node. 315*8404SAndrew.W.Wilson@sun.com * AVL_PREV() will return NULL if at the first node. 316*8404SAndrew.W.Wilson@sun.com * 317*8404SAndrew.W.Wilson@sun.com * node - the node from which the next or previous node is found 318*8404SAndrew.W.Wilson@sun.com */ 319*8404SAndrew.W.Wilson@sun.com #define AVL_NEXT(tree, node) avl_walk(tree, node, AVL_AFTER) 320*8404SAndrew.W.Wilson@sun.com #define AVL_PREV(tree, node) avl_walk(tree, node, AVL_BEFORE) 321*8404SAndrew.W.Wilson@sun.com 322*8404SAndrew.W.Wilson@sun.com 323*8404SAndrew.W.Wilson@sun.com /* 324*8404SAndrew.W.Wilson@sun.com * Find the node with the nearest value either greater or less than 325*8404SAndrew.W.Wilson@sun.com * the value from a previous avl_find(). Returns the node or NULL if 326*8404SAndrew.W.Wilson@sun.com * there isn't a matching one. 327*8404SAndrew.W.Wilson@sun.com * 328*8404SAndrew.W.Wilson@sun.com * where - position as returned from avl_find() 329*8404SAndrew.W.Wilson@sun.com * direction - either AVL_BEFORE or AVL_AFTER 330*8404SAndrew.W.Wilson@sun.com * 331*8404SAndrew.W.Wilson@sun.com * EXAMPLE get the greatest node that is less than a given value: 332*8404SAndrew.W.Wilson@sun.com * 333*8404SAndrew.W.Wilson@sun.com * avl_tree_t *tree; 334*8404SAndrew.W.Wilson@sun.com * struct my_data look_for_value = {....}; 335*8404SAndrew.W.Wilson@sun.com * struct my_data *node; 336*8404SAndrew.W.Wilson@sun.com * struct my_data *less; 337*8404SAndrew.W.Wilson@sun.com * avl_index_t where; 338*8404SAndrew.W.Wilson@sun.com * 339*8404SAndrew.W.Wilson@sun.com * node = avl_find(tree, &look_for_value, &where); 340*8404SAndrew.W.Wilson@sun.com * if (node != NULL) 341*8404SAndrew.W.Wilson@sun.com * less = AVL_PREV(tree, node); 342*8404SAndrew.W.Wilson@sun.com * else 343*8404SAndrew.W.Wilson@sun.com * less = avl_nearest(tree, where, AVL_BEFORE); 344*8404SAndrew.W.Wilson@sun.com */ 345*8404SAndrew.W.Wilson@sun.com extern void *avl_nearest(avl_tree_t *tree, avl_index_t where, int direction); 346*8404SAndrew.W.Wilson@sun.com 347*8404SAndrew.W.Wilson@sun.com 348*8404SAndrew.W.Wilson@sun.com /* 349*8404SAndrew.W.Wilson@sun.com * Add a single node to the tree. 350*8404SAndrew.W.Wilson@sun.com * The node must not be in the tree, and it must not 351*8404SAndrew.W.Wilson@sun.com * compare equal to any other node already in the tree. 352*8404SAndrew.W.Wilson@sun.com * 353*8404SAndrew.W.Wilson@sun.com * node - the node to add 354*8404SAndrew.W.Wilson@sun.com */ 355*8404SAndrew.W.Wilson@sun.com extern void avl_add(avl_tree_t *tree, void *node); 356*8404SAndrew.W.Wilson@sun.com 357*8404SAndrew.W.Wilson@sun.com 358*8404SAndrew.W.Wilson@sun.com /* 359*8404SAndrew.W.Wilson@sun.com * Remove a single node from the tree. The node must be in the tree. 360*8404SAndrew.W.Wilson@sun.com * 361*8404SAndrew.W.Wilson@sun.com * node - the node to remove 362*8404SAndrew.W.Wilson@sun.com */ 363*8404SAndrew.W.Wilson@sun.com extern void avl_remove(avl_tree_t *tree, void *node); 364*8404SAndrew.W.Wilson@sun.com 365*8404SAndrew.W.Wilson@sun.com /* 366*8404SAndrew.W.Wilson@sun.com * Reinsert a node only if its order has changed relative to its nearest 367*8404SAndrew.W.Wilson@sun.com * neighbors. To optimize performance avl_update_lt() checks only the previous 368*8404SAndrew.W.Wilson@sun.com * node and avl_update_gt() checks only the next node. Use avl_update_lt() and 369*8404SAndrew.W.Wilson@sun.com * avl_update_gt() only if you know the direction in which the order of the 370*8404SAndrew.W.Wilson@sun.com * node may change. 371*8404SAndrew.W.Wilson@sun.com */ 372*8404SAndrew.W.Wilson@sun.com extern boolean_t avl_update(avl_tree_t *, void *); 373*8404SAndrew.W.Wilson@sun.com extern boolean_t avl_update_lt(avl_tree_t *, void *); 374*8404SAndrew.W.Wilson@sun.com extern boolean_t avl_update_gt(avl_tree_t *, void *); 375*8404SAndrew.W.Wilson@sun.com 376*8404SAndrew.W.Wilson@sun.com /* 377*8404SAndrew.W.Wilson@sun.com * Return the number of nodes in the tree 378*8404SAndrew.W.Wilson@sun.com */ 379*8404SAndrew.W.Wilson@sun.com extern ulong_t avl_numnodes(avl_tree_t *tree); 380*8404SAndrew.W.Wilson@sun.com 381*8404SAndrew.W.Wilson@sun.com /* 382*8404SAndrew.W.Wilson@sun.com * Return B_TRUE if there are zero nodes in the tree, B_FALSE otherwise. 383*8404SAndrew.W.Wilson@sun.com */ 384*8404SAndrew.W.Wilson@sun.com extern boolean_t avl_is_empty(avl_tree_t *tree); 385*8404SAndrew.W.Wilson@sun.com 386*8404SAndrew.W.Wilson@sun.com /* 387*8404SAndrew.W.Wilson@sun.com * Used to destroy any remaining nodes in a tree. The cookie argument should 388*8404SAndrew.W.Wilson@sun.com * be initialized to NULL before the first call. Returns a node that has been 389*8404SAndrew.W.Wilson@sun.com * removed from the tree and may be free()'d. Returns NULL when the tree is 390*8404SAndrew.W.Wilson@sun.com * empty. 391*8404SAndrew.W.Wilson@sun.com * 392*8404SAndrew.W.Wilson@sun.com * Once you call avl_destroy_nodes(), you can only continuing calling it and 393*8404SAndrew.W.Wilson@sun.com * finally avl_destroy(). No other AVL routines will be valid. 394*8404SAndrew.W.Wilson@sun.com * 395*8404SAndrew.W.Wilson@sun.com * cookie - a "void *" used to save state between calls to avl_destroy_nodes() 396*8404SAndrew.W.Wilson@sun.com * 397*8404SAndrew.W.Wilson@sun.com * EXAMPLE: 398*8404SAndrew.W.Wilson@sun.com * avl_tree_t *tree; 399*8404SAndrew.W.Wilson@sun.com * struct my_data *node; 400*8404SAndrew.W.Wilson@sun.com * void *cookie; 401*8404SAndrew.W.Wilson@sun.com * 402*8404SAndrew.W.Wilson@sun.com * cookie = NULL; 403*8404SAndrew.W.Wilson@sun.com * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL) 404*8404SAndrew.W.Wilson@sun.com * free(node); 405*8404SAndrew.W.Wilson@sun.com * avl_destroy(tree); 406*8404SAndrew.W.Wilson@sun.com */ 407*8404SAndrew.W.Wilson@sun.com extern void *avl_destroy_nodes(avl_tree_t *tree, void **cookie); 408*8404SAndrew.W.Wilson@sun.com 409*8404SAndrew.W.Wilson@sun.com 410*8404SAndrew.W.Wilson@sun.com /* 411*8404SAndrew.W.Wilson@sun.com * Final destroy of an AVL tree. Arguments are: 412*8404SAndrew.W.Wilson@sun.com * 413*8404SAndrew.W.Wilson@sun.com * tree - the empty tree to destroy 414*8404SAndrew.W.Wilson@sun.com */ 415*8404SAndrew.W.Wilson@sun.com extern void avl_destroy(avl_tree_t *tree); 416*8404SAndrew.W.Wilson@sun.com 417*8404SAndrew.W.Wilson@sun.com 418*8404SAndrew.W.Wilson@sun.com 419*8404SAndrew.W.Wilson@sun.com #ifdef __cplusplus 420*8404SAndrew.W.Wilson@sun.com } 421*8404SAndrew.W.Wilson@sun.com #endif 422*8404SAndrew.W.Wilson@sun.com 423*8404SAndrew.W.Wilson@sun.com #endif /* FB_AVL_H */ 424