| 173 const void *elem, cx_compare_func cmp_func); |
173 const void *elem, cx_compare_func cmp_func); |
| 174 |
174 |
| 175 size_t cx_array_binary_search_sup( |
175 size_t cx_array_binary_search_sup( |
| 176 const void *arr, size_t size, size_t elem_size, |
176 const void *arr, size_t size, size_t elem_size, |
| 177 const void *elem, cx_compare_func cmp_func); |
177 const void *elem, cx_compare_func cmp_func); |
| |
178 |
| |
179 size_t cx_array_binary_search_c( |
| |
180 const void *arr, size_t size, size_t elem_size, |
| |
181 const void *elem, cx_compare_func2 cmp_func, void *context); |
| |
182 |
| |
183 size_t cx_array_binary_search_inf_c( |
| |
184 const void *arr, size_t size, size_t elem_size, |
| |
185 const void *elem, cx_compare_func2 cmp_func, void *context); |
| |
186 |
| |
187 size_t cx_array_binary_search_sup_c( |
| |
188 const void *arr, size_t size, size_t elem_size, |
| |
189 const void *elem, cx_compare_func2 cmp_func, void *context); |
| 178 ``` |
190 ``` |
| 179 |
191 |
| 180 The function `cx_array_binary_search()` searches the array `arr` for the data pointed to by `elem` using the compare function `cmp_func`. |
192 The function `cx_array_binary_search()` searches the array `arr` for the data pointed to by `elem` using the compare function `cmp_func`. |
| 181 |
193 |
| 182 Note that these functions do not operate on `CxArray` and are instead more general and also useful on plain C arrays. |
194 Note that these functions do not operate on `CxArray` and are instead more general and also useful on plain C arrays. |
| 192 |
204 |
| 193 When the found element appears more than once in the array, |
205 When the found element appears more than once in the array, |
| 194 the binary search and the infimum report the largest index |
206 the binary search and the infimum report the largest index |
| 195 and the supremum reports the smallest index of the identical items, respectively. |
207 and the supremum reports the smallest index of the identical items, respectively. |
| 196 |
208 |
| |
209 The function variants with the `_c` suffix allow additional `context` for the compare function. |
| |
210 |
| 197 > Note that all the above functions are only well-defined on arrays which are sorted with respect to the given compare function. |
211 > Note that all the above functions are only well-defined on arrays which are sorted with respect to the given compare function. |
| 198 > |
212 > |
| 199 > This can, for example, easily be achieved by calling the standard library's `qsort()` function. |
213 > This can, for example, easily be achieved by calling the standard library's `qsort()` or `qsort_s()` function. |
| 200 >{style="note"} |
214 >{style="note"} |
| 201 |
215 |
| 202 > Despite the name, neither C nor POSIX standards require the standard library's `bsearch()` function to be implemented using binary search. |
216 > Despite the name, neither C nor POSIX standards require the standard library's `bsearch()` function to be implemented using binary search. |
| 203 > But observations show that it usually is. |
217 > But observations show that it usually is. |
| 204 > This makes `cx_array_binary_search()` likely to be equivalent to `bsearch()`, except that it returns an index rather than a pointer. |
218 > This makes `cx_array_binary_search()` likely to be equivalent to `bsearch()`, except that it returns an index rather than a pointer. |