diff options
| author | Tyge Løvset <[email protected]> | 2020-08-30 19:01:02 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-08-30 19:01:02 +0200 |
| commit | fb87f58ad96190a258dccff6f2c5faab031010e0 (patch) | |
| tree | 2e5e00b077b496b2a491d9322b703370266591a3 /examples | |
| parent | e0b2e9126d74ce03ddb1054be709126723c7e7a5 (diff) | |
| download | STC-modified-fb87f58ad96190a258dccff6f2c5faab031010e0.tar.gz STC-modified-fb87f58ad96190a258dccff6f2c5faab031010e0.zip | |
Renamed cstr_destr to cstr_mdestroy(). Added a few more examples.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/random.c | 67 | ||||
| -rw-r--r-- | examples/replace.c | 33 |
2 files changed, 100 insertions, 0 deletions
diff --git a/examples/random.c b/examples/random.c new file mode 100644 index 00000000..bf4bd6bd --- /dev/null +++ b/examples/random.c @@ -0,0 +1,67 @@ +#include <stdio.h>
+#include <time.h>
+#include <stc/crandom.h>
+#include <stc/cstr.h>
+
+int main()
+{
+ enum {R = 30};
+ const size_t N = 1000000000;
+ clock_t difference, before;
+ uint64_t sum = 0;
+
+ uint64_t seed = time(NULL);
+ crand_rng32_t pcg = crand_rng32_init(seed);
+ uint32_t range = crand_i32(&pcg) & ((1u << 28) - 1);
+ crand_uniform_i32_t dist0 = crand_uniform_i32_init(pcg, 0, range);
+
+ printf("32 uniform: %u\n", dist0.range);
+ double fsum = 0;
+ before = clock();
+ for (size_t i = 0; i < N; ++i) {
+ fsum += (double) crand_uniform_i32(&dist0) / dist0.range;
+ }
+ difference = clock() - before;
+ printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
+
+ pcg = crand_rng32_init(seed);
+ dist0 = crand_uniform_i32_init(pcg, 0, range);
+ puts("32 unbiased");
+ fsum = 0;
+ before = clock();
+ for (size_t i = 0; i < N; ++i) {
+ fsum += (double) crand_unbiased_i32(&dist0) / dist0.range;
+ }
+ difference = clock() - before;
+ printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
+
+ puts("64 uniform");
+ crand_rng64_t stc = crand_rng64_init(seed);
+ crand_uniform_i64_t dist1 = crand_uniform_i64_init(stc, 0, N);
+ sum = 0;
+ before = clock();
+ for (size_t i = 0; i < N; ++i) {
+ sum += crand_uniform_i64(&dist1);
+ }
+ difference = clock() - before;
+ printf("%zu %f: %f secs\n", N, (double) sum / N, (float) difference / CLOCKS_PER_SEC);
+
+
+ puts("normal distribution");
+ crand_normal_f64_t dist2 = crand_normal_f64_init(stc, R / 2.0, R / 6.0);
+ size_t N2 = 10000000;
+ int hist[R] = {0};
+ sum = 0;
+ for (size_t i = 0; i < N2; ++i) {
+ int n = (int) (crand_normal_f64(&dist2) + 0.5);
+ sum += n;
+ if (n >= 0 && n < R) ++hist[n];
+ }
+
+ cstr_t bar = cstr_init;
+ for (int i=0; i < R; ++i) {
+ cstr_take(&bar, cstr_with_size(hist[i] * 25ull * R / N2, '*'));
+ printf("%2d %s\n", i, bar.str);
+ }
+ cstr_destroy(&bar);
+}
\ No newline at end of file diff --git a/examples/replace.c b/examples/replace.c new file mode 100644 index 00000000..b31715c1 --- /dev/null +++ b/examples/replace.c @@ -0,0 +1,33 @@ +
+#include <stc/cstr.h>
+
+int main ()
+{
+ const char *base = "this is a test string.";
+ const char *s2 = "n example";
+ const char *s3 = "sample phrase";
+ const char *s4 = "useful.";
+
+ // replace signatures used in the same order as described above:
+
+ // Ustring positions: 0123456789*123456789*12345
+ cstr_t s = cstr_make(base); // "this is a test string."
+
+ cstr_t m = cstr_clone(s);
+ cstr_append(&m, m.str);
+ cstr_append(&m, m.str);
+ printf("%s\n", m.str);
+
+ cstr_replace(&s, 9, 5, s2); // "this is an example string." (1)
+ printf("(1) %s\n", s.str);
+ cstr_replace_n(&s, 19, 6, s3+7, 6); // "this is an example phrase." (2)
+ printf("(2) %s\n", s.str);
+ cstr_replace(&s, 8, 10, "just a"); // "this is just a phrase." (3)
+ printf("(3) %s\n", s.str);
+ cstr_replace_n(&s, 8, 6,"a shorty", 7); // "this is a short phrase." (4)
+ printf("(4) %s\n", s.str);
+ cstr_replace(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5)
+ printf("(5) %s\n", s.str);
+
+ cstr_mdestroy(&s, &m);
+}
|
