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

Function algorithm_replace_if

algorithm/algorithm.c:2184–2216  ·  view source on GitHub ↗

* Replaces elements in an array based on a given predicate function. * * This function iterates over the elements in the array pointed to by `base` and replaces * elements that satisfy the predicate function `pred` with the value pointed to by `new_val`. * * @param base Pointer to the start of the array. * @param num Number of elements in the array. * @param size Size of eac

Source from the content-addressed store, hash-verified

2182 *
2183 * @param first Pointer to the start of the range.
2184 * @param n Number of elements to generate.
2185 * @param size Size of each element in bytes.
2186 * @param gen Generator function that produces values.
2187 */
2188void algorithm_generate_n(void *first, size_t n, size_t size, GeneratorFunc gen) {
2189 ALGORITHM_LOG("[algorithm_generate_n] Info: Generating values for %zu elements.", n);
2190
2191 char *current = (char *)first;
2192 for (size_t i = 0; i < n; ++i) {
2193 gen(current);
2194 current += size;
2195 ALGORITHM_LOG("[algorithm_generate_n] Info: Value generated for element %zu.", i);
2196 }
2197
2198 ALGORITHM_LOG("[algorithm_generate_n] Success: Generation completed.");
2199}
2200
2201
2202/**
2203 * @brief Copies a range of elements in reverse order.
2204 *
2205 * @param first Pointer to the start of the range.
2206 * @param last Pointer to the end of the range.
2207 * @param size Size of each element in bytes.
2208 * @param result Pointer to the destination array where elements will be copied.
2209 */
2210void algorithm_copy_backward(const void *first, const void *last, size_t size, void *result) {
2211 ALGORITHM_LOG("[algorithm_copy_backward] Info: Copying elements in reverse order.");
2212
2213 if (!first || !last || !result || size == 0 || (const char *)last <= (const char *)first) {
2214 return;
2215 }
2216 size_t bytes = (size_t)((const char *)last - (const char *)first) / size * size;
2217 /* the destination ENDS at result; memmove handles the overlap that
2218 copy_backward exists for (shifting a range to the right) */
2219 memmove((char *)result - bytes, (const char *)last - bytes, bytes);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected