diff -r 3f324ea53152 -r 21840975d541 src/cx/tree.h --- a/src/cx/tree.h Wed Feb 14 22:32:13 2024 +0100 +++ b/src/cx/tree.h Thu Feb 15 21:54:43 2024 +0100 @@ -47,6 +47,8 @@ * Links a node to a (new) parent. * * If the node has already a parent, it is unlinked, first. + * If the parent has children already, the node is prepended to the list + * of all currently existing children. * * @param parent the parent node * @param node the node that shall be linked @@ -94,22 +96,59 @@ * contains the given \p data or if one of the children might contain * the data. * + * The function should use the returned integer to indicate how close the + * match is, where a negative number means that it does not match at all. + * * For example if a tree stores file path information, a node that is * describing a parent directory of a filename that is searched, shall - * return 1 to indicate that a child node might contain the searched item. - * On the other hand, if the node denotes a path that is not a prefix of - * the searched filename, the function would return -1 to indicate that - * the search does not need to be continued in that branch. + * return a positive number to indicate that a child node might contain the + * searched item. On the other hand, if the node denotes a path that is not a + * prefix of the searched filename, the function would return -1 to indicate + * that * the search does not need to be continued in that branch. * * @param node the node that is currently investigated * @param data the data that is searched for * * @return 0 if the node contains the data, - * 1 if one of the children might contain the data, - * -1 if neither the node, nor the children contains the data + * positive if one of the children might contain the data, + * negative if neither the node, nor the children contains the data */ typedef int (*cx_tree_search_func)(void const *node, void const* data); + +/** + * Searches for data in a tree. + * + * When the data cannot be found exactly, the search function might return a + * closest result which might be a good starting point for adding a new node + * to the tree. + * + * Depending on the tree structure it is not necessarily guaranteed that the + * "closest" match is uniquely defined. This function will search for a node + * with the best match according to the \p sfunc (meaning: the return value of + * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary + * node matching the criteria is returned. + * + * @param root the root node + * @param data the data to search for + * @param sfunc the search function + * @param result where the result shall be stored + * @param loc_children offset in the node struct for the children linked list + * @param loc_next offset in the node struct for the next pointer + * @return zero if the node was found exactly, positive if a node was found that + * could contain the node (but doesn't right now), negative if the tree does not + * contain any node that might be related to the searched data + */ +__attribute__((__nonnull__)) +int cx_tree_search( + void const *root, + void const *data, + cx_tree_search_func sfunc, + void **result, + ptrdiff_t loc_children, + ptrdiff_t loc_next +); + #ifdef __cplusplus } // extern "C" #endif