summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-07-22 14:15:32 +0200
committerTyge Løvset <[email protected]>2020-07-22 14:15:32 +0200
commit1591c0696586f91b426d023982b5cb1834aff557 (patch)
tree5877684d7d1e0b4d8cb77db2ac2b5aa177b0f767
parentda01d17f03f87d6fa97c7742dcd18a5abdc47af8 (diff)
downloadSTC-modified-1591c0696586f91b426d023982b5cb1834aff557.tar.gz
STC-modified-1591c0696586f91b426d023982b5cb1834aff557.zip
Add some examples.
-rw-r--r--examples/inits.c55
-rw-r--r--examples/priority.c26
-rw-r--r--examples/rngbirthday.c60
3 files changed, 141 insertions, 0 deletions
diff --git a/examples/inits.c b/examples/inits.c
new file mode 100644
index 00000000..0848ef80
--- /dev/null
+++ b/examples/inits.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stc/cmap.h>
+#include <stc/cstr.h>
+
+declare_CSet(sx, int); // Set of int
+declare_CMap(mx, int, char); // Map of int -> char
+declare_CMap(ms, int, CStr, cstr_destroy); // Map of int -> CStr
+declare_CMap_str(si, int);
+
+int main(void) {
+ int year = 2020;
+ CMap_ms ms = cmap_ms_from((CMapInput_ms[]) {
+ 100, cstr_make("Hello"),
+ 110, cstr_make("World"),
+ 120, cstr_from("Howdy, -%d-", year),
+ }, 3);
+
+ c_foreach (i, cmap_ms, ms)
+ printf("%d: %s\n", i.item->key, i.item->value.str);
+ cmap_ms_destroy(&ms);
+ // ------------------
+
+ CMap_si si = cmap_si_from((CMapInput_si[]) {
+ "Norway", 100,
+ "Denmark", 50,
+ "Iceland", 10
+ }, 3);
+
+ cmap_si_at(&si, "Sweden", 0)->value += 20;
+ cmap_si_at(&si, "Norway", 0)->value += 20;
+ cmap_si_at(&si, "Finland", 0)->value += 20;
+
+ c_foreach (i, cmap_si, si)
+ printf("%s: %d\n", i.item->key.str, i.item->value);
+ cmap_si_destroy(&si);
+ // ------------------
+
+ CSet_sx s = cset_init;
+ cset_sx_put(&s, 5);
+ cset_sx_put(&s, 8);
+ c_foreach (i, cset_sx, s) printf("set %d\n", i.item->key);
+ cset_sx_destroy(&s);
+ // ------------------
+
+ CMap_mx m = cmap_mx_from((CMapInput_mx[]) {
+ {5, 'a'}, {8, 'b'}, {12, 'c'}
+ }, 3);
+
+ CMapEntry_mx* e = cmap_mx_find(&m, 10); // = NULL
+ char val = cmap_mx_find(&m, 5)->value;
+ cmap_mx_put(&m, 5, 'd'); // update
+ cmap_mx_erase(&m, 8);
+ c_foreach (i, cmap_mx, m) printf("map %d: %c\n", i.item->key, i.item->value);
+ cmap_mx_destroy(&m);
+} \ No newline at end of file
diff --git a/examples/priority.c b/examples/priority.c
new file mode 100644
index 00000000..d6168a6b
--- /dev/null
+++ b/examples/priority.c
@@ -0,0 +1,26 @@
+
+#include <stdio.h>
+#include <time.h>
+#include <stc/cvecpq.h>
+#include <stc/cmap.h>
+#include <stc/crandom.h>
+
+declare_CVec(i, uint32_t);
+declare_CVec_priorityQ(i, >); // min-heap (increasing values)
+declare_CMap(ii, int, int);
+
+int main() {
+ pcg32_random_t pcg = pcg32_seed(time(NULL), 0);
+ CVec_i heap = cvec_init;
+
+ // Push ten million random numbers to queue
+ for (int i=0; i<10000000; ++i)
+ cvec_i_pushPriorityQ(&heap, pcg32_random(&pcg));
+
+ // Extract the hundred smallest.
+ for (int i=0; i<100; ++i) {
+ printf("%u ", cvec_i_topPriorityQ(&heap));
+ cvec_i_popPriorityQ(&heap);
+ }
+ cvec_i_destroy(&heap);
+} \ No newline at end of file
diff --git a/examples/rngbirthday.c b/examples/rngbirthday.c
new file mode 100644
index 00000000..1be96449
--- /dev/null
+++ b/examples/rngbirthday.c
@@ -0,0 +1,60 @@
+
+#include <stdio.h>
+#include <time.h>
+
+#include <stc/crandom.h>
+#include <stc/cmap.h>
+#include <stc/cvec.h>
+#include <stc/cstr.h>
+
+declare_CMap(ic, uint64_t, uint8_t);
+
+const static uint64_t seed = 1234;
+const static uint64_t N = 1ull << 27;
+const static uint64_t mask = (1ull << 52) - 1;
+
+void repeats(void)
+{
+ sfc64_random_t rng = sfc64_seed(seed);
+ CMap_ic m = cmap_init;
+ cmap_ic_reserve(&m, N);
+ clock_t now = clock();
+ for (size_t i = 0; i < N; ++i) {
+ uint64_t k = sfc64_random(&rng) & mask;
+ int v = ++cmap_ic_at(&m, k, 0)->value;
+ if (v > 1) printf("%zu: %x - %d\n", i, k, v);
+ }
+ float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
+ printf("%.02f", diff);
+}
+
+
+declare_CMap(x, uint32_t, uint64_t);
+declare_CVec(x, uint64_t);
+
+void distribution(void)
+{
+ pcg32_random_t rng = pcg32_seed(seed, seed); // time(NULL), time(NULL));
+ const size_t N = 1ull << 28, M = 1ull << 9; // 1ull << 10;
+ CMap_x map = cmap_x_make(M);
+ clock_t now = clock();
+ for (size_t i = 0; i < N; ++i) {
+ ++cmap_x_at(&map, pcg32_randomBounded(&rng, M), 0)->value;
+ }
+ float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
+
+ uint64_t sum = 0;
+ c_foreach (i, cmap_x, map) sum += i.item->value;
+ sum /= map.size;
+
+ c_foreach (i, cmap_x, map)
+ printf("%zu: %zu - %zu\n", i.item->key, i.item->value, sum);
+
+ printf("%.02f\n", diff);
+}
+
+int main()
+{
+ repeats();
+ //distribution();
+} \ No newline at end of file