summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
committerTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
commitecc0b2108cffeb725e3b8e2574b6fb7927dfd96e (patch)
tree221a40b1e41f64353e2b3143277a250ef2ae2ce5 /misc
parenta203314647b5c37c7e40230551457f006ff36cd5 (diff)
downloadSTC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.tar.gz
STC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.zip
Improved/simplified c_forfilter (): last optional parameter gone. Now c_flt_take() and c_flt_takewhile() breaks the loop always.
c11/fmt.h : renamed fmt_freebuffer(buf) => fmt_destroy(buf).
Diffstat (limited to 'misc')
-rw-r--r--misc/benchmarks/shootout_hashmaps.cpp12
-rw-r--r--misc/examples/forfilter.c12
-rw-r--r--misc/examples/forloops.c2
-rwxr-xr-xmisc/examples/make.sh2
-rw-r--r--misc/examples/prime.c2
-rw-r--r--misc/examples/triples.c34
6 files changed, 33 insertions, 31 deletions
diff --git a/misc/benchmarks/shootout_hashmaps.cpp b/misc/benchmarks/shootout_hashmaps.cpp
index da523d71..39ad1786 100644
--- a/misc/benchmarks/shootout_hashmaps.cpp
+++ b/misc/benchmarks/shootout_hashmaps.cpp
@@ -2,7 +2,7 @@
#include <time.h>
#include <stc/crandom.h>
-#define MAX_LOAD_FACTOR 77
+#define MAX_LOAD_FACTOR 85
#ifdef __cplusplus
#include <limits>
@@ -314,7 +314,7 @@ int main(int argc, char* argv[])
unsigned n = n_mill * 1000000;
unsigned N1 = n, N2 = n, N3 = n, N4 = n, N5 = n;
stc64_t rng;
- size_t seed = time(NULL);
+ size_t seed = 123456; // time(NULL);
printf("\nUnordered hash map shootout\n");
printf("KMAP = https://github.com/attractivechaos/klib\n"
@@ -334,14 +334,14 @@ int main(int argc, char* argv[])
printf("\nT1: Insert %g mill. random keys range [0, 2^%u): map[rnd] = i;\n", N1/1000000.0, keybits);
RUN_TEST(1)
- printf("\nT2: Insert %g mill. SEQUENTIAL keys, erase them in same order:\n", N2/1000000.0);
- RUN_TEST(2)
+ //printf("\nT2: Insert %g mill. SEQUENTIAL keys, erase them in same order:\n", N2/1000000.0);
+ //RUN_TEST(2)
printf("\nT3: Erase all elements by lookup (%u mill. random inserts), key range [0, 2^%u)\n", n_mill*2, keybits);
RUN_TEST(3)
- printf("\nT4: Iterate map with Min(%u mill, 2^%u) inserts repeated times:\n", n_mill, keybits+1);
- RUN_TEST(4)
+ //printf("\nT4: Iterate map with Min(%u mill, 2^%u) inserts repeated times:\n", n_mill, keybits+1);
+ //RUN_TEST(4)
printf("\nT5: Lookup mix of random/existing keys in range [0, 2^%u). Num lookups depends on size.\n", keybits);
RUN_TEST(5)
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c
index 5e1cf15e..2be975a6 100644
--- a/misc/examples/forfilter.c
+++ b/misc/examples/forfilter.c
@@ -36,9 +36,9 @@ void demo1(void)
, c_flt_skipwhile(i, *i.ref != 80)
&& c_flt_skip(i, 1)
&& c_flt_skipwhile(i, *i.ref != 80)
- && flt_isEven(i)
- && flt_skipValue(i, 80)
- , c_flt_take(i, 5) // short-circuit
+ && flt_isEven(i)
+ && flt_skipValue(i, 80)
+ && c_flt_take(i, 5) // short-circuit
){
sum += res = flt_square(i);
printf(" %d", res);
@@ -68,7 +68,7 @@ void demo2(void)
c_forfilter (x, crange, R
, c_flt_skipwhile(x, *x.ref != 11)
&& *x.ref % 2 != 0
- , c_flt_take(x, 5))
+ && c_flt_take(x, 5))
IVec_push(&vector, (int)(*x.ref * *x.ref));
c_foreach (x, IVec, vector)
printf(" %d", *x.ref);
@@ -131,8 +131,8 @@ void demo5(void)
, c_flt_skip(i,15)
&& c_flt_skipwhile(i, flt_mid_decade(i))
&& c_flt_skip(i,30)
- && flt_even(i)
- , c_flt_take(i,10))
+ && flt_even(i)
+ && c_flt_take(i,5))
printf(" %lld", *i.ref);
puts("");
}
diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c
index 66c587c4..707e8285 100644
--- a/misc/examples/forloops.c
+++ b/misc/examples/forloops.c
@@ -73,7 +73,7 @@ int main()
&& c_flt_skipwhile(i, isOdd(i))
&& isOdd(i)
&& c_flt_skip(i, 2)
- , c_flt_take(i, 1))
+ && c_flt_take(i, 2))
printf(" %d", *i.ref);
puts("");
// 189
diff --git a/misc/examples/make.sh b/misc/examples/make.sh
index 5ac0b653..0297e5a1 100755
--- a/misc/examples/make.sh
+++ b/misc/examples/make.sh
@@ -6,7 +6,7 @@ if [ "$(uname)" = 'Linux' ]; then
oflag='-o '
fi
-cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings"
+cc=gcc; cflags="-s -O3 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings"
#cc=gcc; cflags="-g -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize"
#cc=tcc; cflags="-Wall -std=c99"
#cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wno-unused-function -Wsign-compare -Wwrite-strings"
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index e705dcb7..59ee336c 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -45,7 +45,7 @@ int main(void)
crange R = crange_make(n - 1, 0, -2);
c_forfilter (i, crange, R
, cbits_test(&primes, *i.ref>>1)
- , c_flt_take(i, 50)) {
+ && c_flt_take(i, 50)) {
printf("%lld ", *i.ref);
if (c_flt_last(i) % 10 == 0) puts("");
}
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
index 8a46d653..4783d603 100644
--- a/misc/examples/triples.c
+++ b/misc/examples/triples.c
@@ -4,11 +4,11 @@
#include <stdio.h>
void triples_vanilla(int n) {
- for (int i = 1, z = 1;; ++z) {
- for (int x = 1; x < z; ++x) {
- for (int y = x; y < z; ++y) {
- if (x*x + y*y == z*z) {
- printf("{%d, %d, %d},\n", x, y, z);
+ for (int i = 1, c = 1;; ++c) {
+ for (int a = 1; a < c; ++a) {
+ for (int b = a; b < c; ++b) {
+ if (a*a + b*b == c*c) {
+ printf("{%d, %d, %d},\n", a, b, c);
if (++i > n) goto done;
}
}
@@ -19,17 +19,17 @@ void triples_vanilla(int n) {
struct triples {
int n;
- int x, y, z;
+ int a, b, c;
int cco_state;
};
-bool triples_coro(struct triples* t) {
- cco_begin(t);
- for (t->z = 1;; ++t->z) {
- for (t->x = 1; t->x < t->z; ++t->x) {
- for (t->y = t->x; t->y < t->z; ++t->y) {
- if (t->x*t->x + t->y*t->y == t->z*t->z) {
- if (t->n-- == 0) cco_return;
+bool triples_next(struct triples* I) {
+ cco_begin(I);
+ for (I->c = 1;; ++I->c) {
+ for (I->a = 1; I->a < I->c; ++I->a) {
+ for (I->b = I->a; I->b < I->c; ++I->b) {
+ if (I->a*I->a + I->b*I->b == I->c*I->c) {
+ if (I->n-- == 0) cco_return;
cco_yield(true);
}
}
@@ -46,7 +46,9 @@ int main()
triples_vanilla(6);
puts("\nCoroutine triples:");
- struct triples t = {6};
- while (triples_coro(&t))
- printf("{%d, %d, %d},\n", t.x, t.y, t.z);
+ struct triples t = {INT32_MAX};
+ while (triples_next(&t)) {
+ if (t.c < 100) printf("{%d, %d, %d},\n", t.a, t.b, t.c);
+ else cco_stop(&t);
+ }
}