avl-0.3.5/0000755000000000000000000000000007565243752006027 5avl-0.3.5/avl.c0000644000000000000000000003321007565243705006672 /***************************************************************************** avl.c - Source code for the AVL-tree library. Copyright (C) 1998 Michael H. Buselli Copyright (C) 2000-2002 Wessel Dankers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Augmented AVL-tree. Original by Michael H. Buselli . Modified by Wessel Dankers to add a bunch of bloat to the sourcecode, change the interface and squash a few bugs. Mail him if you find new bugs. *****************************************************************************/ #include #include #include #include "avl.h" static void avl_rebalance(avl_tree_t *, avl_node_t *); #ifdef AVL_COUNT #define NODE_COUNT(n) ((n) ? (n)->count : 0) #define L_COUNT(n) (NODE_COUNT((n)->left)) #define R_COUNT(n) (NODE_COUNT((n)->right)) #define CALC_COUNT(n) (L_COUNT(n) + R_COUNT(n) + 1) #endif #ifdef AVL_DEPTH #define NODE_DEPTH(n) ((n) ? (n)->depth : 0) #define L_DEPTH(n) (NODE_DEPTH((n)->left)) #define R_DEPTH(n) (NODE_DEPTH((n)->right)) #define CALC_DEPTH(n) ((L_DEPTH(n)>R_DEPTH(n)?L_DEPTH(n):R_DEPTH(n)) + 1) #endif #ifndef AVL_DEPTH /* Also known as ffs() (from BSD) */ static int lg(unsigned int u) { int r = 1; if(!u) return 0; if(u & 0xffff0000) { u >>= 16; r += 16; } if(u & 0x0000ff00) { u >>= 8; r += 8; } if(u & 0x000000f0) { u >>= 4; r += 4; } if(u & 0x0000000c) { u >>= 2; r += 2; } if(u & 0x00000002) r++; return r; } #endif static int avl_check_balance(avl_node_t *avlnode) { #ifdef AVL_DEPTH int d; d = R_DEPTH(avlnode) - L_DEPTH(avlnode); return d<-1?-1:d>1?1:0; #else /* int d; * d = lg(R_COUNT(avlnode)) - lg(L_COUNT(avlnode)); * d = d<-1?-1:d>1?1:0; */ #ifdef AVL_COUNT int pl, r; pl = lg(L_COUNT(avlnode)); r = R_COUNT(avlnode); if(r>>pl+1) return 1; if(pl<2 || r>>pl-2) return 0; return -1; #else #error No balancing possible. #endif #endif } #ifdef AVL_COUNT unsigned int avl_count(const avl_tree_t *avltree) { return NODE_COUNT(avltree->top); } avl_node_t *avl_at(const avl_tree_t *avltree, unsigned int index) { avl_node_t *avlnode; unsigned int c; avlnode = avltree->top; while(avlnode) { c = L_COUNT(avlnode); if(index < c) { avlnode = avlnode->left; } else if(index > c) { avlnode = avlnode->right; index -= c+1; } else { return avlnode; } } return NULL; } unsigned int avl_index(const avl_node_t *avlnode) { avl_node_t *next; unsigned int c; c = L_COUNT(avlnode); while((next = avlnode->parent)) { if(avlnode == next->right) c += L_COUNT(next) + 1; avlnode = next; } return c; } #endif int avl_search_closest(const avl_tree_t *avltree, const void *item, avl_node_t **avlnode) { avl_node_t *node; avl_compare_t cmp; int c; if(!avlnode) avlnode = &node; node = avltree->top; if(!node) return *avlnode = NULL, 0; cmp = avltree->cmp; for(;;) { c = cmp(item, node->item); if(c < 0) { if(node->left) node = node->left; else return *avlnode = node, -1; } else if(c > 0) { if(node->right) node = node->right; else return *avlnode = node, 1; } else { return *avlnode = node, 0; } } } /* * avl_search: * Return a pointer to a node with the given item in the tree. * If no such item is in the tree, then NULL is returned. */ avl_node_t *avl_search(const avl_tree_t *avltree, const void *item) { avl_node_t *node; return avl_search_closest(avltree, item, &node) ? NULL : node; } avl_tree_t *avl_init_tree(avl_tree_t *rc, avl_compare_t cmp, avl_freeitem_t freeitem) { if(rc) { rc->head = NULL; rc->tail = NULL; rc->top = NULL; rc->cmp = cmp; rc->freeitem = freeitem; } return rc; } avl_tree_t *avl_alloc_tree(avl_compare_t cmp, avl_freeitem_t freeitem) { return avl_init_tree(malloc(sizeof(avl_tree_t)), cmp, freeitem); } void avl_clear_tree(avl_tree_t *avltree) { avltree->top = avltree->head = avltree->tail = NULL; } void avl_free_nodes(avl_tree_t *avltree) { avl_node_t *node, *next; avl_freeitem_t freeitem; freeitem = avltree->freeitem; for(node = avltree->head; node; node = next) { next = node->next; if(freeitem) freeitem(node->item); free(node); } return avl_clear_tree(avltree); } /* * avl_free_tree: * Free all memory used by this tree. If freeitem is not NULL, then * it is assumed to be a destructor for the items referenced in the avl_ * tree, and they are deleted as well. */ void avl_free_tree(avl_tree_t *avltree) { avl_free_nodes(avltree); free(avltree); } static void avl_clear_node(avl_node_t *newnode) { newnode->left = newnode->right = NULL; #ifdef AVL_COUNT newnode->count = 1; #endif #ifdef AVL_DEPTH newnode->depth = 1; #endif } avl_node_t *avl_init_node(avl_node_t *newnode, void *item) { if(newnode) { /* avl_clear_node(newnode); */ newnode->item = item; } return newnode; } avl_node_t *avl_insert_top(avl_tree_t *avltree, avl_node_t *newnode) { avl_clear_node(newnode); newnode->prev = newnode->next = newnode->parent = NULL; avltree->head = avltree->tail = avltree->top = newnode; return newnode; } avl_node_t *avl_insert_before(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { if(!node) return avltree->tail ? avl_insert_after(avltree, avltree->tail, newnode) : avl_insert_top(avltree, newnode); if(node->left) return avl_insert_after(avltree, node->prev, newnode); avl_clear_node(newnode); newnode->next = node; newnode->parent = node; newnode->prev = node->prev; if(node->prev) node->prev->next = newnode; else avltree->head = newnode; node->prev = newnode; node->left = newnode; avl_rebalance(avltree, node); return newnode; } avl_node_t *avl_insert_after(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { if(!node) return avltree->head ? avl_insert_before(avltree, avltree->head, newnode) : avl_insert_top(avltree, newnode); if(node->right) return avl_insert_before(avltree, node->next, newnode); avl_clear_node(newnode); newnode->prev = node; newnode->parent = node; newnode->next = node->next; if(node->next) node->next->prev = newnode; else avltree->tail = newnode; node->next = newnode; node->right = newnode; avl_rebalance(avltree, node); return newnode; } avl_node_t *avl_insert_node(avl_tree_t *avltree, avl_node_t *newnode) { avl_node_t *node; if(!avltree->top) return avl_insert_top(avltree, newnode); switch(avl_search_closest(avltree, newnode->item, &node)) { case -1: return avl_insert_before(avltree, node, newnode); case 1: return avl_insert_after(avltree, node, newnode); } return NULL; } /* * avl_insert: * Create a new node and insert an item there. * Returns the new node on success or NULL if no memory could be allocated. */ avl_node_t *avl_insert(avl_tree_t *avltree, void *item) { avl_node_t *newnode; newnode = avl_init_node(malloc(sizeof(avl_node_t)), item); if(newnode) { if(avl_insert_node(avltree, newnode)) return newnode; free(newnode); errno = EEXIST; } return NULL; } /* * avl_unlink_node: * Removes the given node. Does not delete the item at that node. * The item of the node may be freed before calling avl_unlink_node. * (In other words, it is not referenced by this function.) */ void avl_unlink_node(avl_tree_t *avltree, avl_node_t *avlnode) { avl_node_t *parent; avl_node_t **superparent; avl_node_t *subst, *left, *right; avl_node_t *balnode; if(avlnode->prev) avlnode->prev->next = avlnode->next; else avltree->head = avlnode->next; if(avlnode->next) avlnode->next->prev = avlnode->prev; else avltree->tail = avlnode->prev; parent = avlnode->parent; superparent = parent ? avlnode == parent->left ? &parent->left : &parent->right : &avltree->top; left = avlnode->left; right = avlnode->right; if(!left) { *superparent = right; if(right) right->parent = parent; balnode = parent; } else if(!right) { *superparent = left; left->parent = parent; balnode = parent; } else { subst = avlnode->prev; if(subst == left) { balnode = subst; } else { balnode = subst->parent; balnode->right = subst->left; if(balnode->right) balnode->right->parent = balnode; subst->left = left; left->parent = subst; } subst->right = right; subst->parent = parent; right->parent = subst; *superparent = subst; } avl_rebalance(avltree, balnode); } void *avl_delete_node(avl_tree_t *avltree, avl_node_t *avlnode) { void *item = NULL; if(avlnode) { item = avlnode->item; avl_unlink_node(avltree, avlnode); if(avltree->freeitem) avltree->freeitem(item); free(avlnode); } return item; } void *avl_delete(avl_tree_t *avltree, const void *item) { return avl_delete_node(avltree, avl_search(avltree, item)); } avl_node_t *avl_fixup_node(avl_tree_t *avltree, avl_node_t *newnode) { avl_node_t *oldnode = NULL, *node; if(!avltree || !newnode) return NULL; node = newnode->prev; if(node) { oldnode = node->next; node->next = newnode; } else { avltree->head = newnode; } node = newnode->next; if(node) { oldnode = node->prev; node->prev = newnode; } else { avltree->tail = newnode; } node = newnode->parent; if(node) { if(node->left == oldnode) node->left = newnode; else node->right = newnode; } else { oldnode = avltree->top; avltree->top = newnode; } return oldnode; } /* * avl_rebalance: * Rebalances the tree if one side becomes too heavy. This function * assumes that both subtrees are AVL-trees with consistant data. The * function has the additional side effect of recalculating the count of * the tree at this node. It should be noted that at the return of this * function, if a rebalance takes place, the top of this subtree is no * longer going to be the same node. */ void avl_rebalance(avl_tree_t *avltree, avl_node_t *avlnode) { avl_node_t *child; avl_node_t *gchild; avl_node_t *parent; avl_node_t **superparent; parent = avlnode; while(avlnode) { parent = avlnode->parent; superparent = parent ? avlnode == parent->left ? &parent->left : &parent->right : &avltree->top; switch(avl_check_balance(avlnode)) { case -1: child = avlnode->left; #ifdef AVL_DEPTH if(L_DEPTH(child) >= R_DEPTH(child)) { #else #ifdef AVL_COUNT if(L_COUNT(child) >= R_COUNT(child)) { #else #error No balancing possible. #endif #endif avlnode->left = child->right; if(avlnode->left) avlnode->left->parent = avlnode; child->right = avlnode; avlnode->parent = child; *superparent = child; child->parent = parent; #ifdef AVL_COUNT avlnode->count = CALC_COUNT(avlnode); child->count = CALC_COUNT(child); #endif #ifdef AVL_DEPTH avlnode->depth = CALC_DEPTH(avlnode); child->depth = CALC_DEPTH(child); #endif } else { gchild = child->right; avlnode->left = gchild->right; if(avlnode->left) avlnode->left->parent = avlnode; child->right = gchild->left; if(child->right) child->right->parent = child; gchild->right = avlnode; if(gchild->right) gchild->right->parent = gchild; gchild->left = child; if(gchild->left) gchild->left->parent = gchild; *superparent = gchild; gchild->parent = parent; #ifdef AVL_COUNT avlnode->count = CALC_COUNT(avlnode); child->count = CALC_COUNT(child); gchild->count = CALC_COUNT(gchild); #endif #ifdef AVL_DEPTH avlnode->depth = CALC_DEPTH(avlnode); child->depth = CALC_DEPTH(child); gchild->depth = CALC_DEPTH(gchild); #endif } break; case 1: child = avlnode->right; #ifdef AVL_DEPTH if(R_DEPTH(child) >= L_DEPTH(child)) { #else #ifdef AVL_COUNT if(R_COUNT(child) >= L_COUNT(child)) { #else #error No balancing possible. #endif #endif avlnode->right = child->left; if(avlnode->right) avlnode->right->parent = avlnode; child->left = avlnode; avlnode->parent = child; *superparent = child; child->parent = parent; #ifdef AVL_COUNT avlnode->count = CALC_COUNT(avlnode); child->count = CALC_COUNT(child); #endif #ifdef AVL_DEPTH avlnode->depth = CALC_DEPTH(avlnode); child->depth = CALC_DEPTH(child); #endif } else { gchild = child->left; avlnode->right = gchild->left; if(avlnode->right) avlnode->right->parent = avlnode; child->left = gchild->right; if(child->left) child->left->parent = child; gchild->left = avlnode; if(gchild->left) gchild->left->parent = gchild; gchild->right = child; if(gchild->right) gchild->right->parent = gchild; *superparent = gchild; gchild->parent = parent; #ifdef AVL_COUNT avlnode->count = CALC_COUNT(avlnode); child->count = CALC_COUNT(child); gchild->count = CALC_COUNT(gchild); #endif #ifdef AVL_DEPTH avlnode->depth = CALC_DEPTH(avlnode); child->depth = CALC_DEPTH(child); gchild->depth = CALC_DEPTH(gchild); #endif } break; default: #ifdef AVL_COUNT avlnode->count = CALC_COUNT(avlnode); #endif #ifdef AVL_DEPTH avlnode->depth = CALC_DEPTH(avlnode); #endif } avlnode = parent; } } avl-0.3.5/avl.h0000644000000000000000000001515607565243316006706 /***************************************************************************** avl.h - Source code for the AVL-tree library. Copyright (C) 1998 Michael H. Buselli Copyright (C) 2000-2002 Wessel Dankers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Augmented AVL-tree. Original by Michael H. Buselli . Modified by Wessel Dankers to add a bunch of bloat to the sourcecode, change the interface and squash a few bugs. Mail him if you find new bugs. *****************************************************************************/ #ifndef _AVL_H #define _AVL_H /* We need either depths, counts or both (the latter being the default) */ #if !defined(AVL_DEPTH) && !defined(AVL_COUNT) #define AVL_DEPTH #define AVL_COUNT #endif /* User supplied function to compare two items like strcmp() does. * For example: cmp(a,b) will return: * -1 if a < b * 0 if a = b * 1 if a > b */ typedef int (*avl_compare_t)(const void *, const void *); /* User supplied function to delete an item when a node is free()d. * If NULL, the item is not free()d. */ typedef void (*avl_freeitem_t)(void *); typedef struct avl_node_t { struct avl_node_t *next; struct avl_node_t *prev; struct avl_node_t *parent; struct avl_node_t *left; struct avl_node_t *right; void *item; #ifdef AVL_COUNT unsigned int count; #endif #ifdef AVL_DEPTH unsigned char depth; #endif } avl_node_t; typedef struct avl_tree_t { avl_node_t *head; avl_node_t *tail; avl_node_t *top; avl_compare_t cmp; avl_freeitem_t freeitem; } avl_tree_t; /* Initializes a new tree for elements that will be ordered using * the supplied strcmp()-like function. * Returns the value of avltree (even if it's NULL). * O(1) */ extern avl_tree_t *avl_init_tree(avl_tree_t *avltree, avl_compare_t, avl_freeitem_t); /* Allocates and initializes a new tree for elements that will be * ordered using the supplied strcmp()-like function. * Returns NULL if memory could not be allocated. * O(1) */ extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_freeitem_t); /* Frees the entire tree efficiently. Nodes will be free()d. * If the tree's freeitem is not NULL it will be invoked on every item. * O(n) */ extern void avl_free_tree(avl_tree_t *); /* Reinitializes the tree structure for reuse. Nothing is free()d. * Compare and freeitem functions are left alone. * O(1) */ extern void avl_clear_tree(avl_tree_t *); /* Free()s all nodes in the tree but leaves the tree itself. * If the tree's freeitem is not NULL it will be invoked on every item. * O(n) */ extern void avl_free_nodes(avl_tree_t *); /* Initializes memory for use as a node. Returns NULL if avlnode is NULL. * O(1) */ extern avl_node_t *avl_init_node(avl_node_t *avlnode, void *item); /* Insert an item into the tree and return the new node. * Returns NULL and sets errno if memory for the new node could not be * allocated or if the node is already in the tree (EEXIST). * O(lg n) */ extern avl_node_t *avl_insert(avl_tree_t *, void *item); /* Insert a node into the tree and return it. * Returns NULL if the node is already in the tree. * O(lg n) */ extern avl_node_t *avl_insert_node(avl_tree_t *, avl_node_t *); /* Insert a node in an empty tree. If avlnode is NULL, the tree will be * cleared and ready for re-use. * If the tree is not empty, the old nodes are left dangling. * O(1) */ extern avl_node_t *avl_insert_top(avl_tree_t *, avl_node_t *avlnode); /* Insert a node before another node. Returns the new node. * If old is NULL, the item is appended to the tree. * O(lg n) */ extern avl_node_t *avl_insert_before(avl_tree_t *, avl_node_t *old, avl_node_t *new); /* Insert a node after another node. Returns the new node. * If old is NULL, the item is prepended to the tree. * O(lg n) */ extern avl_node_t *avl_insert_after(avl_tree_t *, avl_node_t *old, avl_node_t *new); /* Deletes a node from the tree. Returns immediately if the node is NULL. * The item will not be free()d regardless of the tree's freeitem handler. * This function comes in handy if you need to update the search key. * O(lg n) */ extern void avl_unlink_node(avl_tree_t *, avl_node_t *); /* Deletes a node from the tree. Returns immediately if the node is NULL. * If the tree's freeitem is not NULL, it is invoked on the item. * If it is, returns the item. * O(lg n) */ extern void *avl_delete_node(avl_tree_t *, avl_node_t *); /* Searches for an item in the tree and deletes it if found. * If the tree's freeitem is not NULL, it is invoked on the item. * If it is, returns the item. * O(lg n) */ extern void *avl_delete(avl_tree_t *, const void *item); /* If exactly one node is moved in memory, this will fix the pointers * in the tree that refer to it. It must be an exact shallow copy. * Returns the pointer to the old position. * O(1) */ extern avl_node_t *avl_fixup_node(avl_tree_t *, avl_node_t *new); /* Searches for a node with the key closest (or equal) to the given item. * If avlnode is not NULL, *avlnode will be set to the node found or NULL * if the tree is empty. Return values: * -1 if the returned node is smaller * 0 if the returned node is equal or if the tree is empty * 1 if the returned node is greater * O(lg n) */ extern int avl_search_closest(const avl_tree_t *, const void *item, avl_node_t **avlnode); /* Searches for the item in the tree and returns a matching node if found * or NULL if not. * O(lg n) */ extern avl_node_t *avl_search(const avl_tree_t *, const void *item); #ifdef AVL_COUNT /* Returns the number of nodes in the tree. * O(1) */ extern unsigned int avl_count(const avl_tree_t *); /* Searches a node by its rank in the list. Counting starts at 0. * Returns NULL if the index exceeds the number of nodes in the tree. * O(lg n) */ extern avl_node_t *avl_at(const avl_tree_t *, unsigned int); /* Returns the rank of a node in the list. Counting starts at 0. * O(lg n) */ extern unsigned int avl_index(const avl_node_t *); #endif #endif avl-0.3.5/avlsort.c0000644000000000000000000000653407565243267007616 /***************************************************************************** avlsort.c - Example program for the AVL-tree library. Copyright (C) 1998 Michael H. Buselli Copyright (C) 2000-2002 Wessel Dankers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ #include #include #include #include #include #include #include "avl.h" void avl_insert_always(avl_tree_t *avltree, void *item) { avl_node_t *newnode, *node; newnode = avl_init_node(malloc(sizeof(avl_node_t)), item); if(newnode) { switch(avl_search_closest(avltree, newnode->item, &node)) { case -1: avl_insert_before(avltree, node, newnode); return; case 0: case 1: avl_insert_after(avltree, node, newnode); } } } int main(int argc, char **argv) { avl_tree_t *t; avl_node_t *c; char *s, *n; char *ob, *bb, *eb; int r, bs; int l, u; struct stat st; int mmapped = 0; t = avl_alloc_tree((avl_compare_t)strcmp, NULL); u = 0; if(!fstat(STDIN_FILENO, &st) && S_ISREG(st.st_mode)) { bs = st.st_size; bb = eb = mmap(NULL, bs+1, PROT_READ|PROT_WRITE, MAP_PRIVATE, STDIN_FILENO, 0); if(bb) { mmapped = 1; eb = bb+bs; } else { bb = eb = malloc(bs+1); if(!bb) { perror("malloc()"); exit(2); } while(bs > 0) { r = read(STDIN_FILENO, eb, bs); if(r <= 0) { perror("moo"); exit(2); } eb += r; bs -= r; } } } else { bs = 65536; r = 0; bb = eb = ob = malloc(bs); if(!bb) { perror("malloc()"); exit(2); } for(;;) { eb += r; r = read(STDIN_FILENO, eb, 65536); if(r < 0) { perror("moo"); exit(2); } if(r == 0) break; bb = realloc(ob = bb, bs += r); if(!bb) { fprintf(stderr, "realloc() to %d bytes failed.\n", bs); exit(2); } if(bb != ob) { eb += bb - ob; u++; } } } if(*(eb-1) != '\n') *eb++ = '\n'; bs = eb - bb; bb = s = mmapped?(ob=bb):realloc(ob = bb, bs); if(bb != ob) u++; eb = bb + bs; if(mmapped) fprintf(stderr, "Finished reading input (mmap()ed)\n"); else fprintf(stderr, "Finished reading input (%d relocations)\n", u); l = 0; for(n = s = bb; shead; c; c = c->next) { puts(c->item); l++; } fprintf(stderr, "Wrote %d lines, depth %d\n", l, t->top?t->top->depth:0); for(; l; l--) avl_delete_node(t, t->top); if(!t->top) fprintf(stderr, "Deleted all lines\n"); avl_free_nodes(t); return 0; } avl-0.3.5/GNUmakefile0000644000000000000000000000266607565241654010032 # You may select DEPTHs (no indexing), COUNTs (slower) or both (more memory). #CPPFLAGS += -DAVL_COUNT -DAVL_DEPTH LN ?= ln INSTALL ?= /usr/bin/install LDCONFIG ?= /sbin/ldconfig # Some suggestions: (-mcpu= generates i386 compatible code) CFLAGS ?= -O2 -fomit-frame-pointer -pipe -mcpu=i686 -w #CFLAGS = -O2 -fomit-frame-pointer -pipe -march=i586 -Wall -g #CFLAGS = -O6 -fomit-frame-pointer -pipe -march=i586 -Wall -ansi -pedantic #CFLAGS = -O6 -fomit-frame-pointer -pipe -march=i686 -Wall -ansi -pedantic #CFLAGS = -O6 -march=k6 -fforce-mem -fforce-addr -pipe #CFLAGS = -g -fomit-frame-pointer -pipe -march=i686 -Wall -ansi -pedantic #CFLAGS = -g -pg -a -pipe -march=i686 -Wall #LDFLAGS = -s prefix ?= /usr/local libdir ?= $(prefix)/lib includedir ?= $(prefix)/include includedir ?= /usr/include PROGRAMS = avlsort setdiff LIBRARY = libavl.so.1.5 all: $(LIBRARY) test: $(PROGRAMS) setdiff: setdiff.o avl.o $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) avlsort: avlsort.o avl.o $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) $(LIBRARY): avl.o $(CC) -nostdlib -shared -Wl,-soname,libavl.so.1 $^ -o $@ -lc clean: $(RM) *.o $(PROGRAMS) libavl.* install: all $(INSTALL) -d $(DESTDIR)$(libdir) $(INSTALL) avl.h $(DESTDIR)$(includedir) $(INSTALL) $(LIBRARIES) $(DESTDIR)$(libdir) for i in $(LIBRARIES); do\ $(LN) -sf $$i $(DESTDIR)$(libdir)/$${i%.*};\ $(LN) -sf $${i%.*} $(DESTDIR)$(libdir)/$${i%.*.*};\ done -$(LDCONFIG) .PHONY: clean install all .PRECIOUS: %.h %.c avl-0.3.5/Makefile0000644000000000000000000000017307226134651007377 # compatibility with FreeBSD make, invokes gmake for all targets. .BEGIN: @gmake ${.TARGETS} . .DEFAULT: @: .PHONY: . avl-0.3.5/README0000644000000000000000000000620707565242133006624 ======================================================================== WHAT IS AVLTREE? AVLTree is a small implementation of AVL trees for the C programming language. It is distributed under the Library GNU Public License (see the file LICENSE for details). This library does the basic stuff. It allows for inserts, searches, and deletes in O(log n) time. It also allows the tree to be used as a linked indexable list (search/insert functions cannot be used in that case). If you find a bug, you should mail Wessel Dankers , who produced this version of the library and therefore is to blame. The original author is Michael H. Buselli , who can be reached at the following address: Michael H. Buselli 4334 N. Hazel St. #515 Chicago, IL 60613-1456 ======================================================================== COMPILING THE LIBRARY There is a Makefile included in the distribution. ======================================================================== USING THE LIBRARIES There are no real usage documents yet. Look at avl.h (you need to include these headers in your programs to use the library) to see what functions and structures are available. As a small example: #define BUFSIZE 8192 int main(void) { char *buf[BUFSIZE]; AVLTree *tree; AVLTree *node; tree = avl_alloc_tree((avl_compare_t)strcmp, (avl_freeitem_t)free); while(fgets(buf, BUFSIZE, stdin)) avl_insert(tree, strdup(buf)); for(node = tree->head; node; node = node->next) printf("%s", node->item); avl_free_tree(tree, free); } A real implementation would check the return values of avl_alloc_tree, avl_insert and strdup of course. ======================================================================== HISTORY Version 0.1.0 (alpha): This is the initial alpha version of AVLTree. It's already fully functional for many applications, including the one that I developed it for. I've only tested it on my Linux 2.0.35/glibc2 system, so I have no idea what it will do anywhere else so far. Let me know if you have good results or bad if you try a platform that I don't mention above. This version is considered alpha because it does not yet contain all of the features that I plan for version 1.0.0. It should not contain any bugs as it is. Version 0.2.0 2000-11-28 Wessel Dankers Modifications to support fast traversal and accessing by index. The tree is generalized to allow arbitrary comparison functions. Fixed bug: when deleting, in some cases rebalancing would not occur Version 0.3.0 2001-01-07 Wessel Dankers Tree can now be balanced on count or depth (but depth works better). Fixed bug: balancing on count now is done correctly. Version 0.3.1 2001-07-17 Wessel Dankers Node initialization is moved to make matters a bit more robust Version 0.3.4 2002-06-11 Wessel Dankers Fixed a bug in the node counting. Version 0.3.5 2002-11-15 Wessel Dankers Added avl_node_fixup() and avl_clear_tree(). Removed obsolete files from source tree. ======================================================================== avl-0.3.5/setdiff.c0000644000000000000000000000421207565243347007536 /***************************************************************************** setdiff.c - Example program for the AVL-tree library. Copyright (C) 1998 Michael H. Buselli Copyright (C) 2000-2002 Wessel Dankers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ #include #include #include #include #include "avl.h" int depth(avl_node_t *n) { int d; for(d = 0; n; n = n->parent) d++; return d; } void readinto(avl_tree_t *t, char *fname) { FILE *f; char *s, *e; int len; f = fopen(fname, "r"); if(!f) { perror(fname); exit(1); } while((e = s = malloc(65536)) && fgets(s, 65536, f)) { len = strlen(s); if(s[len-1] == '\n') s[len-1] = '\0'; else len++; e = s = realloc(s, len); if(!s) break; avl_insert(t, s); } if(!e) { perror("malloc()"); exit(2); } fclose(f); } int main(int argc, char **argv) { avl_tree_t t, u; avl_node_t *c; if(argc != 3) { fprintf(stderr, "Requires exactly 2 arguments.\n"); exit(2); } avl_init_tree(&t, (avl_compare_t)strcmp, NULL); avl_init_tree(&u, (avl_compare_t)strcmp, NULL); readinto(&t, argv[1]); readinto(&u, argv[2]); for(c = t.head; c; c = c->next) if(!avl_search(&u, c->item)) printf("-%s\n", c->item); for(c = u.head; c; c = c->next) if(!avl_search(&t, c->item)) printf("+%s\n", c->item); return 0; } avl-0.3.5/COPYING0000644000000000000000000004405507565243146007007 GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. ^L Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. ^L 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. ^L 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. ^L 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. ^L 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS