903 /** |
903 /** |
904 * Creates a new tree structure based on a default layout. |
904 * Creates a new tree structure based on a default layout. |
905 * |
905 * |
906 * Nodes created by \p create_func MUST contain #cx_tree_node_base_s as first |
906 * Nodes created by \p create_func MUST contain #cx_tree_node_base_s as first |
907 * member (or at least respect the default offsets specified in the tree |
907 * member (or at least respect the default offsets specified in the tree |
908 * struct) and they MUST be allocated with the default stdlib allocator. |
908 * struct) and they MUST be allocated with the specified allocator. |
909 * |
909 * |
910 * \note This function will also register an advanced destructor which |
910 * \note This function will also register an advanced destructor which |
911 * will free the nodes with the #cxDefaultAllocator. |
911 * will free the nodes with the allocator's free() method. |
912 * |
912 * |
|
913 * @param allocator the allocator that shall be used |
913 * @param create_func a function that creates new nodes |
914 * @param create_func a function that creates new nodes |
914 * @param search_func a function that compares two nodes |
915 * @param search_func a function that compares two nodes |
915 * @return the new tree |
916 * @return the new tree |
916 * @see cxTreeCreate() |
917 * @see cxTreeCreate() |
917 */ |
918 */ |
918 __attribute__((__nonnull__, __warn_unused_result__)) |
919 __attribute__((__nonnull__, __warn_unused_result__)) |
919 static inline CxTree *cxTreeCreateSimple( |
920 static inline CxTree *cxTreeCreateSimple( |
|
921 const CxAllocator *allocator, |
920 cx_tree_node_create_func create_func, |
922 cx_tree_node_create_func create_func, |
921 cx_tree_search_func search_func |
923 cx_tree_search_func search_func |
922 ) { |
924 ) { |
923 return cxTreeCreate(cxDefaultAllocator, |
925 return cxTreeCreate( |
924 create_func, search_func, cx_tree_node_base_layout); |
926 allocator, |
|
927 create_func, |
|
928 search_func, |
|
929 cx_tree_node_base_layout |
|
930 ); |
925 } |
931 } |
926 |
932 |
927 /** |
933 /** |
928 * Creates a new tree structure based on an existing tree. |
934 * Creates a new tree structure based on an existing tree. |
929 * |
935 * |
958 /** |
964 /** |
959 * Destroys the tree structure. |
965 * Destroys the tree structure. |
960 * |
966 * |
961 * \attention This function will only invoke the destructor functions |
967 * \attention This function will only invoke the destructor functions |
962 * on the nodes, if specified. |
968 * on the nodes, if specified. |
963 * It will NOT automatically free the nodes with the allocator, because that |
969 * It will NOT additionally free the nodes with the tree's allocator, because |
964 * will cause a double-free in scenarios where this tree structure is wrapping |
970 * that would cause a double-free in most scenarios. |
965 * a tree which memory is managed by someone else. |
|
966 * |
971 * |
967 * @param tree the tree to destroy |
972 * @param tree the tree to destroy |
968 */ |
973 */ |
969 __attribute__((__nonnull__)) |
974 __attribute__((__nonnull__)) |
970 static inline void cxTreeDestroy(CxTree *tree) { |
975 static inline void cxTreeDestroy(CxTree *tree) { |
1032 size_t n |
1037 size_t n |
1033 ) { |
1038 ) { |
1034 if (n == 0) return 0; |
1039 if (n == 0) return 0; |
1035 if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0; |
1040 if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0; |
1036 CxIterator iter = cxIterator(data, elem_size, n); |
1041 CxIterator iter = cxIterator(data, elem_size, n); |
1037 return tree->cl->insert_many(tree, cxIteratorRef(iter), n); |
1042 return cxTreeInsertIter(tree, cxIteratorRef(iter), n); |
1038 } |
1043 } |
1039 |
1044 |
1040 /** |
1045 /** |
1041 * Searches the data in the specified tree. |
1046 * Searches the data in the specified tree. |
1042 * |
1047 * |