MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / algorithm_copy_n

Function algorithm_copy_n

algorithm/algorithm.c:1346–1361  ·  view source on GitHub ↗

* @brief Copies the first n elements from a source array to a destination array. * * @param first Pointer to the start of the source array. * @param n Number of elements to copy. * @param size Size of each element in bytes. * @param result Pointer to the destination array. */

Source from the content-addressed store, hash-verified

1344size_t algorithm_binary_search(const void *base, size_t num, size_t size, const void *val, CompareFunc comp) {
1345 ALGORITHM_LOG("[algorithm_binary_search] Info: Performing binary search on a sorted array of %zu elements.", num);
1346
1347 size_t low = 0;
1348 size_t high = num;
1349
1350 while (low < high) {
1351 size_t mid = low + (high - low) / 2;
1352 const void *mid_elem = (const char *)base + mid * size;
1353
1354 int result = comp(mid_elem, val);
1355 if (result < 0) {
1356 low = mid + 1;
1357 }
1358 else if (result > 0) {
1359 high = mid;
1360 }
1361 else {
1362 ALGORITHM_LOG("[algorithm_binary_search] Success: Element found at index %zu.", mid);
1363 return (size_t)mid;
1364 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected