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

Function algorithm_count

algorithm/algorithm.c:790–805  ·  view source on GitHub ↗

* @brief Counts the number of occurrences of a value in a range. * * This function iterates over the elements in the range `[base, base + num * size)` and counts * how many times the value `val` appears, as determined by the comparison function `comp`. * * @param base Pointer to the start of the array. * @param num Number of elements in the array. * @param size Size of each element in bytes

Source from the content-addressed store, hash-verified

788 * comparison chains). */
789ALG_INLINE void stb_merge_pass(const char *src, char *dst, size_t n, size_t width, size_t sz, AlgorithmCompareFunc comp) {
790 const char *pend = NULL; /* a run pair waiting for a partner */
791 char *pend_out = NULL;
792 size_t pend_n = 0;
793 for (size_t i = 0; i < n; ) {
794 size_t mid = i + ((width < n - i) ? width : n - i);
795 size_t end = mid + ((width < n - mid) ? width : n - mid);
796 const char *l = src + i * sz, *r = src + mid * sz, *re = src + end * sz;
797 char *out = dst + i * sz;
798 if (r == re || !(comp(r, r - sz) < 0)) {
799 memcpy(out, l, (size_t)(re - l)); /* already in order */
800 }
801 else if (comp(re - sz, l) < 0) { /* right run entirely smaller */
802 memcpy(out, r, (size_t)(re - r));
803 memcpy(out + (re - r), l, (size_t)(r - l));
804 }
805 else if (end - mid == mid - i) {
806 if (pend && pend_n == mid - i) {
807 stb_parity_merge2(pend, l, pend_n, pend_out, out, sz, comp);
808 pend = NULL;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected