mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Remove old shuffle header
This commit is contained in:
parent
c7e3d9f7ff
commit
a51ccc8fb1
5 changed files with 64 additions and 32 deletions
|
@ -1,21 +0,0 @@
|
||||||
#ifndef COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_
|
|
||||||
#define COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_
|
|
||||||
#include "libc/intrin/xchg.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 /* COSMOPOLITAN_LIBC_RAND_SHUFFLE_H_ */
|
|
|
@ -44,9 +44,8 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) {
|
||||||
*n = 0;
|
*n = 0;
|
||||||
for (i = 0;; i += m) {
|
for (i = 0;; i += m) {
|
||||||
m = f->end - f->beg;
|
m = f->end - f->beg;
|
||||||
if ((p = memchr(f->buf + f->beg, delim, m))) {
|
if ((p = memchr(f->buf + f->beg, delim, m)))
|
||||||
m = p + 1 - (f->buf + f->beg);
|
m = p + 1 - (f->buf + f->beg);
|
||||||
}
|
|
||||||
if (i + m + 1 > *n) {
|
if (i + m + 1 > *n) {
|
||||||
n2 = i + m + 1;
|
n2 = i + m + 1;
|
||||||
s2 = realloc(*s, n2);
|
s2 = realloc(*s, n2);
|
||||||
|
@ -59,10 +58,9 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memcpy(*s + i, f->buf + f->beg, m);
|
memcpy(*s + i, f->buf + f->beg, m);
|
||||||
(*s)[i + m] = '\0';
|
(*s)[i + m] = 0;
|
||||||
if ((f->beg += m) == f->end) {
|
if ((f->beg += m) == f->end)
|
||||||
f->beg = f->end = 0;
|
f->beg = f->end = 0;
|
||||||
}
|
|
||||||
if (p) {
|
if (p) {
|
||||||
return i + m;
|
return i + m;
|
||||||
} else if (f->fd == -1) {
|
} else if (f->fd == -1) {
|
||||||
|
@ -71,7 +69,7 @@ ssize_t getdelim_unlocked(char **s, size_t *n, int delim, FILE *f) {
|
||||||
if (!rc)
|
if (!rc)
|
||||||
break;
|
break;
|
||||||
f->end = rc;
|
f->end = rc;
|
||||||
} else if (errno != EINTR) {
|
} else {
|
||||||
f->state = errno;
|
f->state = errno;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,23 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/mem/shuffle.internal.h"
|
|
||||||
#include "libc/stdio/rand.h"
|
#include "libc/stdio/rand.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jumbles up string.
|
* Performs Fisher-Yates shuffle on string in-place to create anagram.
|
||||||
|
*
|
||||||
|
* This implementation uses rand() so `srand(time(0))` may be desired.
|
||||||
*/
|
*/
|
||||||
char *strfry(char *s) {
|
char *strfry(char *s) {
|
||||||
shuffle(rand, s, strlen(s));
|
size_t i = strlen(s);
|
||||||
|
while (i > 1) {
|
||||||
|
size_t x = rand();
|
||||||
|
size_t y = rand();
|
||||||
|
size_t j = ((x << 31) ^ y) % i--;
|
||||||
|
char t = s[j];
|
||||||
|
s[j] = s[i];
|
||||||
|
s[i] = t;
|
||||||
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
38
test/libc/stdio/strfry_test.c
Normal file
38
test/libc/stdio/strfry_test.c
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2024 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/stdio/rand.h"
|
||||||
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
TEST(strfry, empty) {
|
||||||
|
char s[1] = "";
|
||||||
|
EXPECT_EQ(s, strfry(s));
|
||||||
|
EXPECT_STREQ("", s);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(strfry, one) {
|
||||||
|
char s[2] = "a";
|
||||||
|
EXPECT_EQ(s, strfry(s));
|
||||||
|
EXPECT_STREQ("a", s);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(strfry, test) {
|
||||||
|
char s[5] = "abcd";
|
||||||
|
EXPECT_EQ(s, strfry(s));
|
||||||
|
EXPECT_STREQ("cbda", s);
|
||||||
|
}
|
|
@ -18,7 +18,6 @@
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/mem/gc.h"
|
#include "libc/mem/gc.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/mem/shuffle.internal.h"
|
|
||||||
#include "libc/stdio/rand.h"
|
#include "libc/stdio/rand.h"
|
||||||
#include "libc/testlib/ezbench.h"
|
#include "libc/testlib/ezbench.h"
|
||||||
#include "libc/testlib/hyperion.h"
|
#include "libc/testlib/hyperion.h"
|
||||||
|
@ -82,11 +81,20 @@ TEST(utf32to8, testLargeThompsonPikeEncoded) {
|
||||||
-1, 0)));
|
-1, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void shuffle(wchar_t *a, int n) {
|
||||||
|
for (int i = n - 1; i >= 1; --i) {
|
||||||
|
int r = rand() % (i + 1);
|
||||||
|
wchar_t t = a[r];
|
||||||
|
a[r] = a[i];
|
||||||
|
a[i] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char *GenerateBranchyUtf8Text(size_t *out_n) {
|
char *GenerateBranchyUtf8Text(size_t *out_n) {
|
||||||
char *p;
|
char *p;
|
||||||
size_t n;
|
size_t n;
|
||||||
wchar_t *q = gc(utf8to32(kViewables, kViewablesSize, &n));
|
wchar_t *q = gc(utf8to32(kViewables, kViewablesSize, &n));
|
||||||
shuffle(lemur64, q, n);
|
shuffle(q, n);
|
||||||
p = utf32to8(q, n, &n);
|
p = utf32to8(q, n, &n);
|
||||||
if (out_n)
|
if (out_n)
|
||||||
*out_n = n;
|
*out_n = n;
|
||||||
|
|
Loading…
Reference in a new issue