summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/birthday.c2
-rw-r--r--examples/cbits_prime.c39
-rw-r--r--examples/crandom_ex.c51
-rw-r--r--examples/csmap_ex.c2
-rw-r--r--examples/ex_gauss1.c2
-rw-r--r--examples/ex_gauss2.c2
-rw-r--r--examples/list.c2
-rw-r--r--examples/phonebook.c72
-rw-r--r--examples/priority.c2
-rw-r--r--examples/queue.c2
-rw-r--r--examples/random.c2
11 files changed, 170 insertions, 8 deletions
diff --git a/examples/birthday.c b/examples/birthday.c
index dc94fa53..45786fa9 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crand.h>
+#include <stc/crandom.h>
#include <stc/cmap.h>
using_cmap(ic, uint64_t, uint8_t);
diff --git a/examples/cbits_prime.c b/examples/cbits_prime.c
new file mode 100644
index 00000000..5c5d3969
--- /dev/null
+++ b/examples/cbits_prime.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stc/cbits.h>
+
+static inline cbits sieveOfEratosthenes(size_t n)
+{
+ cbits primes = cbits_with_size(n + 1, true);
+ cbits_reset(&primes, 0);
+ cbits_reset(&primes, 1);
+
+ c_forrange (i, size_t, 2, n+1) {
+ // If primes[i] is not changed, then it is a prime
+ if (cbits_test(primes, i) && i*i <= n) {
+ c_forrange (j, size_t, i*i, n+1, i) {
+ cbits_reset(&primes, j);
+ }
+ }
+ }
+ return primes;
+}
+
+
+int main(void)
+{
+ int n = 100000000;
+ printf("computing prime numbers up to %u\n", n);
+
+ cbits primes = sieveOfEratosthenes(n);
+ puts("done");
+
+ size_t np = cbits_count(primes);
+ printf("number of primes: %zu\n", np);
+
+ printf("2 ");
+ c_forrange (i, int, 3, 1001, 2) {
+ if (cbits_test(primes, i)) printf("%d ", i);
+ }
+ puts("");
+ cbits_del(&primes);
+} \ No newline at end of file
diff --git a/examples/crandom_ex.c b/examples/crandom_ex.c
new file mode 100644
index 00000000..c923debd
--- /dev/null
+++ b/examples/crandom_ex.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <time.h>
+#include <math.h>
+#include <stc/crandom.h>
+#include <stc/cstr.h>
+
+int main()
+{
+ enum {R = 30};
+ const size_t N = 1000000000;
+ uint64_t seed = 1234; // time(NULL);
+ stc64_t rng = stc64_init(seed);
+
+ uint64_t sum = 0;
+
+ stc64_normalf_t dist2 = stc64_normalf_init(R / 2.0, R / 6.0);
+ size_t N2 = 10000000;
+ int hist[R] = {0};
+ sum = 0;
+ c_forrange (N2) {
+ int n = round((stc64_normalf(&rng, &dist2) + 0.5));
+ sum += n;
+ if (n >= 0 && n < R) ++hist[n];
+ }
+ cstr_t bar = cstr_inits;
+ c_forrange (i, int, R) {
+ cstr_resize(&bar, hist[i] * 25ull * R / N2, '*');
+ printf("%3d %s\n", i, bar.str);
+ }
+
+ clock_t diff, before;
+
+ sum = 0;
+ before = clock();
+ c_forrange (N) {
+ sum += stc64_rand(&rng);
+ }
+ diff = clock() - before;
+ printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
+
+ stc64_uniform_t dist1 = stc64_uniform_init(0, 1000);
+ sum = 0;
+ before = clock();
+ c_forrange (N) {
+ sum += stc64_uniform(&rng, &dist1);
+ }
+ diff = clock() - before;
+ printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
+
+ cstr_del(&bar);
+} \ No newline at end of file
diff --git a/examples/csmap_ex.c b/examples/csmap_ex.c
index aa78028b..ea8fd81c 100644
--- a/examples/csmap_ex.c
+++ b/examples/csmap_ex.c
@@ -1,6 +1,6 @@
#include <stc/csmap.h>
#include <stc/cstr.h>
-#include <stc/crand.h>
+#include <stc/crandom.h>
#include <stdio.h>
using_csmap(i, int, size_t);
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 4139210c..85d818d5 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
-#include "stc/crand.h"
+#include "stc/crandom.h"
#include "stc/cstr.h"
#include "stc/cmap.h"
#include "stc/cvec.h"
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index cff0cef3..434a7e5e 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
-#include "stc/crand.h"
+#include "stc/crandom.h"
#include "stc/cstr.h"
#include "stc/csmap.h"
diff --git a/examples/list.c b/examples/list.c
index 9d6e8d89..394d0b38 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <stc/clist.h>
-#include <stc/crand.h>
+#include <stc/crandom.h>
using_clist(fx, double);
int main() {
diff --git a/examples/phonebook.c b/examples/phonebook.c
new file mode 100644
index 00000000..91d01072
--- /dev/null
+++ b/examples/phonebook.c
@@ -0,0 +1,72 @@
+// The MIT License (MIT)
+// Copyright (c) 2018 Maksim Andrianov
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+
+// Program to emulates the phone book.
+
+#include <stdio.h>
+#include <stc/cmap.h>
+#include <stc/cstr.h>
+
+using_cmap_str();
+
+void print_phone_book(cmap_str phone_book)
+{
+ c_foreach (i, cmap_str, phone_book)
+ printf("%s\t- %s\n", i.ref->first.str, i.ref->second.str);
+}
+
+int main(int argc, char **argv)
+{
+ bool erased;
+ cmap_str phone_book = cmap_inits;
+ c_push_items(&phone_book, cmap_str, {
+ {"Lilia Friedman", "(892) 670-4739"},
+ {"Tariq Beltran", "(489) 600-7575"},
+ {"Laiba Juarez", "(303) 885-5692"},
+ {"Elliott Mooney", "(945) 616-4482"},
+ });
+
+ printf("Phone book:\n");
+ print_phone_book(phone_book);
+
+ c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1880"));
+ c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1990"));
+
+ printf("\nPhone book after adding Zak Byers:\n");
+ print_phone_book(phone_book);
+
+ if (cmap_str_find(&phone_book, "Tariq Beltran") != NULL)
+ printf("\nTariq Beltran is in phone book\n");
+
+ erased = cmap_str_erase(&phone_book, "Tariq Beltran");
+ erased = cmap_str_erase(&phone_book, "Elliott Mooney");
+
+ printf("\nPhone book after erasing Tariq and Elliott:\n");
+ print_phone_book(phone_book);
+
+ cmap_str_insert_or_assign(&phone_book, "Zak Byers", "(555) 396-188");
+
+ printf("\nPhone book after update phone of Zak Byers:\n");
+ print_phone_book(phone_book);
+
+ cmap_str_del(&phone_book);
+ puts("done");
+} \ No newline at end of file
diff --git a/examples/priority.c b/examples/priority.c
index 4eceb29f..548cb06d 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -4,7 +4,7 @@
#include <stc/cvec.h>
#include <stc/cpque.h>
#include <stc/cmap.h>
-#include <stc/crand.h>
+#include <stc/crandom.h>
using_cvec(i, int64_t);
using_cpque(i, cvec_i, >); // min-heap (increasing values)
diff --git a/examples/queue.c b/examples/queue.c
index 8c0a8777..23640a39 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -1,4 +1,4 @@
-#include <stc/crand.h>
+#include <stc/crandom.h>
#include <stc/cqueue.h>
#include <stdio.h>
diff --git a/examples/random.c b/examples/random.c
index 7325e023..c923debd 100644
--- a/examples/random.c
+++ b/examples/random.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
-#include <stc/crand.h>
+#include <stc/crandom.h>
#include <stc/cstr.h>
int main()