2 * ANSI C Library for maintainance of AVL Balanced Trees
5 * G. M. Adelson-Velskij & E. M. Landis
6 * Doklady Akad. Nauk SSSR 146 (1962), 263-266
9 * D. E. Knuth: The Art of Computer Programming Vol.3 (Sorting and Searching)
11 * (C) 2000 Daniel Nagy, Budapest University of Technology and Economics
12 * Released under GNU General Public License (GPL) version 2
18 #ifndef AVL_WITHOUT_PTHREADS
21 // #define AVL_LOCK_WITH_MUTEX 1
23 #ifdef AVL_LOCK_WITH_MUTEX
24 #define AVL_LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER
25 #else /* AVL_LOCK_WITH_MUTEX */
26 #define AVL_LOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
27 #endif /* AVL_LOCK_WITH_MUTEX */
29 #else /* AVL_WITHOUT_PTHREADS */
30 #define AVL_LOCK_INITIALIZER
31 #endif /* AVL_WITHOUT_PTHREADS */
35 /* One element of the AVL tree */
43 typedef struct avl_tree {
46 int (*compar)(void *a, void *b);
49 typedef struct avl_tree_lock {
52 #ifndef AVL_WITHOUT_PTHREADS
53 #ifdef AVL_LOCK_WITH_MUTEX
54 pthread_mutex_t mutex;
55 #else /* AVL_LOCK_WITH_MUTEX */
56 pthread_rwlock_t rwlock;
57 #endif /* AVL_LOCK_WITH_MUTEX */
58 #endif /* AVL_WITHOUT_PTHREADS */
63 /* Insert element a into the AVL tree t
64 * returns 1 if the depth of the tree has grown
65 * Warning: do not insert elements already present
67 int avl_insert_lock(avl_tree_lock *t, avl *a);
68 int avl_insert(avl_tree *t, avl *a);
70 /* Remove an element a from the AVL tree t
71 * returns -1 if the depth of the tree has shrunk
72 * Warning: if the element is not present in the tree,
73 * returns 0 as if it had been removed succesfully.
75 int avl_remove_lock(avl_tree_lock *t, avl *a);
76 int avl_remove(avl_tree *t, avl *a);
78 /* Remove the root of the AVL tree t
79 * Warning: dumps core if t is empty
81 int avl_removeroot_lock(avl_tree_lock *t);
82 int avl_removeroot(avl_tree *t);
84 /* Iterate through elements in t from a range between a and b (inclusive)
85 * for each element calls iter(a) until it returns 0
86 * returns the last value returned by iterator or 0 if there were no calls
87 * Warning: a<=b must hold
89 int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret);
90 int avl_range(avl_tree *t, avl *a, avl *b, int (*iter)(avl *), avl **ret);
92 /* Iterate through elements in t equal to a
93 * for each element calls iter(a) until it returns 0
94 * returns the last value returned by iterator or 0 if there were no calls
96 #define avl_search_lock(t, a, iter, ret) avl_range_lock(t, a, a, iter, ret)
97 #define avl_search(t, a, iter, ret) avl_range(t, a, a, iter, ret)
99 /* Initialize the avl_tree_lock
101 void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b));
102 void avl_init(avl_tree *t, int (*compar)(void *a, void *b));