libvieter/include/vieter_tree.h

89 lines
2.0 KiB
C
Raw Normal View History

2023-01-19 21:22:08 +01:00
#ifndef VIETER_TREE
#define VIETER_TREE
#include <stdint.h>
#include <stdlib.h>
2023-01-19 21:22:08 +01:00
typedef struct vieter_tree vieter_tree;
typedef enum vieter_tree_error {
vieter_tree_ok = 0,
vieter_tree_already_present = 1,
2023-02-02 14:10:35 +01:00
vieter_tree_not_present = 2,
vieter_tree_iterator_done = 3
2023-01-19 21:22:08 +01:00
} vieter_tree_error;
2023-01-28 09:09:17 +01:00
/*
* Allocate and initialize a new red-black binary tree.
*/
vieter_tree *vieter_tree_init();
2023-01-28 09:09:17 +01:00
/*
* Deallocate a red-black binary tree.
*/
void vieter_tree_free(vieter_tree *tree);
2023-01-28 09:09:17 +01:00
/*
* Insert a new entry into the tree.
*/
vieter_tree_error vieter_tree_insert(vieter_tree *tree, uint64_t key,
void *data);
2023-01-28 09:09:17 +01:00
/*
* Look for an entry in the tree.
*/
vieter_tree_error vieter_tree_search(void **out, vieter_tree *tree,
uint64_t key);
2023-01-28 09:09:17 +01:00
/*
* Remove a given entry from the tree.
*/
vieter_tree_error vieter_tree_remove(void **out, vieter_tree *tree,
uint64_t key);
2023-01-28 09:09:17 +01:00
/*
* Return how many entries are currently in the tree.
*/
uint64_t vieter_tree_size(vieter_tree *tree);
2023-01-19 21:22:08 +01:00
2023-02-02 14:10:35 +01:00
/*
* An iterator that can be used to traverse a tree in-order.
*/
typedef struct vieter_tree_iterator vieter_tree_iterator;
/*
* Allocate and initialize a new iterator from the given tree.
*/
vieter_tree_iterator *vieter_tree_iterator_from(vieter_tree *tree);
/*
* Free an iterator.
*/
void vieter_tree_iterator_free(vieter_tree_iterator **ptp);
/*
* Return the iterator's next element.
*/
vieter_tree_error vieter_tree_iterator_next(void **out,
vieter_tree_iterator *iter);
2023-03-07 12:12:46 +01:00
/*
* Acquire a read lock on the tree. Return value is the result of
* pthread_rwlock_rdlock.
*/
int vieter_tree_rlock(vieter_tree *tree);
/*
* Acquire a write lock on the tree. Return value is the result of
* pthread_rwlock_wrlock.
*/
int vieter_tree_wlock(vieter_tree *tree);
/*
* Unlock the lock after having acquired it. Return value is the result of
* pthread_rwlock_unlock.
*/
int vieter_tree_unlock(vieter_tree *tree);
2023-01-19 21:22:08 +01:00
#endif