summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/books.c60
-rw-r--r--examples/box.c61
-rw-r--r--examples/box2.c86
-rw-r--r--examples/hashmap.c48
-rw-r--r--examples/rawptr_elements.c (renamed from examples/ptr_elems.c)0
-rw-r--r--examples/sptr_demo.c (renamed from examples/sharedptr.c)0
-rw-r--r--examples/sptr_music.c (renamed from examples/sptr_ex.c)0
-rw-r--r--examples/sview_split.c17
8 files changed, 272 insertions, 0 deletions
diff --git a/examples/books.c b/examples/books.c
new file mode 100644
index 00000000..f9340679
--- /dev/null
+++ b/examples/books.c
@@ -0,0 +1,60 @@
+// https://doc.rust-lang.org/std/collections/struct.HashMap.html
+
+#define i_key_str
+#define i_val_str
+#include <stc/cmap.h>
+
+// Type inference lets us omit an explicit type signature (which
+// would be `HashMap<String, String>` in this example).
+int main()
+{
+ c_auto (cmap_str, book_reviews)
+ {
+ // Review some books.
+ cmap_str_emplace(&book_reviews,
+ "Adventures of Huckleberry Finn",
+ "My favorite book."
+ );
+ cmap_str_emplace(&book_reviews,
+ "Grimms' Fairy Tales",
+ "Masterpiece."
+ );
+ cmap_str_emplace(&book_reviews,
+ "Pride and Prejudice",
+ "Very enjoyable"
+ );
+ cmap_str_insert(&book_reviews,
+ cstr_new("The Adventures of Sherlock Holmes"),
+ cstr_new("Eye lyked it alot.")
+ );
+
+ // Check for a specific one.
+ // When collections store owned values (String), they can still be
+ // queried using references (&str).
+ if (cmap_str_contains(&book_reviews, "Les Misérables")) {
+ printf("We've got %zu reviews, but Les Misérables ain't one.",
+ cmap_str_size(book_reviews));
+ }
+
+ // oops, this review has a lot of spelling mistakes, let's delete it.
+ cmap_str_erase(&book_reviews, "The Adventures of Sherlock Holmes");
+
+ // Look up the values associated with some keys.
+ const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"};
+ c_forrange (i, c_arraylen(to_find)) {
+ const cmap_str_value* b;
+ if ((b = cmap_str_get(&book_reviews, to_find[i])))
+ printf("%s: %s\n", b->first.str, b->second.str);
+ else
+ printf("%s is unreviewed.\n", to_find[i]);
+ }
+
+ // Look up the value for a key (will panic if the key is not found).
+ printf("Review for Jane: %s\n", cmap_str_at(&book_reviews, "Pride and Prejudice")->str);
+
+ // Iterate over everything.
+ c_forpair (book, review, cmap_str, book_reviews) {
+ printf("%s: \"%s\"\n", _.book.str, _.review.str);
+ }
+ }
+} \ No newline at end of file
diff --git a/examples/box.c b/examples/box.c
new file mode 100644
index 00000000..c7763bb8
--- /dev/null
+++ b/examples/box.c
@@ -0,0 +1,61 @@
+/* cbox: heap allocated boxed type */
+#include <stc/cstr.h>
+
+typedef struct { cstr name, last; } Person;
+
+Person Person_from(const char* name, const char* last) {
+ return (Person){.name = cstr_from(name), .last = cstr_from(last)};
+}
+
+int Person_cmp(const Person* a, const Person* b) {
+ int c = strcmp(a->name.str, b->name.str);
+ return c ? c : strcmp(a->last.str, b->last.str);
+}
+
+Person Person_clone(Person p) {
+ p.name = cstr_clone(p.name);
+ p.last = cstr_clone(p.last);
+ return p;
+}
+void Person_del(Person* p) {
+ printf("del: %s %s\n", p->name.str, p->last.str);
+ c_del(cstr, &p->name, &p->last);
+}
+
+#define i_val Person
+#define i_cmp Person_cmp
+#define i_from Person_clone
+#define i_del Person_del
+#define i_tag prs
+#include <stc/cbox.h>
+
+#define i_val_ref cbox_prs
+#define i_tag prs
+#include <stc/cvec.h>
+
+
+int main()
+{
+ c_auto (cvec_prs, vec)
+ c_auto (cbox_prs, p, q)
+ {
+ p = cbox_prs_new(Person_from("Dave", "Cooper"));
+
+ q = cbox_prs_clone(p);
+ cstr_assign(&q.get->name, "Dale");
+
+ printf("%s %s.\n", p.get->name.str, p.get->last.str);
+ printf("%s %s.\n", q.get->name.str, q.get->last.str);
+
+ cvec_prs_push_back(&vec, cbox_prs_new(Person_from("Laura", "Palmer")));
+ cvec_prs_push_back(&vec, cbox_prs_new(Person_from("Shelly", "Johnson")));
+
+ c_foreach (i, cvec_prs, vec)
+ printf("%s: %s\n", i.ref->get->name.str, i.ref->get->last.str);
+
+ c_autovar (Person per = Person_from("Laura", "Palmer"), Person_del(&per)) {
+ const cbox_prs *v = cvec_prs_get(&vec, (cbox_prs){&per});
+ if (v) printf("found: %s: %s\n", v->get->name.str, v->get->last.str);
+ }
+ }
+}
diff --git a/examples/box2.c b/examples/box2.c
new file mode 100644
index 00000000..79304609
--- /dev/null
+++ b/examples/box2.c
@@ -0,0 +1,86 @@
+// https://doc.rust-lang.org/rust-by-example/std/box.html
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stc/ccommon.h>
+
+struct {
+ double x;
+ double y;
+} typedef Point;
+
+// A Rectangle can be specified by where its top left and bottom right
+// corners are in space
+struct {
+ Point top_left;
+ Point bottom_right;
+} typedef Rectangle;
+
+#define i_val Point
+#define i_opt c_no_compare
+#include <stc/cbox.h>
+
+#define i_val cbox_Point
+#define i_opt c_no_compare
+#define i_tag BoxPoint
+#include <stc/cbox.h>
+
+#define i_val Rectangle
+#define i_opt c_no_compare
+#include <stc/cbox.h>
+
+Point origin(void) {
+ return (Point){ .x=0.0, .y=0.0 };
+}
+
+cbox_Point boxed_origin(void) {
+ // Allocate this point on the heap, and return a pointer to it
+ return cbox_Point_new((Point){ .x=0.0, .y=0.0 });
+}
+
+
+int main(void) {
+ // (all the type annotations are superfluous)
+ // Stack allocated variables
+ Point point = origin();
+ Rectangle rectangle = (Rectangle){
+ .top_left = origin(),
+ .bottom_right = (Point){ .x=3.0, .y=-4.0 }
+ };
+
+ // Heap allocated rectangle
+ c_auto (cbox_Rectangle, boxed_rectangle)
+ c_auto (cbox_Point, boxed_point)
+ c_auto (cbox_BoxPoint, box_in_a_box)
+ {
+ boxed_rectangle = cbox_Rectangle_new((Rectangle){
+ .top_left = origin(),
+ .bottom_right = (Point){ .x=3.0, .y=-4.0 }
+ });
+
+ // The output of functions can be boxed
+ boxed_point = cbox_Point_new(origin());
+
+ // Double indirection
+ box_in_a_box = cbox_BoxPoint_new(boxed_origin());
+
+ printf("Point occupies %zu bytes on the stack\n",
+ sizeof(point));
+ printf("Rectangle occupies %zu bytes on the stack\n",
+ sizeof(rectangle));
+
+ // box size == pointer size
+ printf("Boxed point occupies %zu bytes on the stack\n",
+ sizeof(boxed_point));
+ printf("Boxed rectangle occupies %zu bytes on the stack\n",
+ sizeof(boxed_rectangle));
+ printf("Boxed box occupies %zu bytes on the stack\n",
+ sizeof(box_in_a_box));
+
+ // Copy the data contained in `boxed_point` into `unboxed_point`
+ Point unboxed_point = *boxed_point.get;
+ printf("Unboxed point occupies %zu bytes on the stack\n",
+ sizeof(unboxed_point));
+ }
+} \ No newline at end of file
diff --git a/examples/hashmap.c b/examples/hashmap.c
new file mode 100644
index 00000000..c3e9afce
--- /dev/null
+++ b/examples/hashmap.c
@@ -0,0 +1,48 @@
+// https://doc.rust-lang.org/rust-by-example/std/hash.html
+
+#define i_key_str
+#define i_val_str
+#include <stdio.h>
+#include <stc/cmap.h>
+
+const char* call(const char* number) {
+ if (!strcmp(number, "798-1364"))
+ return "We're sorry, the call cannot be completed as dialed."
+ " Please hang up and try again.";
+ else if (!strcmp(number, "645-7689"))
+ return "Hello, this is Mr. Awesome's Pizza. My name is Fred."
+ " What can I get for you today?";
+ else
+ return "Hi! Who is this again?";
+}
+
+int main(void) {
+ c_auto (cmap_str, contacts)
+ {
+ cmap_str_emplace(&contacts, "Daniel", "798-1364");
+ cmap_str_emplace(&contacts, "Ashley", "645-7689");
+ cmap_str_emplace(&contacts, "Katie", "435-8291");
+ cmap_str_emplace(&contacts, "Robert", "956-1745");
+
+ const cmap_str_value* v;
+ if ((v = cmap_str_get(&contacts, "Daniel")))
+ printf("Calling Daniel: %s\n", call(v->second.str));
+ else
+ printf("Don't have Daniel's number.");
+
+ cmap_str_emplace(&contacts, "Daniel", "164-6743");
+
+ if ((v = cmap_str_get(&contacts, "Ashley")))
+ printf("Calling Ashley: %s\n", call(v->second.str));
+ else
+ printf("Don't have Ashley's number.");
+
+ cmap_str_erase(&contacts, "Ashley");
+
+ puts("");
+ c_forpair (contact, number, cmap_str, contacts) {
+ printf("Calling %s: %s\n", _.contact.str, call(_.number.str));
+ }
+ puts("");
+ }
+}
diff --git a/examples/ptr_elems.c b/examples/rawptr_elements.c
index c5416822..c5416822 100644
--- a/examples/ptr_elems.c
+++ b/examples/rawptr_elements.c
diff --git a/examples/sharedptr.c b/examples/sptr_demo.c
index c3da5287..c3da5287 100644
--- a/examples/sharedptr.c
+++ b/examples/sptr_demo.c
diff --git a/examples/sptr_ex.c b/examples/sptr_music.c
index e8e08b5f..e8e08b5f 100644
--- a/examples/sptr_ex.c
+++ b/examples/sptr_music.c
diff --git a/examples/sview_split.c b/examples/sview_split.c
new file mode 100644
index 00000000..3a079583
--- /dev/null
+++ b/examples/sview_split.c
@@ -0,0 +1,17 @@
+#include <stc/csview.h>
+#define c_arg(a) a
+int main()
+{
+ // No memory allocations or string length calculations!
+ const csview date = c_sv("2021/03/12");
+ const csview year = csview_first_token(date, c_sv("/"));
+ const csview month = csview_next_token(date, c_sv("/"), year);
+ const csview day = csview_next_token(date, c_sv("/"), month);
+
+ printf(c_svfmt ", " c_svfmt ", " c_svfmt "\n", c_svarg(year), c_svarg(month), c_svarg(day));
+
+ c_auto (cstr, y, m, d) {
+ y = cstr_from_v(year), m = cstr_from_v(month), d = cstr_from_v(day);
+ printf("%s, %s, %s\n", y.str, m.str, d.str);
+ }
+} \ No newline at end of file