296 ptrdiff_t loc_prev, |
296 ptrdiff_t loc_prev, |
297 ptrdiff_t loc_next |
297 ptrdiff_t loc_next |
298 ); |
298 ); |
299 |
299 |
300 /** |
300 /** |
|
301 * Macro that can be used instead of the magic value for infinite search depth. |
|
302 */ |
|
303 #define CX_TREE_SEARCH_INFINITE_DEPTH 0 |
|
304 |
|
305 /** |
301 * Function pointer for a search function. |
306 * Function pointer for a search function. |
302 * |
307 * |
303 * A function of this kind shall check if the specified \p node |
308 * A function of this kind shall check if the specified \p node |
304 * contains the given \p data or if one of the children might contain |
309 * contains the given \p data or if one of the children might contain |
305 * the data. |
310 * the data. |
306 * |
311 * |
307 * The function should use the returned integer to indicate how close the |
312 * The function should use the returned integer to indicate how close the |
308 * match is, where a negative number means that it does not match at all. |
313 * match is, where a negative number means that it does not match at all. |
|
314 * Zero means exact match and a positive number is an implementation defined |
|
315 * measure for the distance to an exact match. |
309 * |
316 * |
310 * For example if a tree stores file path information, a node that is |
317 * For example if a tree stores file path information, a node that is |
311 * describing a parent directory of a filename that is searched, shall |
318 * describing a parent directory of a filename that is searched, shall |
312 * return a positive number to indicate that a child node might contain the |
319 * return a positive number to indicate that a child node might contain the |
313 * searched item. On the other hand, if the node denotes a path that is not a |
320 * searched item. On the other hand, if the node denotes a path that is not a |
331 * contains the same \p data as \p new_node or if one of the children might |
338 * contains the same \p data as \p new_node or if one of the children might |
332 * contain the data. |
339 * contain the data. |
333 * |
340 * |
334 * The function should use the returned integer to indicate how close the |
341 * The function should use the returned integer to indicate how close the |
335 * match is, where a negative number means that it does not match at all. |
342 * match is, where a negative number means that it does not match at all. |
|
343 * Zero means exact match and a positive number is an implementation defined |
|
344 * measure for the distance to an exact match. |
336 * |
345 * |
337 * For example if a tree stores file path information, a node that is |
346 * For example if a tree stores file path information, a node that is |
338 * describing a parent directory of a filename that is searched, shall |
347 * describing a parent directory of a filename that is searched, shall |
339 * return a positive number to indicate that a child node might contain the |
348 * return a positive number to indicate that a child node might contain the |
340 * searched item. On the other hand, if the node denotes a path that is not a |
349 * searched item. On the other hand, if the node denotes a path that is not a |
362 * with the best match according to the \p sfunc (meaning: the return value of |
371 * with the best match according to the \p sfunc (meaning: the return value of |
363 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
372 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
364 * node matching the criteria is returned. |
373 * node matching the criteria is returned. |
365 * |
374 * |
366 * @param root the root node |
375 * @param root the root node |
|
376 * @param depth the maximum depth (zero=indefinite, one=just root) |
367 * @param data the data to search for |
377 * @param data the data to search for |
368 * @param sfunc the search function |
378 * @param sfunc the search function |
369 * @param result where the result shall be stored |
379 * @param result where the result shall be stored |
370 * @param loc_children offset in the node struct for the children linked list |
380 * @param loc_children offset in the node struct for the children linked list |
371 * @param loc_next offset in the node struct for the next pointer |
381 * @param loc_next offset in the node struct for the next pointer |
374 * contain any node that might be related to the searched data |
384 * contain any node that might be related to the searched data |
375 */ |
385 */ |
376 __attribute__((__nonnull__)) |
386 __attribute__((__nonnull__)) |
377 int cx_tree_search_data( |
387 int cx_tree_search_data( |
378 const void *root, |
388 const void *root, |
|
389 size_t depth, |
379 const void *data, |
390 const void *data, |
380 cx_tree_search_data_func sfunc, |
391 cx_tree_search_data_func sfunc, |
381 void **result, |
392 void **result, |
382 ptrdiff_t loc_children, |
393 ptrdiff_t loc_children, |
383 ptrdiff_t loc_next |
394 ptrdiff_t loc_next |
395 * with the best match according to the \p sfunc (meaning: the return value of |
406 * with the best match according to the \p sfunc (meaning: the return value of |
396 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
407 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
397 * node matching the criteria is returned. |
408 * node matching the criteria is returned. |
398 * |
409 * |
399 * @param root the root node |
410 * @param root the root node |
|
411 * @param depth the maximum depth (zero=indefinite, one=just root) |
400 * @param node the node to search for |
412 * @param node the node to search for |
401 * @param sfunc the search function |
413 * @param sfunc the search function |
402 * @param result where the result shall be stored |
414 * @param result where the result shall be stored |
403 * @param loc_children offset in the node struct for the children linked list |
415 * @param loc_children offset in the node struct for the children linked list |
404 * @param loc_next offset in the node struct for the next pointer |
416 * @param loc_next offset in the node struct for the next pointer |
407 * contain any node that might be related to the searched data |
419 * contain any node that might be related to the searched data |
408 */ |
420 */ |
409 __attribute__((__nonnull__)) |
421 __attribute__((__nonnull__)) |
410 int cx_tree_search( |
422 int cx_tree_search( |
411 const void *root, |
423 const void *root, |
|
424 size_t depth, |
412 const void *node, |
425 const void *node, |
413 cx_tree_search_func sfunc, |
426 cx_tree_search_func sfunc, |
414 void **result, |
427 void **result, |
415 ptrdiff_t loc_children, |
428 ptrdiff_t loc_children, |
416 ptrdiff_t loc_next |
429 ptrdiff_t loc_next |
1029 __attribute__((__nonnull__)) |
1043 __attribute__((__nonnull__)) |
1030 static inline void *cxTreeFind( |
1044 static inline void *cxTreeFind( |
1031 CxTree *tree, |
1045 CxTree *tree, |
1032 const void *data |
1046 const void *data |
1033 ) { |
1047 ) { |
1034 return tree->cl->find(tree, tree->root, data); |
1048 return tree->cl->find(tree, tree->root, data, 0); |
1035 } |
1049 } |
1036 |
1050 |
1037 /** |
1051 /** |
1038 * Searches the data in the specified subtree. |
1052 * Searches the data in the specified subtree. |
|
1053 * |
|
1054 * When \p max_depth is zero, the depth is not limited. |
|
1055 * The \p subtree_root itself is on depth 1 and its children have depth 2. |
1039 * |
1056 * |
1040 * \note When \p subtree_root is not part of the \p tree, the behavior is |
1057 * \note When \p subtree_root is not part of the \p tree, the behavior is |
1041 * undefined. |
1058 * undefined. |
1042 * |
1059 * |
1043 * \remark For this function to work, the tree needs a specified \c search_data |
1060 * \remark For this function to work, the tree needs a specified \c search_data |
1045 * (see #cxTreeCreateWrapped()). |
1062 * (see #cxTreeCreateWrapped()). |
1046 * |
1063 * |
1047 * @param tree the tree |
1064 * @param tree the tree |
1048 * @param data the data to search for |
1065 * @param data the data to search for |
1049 * @param subtree_root the node where to start |
1066 * @param subtree_root the node where to start |
|
1067 * @param max_depth the maximum search depth |
1050 * @return the first matching node, or \c NULL when the data cannot be found |
1068 * @return the first matching node, or \c NULL when the data cannot be found |
1051 */ |
1069 */ |
1052 __attribute__((__nonnull__)) |
1070 __attribute__((__nonnull__)) |
1053 static inline void *cxTreeFindInSubtree( |
1071 static inline void *cxTreeFindInSubtree( |
1054 CxTree *tree, |
1072 CxTree *tree, |
1055 const void *data, |
1073 const void *data, |
1056 void *subtree_root |
1074 void *subtree_root, |
|
1075 size_t max_depth |
1057 ) { |
1076 ) { |
1058 return tree->cl->find(tree, subtree_root, data); |
1077 return tree->cl->find(tree, subtree_root, data, max_depth); |
1059 } |
1078 } |
1060 |
1079 |
1061 /** |
1080 /** |
1062 * Determines the size of the specified subtree. |
1081 * Determines the size of the specified subtree. |
1063 * |
1082 * |
1093 * If the node is not part of the tree, the behavior is undefined. |
1112 * If the node is not part of the tree, the behavior is undefined. |
1094 * |
1113 * |
1095 * @param tree the tree to iterate |
1114 * @param tree the tree to iterate |
1096 * @param node the node where to start |
1115 * @param node the node where to start |
1097 * @param visit_on_exit true, if the iterator shall visit a node again when |
1116 * @param visit_on_exit true, if the iterator shall visit a node again when |
1098 * leaving the sub-tree |
1117 * leaving the subtree |
1099 * @return a tree iterator (depth-first) |
1118 * @return a tree iterator (depth-first) |
1100 * @see cxTreeVisit() |
1119 * @see cxTreeVisit() |
1101 */ |
1120 */ |
1102 __attribute__((__nonnull__, __warn_unused_result__)) |
1121 __attribute__((__nonnull__, __warn_unused_result__)) |
1103 static inline CxTreeIterator cxTreeIterateSubtree( |
1122 static inline CxTreeIterator cxTreeIterateSubtree( |
1131 /** |
1150 /** |
1132 * Creates a depth-first iterator for the specified tree. |
1151 * Creates a depth-first iterator for the specified tree. |
1133 * |
1152 * |
1134 * @param tree the tree to iterate |
1153 * @param tree the tree to iterate |
1135 * @param visit_on_exit true, if the iterator shall visit a node again when |
1154 * @param visit_on_exit true, if the iterator shall visit a node again when |
1136 * leaving the sub-tree |
1155 * leaving the subtree |
1137 * @return a tree iterator (depth-first) |
1156 * @return a tree iterator (depth-first) |
1138 * @see cxTreeVisit() |
1157 * @see cxTreeVisit() |
1139 */ |
1158 */ |
1140 __attribute__((__nonnull__, __warn_unused_result__)) |
1159 __attribute__((__nonnull__, __warn_unused_result__)) |
1141 static inline CxTreeIterator cxTreeIterate( |
1160 static inline CxTreeIterator cxTreeIterate( |