| 37 #ifndef UCX_ARRAY_LIST_H |
37 #ifndef UCX_ARRAY_LIST_H |
| 38 #define UCX_ARRAY_LIST_H |
38 #define UCX_ARRAY_LIST_H |
| 39 |
39 |
| 40 #include "list.h" |
40 #include "list.h" |
| 41 |
41 |
| 42 #ifdef __cplusplus |
|
| 43 extern "C" { |
|
| 44 #endif |
|
| 45 |
|
| 46 /** |
42 /** |
| 47 * The maximum item size in an array list that fits into |
43 * The maximum item size in an array list that fits into |
| 48 * a stack buffer when swapped. |
44 * a stack buffer when swapped. |
| 49 */ |
45 */ |
| 50 CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
46 CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
| 86 * @param elem_size size of one element |
82 * @param elem_size size of one element |
| 87 * @param capacity the initial maximum number of elements |
83 * @param capacity the initial maximum number of elements |
| 88 * @retval zero allocation was successful |
84 * @retval zero allocation was successful |
| 89 * @retval non-zero allocation failed |
85 * @retval non-zero allocation failed |
| 90 */ |
86 */ |
| 91 cx_attr_nonnull |
87 CX_EXTERN CX_NONNULL |
| 92 CX_EXPORT int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
88 int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
| 93 |
89 |
| 94 /** |
90 /** |
| 95 * Initializes an array by allocating memory. |
91 * Initializes an array by allocating memory. |
| 96 * |
92 * |
| 97 * The size is set to zero. |
93 * The size is set to zero. |
| 129 * @param array a pointer to the array structure |
125 * @param array a pointer to the array structure |
| 130 * @param data the fixed size array |
126 * @param data the fixed size array |
| 131 * @param capacity the capacity of the fixed size array |
127 * @param capacity the capacity of the fixed size array |
| 132 * @param size the number of initialized elements in the fixed size array |
128 * @param size the number of initialized elements in the fixed size array |
| 133 */ |
129 */ |
| 134 cx_attr_nonnull |
130 CX_EXTERN CX_NONNULL |
| 135 CX_EXPORT void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size); |
131 void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size); |
| 136 |
132 |
| 137 /** |
133 /** |
| 138 * Initializes an array with fixed size memory. |
134 * Initializes an array with fixed size memory. |
| 139 * |
135 * |
| 140 * This is useful, for example, when you want to work with memory on the stack |
136 * This is useful, for example, when you want to work with memory on the stack |
| 171 * @param elem_size the size of one element |
167 * @param elem_size the size of one element |
| 172 * @param capacity the new capacity |
168 * @param capacity the new capacity |
| 173 * @retval zero allocation was successful |
169 * @retval zero allocation was successful |
| 174 * @retval non-zero allocation failed |
170 * @retval non-zero allocation failed |
| 175 */ |
171 */ |
| 176 cx_attr_nonnull |
172 CX_EXTERN CX_NONNULL |
| 177 CX_EXPORT int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
173 int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
| 178 |
174 |
| 179 /** |
175 /** |
| 180 * Changes the capacity of an array. |
176 * Changes the capacity of an array. |
| 181 * |
177 * |
| 182 * If required, the size is reduced to fit into the new capacity. |
178 * If required, the size is reduced to fit into the new capacity. |
| 213 * @param elem_size the size of one element |
209 * @param elem_size the size of one element |
| 214 * @param capacity the new capacity |
210 * @param capacity the new capacity |
| 215 * @retval zero allocation was successful |
211 * @retval zero allocation was successful |
| 216 * @retval non-zero allocation failed |
212 * @retval non-zero allocation failed |
| 217 */ |
213 */ |
| 218 cx_attr_nonnull |
214 CX_EXTERN CX_NONNULL |
| 219 CX_EXPORT int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
215 int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); |
| 220 |
216 |
| 221 /** |
217 /** |
| 222 * Copies the array to a new memory region. |
218 * Copies the array to a new memory region. |
| 223 * |
219 * |
| 224 * This is useful when you have initialized the array with a fixed size memory |
220 * This is useful when you have initialized the array with a fixed size memory |
| 267 * @param other a pointer to an array of data that shall be inserted |
263 * @param other a pointer to an array of data that shall be inserted |
| 268 * @param n the number of elements that shall be inserted |
264 * @param n the number of elements that shall be inserted |
| 269 * @retval zero success |
265 * @retval zero success |
| 270 * @retval non-zero a re-allocation was necessary but failed |
266 * @retval non-zero a re-allocation was necessary but failed |
| 271 */ |
267 */ |
| 272 cx_attr_nonnull_arg(1, 2) |
268 CX_EXTERN CX_NONNULL_ARG(1, 2) |
| 273 CX_EXPORT int cx_array_insert_(const CxAllocator *allocator, CxArray *array, |
269 int cx_array_insert_(const CxAllocator *allocator, CxArray *array, |
| 274 size_t elem_size, size_t index, const void *other, size_t n); |
270 size_t elem_size, size_t index, const void *other, size_t n); |
| 275 |
271 |
| 276 /** |
272 /** |
| 277 * Appends an element to an array. |
273 * Appends an element to an array. |
| 278 * |
274 * |
| 402 * @param cmp_func the compare function |
398 * @param cmp_func the compare function |
| 403 * @param allow_duplicates @c false if duplicates shall be skipped during insertion |
399 * @param allow_duplicates @c false if duplicates shall be skipped during insertion |
| 404 * @retval zero success |
400 * @retval zero success |
| 405 * @retval non-zero a re-allocation was necessary but failed |
401 * @retval non-zero a re-allocation was necessary but failed |
| 406 */ |
402 */ |
| 407 cx_attr_nonnull |
403 CX_EXTERN CX_NONNULL |
| 408 CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, |
404 int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, |
| 409 size_t elem_size, const void *sorted_data, size_t n, |
405 size_t elem_size, const void *sorted_data, size_t n, |
| 410 cx_compare_func cmp_func, bool allow_duplicates); |
406 cx_compare_func cmp_func, bool allow_duplicates); |
| 411 |
407 |
| 412 /** |
408 /** |
| 413 * Inserts an element into a sorted array. |
409 * Inserts an element into a sorted array. |
| 559 * @param context additional context for the compare function |
555 * @param context additional context for the compare function |
| 560 * @param allow_duplicates @c false if duplicates shall be skipped during insertion |
556 * @param allow_duplicates @c false if duplicates shall be skipped during insertion |
| 561 * @retval zero success |
557 * @retval zero success |
| 562 * @retval non-zero a re-allocation was necessary but failed |
558 * @retval non-zero a re-allocation was necessary but failed |
| 563 */ |
559 */ |
| 564 cx_attr_nonnull_arg(1, 2, 4, 6) |
560 CX_EXTERN CX_NONNULL_ARG(1, 2, 4, 6) |
| 565 CX_EXPORT int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array, |
561 int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array, |
| 566 size_t elem_size, const void *sorted_data, size_t n, |
562 size_t elem_size, const void *sorted_data, size_t n, |
| 567 cx_compare_func2 cmp_func, void *context, bool allow_duplicates); |
563 cx_compare_func2 cmp_func, void *context, bool allow_duplicates); |
| 568 |
564 |
| 569 /** |
565 /** |
| 570 * Inserts an element into a sorted array. |
566 * Inserts an element into a sorted array. |
| 719 * @param nmemb the number of elements in the array |
715 * @param nmemb the number of elements in the array |
| 720 * @param size the size of one element |
716 * @param size the size of one element |
| 721 * @param fn the compare function |
717 * @param fn the compare function |
| 722 * @param context the context for the compare function |
718 * @param context the context for the compare function |
| 723 */ |
719 */ |
| 724 CX_EXPORT void cx_array_qsort_c(void *array, size_t nmemb, size_t size, |
720 CX_EXTERN CX_NONNULL |
| |
721 void cx_array_qsort_c(void *array, size_t nmemb, size_t size, |
| 725 cx_compare_func2 fn, void *context); |
722 cx_compare_func2 fn, void *context); |
| 726 |
723 |
| 727 /** |
724 /** |
| 728 * Sorts an array. |
725 * Sorts an array. |
| 729 * |
726 * |
| 731 * |
728 * |
| 732 * @param array a pointer to the array structure |
729 * @param array a pointer to the array structure |
| 733 * @param elem_size the size of one element |
730 * @param elem_size the size of one element |
| 734 * @param fn the compare function |
731 * @param fn the compare function |
| 735 */ |
732 */ |
| 736 CX_EXPORT void cx_array_sort_(CxArray *array, size_t elem_size, |
733 CX_EXTERN CX_NONNULL |
| |
734 void cx_array_sort_(CxArray *array, size_t elem_size, |
| 737 cx_compare_func fn); |
735 cx_compare_func fn); |
| 738 |
736 |
| 739 /** |
737 /** |
| 740 * Sorts an array. |
738 * Sorts an array. |
| 741 * |
739 * |
| 744 * @param array a pointer to the array structure |
742 * @param array a pointer to the array structure |
| 745 * @param elem_size the size of one element |
743 * @param elem_size the size of one element |
| 746 * @param fn the compare function |
744 * @param fn the compare function |
| 747 * @param context the context for the compare function |
745 * @param context the context for the compare function |
| 748 */ |
746 */ |
| 749 CX_EXPORT void cx_array_sort_c_(CxArray *array, size_t elem_size, |
747 CX_EXTERN CX_NONNULL |
| |
748 void cx_array_sort_c_(CxArray *array, size_t elem_size, |
| 750 cx_compare_func2 fn, void *context); |
749 cx_compare_func2 fn, void *context); |
| 751 |
750 |
| 752 /** |
751 /** |
| 753 * Sorts an array. |
752 * Sorts an array. |
| 754 * |
753 * |
| 776 * |
775 * |
| 777 * @param array a pointer to the array structure |
776 * @param array a pointer to the array structure |
| 778 * @param elem_size the size of one element |
777 * @param elem_size the size of one element |
| 779 * @return an iterator over the elements |
778 * @return an iterator over the elements |
| 780 */ |
779 */ |
| 781 cx_attr_nodiscard cx_attr_nonnull |
780 CX_EXTERN CX_NODISCARD CX_NONNULL |
| 782 CX_EXPORT CxIterator cx_array_iterator_(CxArray *array, size_t elem_size); |
781 CxIterator cx_array_iterator_(CxArray *array, size_t elem_size); |
| 783 |
782 |
| 784 /** |
783 /** |
| 785 * Creates an iterator over the elements of an array. |
784 * Creates an iterator over the elements of an array. |
| 786 * |
785 * |
| 787 * The iterator will yield pointers to the elements. |
786 * The iterator will yield pointers to the elements. |
| 802 * Internal function - do not use. |
801 * Internal function - do not use. |
| 803 * |
802 * |
| 804 * @param array the name of the array |
803 * @param array the name of the array |
| 805 * @return an iterator over the elements |
804 * @return an iterator over the elements |
| 806 */ |
805 */ |
| 807 cx_attr_nodiscard cx_attr_nonnull |
806 CX_EXTERN CX_NODISCARD CX_NONNULL |
| 808 CX_EXPORT CxIterator cx_array_iterator_ptr_(CxArray *array); |
807 CxIterator cx_array_iterator_ptr_(CxArray *array); |
| 809 |
808 |
| 810 /** |
809 /** |
| 811 * Creates an iterator over the elements of an array containing pointers. |
810 * Creates an iterator over the elements of an array containing pointers. |
| 812 * |
811 * |
| 813 * The iterator will yield the elements themselves, which are supposed to |
812 * The iterator will yield the elements themselves, which are supposed to |
| 833 * @param elem_size the size of one element |
832 * @param elem_size the size of one element |
| 834 * @param index the index of the first element to remove |
833 * @param index the index of the first element to remove |
| 835 * @param n the number of elements to remove |
834 * @param n the number of elements to remove |
| 836 * @param fast indicates whether tail elements should be copied into the gap |
835 * @param fast indicates whether tail elements should be copied into the gap |
| 837 */ |
836 */ |
| 838 cx_attr_nonnull |
837 CX_EXTERN CX_NONNULL |
| 839 CX_EXPORT void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast); |
838 void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast); |
| 840 |
839 |
| 841 /** |
840 /** |
| 842 * Removes one element from the array. |
841 * Removes one element from the array. |
| 843 * |
842 * |
| 844 * Tail elements are all moved by one. If you don't need a stable order |
843 * Tail elements are all moved by one. If you don't need a stable order |
| 910 * Internal function - do not use. |
909 * Internal function - do not use. |
| 911 * |
910 * |
| 912 * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array |
911 * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array |
| 913 * @param array a pointer to the array structure |
912 * @param array a pointer to the array structure |
| 914 */ |
913 */ |
| 915 cx_attr_nonnull |
914 CX_EXTERN CX_NONNULL |
| 916 CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array); |
915 void cx_array_free_(const CxAllocator *allocator, CxArray *array); |
| 917 |
916 |
| 918 /** |
917 /** |
| 919 * Deallocates an array. |
918 * Deallocates an array. |
| 920 * |
919 * |
| 921 * The structure is reset to zero and can be re-initialized with |
920 * The structure is reset to zero and can be re-initialized with |
| 960 * @param cmp_func the compare function |
959 * @param cmp_func the compare function |
| 961 * @return the index of the largest lower bound, or @p size |
960 * @return the index of the largest lower bound, or @p size |
| 962 * @see cx_array_binary_search_sup() |
961 * @see cx_array_binary_search_sup() |
| 963 * @see cx_array_binary_search() |
962 * @see cx_array_binary_search() |
| 964 */ |
963 */ |
| 965 cx_attr_nonnull |
964 CX_EXTERN CX_NONNULL |
| 966 CX_EXPORT size_t cx_array_binary_search_inf(const void *arr, size_t size, |
965 size_t cx_array_binary_search_inf(const void *arr, size_t size, |
| 967 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
966 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
| 968 |
967 |
| 969 /** |
968 /** |
| 970 * Searches an item in a sorted array. |
969 * Searches an item in a sorted array. |
| 971 * |
970 * |
| 983 * @return the index of the element in the array, or @p size if the element |
982 * @return the index of the element in the array, or @p size if the element |
| 984 * cannot be found |
983 * cannot be found |
| 985 * @see cx_array_binary_search_inf() |
984 * @see cx_array_binary_search_inf() |
| 986 * @see cx_array_binary_search_sup() |
985 * @see cx_array_binary_search_sup() |
| 987 */ |
986 */ |
| 988 cx_attr_nonnull |
987 CX_EXTERN CX_NONNULL |
| 989 CX_EXPORT size_t cx_array_binary_search(const void *arr, size_t size, |
988 size_t cx_array_binary_search(const void *arr, size_t size, |
| 990 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
989 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
| 991 |
990 |
| 992 /** |
991 /** |
| 993 * Searches the smallest upper bound in a sorted array. |
992 * Searches the smallest upper bound in a sorted array. |
| 994 * |
993 * |
| 1012 * @param cmp_func the compare function |
1011 * @param cmp_func the compare function |
| 1013 * @return the index of the smallest upper bound, or @p size |
1012 * @return the index of the smallest upper bound, or @p size |
| 1014 * @see cx_array_binary_search_inf() |
1013 * @see cx_array_binary_search_inf() |
| 1015 * @see cx_array_binary_search() |
1014 * @see cx_array_binary_search() |
| 1016 */ |
1015 */ |
| 1017 cx_attr_nonnull |
1016 CX_EXTERN CX_NONNULL |
| 1018 CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size, |
1017 size_t cx_array_binary_search_sup(const void *arr, size_t size, |
| 1019 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
1018 size_t elem_size, const void *elem, cx_compare_func cmp_func); |
| 1020 |
1019 |
| 1021 |
1020 |
| 1022 /** |
1021 /** |
| 1023 * Searches the largest lower bound in a sorted array. |
1022 * Searches the largest lower bound in a sorted array. |
| 1043 * @param context the context for the compare function |
1042 * @param context the context for the compare function |
| 1044 * @return the index of the largest lower bound, or @p size |
1043 * @return the index of the largest lower bound, or @p size |
| 1045 * @see cx_array_binary_search_sup() |
1044 * @see cx_array_binary_search_sup() |
| 1046 * @see cx_array_binary_search() |
1045 * @see cx_array_binary_search() |
| 1047 */ |
1046 */ |
| 1048 cx_attr_nonnull |
1047 CX_EXTERN CX_NONNULL |
| 1049 CX_EXPORT size_t cx_array_binary_search_inf_c(const void *arr, size_t size, |
1048 size_t cx_array_binary_search_inf_c(const void *arr, size_t size, |
| 1050 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
1049 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
| 1051 |
1050 |
| 1052 /** |
1051 /** |
| 1053 * Searches an item in a sorted array. |
1052 * Searches an item in a sorted array. |
| 1054 * |
1053 * |
| 1067 * @return the index of the element in the array, or @p size if the element |
1066 * @return the index of the element in the array, or @p size if the element |
| 1068 * cannot be found |
1067 * cannot be found |
| 1069 * @see cx_array_binary_search_inf() |
1068 * @see cx_array_binary_search_inf() |
| 1070 * @see cx_array_binary_search_sup() |
1069 * @see cx_array_binary_search_sup() |
| 1071 */ |
1070 */ |
| 1072 cx_attr_nonnull |
1071 CX_EXTERN CX_NONNULL |
| 1073 CX_EXPORT size_t cx_array_binary_search_c(const void *arr, size_t size, |
1072 size_t cx_array_binary_search_c(const void *arr, size_t size, |
| 1074 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
1073 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
| 1075 |
1074 |
| 1076 /** |
1075 /** |
| 1077 * Searches the smallest upper bound in a sorted array. |
1076 * Searches the smallest upper bound in a sorted array. |
| 1078 * |
1077 * |
| 1097 * @param context the context for the compare function |
1096 * @param context the context for the compare function |
| 1098 * @return the index of the smallest upper bound, or @p size |
1097 * @return the index of the smallest upper bound, or @p size |
| 1099 * @see cx_array_binary_search_inf() |
1098 * @see cx_array_binary_search_inf() |
| 1100 * @see cx_array_binary_search() |
1099 * @see cx_array_binary_search() |
| 1101 */ |
1100 */ |
| 1102 cx_attr_nonnull |
1101 CX_EXTERN CX_NONNULL |
| 1103 CX_EXPORT size_t cx_array_binary_search_sup_c(const void *arr, size_t size, |
1102 size_t cx_array_binary_search_sup_c(const void *arr, size_t size, |
| 1104 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
1103 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); |
| 1105 |
1104 |
| 1106 /** |
1105 /** |
| 1107 * Swaps two array elements. |
1106 * Swaps two array elements. |
| 1108 * |
1107 * |
| 1109 * @param arr the array |
1108 * @param arr the array |
| 1110 * @param elem_size the element size |
1109 * @param elem_size the element size |
| 1111 * @param idx1 index of the first element |
1110 * @param idx1 index of the first element |
| 1112 * @param idx2 index of the second element |
1111 * @param idx2 index of the second element |
| 1113 */ |
1112 */ |
| 1114 cx_attr_nonnull |
1113 CX_EXTERN CX_NONNULL |
| 1115 CX_EXPORT void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2); |
1114 void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2); |
| 1116 |
1115 |
| 1117 /** |
1116 /** |
| 1118 * Allocates an array list for storing elements with @p elem_size bytes each. |
1117 * Allocates an array list for storing elements with @p elem_size bytes each. |
| 1119 * |
1118 * |
| 1120 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
1119 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 1125 * (if @c NULL, the cxDefaultAllocator will be used) |
1124 * (if @c NULL, the cxDefaultAllocator will be used) |
| 1126 * @param elem_size the size of each element in bytes |
1125 * @param elem_size the size of each element in bytes |
| 1127 * @param initial_capacity the initial number of elements the array can store |
1126 * @param initial_capacity the initial number of elements the array can store |
| 1128 * @return the created list |
1127 * @return the created list |
| 1129 */ |
1128 */ |
| 1130 cx_attr_nodiscard |
1129 CX_EXTERN CX_NODISCARD CX_MALLOC CX_DEALLOC(cxListFree, 1) |
| 1131 cx_attr_malloc |
1130 CxList *cxArrayListCreate(const CxAllocator *allocator, |
| 1132 cx_attr_dealloc(cxListFree, 1) |
|
| 1133 CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator, |
|
| 1134 size_t elem_size, size_t initial_capacity); |
1131 size_t elem_size, size_t initial_capacity); |
| 1135 |
1132 |
| 1136 #ifdef __cplusplus |
|
| 1137 } // extern "C" |
|
| 1138 #endif |
|
| 1139 |
|
| 1140 #endif // UCX_ARRAY_LIST_H |
1133 #endif // UCX_ARRAY_LIST_H |