mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Fold LIBC_ALG into LIBC_MEM
This commit is contained in:
parent
7cf66bc161
commit
17aea99bb3
162 changed files with 265 additions and 430 deletions
23
libc/mem/shuffle.internal.h
Normal file
23
libc/mem/shuffle.internal.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_
|
||||
#define COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_
|
||||
#include "libc/intrin/xchg.internal.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
/**
|
||||
* Fisher-Yates shuffle.
|
||||
*
|
||||
* @param R is a function like rand() → ≥0
|
||||
* @param A is a typed array
|
||||
* @param n is the number of items in A
|
||||
* @see ARRAYLEN()
|
||||
*/
|
||||
#define shuffle(R, A, n) \
|
||||
do { \
|
||||
autotype(A) Array = (A); \
|
||||
for (size_t i = (n)-1; i >= 1; --i) { \
|
||||
xchg(&Array[i], &Array[R() % (i + 1)]); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue