| avlc_t * |
avlc_init(int allow_dupes); |
Create an instance of a tree. Set allow_dupes to 0 to cause avlc_add()
to reject duplicate keys, or 1 to accept and store them.
|
| int |
| avlc_add( | avlc_t *tree, char *key, |
| void *value); |
|
Adds a key/value pair to the specified tree. Key must be a null-terminated
string. A copy of the key is made, so the caller's copy of the key can be
freed after calling this function. Value can be anything that fits into
sizeof(void *) bytes. Returns 1 to indicate success, or 0 to indicate
failure due to attempted duplicate key insertion on a !allow_dupes tree.
|
| void * |
avlc_get(avlc_t *tree, char *key); |
Search the tree for an exact match of key and return the associated value.
If multiple possible matches exist, the value returned is the one associated
with the first match encountered as the tree is descended. Returns NULL
if not found.
|
| void * |
avlc_del(avlc_t *tree, char *key); |
Search the tree for an exact match of key and delete the key/value pair
if found. The first match encountered as the tree is descended will be
deleted. The copy of the key string made earlier by avlc_add() is freed,
but it is the responsibility of the caller to free any data structure
pointed at by the void * value. The value is returned to indicate key
found and node deleted, or NULL if key was not found.
|
| int |
avlc_free(avlc_t *tree); |
Free all of the key/value pairs in the tree. Before using this function,
the caller may wish to use avlc_walk() and pass a function pointer to a
routine to free data structures pointed at by value.
|
| void * |
| avlc_get_len( | avlc_t *tree, char *key, |
| int len); |
|
This form of avlc_get() is intended for cases where key is not
null-terminated. If multiple possible matches exist, the key
returned is the first match encountered as the tree is descended.
|
| void * |
| avlc_search( | avlc_t *tree, char *key, |
| char *next, int maxlen); |
|
This function performs partial matching and seeks the lowest ordered
match in the list rather than the first match encountered. If a tree
consists of keys "xy" and "xz" and an avlc_search for the key "x" is
performed, the value associated with key "xy" will be returned, and
"xz" will be copied into the memory pointed at by next. Set maxlen as
you would for strncpy(next, source, maxlen).
|
| int |
avlc_walk(avlc_t *tree, avlc_func_t *func); |
Traverse the tree in order, passing the key and value to the caller's
function for each node, and halting if the caller's function returns
nonzero. This function is typically used prior to calling avlc_free()
to free memory associated with data structures pointed at by void *value.
|
| int |
| avlc_walk_parm( | avlc_t *tree, |
| avlc_func_parm_t *func, |
| void *parm); |
|
This function works just like avlc_walk(), but allows a parameter to be
passed to the caller's function.
|
|
The following five functions require #define TRACK_SUBTREE_SIZE in avlc.h.
If indexing is not needed, a small performance gain can be had by #undef-ing
the TRACK_SUBTREE_SIZE code out, which will remove the node subtree size counts
and the code that maintains them when adding to, deleting from, and rebalancing
the tree.
|
| char * |
| avlc_get_key_by_index( | avlc_t *tree, |
| int index); |
|
Return a pointer to the key at the position requested by the index if
the keys were in sorted order. Index is 1-based, so a tree with 3 elements
would return keys for index values 1, 2, or 3. Returns NULL if index is
out of range.
|
| void * |
| avlc_get_value_by_index( | avlc_t *tree, |
| int index); |
|
Return the value associated with the key at the position requested
by the index. Returns NULL if index is out of range.
|
| void * |
| avlc_get_value_and_index( | avlc_t *tree, |
| char *key, |
| int *index); |
|
Search the tree for an exact match of key and return the associated
value along with index, its ordinal position in a sorted list of all keys in the
tree. If multiple possible matches exist, the returned is the one with the lowest
ordinal value, as opposed to the first match encountered as the tree is descended.
If the key is not found, the value is NULL and the index is negative or zero.
|
| void * |
| avlc_del_index( | avlc_t *tree, |
| int *index); |
|
Deletes the key/value pair at the specified 1-based index. Returns
NULL if the index was out of range, or the value stored in the node
that was deleted. The caller can then free data structures pointed
at by the value, if any.
|
| int |
| avlc_walk_range( | avlc_t *tree, |
| avlc_func_parm_t *func, |
| void *parm, |
| int first, int last); |
|
Works like avlc_walk_parm, but only walks the tree from the specified
first index to the last. The caller's function can halt the walk by
returning nonzero.
|
Typedefs for the caller-provided functions that are called by the avlc_walk functions:
typedef int avlc_func_t(char *key, void *value);
typedef int avlc_func_parm_t(char *key, void *value, void *parm);
|