748 /** |
748 /** |
749 * The number of currently stored elements. |
749 * The number of currently stored elements. |
750 */ |
750 */ |
751 size_t size; |
751 size_t size; |
752 |
752 |
753 /* |
753 /** |
754 * Offset in the node struct for the parent pointer. |
754 * Offset in the node struct for the parent pointer. |
755 */ |
755 */ |
756 ptrdiff_t loc_parent; |
756 ptrdiff_t loc_parent; |
757 |
757 |
758 /* |
758 /** |
759 * Offset in the node struct for the children linked list. |
759 * Offset in the node struct for the children linked list. |
760 */ |
760 */ |
761 ptrdiff_t loc_children; |
761 ptrdiff_t loc_children; |
762 |
762 |
763 /* |
763 /** |
764 * Optional offset in the node struct for the pointer to the last child |
764 * Optional offset in the node struct for the pointer to the last child |
765 * in the linked list (negative if there is no such pointer). |
765 * in the linked list (negative if there is no such pointer). |
766 */ |
766 */ |
767 ptrdiff_t loc_last_child; |
767 ptrdiff_t loc_last_child; |
768 |
768 |
769 /* |
769 /** |
770 * Offset in the node struct for the previous sibling pointer. |
770 * Offset in the node struct for the previous sibling pointer. |
771 */ |
771 */ |
772 ptrdiff_t loc_prev; |
772 ptrdiff_t loc_prev; |
773 |
773 |
774 /* |
774 /** |
775 * Offset in the node struct for the next sibling pointer. |
775 * Offset in the node struct for the next sibling pointer. |
776 */ |
776 */ |
777 ptrdiff_t loc_next; |
777 ptrdiff_t loc_next; |
778 }; |
778 }; |
779 |
779 |
859 /** |
859 /** |
860 * Common type for all tree implementations. |
860 * Common type for all tree implementations. |
861 */ |
861 */ |
862 typedef struct cx_tree_s CxTree; |
862 typedef struct cx_tree_s CxTree; |
863 |
863 |
|
864 /** |
|
865 * Creates a new tree structure based on the specified layout. |
|
866 * |
|
867 * The specified \p allocator will be used for creating the tree struct |
|
868 * and SHALL be used by \p create_func to allocate memory for the nodes. |
|
869 * |
|
870 * @param allocator the allocator that shall be used |
|
871 * @param create_func a function that creates new nodes |
|
872 * @param search_func a function that compares two nodes |
|
873 * @param loc_parent offset in the node struct for the parent pointer |
|
874 * @param loc_children offset in the node struct for the children linked list |
|
875 * @param loc_last_child optional offset in the node struct for the pointer to |
|
876 * the last child in the linked list (negative if there is no such pointer) |
|
877 * @param loc_prev offset in the node struct for the prev pointer |
|
878 * @param loc_next offset in the node struct for the next pointer |
|
879 * @return the new tree |
|
880 * @see cxTreeCreateSimple() |
|
881 */ |
|
882 __attribute__((__nonnull__, __warn_unused_result__)) |
|
883 CxTree *cxTreeCreate( |
|
884 const CxAllocator *allocator, |
|
885 cx_tree_node_create_func create_func, |
|
886 cx_tree_search_func search_func, |
|
887 ptrdiff_t loc_parent, |
|
888 ptrdiff_t loc_children, |
|
889 ptrdiff_t loc_last_child, |
|
890 ptrdiff_t loc_prev, |
|
891 ptrdiff_t loc_next |
|
892 ); |
|
893 |
|
894 /** |
|
895 * Creates a new tree structure based on a default layout. |
|
896 * |
|
897 * Nodes created by \p create_func MUST contain #cx_tree_node_base_s as first |
|
898 * member (or at least respect the default offsets specified in the tree |
|
899 * struct) and they MUST be allocated with the default stdlib allocator. |
|
900 * |
|
901 * \attention This function will also register a simple destructor which |
|
902 * will free the nodes with the default stdlib allocator. |
|
903 * |
|
904 * @param create_func a function that creates new nodes |
|
905 * @param search_func a function that compares two nodes |
|
906 * @return the new tree |
|
907 * @see cxTreeCreate() |
|
908 */ |
|
909 __attribute__((__nonnull__, __warn_unused_result__)) |
|
910 static inline CxTree *cxTreeCreateSimple( |
|
911 cx_tree_node_create_func create_func, |
|
912 cx_tree_search_func search_func |
|
913 ) { |
|
914 return cxTreeCreate(cxDefaultAllocator, |
|
915 create_func, search_func, cx_tree_node_base_layout); |
|
916 } |
|
917 |
|
918 /** |
|
919 * Destroys the tree structure. |
|
920 * |
|
921 * \attention This function will only invoke the destructor functions |
|
922 * on the nodes, if specified. |
|
923 * It will NOT automatically free the nodes with the allocator, because that |
|
924 * will cause a double-free in scenarios where this tree structure is wrapping |
|
925 * a tree which memory is managed by someone else. |
|
926 * |
|
927 * @param tree the tree to destroy |
|
928 */ |
|
929 __attribute__((__nonnull__)) |
|
930 void cxTreeDestroy(CxTree *tree); |
|
931 |
864 #ifdef __cplusplus |
932 #ifdef __cplusplus |
865 } // extern "C" |
933 } // extern "C" |
866 #endif |
934 #endif |
867 |
935 |
868 #endif //UCX_TREE_H |
936 #endif //UCX_TREE_H |