summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-26 19:28:48 +0200
committerTyge Løvset <[email protected]>2022-09-26 19:28:48 +0200
commit2c31b421b6c867ab956a843dace0d95769a97667 (patch)
tree07c9fe0b12c78117e6b59a58c70de3d6e7bd72a9 /examples
parent2e3db1c709e88d68cece8c46566ca6b8481b7c07 (diff)
downloadSTC-modified-2c31b421b6c867ab956a843dace0d95769a97667.tar.gz
STC-modified-2c31b421b6c867ab956a843dace0d95769a97667.zip
Added .count to c_forfilter() algorithm. Updated prime.c example, and added forloops.c example.
Diffstat (limited to 'examples')
-rw-r--r--examples/forloops.c80
-rw-r--r--examples/prime.c16
2 files changed, 89 insertions, 7 deletions
diff --git a/examples/forloops.c b/examples/forloops.c
new file mode 100644
index 00000000..f119e33a
--- /dev/null
+++ b/examples/forloops.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+
+#define i_type IVec
+#define i_val int
+#include <stc/cstack.h>
+
+#define i_type IMap
+#define i_key int
+#define i_val int
+#include <stc/cmap.h>
+
+
+int main()
+{
+ puts("c_forloop:");
+ c_forloop (30) printf(" xx");
+ puts("");
+
+ c_forloop (i, 30) printf(" %lld", i);
+ puts("");
+
+ c_forloop (i, 30, 60) printf(" %lld", i);
+ puts("");
+
+ c_forloop (i, 30, 90, 2) printf(" %lld", i);
+
+
+ puts("\n\nc_forlist:");
+ c_forlist (i, int, {12, 23, 453, 65, 676})
+ printf(" %d", *i.ref);
+ puts("");
+
+ c_forlist (i, const char*, {"12", "23", "453", "65", "676"})
+ printf(" %s", *i.ref);
+ puts("");
+
+ c_forlist (i, const char*, {"12", "23", "453", "65", "676"})
+ printf(" %s", i.data[i.size - 1 - i.index]);
+
+
+ c_auto (IVec, vec)
+ c_auto (IMap, map)
+ {
+ c_forlist (i, int, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199})
+ IVec_push(&vec, *i.ref);
+
+ c_forlist (i, IMap_value, {{12, 23}, {453, 65}, {676, 123}, {34, 67}})
+ IMap_push(&map, *i.ref);
+
+ puts("\n\nc_foreach:");
+ c_foreach (i, IVec, vec)
+ printf(" %d", *i.ref);
+ puts("");
+
+ c_foreach (i, IMap, map)
+ printf(" (%d %d)", i.ref->first, i.ref->second);
+
+ puts("\n\nc_forpair:");
+ c_forpair (key, val, IMap, map)
+ printf(" (%d %d)", *_.key, *_.val);
+
+ puts("\n\nc_forwhile:");
+ c_forwhile (i, IVec, vec, i.index < 3)
+ printf(" %d", *i.ref);
+
+ #define isOdd(i) (*i.ref & 1)
+
+ puts("\n\nc_forfilter:");
+ c_forfilter (i, IVec, vec,
+ c_flt_dropwhile(i, *i.ref != 65)
+ && c_flt_takewhile(i, *i.ref != 280)
+ && c_flt_dropwhile(i, isOdd(i))
+ && isOdd(i)
+ && c_flt_drop(i, 2)
+ , c_flt_take(i, 1))
+ printf(" %d", *i.ref);
+ puts("");
+ // 189
+ }
+}
diff --git a/examples/prime.c b/examples/prime.c
index 12d33a9e..613048bf 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -33,16 +33,18 @@ int main(void)
clock_t t2 = clock();
printf("number of primes: %" PRIuMAX ", time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC);
+ puts("Show all the primes in the range [2, 1000):");
printf("2");
- for (size_t i = 3; i < 1000; i += 2)
- if (cbits_test(&primes, i>>1)) printf(" %" PRIuMAX "", i);
+ c_forloop (i, 3, 1000, 2)
+ if (cbits_test(&primes, i>>1)) printf(" %lld", i);
puts("");
- int k = 20;
- c_forloop (i, n-1, 1, -2) {
- if (k == 0) break;
- else if (cbits_test(&primes, i>>1)) printf("%lld\n", i), k--;
+ puts("Show the last 50 primes using a temporary crange generator:");
+ c_forfilter (i, crange, *(crange[]){crange_make(n - 1, 0, -2)}
+ , cbits_test(&primes, *i.ref>>1)
+ , c_flt_take(i, 50)) {
+ printf("%lld ", *i.ref);
+ if (i.count % 10 == 0) puts("");
}
- puts("");
}
}