5 /* Maximum AVL tree height. */
7 #define AVL_MAX_HEIGHT 92
10 #ifndef AVL_WITHOUT_PTHREADS
13 // #define AVL_LOCK_WITH_MUTEX 1
15 #ifdef AVL_LOCK_WITH_MUTEX
16 #define AVL_LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER
17 #else /* AVL_LOCK_WITH_MUTEX */
18 #define AVL_LOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
19 #endif /* AVL_LOCK_WITH_MUTEX */
21 #else /* AVL_WITHOUT_PTHREADS */
22 #define AVL_LOCK_INITIALIZER
23 #endif /* AVL_WITHOUT_PTHREADS */
27 /* One element of the AVL tree */
29 struct avl *avl_link[2]; /* Subtrees. */
30 signed char avl_balance; /* Balance factor. */
34 typedef struct avl_tree {
36 int (*compar)(void *a, void *b);
39 typedef struct avl_tree_lock {
42 #ifndef AVL_WITHOUT_PTHREADS
43 #ifdef AVL_LOCK_WITH_MUTEX
44 pthread_mutex_t mutex;
45 #else /* AVL_LOCK_WITH_MUTEX */
46 pthread_rwlock_t rwlock;
47 #endif /* AVL_LOCK_WITH_MUTEX */
48 #endif /* AVL_WITHOUT_PTHREADS */
53 /* Insert element a into the AVL tree t
54 * returns 1 if the depth of the tree has grown
55 * Warning: do not insert elements already present
57 avl *avl_insert_lock(avl_tree_lock *t, avl *a);
58 avl *avl_insert(avl_tree *t, avl *a);
60 /* Remove an element a from the AVL tree t
61 * returns -1 if the depth of the tree has shrunk
62 * Warning: if the element is not present in the tree,
63 * returns 0 as if it had been removed succesfully.
65 avl *avl_remove_lock(avl_tree_lock *t, avl *a);
66 avl *avl_remove(avl_tree *t, avl *a);
68 /* Iterate through elements in t equal to a
69 * for each element calls iter(a) until it returns 0
70 * returns the last value returned by iterator or 0 if there were no calls
72 avl *avl_search_lock(avl_tree_lock *t, avl *a);
73 avl *avl_search(avl_tree *t, avl *a);
75 /* Initialize the avl_tree_lock
77 void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b));
78 void avl_init(avl_tree *t, int (*compar)(void *a, void *b));