Each "tree" is defined by a number of "clientnodes" which are ordered according to a client defined "key".
A "clientkey" is a client specific entity which can be compared with a "clientnode" to determine which is the "greatest". In order to do this the client needs provide a comparison function for comparing a "clientnode" with a "clientkey", and a function to convert a "clientnode" into a "clientkey".
The client is responsible for providing each "clientnode" in a tree. This will typically contain some client specific data, but will also need to contain a bintree "node" which is used to structurally relate each node to one another in the tree. A specific "node" may only belong to one tree at a time, but an individual "clientnode" may contain multiple of these if necessary.
#define lub_bintree_MAX_KEY_STORAGE (200) |
This is used to size the key storage area for an opaque key. If any client requires a greater storage size then this will need to be increased.
typedef int lub_bintree_compare_fn(const void *clientnode, const void *clientkey) |
This type defines a callback function which will compare two "keys" with each other
clientnode | the client node to compare | |
clientkey | the key to compare with a node |
typedef void lub_bintree_getkey_fn(const void *clientnode, lub_bintree_key_t *key) |
This type defines a callback function which will convert a client's "node" into a search "key"
clientnode | the node from which to derive a key | |
key | a reference to the key to fill out |
typedef struct lub_bintree_iterator_s lub_bintree_iterator_t |
This is used to perform iterations of a tree
typedef struct lub_bintree_key_s lub_bintree_key_t |
This is used to declare an opaque key structure Typically a client would declare their own non-opaque structure which they would fill out appropriately
typedef struct lub_bintree_node_s lub_bintree_node_t |
This type represents a bintree "node". Typically the client will have a "clientnode" structure which contains it's data. A bintree "node" is made one of the data elements of that structure. When the tree is initialised the client provides the offset into the structure of the "node" which is to be used for that tree.
typedef struct lub_bintree_s lub_bintree_t |
This type represents an binary tree instance
void lub_bintree_dump | ( | lub_bintree_t * | tree | ) |
This operation dumps the node list of the specified tree to stdout
tree | the "tree" instance to invoke this operation upon |
void* lub_bintree_find | ( | lub_bintree_t * | tree, | |
const void * | key | |||
) |
This operation searches the specified "tree" for a "clientnode" which matches the specified "key"
tree | the "tree" instance to invoke this operation upon |
key | the "key" to search with |
void* lub_bintree_findfirst | ( | lub_bintree_t * | tree | ) |
This operation returns the first "clientnode" present in the specified "tree"
tree | the "tree" instance to invoke this operation upon |
void* lub_bintree_findlast | ( | lub_bintree_t * | tree | ) |
This operation returns the last "clientnode" present in the specified "tree"
tree | the "tree" instance to invoke this operation upon |
void* lub_bintree_findnext | ( | lub_bintree_t * | tree, | |
const void * | key | |||
) |
This operation searches the specified "tree" for a "clientnode" which is the one which logically follows the specified "key"
A "clientnode" with the specified "key" doesn't need to be in the tree.
tree | the "tree" instance to invoke this operation upon |
key | the "key" to search with |
void* lub_bintree_findprevious | ( | lub_bintree_t * | tree, | |
const void * | key | |||
) |
This operation searches the specified "tree" for a "clientnode" which is the one which logically preceeds the specified "key"
A "clientnode" with the specified "key" doesn't need to be in the tree.
tree | the "tree" instance to invoke this operation upon |
key | the "key" to search with |
void lub_bintree_init | ( | lub_bintree_t * | tree, | |
size_t | node_offset, | |||
lub_bintree_compare_fn | compareFn, | |||
lub_bintree_getkey_fn | getkeyFn | |||
) |
This operation initialises an instance of a binary tree.
tree | the "tree" instance to initialise |
node_offset | the offset of the bintree "node" structure within the "clientnode" structure. This is typically passed using the offsetof() macro. |
compareFn | a comparison function for comparing a "clientnode" with a "clientkey" |
getkeyFn | a function which will fill out a "key" from a clientnode |
int lub_bintree_insert | ( | lub_bintree_t * | tree, | |
void * | clientnode | |||
) |
This operation adds a client node to the specified tree.
The clientnode must be initialised
tree | the "tree" instance to invoke this operation upon |
clientnode | a pointer to a client node. NB the tree can find the necessary lub_BintreeNodeT from it's stored offset. |
void lub_bintree_iterator_init | ( | lub_bintree_iterator_t * | iter, | |
lub_bintree_t * | tree, | |||
const void * | clientnode | |||
) |
This operation initialises an iterator. This can then be subsequently used for iterating through a tree. This will work even if the "clientnode" which defined the current iterator has been removed before the next iterator operation.
The clientnode must be initialised and valid at the time of this call
iter | the iterator instance to initialise |
tree | the tree to associate with this iterator |
clientnode | the starting point for the iteration |
void* lub_bintree_iterator_next | ( | lub_bintree_iterator_t * | iter | ) |
This operation returns the next "clientnode" in an iteration.
iter | the iterator instance to invoke this operation upon. |
void* lub_bintree_iterator_previous | ( | lub_bintree_iterator_t * | iter | ) |
This operation returns the previous "clientnode" in an iteration.
iter | the iterator instance to invoke this operation upon. |
void lub_bintree_node_init | ( | lub_bintree_node_t * | node | ) |
This operation is called to initialise a "clientnode" ready for insertion into a tree. This is only required once after the memory for a node has been allocated.
node | the bintree node to initialise |
void lub_bintree_remove | ( | lub_bintree_t * | tree, | |
void * | clientnode | |||
) |
This operation removes a "clientnode" from the specified "tree"
The clientnode must be initialised
If the clientnode is not present in the specified tree, then an assert will fire.
tree | the "tree" instance to invoke this operation upon |
clientnode | the node to remove |