ucx
UAP Common Extensions
|
AVL tree implementation. More...
Go to the source code of this file.
Data Structures | |
struct | UcxAVLNode |
UCX AVL Node. More... | |
struct | UcxAVLTree |
UCX AVL Tree. More... | |
Macros | |
#define | ucx_avl_default_new() ucx_avl_new_a(ucx_cmp_ptr, ucx_default_allocator()) |
Macro for initializing a new UcxAVLTree with the default allocator and a ucx_cmp_ptr() compare function. More... | |
#define | UCX_AVL_FIND_EXACT 0 |
A mode for ucx_avl_find_node() with the same behavior as ucx_avl_get_node(). | |
#define | UCX_AVL_FIND_LOWER_BOUNDED 1 |
A mode for ucx_avl_find_node() finding the node whose key is at least as large as the specified key. | |
#define | UCX_AVL_FIND_UPPER_BOUNDED 2 |
A mode for ucx_avl_find_node() finding the node whose key is at most as large as the specified key. | |
#define | UCX_AVL_FIND_CLOSEST 3 |
A mode for ucx_avl_find_node() finding the node with a key that is as close to the specified key as possible. More... | |
Typedefs | |
typedef struct UcxAVLNode | UcxAVLNode |
UCX AVL Node type. More... | |
Functions | |
UcxAVLTree * | ucx_avl_new (cmp_func cmpfunc) |
Initializes a new UcxAVLTree with a default allocator. More... | |
UcxAVLTree * | ucx_avl_new_a (cmp_func cmpfunc, UcxAllocator *allocator) |
Initializes a new UcxAVLTree with the specified allocator. More... | |
void | ucx_avl_free (UcxAVLTree *tree) |
Destroys a UcxAVLTree. More... | |
void | ucx_avl_free_content (UcxAVLTree *tree, ucx_destructor destr) |
Frees the contents of a UcxAVLTree. More... | |
UcxAVLNode * | ucx_avl_get_node (UcxAVLTree *tree, intptr_t key) |
Gets the node from the tree, that is associated with the specified key. More... | |
void * | ucx_avl_get (UcxAVLTree *tree, intptr_t key) |
Gets the value from the tree, that is associated with the specified key. More... | |
UcxAVLNode * | ucx_avl_find_node (UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode) |
Finds a node within the tree. More... | |
void * | ucx_avl_find (UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode) |
Finds a value within the tree. More... | |
int | ucx_avl_put (UcxAVLTree *tree, intptr_t key, void *value) |
Puts a key/value pair into the tree. More... | |
int | ucx_avl_put_s (UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue) |
Puts a key/value pair into the tree. More... | |
int | ucx_avl_remove_node (UcxAVLTree *tree, UcxAVLNode *node) |
Removes a node from the AVL tree. More... | |
int | ucx_avl_remove (UcxAVLTree *tree, intptr_t key) |
Removes an element from the AVL tree. More... | |
int | ucx_avl_remove_s (UcxAVLTree *tree, intptr_t key, intptr_t *oldkey, void **oldvalue) |
Removes an element from the AVL tree. More... | |
size_t | ucx_avl_count (UcxAVLTree *tree) |
Counts the nodes in the specified UcxAVLTree. More... | |
UcxAVLNode * | ucx_avl_pred (UcxAVLNode *node) |
Finds the in-order predecessor of the given node. More... | |
UcxAVLNode * | ucx_avl_succ (UcxAVLNode *node) |
Finds the in-order successor of the given node. More... | |
AVL tree implementation.
This binary search tree implementation allows average O(1) insertion and removal of elements (excluding binary search time).
#define ucx_avl_default_new | ( | ) | ucx_avl_new_a(ucx_cmp_ptr, ucx_default_allocator()) |
Macro for initializing a new UcxAVLTree with the default allocator and a ucx_cmp_ptr() compare function.
#define UCX_AVL_FIND_CLOSEST 3 |
A mode for ucx_avl_find_node() finding the node with a key that is as close to the specified key as possible.
If the key is present, the behavior is like ucx_avl_get_node(). This mode only returns NULL
on empty trees.
typedef struct UcxAVLNode UcxAVLNode |
UCX AVL Node type.
size_t ucx_avl_count | ( | UcxAVLTree * | tree | ) |
void* ucx_avl_find | ( | UcxAVLTree * | tree, |
intptr_t | key, | ||
distance_func | dfnc, | ||
int | mode | ||
) |
Finds a value within the tree.
See ucx_avl_find_node() for details.
tree | the UcxAVLTree |
key | the key |
dfnc | the distance function |
mode | the find mode |
NULL
, if no value can be found) UcxAVLNode* ucx_avl_find_node | ( | UcxAVLTree * | tree, |
intptr_t | key, | ||
distance_func | dfnc, | ||
int | mode | ||
) |
Finds a node within the tree.
The following modes are supported:
NULL
on empty trees. The distance function provided MUST agree with the compare function of the AVL tree.
tree | the UcxAVLTree |
key | the key |
dfnc | the distance function |
mode | the find mode |
NULL
, if no node can be found) void ucx_avl_free | ( | UcxAVLTree * | tree | ) |
Destroys a UcxAVLTree.
Note, that the contents are not automatically freed. Use may use ucx_avl_free_content() before calling this function.
tree | the tree to destroy |
void ucx_avl_free_content | ( | UcxAVLTree * | tree, |
ucx_destructor | destr | ||
) |
Frees the contents of a UcxAVLTree.
This is a convenience function that iterates over the tree and passes all values to the specified destructor function.
If no destructor is specified (NULL
), the free() function of the tree's own allocator is used.
You must ensure, that it is valid to pass each value in the map to the same destructor function.
You should free the entire tree afterwards, as the contents will be invalid.
tree | for which the contents shall be freed |
destr | optional pointer to a destructor function |
void* ucx_avl_get | ( | UcxAVLTree * | tree, |
intptr_t | key | ||
) |
Gets the value from the tree, that is associated with the specified key.
tree | the UcxAVLTree |
key | the key |
NULL
, if the key is not present) UcxAVLNode* ucx_avl_get_node | ( | UcxAVLTree * | tree, |
intptr_t | key | ||
) |
Gets the node from the tree, that is associated with the specified key.
tree | the UcxAVLTree |
key | the key |
NULL
, if the key is not present) UcxAVLTree* ucx_avl_new | ( | cmp_func | cmpfunc | ) |
Initializes a new UcxAVLTree with a default allocator.
cmpfunc | the compare function that shall be used |
UcxAVLTree* ucx_avl_new_a | ( | cmp_func | cmpfunc, |
UcxAllocator * | allocator | ||
) |
Initializes a new UcxAVLTree with the specified allocator.
The cmpfunc should be capable of comparing two keys within this AVL tree. So if you want to use null terminated strings as keys, you could use the ucx_cmp_str() function here.
cmpfunc | the compare function that shall be used |
allocator | the UcxAllocator that shall be used |
UcxAVLNode* ucx_avl_pred | ( | UcxAVLNode * | node | ) |
Finds the in-order predecessor of the given node.
node | an AVL node |
NULL
if the given node is the in-order minimum int ucx_avl_put | ( | UcxAVLTree * | tree, |
intptr_t | key, | ||
void * | value | ||
) |
Puts a key/value pair into the tree.
Attention: use this function only, if a possible old value does not need to be preserved.
tree | the UcxAVLTree |
key | the key |
value | the new value |
int ucx_avl_put_s | ( | UcxAVLTree * | tree, |
intptr_t | key, | ||
void * | value, | ||
void ** | oldvalue | ||
) |
Puts a key/value pair into the tree.
This is a secure function which saves the old value to the variable pointed at by oldvalue.
tree | the UcxAVLTree |
key | the key |
value | the new value |
oldvalue | optional: a pointer to the location where a possible old value shall be stored |
int ucx_avl_remove | ( | UcxAVLTree * | tree, |
intptr_t | key | ||
) |
Removes an element from the AVL tree.
tree | the UcxAVLTree |
key | the key |
int ucx_avl_remove_node | ( | UcxAVLTree * | tree, |
UcxAVLNode * | node | ||
) |
Removes a node from the AVL tree.
Note: the specified node is logically removed. The tree implementation decides which memory area is freed. In most cases the here provided node is freed, so its further use is generally undefined.
tree | the UcxAVLTree |
node | the node to remove |
int ucx_avl_remove_s | ( | UcxAVLTree * | tree, |
intptr_t | key, | ||
intptr_t * | oldkey, | ||
void ** | oldvalue | ||
) |
Removes an element from the AVL tree.
This is a secure function which saves the old key and value data from node to the variables at the location of oldkey and oldvalue (if specified), so they can be freed afterwards (if necessary).
Note: the returned key in oldkey is possibly not the same as the provided key for the lookup (in terms of memory location).
tree | the UcxAVLTree |
key | the key of the element to remove |
oldkey | optional: a pointer to the location where the old key shall be stored |
oldvalue | optional: a pointer to the location where the old value shall be stored |
UcxAVLNode* ucx_avl_succ | ( | UcxAVLNode * | node | ) |
Finds the in-order successor of the given node.
node | an AVL node |
NULL
if the given node is the in-order maximum