summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-08-31 23:43:32 +0200
committerTyge Løvset <[email protected]>2020-08-31 23:43:32 +0200
commit8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d (patch)
treedc3e7a19038f29ac905bbc938cacda2f9b5b8bb6
parent55c84dee6cce33e31a4ca3e0cdcbdbe85fcccac6 (diff)
downloadSTC-modified-8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d.tar.gz
STC-modified-8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d.zip
Added example gaussian. Some cleanup.
-rw-r--r--examples/ex_gaussian.c58
-rw-r--r--stc/cdefs.h3
-rw-r--r--stc/clist.h13
3 files changed, 67 insertions, 7 deletions
diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c
new file mode 100644
index 00000000..62ab7994
--- /dev/null
+++ b/examples/ex_gaussian.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <time.h>
+#include <math.h>
+#include <stc/crandom.h>
+#include <stc/cstr.h>
+#include <stc/cmap.h>
+#include <stc/cvec.h>
+
+// Declare int -> int hashmap. Uses typetag 'i' for ints.
+declare_cmap(i, int, size_t);
+
+// Declare int vector with map entries that can be sorted by map keys.
+static int compare(cmap_i_entry_t *a, cmap_i_entry_t *b) {
+ return c_default_compare(&a->key, &b->key);
+}
+// Vector: typetag 'e' for (map) entry
+declare_cvec(e, cmap_i_entry_t, c_default_destroy, compare);
+
+int main()
+{
+ enum {Rows = 40, N = 5000000};
+ const double StdDev = 6.0, Mag = 35.0, Mean = 12;
+
+ printf("Demo of gaussian / normal distribution of %d random samples\n", N);
+
+ // Setup random engine with normal distribution.
+ uint64_t seed = time(NULL);
+ crand_rng64_t rng = crand_rng64_init(seed);
+ crand_normal_f64_t dist = crand_normal_f64_init(rng, Mean, Rows / StdDev);
+
+ // Create histogram map
+ cmap_i mhist = cmap_init;
+ for (size_t i = 0; i < N; ++i) {
+ int index = round( crand_normal_f64(&dist) );
+ ++ cmap_i_insert(&mhist, index, 0)->value;
+ }
+
+ // Transfer map to vec and sort it by map keys.
+ cvec_e vhist = cvec_init;
+ c_foreach (i, cmap_i, mhist)
+ cvec_e_push_back(&vhist, *i.item);
+ cvec_e_sort(&vhist);
+
+ // Print the gaussian bar chart
+ cstr_t bar = cstr_init;
+ c_foreach (i, cvec_e, vhist) {
+ size_t n = (size_t) (i.item->value * Mag * Rows / N);
+ if (n > 0) {
+ // bar string: take ownership in new str after freeing current.
+ cstr_take(&bar, cstr_with_size(n, '*'));
+ printf("%4d %s\n", i.item->key, bar.str);
+ }
+ }
+ // Cleanup
+ cstr_destroy(&bar);
+ cmap_i_destroy(&mhist);
+ cvec_e_destroy(&vhist);
+} \ No newline at end of file
diff --git a/stc/cdefs.h b/stc/cdefs.h
index 73f3e9d3..8692cdc6 100644
--- a/stc/cdefs.h
+++ b/stc/cdefs.h
@@ -74,10 +74,11 @@
#define c_foreach(it, prefix, container) \
for (prefix##_iter_t it = prefix##_begin(&container); it.item != it.end; prefix##_next(&it))
+
#define c_items(...) __VA_ARGS__
#define c_push(container, prefix, items) do { \
const prefix##_input_t __arr[] = { items }; \
- prefix##_push_n(container, __arr, sizeof(__arr)/sizeof(prefix##_input_t)); \
+ prefix##_push_n(container, __arr, sizeof(__arr)/sizeof(__arr[0])); \
} while (0)
diff --git a/stc/clist.h b/stc/clist.h
index e5dbf6f3..3a972a96 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -56,16 +56,17 @@
}
*/
-#define declare_clist(...) c_MACRO_OVERLOAD(declare_clist, __VA_ARGS__)
+#define declare_clist(...) c_MACRO_OVERLOAD(declare_clist, __VA_ARGS__)
#define declare_clist_2(tag, Value) \
- declare_clist_3(tag, Value, c_default_destroy)
+ declare_clist_3(tag, Value, c_default_destroy)
#define declare_clist_3(tag, Value, valueDestroy) \
- declare_clist_4(tag, Value, valueDestroy, c_default_compare)
+ declare_clist_4(tag, Value, valueDestroy, c_default_compare)
#define declare_clist_4(tag, Value, valueDestroy, valueCompare) \
- declare_clist_7(tag, Value, valueDestroy, Value, valueCompare, c_default_to_raw, c_default_from_raw)
-#define declare_clist_str() \
- declare_clist_7(str, cstr_t, cstr_destroy, const char*, cstr_compare_raw, cstr_to_raw, cstr_make)
+ declare_clist_7(tag, Value, valueDestroy, Value, \
+ valueCompare, c_default_to_raw, c_default_from_raw)
+#define declare_clist_str() declare_clist_7(str, cstr_t, cstr_destroy, const char*, \
+ cstr_compare_raw, cstr_to_raw, cstr_make)
#define declare_clist_types(tag, Value) \
typedef struct clist_##tag##_node { \