| 111 |
111 |
| 112 > Be careful when using these functions on an array that was initialized with fixed-sized memory. |
112 > Be careful when using these functions on an array that was initialized with fixed-sized memory. |
| 113 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array |
113 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array |
| 114 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements. |
114 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements. |
| 115 >{style="note"} |
115 >{style="note"} |
| |
116 |
| |
117 ## Sorting |
| |
118 |
| |
119 ```C |
| |
120 #include <cx/array_list.h> |
| |
121 |
| |
122 void cx_array_qsort_c(void *array, size_t nmemb, size_t size, |
| |
123 cx_compare_func2 fn, void *context); |
| |
124 |
| |
125 void cx_array_sort(CxArray array, cx_compare_func fn); |
| |
126 |
| |
127 void cx_array_sort_c(CxArray array, |
| |
128 cx_compare_func2 fn, void *context); |
| |
129 ``` |
| |
130 |
| |
131 With simple `cx_compare_func` functions, arrays can always be sorted with standard `qsort()`. |
| |
132 Sorting arrays with `cx_compare_func2` functions, however, need special support by either `qsort_r()` (GNU) or `qsort_s()` (ISO). |
| |
133 However, both functions come with their own challenges. |
| |
134 On the one hand, `qsort_r()` is not ISO standard, and on the other hand `qsort_s()` is only optional and has an incorrect parameter order in the Microsoft C library. |
| |
135 |
| |
136 To provide a platform independent solution, UCX detects if `qsort_r()` is available and implements a fallback when it is not. |
| |
137 You can safely use `cx_array_qsort_c()` everywhere wehere you would use `qsort_r()`. |
| |
138 |
| |
139 The functions `cx_array_sort()` and `cx_array_sort_c()` are for convenient work with arrays declared with `CX_ARRAY()`. |
| 116 |
140 |
| 117 ## Insertion Sort |
141 ## Insertion Sort |
| 118 |
142 |
| 119 ```C |
143 ```C |
| 120 #include <cx/array_list.h> |
144 #include <cx/array_list.h> |