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

Function algorithm_shuffle

algorithm/algorithm.c:846–871  ·  view source on GitHub ↗

* @brief Randomly shuffles elements in an array. * * @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. * @param rng Random number generator function. */

Source from the content-addressed store, hash-verified

844 }
845 if (desc == n) { /* strictly descending */
846 reverse(a, a + n * sz, sz);
847 return;
848 }
849 }
850
851 /* Cache blocking: each block of about STB_BLOCK_BYTES is sorted completely
852 while it sits in the cache; only the remaining merge levels stream
853 through the whole array. */
854 size_t block = STB_BLOCK_BYTES / sz;
855 size_t run_levels = 0;
856 while (((size_t)STB_RUN << run_levels) < block) {
857 ++run_levels;
858 }
859 block = (size_t)STB_RUN << run_levels; /* a power-of-two multiple of STB_RUN */
860 for (size_t b0 = 0; b0 < n; b0 += block) {
861 size_t bn = (n - b0 < block) ? n - b0 : block;
862 char *ba = a + b0 * sz;
863 for (size_t i = 0; i < bn; i += STB_RUN) {
864 size_t m = (bn - i < STB_RUN) ? bn - i : STB_RUN;
865 pdq_insertion(ba + i * sz, m, sz, comp, tmp, 1);
866 }
867 const char *bsrc = ba;
868 char *bdst = buf + b0 * sz;
869 for (size_t width = STB_RUN; width < bn; width *= 2) {
870 stb_merge_pass(bsrc, bdst, bn, width, sz, comp);
871 const char *t = bsrc;
872 bsrc = bdst;
873 bdst = (char *)t;
874 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected