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

Function algorithm_partition

algorithm/algorithm.c:1204–1238  ·  view source on GitHub ↗

* @brief Partitions a range of elements based on a predicate function. * * This function rearranges the elements in the range `[base, base + num * size)` so that all elements * for which the predicate function `pred` returns `true` appear before those for which it returns `false`. * * @param base Pointer to the start of the array. * @param num Number of elements in the array. * @param size

Source from the content-addressed store, hash-verified

1202 if (pred(ptr + i * size)) {
1203 ALGORITHM_LOG("[algorithm_find_if] Element matching predicate found at index %zu.", i);
1204 return (void *)(ptr + i * size);
1205 }
1206 }
1207
1208 ALGORITHM_LOG("[algorithm_find_if] No element matching the predicate found.");
1209 return NULL;
1210}
1211
1212
1213/**
1214 * @brief Finds the first element in a range that does not satisfy a predicate.
1215 *
1216 * This function searches for the first element in the range `[base, base + num * size)` that does not satisfy
1217 * the predicate function `pred`.
1218 *
1219 * @param base Pointer to the start of the array.
1220 * @param num Number of elements in the array.
1221 * @param size Size of each element in bytes.
1222 * @param pred Predicate function that returns `true` for elements to be skipped.
1223 *
1224 * @return Pointer to the first element that does not satisfy the predicate, or `NULL` if all elements satisfy it.
1225 */
1226void *algorithm_find_if_not(const void *base, size_t num, size_t size, BoolPredicateFunc pred) {
1227 ALGORITHM_LOG("[algorithm_find_if_not] Info: Searching for the first element that does not satisfy the predicate in %zu elements.", num);
1228
1229 const char *ptr = (const char*)base;
1230 for (size_t i = 0; i < num; i++) {
1231 if (!pred(ptr + i * size)) {
1232 ALGORITHM_LOG("[algorithm_find_if_not] Success: Element found at index %zu.", i);
1233 return (void *)(ptr + i * size);
1234 }
1235 }
1236
1237 ALGORITHM_LOG("[algorithm_find_if_not] Info: No element found that does not satisfy the predicate.");
1238 return NULL;
1239}
1240
1241

Callers

nothing calls this directly

Calls 1

swapFunction · 0.85

Tested by

no test coverage detected