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

Function algorithm_find_end

algorithm/algorithm.c:383–422  ·  view source on GitHub ↗

* @brief Finds the last occurrence of a subsequence in a range. * * This function searches for the last occurrence of the sequence `[base2, base2 + num2 * size2)` in the range * `[base1, base1 + num1 * size1)`. * * @param base1 Pointer to the start of the main array. * @param num1 Number of elements in the main array. * @param size1 Size of each element in the main array in bytes. * @param

Source from the content-addressed store, hash-verified

381 if (comp(cur, cur - sz) < 0) {
382 alg_copy_elem(tmp, cur, sz);
383 char *hole = cur;
384 do {
385 alg_copy_elem(hole, hole - sz, sz);
386 hole -= sz;
387 } while ((!guarded || hole != a) && comp(tmp, hole - sz) < 0);
388 alg_copy_elem(hole, tmp, sz);
389 }
390 }
391}
392
393/* Insertion sort that gives up after PDQ_PARTIAL_LIMIT element moves; true
394 * when it sorted the range. */
395ALG_INLINE int pdq_partial_insertion(char *a, size_t n, size_t sz, AlgorithmCompareFunc comp, char *tmp) {
396 size_t moved = 0;
397 for (size_t i = 1; i < n; ++i) {
398 char *cur = a + i * sz;
399 if (comp(cur, cur - sz) < 0) {
400 alg_copy_elem(tmp, cur, sz);
401 char *hole = cur;
402 do {
403 alg_copy_elem(hole, hole - sz, sz);
404 hole -= sz;
405 } while (hole != a && comp(tmp, hole - sz) < 0);
406 alg_copy_elem(hole, tmp, sz);
407 moved += (size_t)(cur - hole) / sz;
408 if (moved > PDQ_PARTIAL_LIMIT) {
409 return 0;
410 }
411 }
412 }
413 return 1;
414}
415
416/* Sift a[i] down the max-heap a[0, end). With `tmp` the element travels
417 * through it; without (no memory for a large element) it is swapped down. */
418static void pdq_sift_down(char *a, size_t i, size_t end, size_t sz, AlgorithmCompareFunc comp, char *tmp) {
419 if (tmp) {
420 alg_copy_elem(tmp, a + i * sz, sz);
421 }
422 for (;;) {
423 size_t c = 2 * i + 1;
424 if (c >= end) {
425 break;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected