src/cx/tree.h

changeset 905
39aa4f106a71
parent 904
cdc49211d87f
equal deleted inserted replaced
904:cdc49211d87f 905:39aa4f106a71
434 * @param loc_children offset in the node struct for the children linked list 434 * @param loc_children offset in the node struct for the children linked list
435 * @param loc_next offset in the node struct for the next pointer 435 * @param loc_next offset in the node struct for the next pointer
436 * @return the new tree iterator 436 * @return the new tree iterator
437 * @see cxTreeIteratorDispose() 437 * @see cxTreeIteratorDispose()
438 */ 438 */
439 __attribute__((__nonnull__))
440 CxTreeIterator cx_tree_iterator( 439 CxTreeIterator cx_tree_iterator(
441 void *root, 440 void *root,
442 bool visit_on_exit, 441 bool visit_on_exit,
443 ptrdiff_t loc_children, 442 ptrdiff_t loc_children,
444 ptrdiff_t loc_next 443 ptrdiff_t loc_next
460 * @param loc_children offset in the node struct for the children linked list 459 * @param loc_children offset in the node struct for the children linked list
461 * @param loc_next offset in the node struct for the next pointer 460 * @param loc_next offset in the node struct for the next pointer
462 * @return the new tree visitor 461 * @return the new tree visitor
463 * @see cxTreeVisitorDispose() 462 * @see cxTreeVisitorDispose()
464 */ 463 */
465 __attribute__((__nonnull__))
466 CxTreeVisitor cx_tree_visitor( 464 CxTreeVisitor cx_tree_visitor(
467 void *root, 465 void *root,
468 ptrdiff_t loc_children, 466 ptrdiff_t loc_children,
469 ptrdiff_t loc_next 467 ptrdiff_t loc_next
470 ); 468 );
739 * A function to compare two nodes. 737 * A function to compare two nodes.
740 */ 738 */
741 cx_tree_search_func search; 739 cx_tree_search_func search;
742 740
743 /** 741 /**
742 * A function to compare a node with data.
743 */
744 cx_tree_search_data_func search_data;
745
746 /**
744 * The number of currently stored elements. 747 * The number of currently stored elements.
745 */ 748 */
746 size_t size; 749 size_t size;
747 750
748 /** 751 /**
876 * will free the nodes with the allocator's free() method. 879 * will free the nodes with the allocator's free() method.
877 * 880 *
878 * @param allocator the allocator that shall be used 881 * @param allocator the allocator that shall be used
879 * @param create_func a function that creates new nodes 882 * @param create_func a function that creates new nodes
880 * @param search_func a function that compares two nodes 883 * @param search_func a function that compares two nodes
884 * @param search_data_func a function that compares a node with data
881 * @param loc_parent offset in the node struct for the parent pointer 885 * @param loc_parent offset in the node struct for the parent pointer
882 * @param loc_children offset in the node struct for the children linked list 886 * @param loc_children offset in the node struct for the children linked list
883 * @param loc_last_child optional offset in the node struct for the pointer to 887 * @param loc_last_child optional offset in the node struct for the pointer to
884 * the last child in the linked list (negative if there is no such pointer) 888 * the last child in the linked list (negative if there is no such pointer)
885 * @param loc_prev offset in the node struct for the prev pointer 889 * @param loc_prev offset in the node struct for the prev pointer
891 __attribute__((__nonnull__, __warn_unused_result__)) 895 __attribute__((__nonnull__, __warn_unused_result__))
892 CxTree *cxTreeCreate( 896 CxTree *cxTreeCreate(
893 const CxAllocator *allocator, 897 const CxAllocator *allocator,
894 cx_tree_node_create_func create_func, 898 cx_tree_node_create_func create_func,
895 cx_tree_search_func search_func, 899 cx_tree_search_func search_func,
900 cx_tree_search_data_func search_data_func,
896 ptrdiff_t loc_parent, 901 ptrdiff_t loc_parent,
897 ptrdiff_t loc_children, 902 ptrdiff_t loc_children,
898 ptrdiff_t loc_last_child, 903 ptrdiff_t loc_last_child,
899 ptrdiff_t loc_prev, 904 ptrdiff_t loc_prev,
900 ptrdiff_t loc_next 905 ptrdiff_t loc_next
911 * will free the nodes with the allocator's free() method. 916 * will free the nodes with the allocator's free() method.
912 * 917 *
913 * @param allocator the allocator that shall be used 918 * @param allocator the allocator that shall be used
914 * @param create_func a function that creates new nodes 919 * @param create_func a function that creates new nodes
915 * @param search_func a function that compares two nodes 920 * @param search_func a function that compares two nodes
921 * @param search_data_func a function that compares a node with data
916 * @return the new tree 922 * @return the new tree
917 * @see cxTreeCreate() 923 * @see cxTreeCreate()
918 */ 924 */
919 __attribute__((__nonnull__, __warn_unused_result__)) 925 __attribute__((__nonnull__, __warn_unused_result__))
920 static inline CxTree *cxTreeCreateSimple( 926 static inline CxTree *cxTreeCreateSimple(
921 const CxAllocator *allocator, 927 const CxAllocator *allocator,
922 cx_tree_node_create_func create_func, 928 cx_tree_node_create_func create_func,
923 cx_tree_search_func search_func 929 cx_tree_search_func search_func,
930 cx_tree_search_data_func search_data_func
924 ) { 931 ) {
925 return cxTreeCreate( 932 return cxTreeCreate(
926 allocator, 933 allocator,
927 create_func, 934 create_func,
928 search_func, 935 search_func,
936 search_data_func,
929 cx_tree_node_base_layout 937 cx_tree_node_base_layout
930 ); 938 );
931 } 939 }
932 940
933 /** 941 /**
934 * Creates a new tree structure based on an existing tree. 942 * Creates a new tree structure based on an existing tree.
935 * 943 *
936 * The specified \p allocator will be used for creating the tree struct 944 * The specified \p allocator will be used for creating the tree struct.
937 * and SHALL be used by \p create_func to allocate memory for the nodes.
938 * 945 *
939 * \attention This function will create an incompletely defined tree structure 946 * \attention This function will create an incompletely defined tree structure
940 * where neither the create function, the search function, nor a destructor 947 * where neither the create function, the search function, nor a destructor
941 * will be set. If you wish to use any of this functionality for the wrapped 948 * will be set. If you wish to use any of this functionality for the wrapped
942 * tree, you need to specify those functions afterwards. 949 * tree, you need to specify those functions afterwards.
951 * @return the new tree 958 * @return the new tree
952 * @see cxTreeCreate() 959 * @see cxTreeCreate()
953 */ 960 */
954 __attribute__((__nonnull__, __warn_unused_result__)) 961 __attribute__((__nonnull__, __warn_unused_result__))
955 CxTree *cxTreeCreateWrapped( 962 CxTree *cxTreeCreateWrapped(
963 const CxAllocator *allocator,
956 void *root, 964 void *root,
957 ptrdiff_t loc_parent, 965 ptrdiff_t loc_parent,
958 ptrdiff_t loc_children, 966 ptrdiff_t loc_children,
959 ptrdiff_t loc_last_child, 967 ptrdiff_t loc_last_child,
960 ptrdiff_t loc_prev, 968 ptrdiff_t loc_prev,
1043 } 1051 }
1044 1052
1045 /** 1053 /**
1046 * Searches the data in the specified tree. 1054 * Searches the data in the specified tree.
1047 * 1055 *
1048 * \remark For this function to work, the tree needs a specified search 1056 * \remark For this function to work, the tree needs a specified \c search_data
1049 * function, which might not be available wrapped trees 1057 * function, which might not be available wrapped trees
1050 * (see #cxTreeCreateWrapped()). 1058 * (see #cxTreeCreateWrapped()).
1051 * 1059 *
1052 * @param tree the tree 1060 * @param tree the tree
1053 * @param data the data to search for 1061 * @param data the data to search for
1065 * Searches the data in the specified subtree. 1073 * Searches the data in the specified subtree.
1066 * 1074 *
1067 * \note When \p subtree_root is not part of the \p tree, the behavior is 1075 * \note When \p subtree_root is not part of the \p tree, the behavior is
1068 * undefined. 1076 * undefined.
1069 * 1077 *
1070 * \remark For this function to work, the tree needs a specified search 1078 * \remark For this function to work, the tree needs a specified \c search_data
1071 * function, which might not be the case for wrapped trees 1079 * function, which might not be the case for wrapped trees
1072 * (see #cxTreeCreateWrapped()). 1080 * (see #cxTreeCreateWrapped()).
1073 * 1081 *
1074 * @param tree the tree 1082 * @param tree the tree
1075 * @param data the data to search for 1083 * @param data the data to search for
1104 */ 1112 */
1105 __attribute__((__nonnull__)) 1113 __attribute__((__nonnull__))
1106 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); 1114 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root);
1107 1115
1108 /** 1116 /**
1117 * Determines the depth of the entire tree.
1118 *
1119 * @param tree the tree
1120 * @return the tree depth, counting the root as one
1121 */
1122 __attribute__((__nonnull__))
1123 size_t cxTreeDepth(CxTree *tree);
1124
1125 /**
1109 * Creates a depth-first iterator for the specified tree. 1126 * Creates a depth-first iterator for the specified tree.
1110 * 1127 *
1111 * @param tree the tree to iterate 1128 * @param tree the tree to iterate
1112 * @param visit_on_exit true, if the iterator shall visit a node again when 1129 * @param visit_on_exit true, if the iterator shall visit a node again when
1113 * leaving the sub-tree 1130 * leaving the sub-tree
1177 void *parent, 1194 void *parent,
1178 const void *data 1195 const void *data
1179 ); 1196 );
1180 1197
1181 /** 1198 /**
1182 * Removes a node from the tree. 1199 * Removes a node and it's subtree from the tree.
1183 * 1200 *
1184 * If the node is not part of the tree, the behavior is undefined. 1201 * If the node is not part of the tree, the behavior is undefined.
1185 * 1202 *
1186 * \note The destructor function, if any, will \em not be invoked. 1203 * \note The destructor function, if any, will \em not be invoked. That means
1204 * you will need to free the removed subtree by yourself, eventually.
1187 * 1205 *
1188 * @param tree the tree 1206 * @param tree the tree
1189 * @param node the node to remove 1207 * @param node the node to remove
1190 */ 1208 */
1191 __attribute__((__nonnull__)) 1209 __attribute__((__nonnull__))
1192 static inline void cxTreeRemove( 1210 void cxTreeRemove(CxTree *tree, void *node);
1193 CxTree *tree,
1194 void *node) {
1195 cx_tree_unlink(node, cx_tree_node_layout(tree));
1196 tree->size--;
1197 }
1198 1211
1199 #ifdef __cplusplus 1212 #ifdef __cplusplus
1200 } // extern "C" 1213 } // extern "C"
1201 #endif 1214 #endif
1202 1215

mercurial