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

Function algorithm_remove_copy_if

algorithm/algorithm.c:2107–2130  ·  view source on GitHub ↗

* Removes elements from the source array based on a given predicate function and copies the remaining elements to the result array. * * @param source The source array. * @param num The number of elements in the source array. * @param size The size of each element in bytes. * @param result The result array where the remaining elements will be copied. * @param pred The predicate function that

Source from the content-addressed store, hash-verified

2105}
2106
2107
2108/**
2109 * @brief Partitions a range of elements based on a predicate function.
2110 *
2111 * This function rearranges the elements in the range `[base, base + num * size)` so that all elements
2112 * for which the predicate function `pred` returns `true` appear before those for which it returns `false`.
2113 *
2114 * @param base Pointer to the start of the array.
2115 * @param num Number of elements in the array.
2116 * @param size Size of each element in bytes.
2117 * @param pred Predicate function that returns `true` for elements that should appear before others.
2118 *
2119 * @return Pointer to the first element in the second group (where `pred` returns `false`).
2120 */
2121void *algorithm_partition(void *base, size_t num, size_t size, BoolPredicateFunc pred) {
2122 ALGORITHM_LOG("[algorithm_partition] Info: Partitioning array of %zu elements.", num);
2123
2124 if (!base || !pred || num == 0 || size == 0) {
2125 return base;
2126 }
2127
2128 char *first = (char *)base;
2129 char *last = first + num * size;
2130
2131 while (1) {
2132 /* Advance `first` over pred-true elements. */
2133 while (first != last && pred(first)) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected