* @brief Fill a range with `start, ++start, ++start, ...` for any type. * * Genuinely generic: it doesn't care what `T` is. The caller supplies the * element size and an `increment` function that does the type-specific * `++` in place on a working value. For built-in numeric types this * library ships ready-made incrementers (`algorithm_inc_int`, * `algorithm_inc_double`, …) so the common ca
| 2286 | * zero if they are equal, and a positive value if the first element is greater than the second. |
| 2287 | * |
| 2288 | * @return A `Pair` representing the range of elements equal to `val`. |
| 2289 | * `first` points to the first element in the range, and `second` points to one past the last element. |
| 2290 | */ |
| 2291 | Pair algorithm_equal_range(const void *base, size_t num, size_t size, const void *val, CompareFunc comp) { |
| 2292 | ALGORITHM_LOG("[algorithm_equal_range] Info: Finding equal range for value in array with %zu elements.", num); |
| 2293 | |
| 2294 | Pair range; |
| 2295 | range.first = algorithm_lower_bound(base, num, size, val, comp); |
| 2296 | /* the upper bound lies at or after the lower one */ |
| 2297 | size_t skipped = (base && size) ? (size_t)((char *)range.first - (const char *)base) / size : 0; |
| 2298 | range.second = algorithm_upper_bound(range.first, num - skipped, size, val, comp); |
| 2299 | |
| 2300 | ALGORITHM_LOG("[algorithm_equal_range] Success: Equal range found."); |
| 2301 | return range; |
| 2302 | } |
| 2303 | |
| 2304 | |
| 2305 | /** |
| 2306 | * @brief Determines if a sorted range includes all the elements of another sorted range. |
| 2307 | * |
| 2308 | * This function checks if all elements of the sorted range `[first2, first2 + num2)` are contained |
| 2309 | * within the sorted range `[first1, first1 + num1)` as determined by the comparison function `comp`. |
| 2310 | * |
| 2311 | * @param first1 Pointer to the start of the first sorted array. |
| 2312 | * @param num1 Number of elements in the first array. |
| 2313 | * @param size1 Size of each element in the first array. |
| 2314 | * @param first2 Pointer to the start of the second sorted array. |
| 2315 | * @param num2 Number of elements in the second array. |
| 2316 | * @param size2 Size of each element in the second array. |
| 2317 | * @param comp Comparison function used to compare elements. |
| 2318 | * It should return a negative value if the first element is less than the second, |
| 2319 | * zero if they are equal, and a positive value if the first element is greater than the second. |
| 2320 | * |
| 2321 | * @return `true` if all elements of the second range are contained within the first range, `false` otherwise. |
| 2322 | */ |
| 2323 | bool algorithm_includes(const void *first1, size_t num1, size_t size1, const void *first2, size_t num2, size_t size2, |
| 2324 | CompareFunc comp) { |
| 2325 | ALGORITHM_LOG("[algorithm_includes] Info: Checking if array1 includes array2 with %zu and %zu elements respectively.", num1, num2); |
| 2326 | |
| 2327 | const char *ptr1 = (const char *)first1; |
| 2328 | const char *ptr2 = (const char *)first2; |
| 2329 | const char *last1 = ptr1 + num1 * size1; |
| 2330 | const char *last2 = ptr2 + num2 * size2; |
| 2331 | |
| 2332 | while (ptr2 != last2) { |
| 2333 | if ((ptr1 == last1) || comp(ptr2, ptr1) < 0) { |
nothing calls this directly
no outgoing calls
no test coverage detected