summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-25 10:27:07 +0200
committerTyge Løvset <[email protected]>2020-09-25 10:27:07 +0200
commitc2ab107bc1bcd196b10b1d5f130a1a32449f7773 (patch)
treed7b64a12ac6afad3880319a86f5b009f696054ba /examples
parent3be928828630984f4ff2c5cf352ace1366c52341 (diff)
downloadSTC-modified-c2ab107bc1bcd196b10b1d5f130a1a32449f7773.tar.gz
STC-modified-c2ab107bc1bcd196b10b1d5f130a1a32449f7773.zip
Added cptr.h and good examples for cptr and csptr (share_ptr.c). Some internal refactor.
Diffstat (limited to 'examples')
-rw-r--r--examples/ptr.c34
-rw-r--r--examples/share_ptr.c61
-rw-r--r--examples/shared_ptr.c48
3 files changed, 95 insertions, 48 deletions
diff --git a/examples/ptr.c b/examples/ptr.c
new file mode 100644
index 00000000..a50a0d9f
--- /dev/null
+++ b/examples/ptr.c
@@ -0,0 +1,34 @@
+#include <stc/cptr.h>
+#include <stc/cstr.h>
+#include <stc/cvec.h>
+
+typedef struct { cstr_t name, last; } Person;
+
+Person* Person_make(Person* p, const char* name, const char* last) {
+ p->name = cstr(name), p->last = cstr(last);
+ return p;
+}
+void Person_del(Person* p) {
+ printf("del: %s\n", p->name.str);
+ c_del(cstr, &p->name, &p->last);
+}
+int Person_compare(const Person* p, const Person* q) {
+ int cmp = strcmp(p->name.str, q->name.str);
+ return cmp == 0 ? strcmp(p->last.str, q->last.str) : cmp;
+}
+
+using_cptr(pe, Person, Person_del, Person_compare);
+using_cvec(pe, Person*, cptr_pe_del, cptr_pe_compare);
+
+int main() {
+ cvec_pe vec = cvec_pe_init();
+ cvec_pe_push_back(&vec, Person_make(c_new(Person), "Joe", "Jordan"));
+ cvec_pe_push_back(&vec, Person_make(c_new(Person), "Annie", "Aniston"));
+ cvec_pe_push_back(&vec, Person_make(c_new(Person), "Jane", "Jacobs"));
+
+ cvec_pe_sort(&vec);
+ c_foreach (i, cvec_pe, vec)
+ printf("%s %s\n", (*i.val)->name.str, (*i.val)->last.str);
+
+ cvec_pe_del(&vec);
+} \ No newline at end of file
diff --git a/examples/share_ptr.c b/examples/share_ptr.c
new file mode 100644
index 00000000..9180fcd4
--- /dev/null
+++ b/examples/share_ptr.c
@@ -0,0 +1,61 @@
+#include <stc/csptr.h>
+#include <stc/clist.h>
+#include <stc/cvec.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+typedef struct { cstr_t name, last; } Person;
+
+Person* Person_make(Person* p, const char* name, const char* last) {
+ p->name = cstr(name), p->last = cstr(last);
+ return p;
+}
+void Person_del(Person* p) {
+ printf("del: %s\n", p->name.str);
+ c_del(cstr, &p->name, &p->last);
+}
+int Person_compare(const Person* p, const Person* q) {
+ int cmp = strcmp(p->name.str, q->name.str);
+ return cmp == 0 ? strcmp(p->last.str, q->last.str) : cmp;
+}
+
+using_csptr(pe, Person, Person_del, Person_compare);
+using_clist(pe, csptr_pe, csptr_pe_del, csptr_pe_compare);
+using_cvec(pe, csptr_pe, csptr_pe_del, csptr_pe_compare);
+
+int main() {
+ clist_pe queue = clist_pe_init();
+ cvec_pe vec = cvec_pe_init();
+
+ puts("Push 10:");
+ c_forrange (i, 10) {
+ csptr_pe p = csptr_pe_make(c_new(Person));
+ p.get->name = cstr_from("Name %d", (i * 7) % 10);
+ p.get->last = cstr_from("Last %d", (i * 9) % 10);
+ clist_pe_push_back(&queue, p);
+ cvec_pe_push_back(&vec, csptr_pe_share(p)); // Don't forget to share!
+ }
+ c_foreach (i, clist_pe, queue)
+ printf(" %s\n", i.val->get->name.str);
+
+ puts("Sort and pop 3:");
+ clist_pe_sort(&queue);
+ cvec_pe_sort(&vec);
+ c_forrange (3) {
+ clist_pe_pop_front(&queue);
+ cvec_pe_pop_back(&vec);
+ }
+
+ puts("Sorted queue:");
+ c_foreach (i, clist_pe, queue)
+ printf(" %s\n", i.val->get->name.str);
+ puts("Sorted vec:");
+ c_foreach (i, cvec_pe, vec)
+ printf(" %s\n", i.val->get->name.str);
+
+ puts("Destroy queue:");
+ clist_pe_del(&queue);
+
+ puts("Destroy vec:");
+ cvec_pe_del(&vec);
+} \ No newline at end of file
diff --git a/examples/shared_ptr.c b/examples/shared_ptr.c
deleted file mode 100644
index 7bff502c..00000000
--- a/examples/shared_ptr.c
+++ /dev/null
@@ -1,48 +0,0 @@
-#include <stc/cqueue.h>
-#include <stc/cstr.h>
-#include <stc/csptr.h>
-#include <stdio.h>
-
-typedef struct { cstr_t name, last; } Person;
-
-Person* Person_make(Person* p, const char* name, const char* last) {
- p->name = cstr(name), p->last = cstr(last);
- return p;
-}
-void Person_del(Person* p) {
- printf("del %s\n", p->name.str);
- c_del(cstr, &p->name, &p->last);
-}
-int Person_compare(const Person* p, const Person* q) {
- int cmp = cstr_equals_s(p->name, q->name);
- return cmp == 0 ? cstr_equals_s(p->last, q->last) : cmp;
-}
-
-//using_clist(p, Person, Person_del);
-//using_cqueue(p, clist_p);
-
-using_csptr(p, Person, Person_del);
-int csptr_p_compare(const csptr_p* p, const csptr_p* q) {return Person_compare(p->get, q->get);}
-
-using_clist(pp, csptr_p, csptr_p_del, csptr_p_compare);
-using_cqueue(pp, clist_pp);
-
-int main() {
- cqueue_pp queue = cqueue_pp_init();
-
- // push() and pop() a few.
- c_forrange (i, 20) {
- csptr_p p = csptr_p_make(c_new(Person));
- p.get->name = cstr_from("Name %d", i);
- p.get->last = cstr_from("Last %d", i);
- cqueue_pp_push(&queue, p);
- }
-
- c_forrange (5)
- cqueue_pp_pop(&queue);
-
- c_foreach (i, cqueue_pp, queue)
- printf(" %s %s\n", i.val->get->name.str, i.val->get->last.str);
-
- cqueue_pp_del(&queue);
-} \ No newline at end of file