diff options
Diffstat (limited to 'src/avl.h')
-rw-r--r-- | src/avl.h | 42 |
1 files changed, 31 insertions, 11 deletions
@@ -17,10 +17,19 @@ #ifndef AVL_WITHOUT_PTHREADS #include <pthread.h> -#endif /* AVL_WITHOUT_PTHREADS */ // #define AVL_LOCK_WITH_MUTEX 1 +#ifdef AVL_LOCK_WITH_MUTEX +#define AVL_LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#else /* AVL_LOCK_WITH_MUTEX */ +#define AVL_LOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER +#endif /* AVL_LOCK_WITH_MUTEX */ + +#else /* AVL_WITHOUT_PTHREADS */ +#define AVL_LOCK_INITIALIZER +#endif /* AVL_WITHOUT_PTHREADS */ + /* Data structures */ /* One element of the AVL tree */ @@ -32,8 +41,13 @@ typedef struct avl { /* An AVL tree */ typedef struct avl_tree { - avl* root; - int (*compar)(void* a, void* b); + avl *root; + + int (*compar)(void *a, void *b); +} avl_tree; + +typedef struct avl_tree_lock { + avl_tree avl_tree; #ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX @@ -42,7 +56,7 @@ typedef struct avl_tree { pthread_rwlock_t rwlock; #endif /* AVL_LOCK_WITH_MUTEX */ #endif /* AVL_WITHOUT_PTHREADS */ -} avl_tree; +} avl_tree_lock; /* Public methods */ @@ -50,35 +64,41 @@ typedef struct avl_tree { * returns 1 if the depth of the tree has grown * Warning: do not insert elements already present */ -int avl_insert(avl_tree* t, avl* a); +int avl_insert_lock(avl_tree_lock *t, avl *a); +int avl_insert(avl_tree *t, avl *a); /* Remove an element a from the AVL tree t * returns -1 if the depth of the tree has shrunk * Warning: if the element is not present in the tree, * returns 0 as if it had been removed succesfully. */ -int avl_remove(avl_tree* t, avl* a); +int avl_remove_lock(avl_tree_lock *t, avl *a); +int avl_remove(avl_tree *t, avl *a); /* Remove the root of the AVL tree t * Warning: dumps core if t is empty */ -int avl_removeroot(avl_tree* t); +int avl_removeroot_lock(avl_tree_lock *t); +int avl_removeroot(avl_tree *t); /* Iterate through elements in t from a range between a and b (inclusive) * for each element calls iter(a) until it returns 0 * returns the last value returned by iterator or 0 if there were no calls * Warning: a<=b must hold */ -int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret); +int avl_range_lock(avl_tree_lock *t, avl *a, avl *b, int (*iter)(avl *), avl **ret); +int avl_range(avl_tree *t, avl *a, avl *b, int (*iter)(avl *), avl **ret); /* Iterate through elements in t equal to a * for each element calls iter(a) until it returns 0 * returns the last value returned by iterator or 0 if there were no calls */ -int avl_search(avl_tree* t, avl* a, int (*iter)(avl*), avl** ret); +avl *avl_search_lock(avl_tree_lock *t, avl *a); +avl *avl_search(avl_tree *t, avl *a); -/* Initialize the avl_tree +/* Initialize the avl_tree_lock */ -void avl_init(avl_tree* t, int (*compar)(void* a, void* b)); +void avl_init_lock(avl_tree_lock *t, int (*compar)(void *a, void *b)); +void avl_init(avl_tree *t, int (*compar)(void *a, void *b)); #endif /* avl.h */ |