From c2ab107bc1bcd196b10b1d5f130a1a32449f7773 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 25 Sep 2020 10:27:07 +0200 Subject: Added cptr.h and good examples for cptr and csptr (share_ptr.c). Some internal refactor. --- examples/ptr.c | 34 ++++++++++++++++++++++++++++ examples/share_ptr.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ examples/shared_ptr.c | 48 ---------------------------------------- stc/carray.h | 4 ++-- stc/cdefs.h | 1 - stc/clist.h | 20 ++++++++--------- stc/cmap.h | 4 ++-- stc/cpqueue.h | 4 ++-- stc/cqueue.h | 4 ++-- stc/csptr.h | 33 +++++++++++++++++++++++----- stc/cstack.h | 4 ++-- stc/cvec.h | 4 ++-- 12 files changed, 145 insertions(+), 76 deletions(-) create mode 100644 examples/ptr.c create mode 100644 examples/share_ptr.c delete mode 100644 examples/shared_ptr.c 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 +#include +#include + +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 +#include +#include +#include +#include + +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 -#include -#include -#include - -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 diff --git a/stc/carray.h b/stc/carray.h index 4808307d..cf46bbba 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -104,7 +104,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { typedef struct { \ Value *data; \ size_t _xdim; \ - } carray1##X, carray1##X##_t; \ + } carray1##X; \ \ typedef struct { \ Value *data; \ @@ -187,6 +187,6 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { carray3##X##_at(carray3##X *a, size_t z, size_t y, size_t x) { \ return a->data + z*a->_yxdim + y*carray3_xdim(*a) + x; \ } \ - typedef int carray1##X##_dud + typedef carray1##X carray1##X##_t #endif diff --git a/stc/cdefs.h b/stc/cdefs.h index 8328c837..7bf37189 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -77,7 +77,6 @@ enum {_c_max_buffer = 512}; #define c_default_from_raw(x) (x) #define c_default_to_raw(ptr) (*(ptr)) #define c_default_del(ptr) ((void) (ptr)) -#define c_pointer_del(pptr) free(*(pptr)) #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) #define c_foreach_3(it, ctype, cnt) \ diff --git a/stc/clist.h b/stc/clist.h index 9483e0f4..4ff9c114 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -62,10 +62,10 @@ #define using_clist_3(X, Value, valueDestroy) \ using_clist_4(X, Value, valueDestroy, c_default_compare) #define using_clist_4(X, Value, valueDestroy, valueCompare) \ - using_clist_7(X, Value, valueDestroy, Value, \ - valueCompare, c_default_to_raw, c_default_from_raw) -#define using_clist_str() using_clist_7(str, cstr_t, cstr_del, const char*, \ - cstr_compare_raw, cstr_to_raw, cstr) + using_clist_7(X, Value, valueDestroy, valueCompare, \ + Value, c_default_to_raw, c_default_from_raw) +#define using_clist_str() using_clist_7(str, cstr_t, cstr_del, cstr_compare_raw, \ + const char*, cstr_to_raw, cstr) #define using_clist_types(X, Value) \ typedef Value clist_##X##_value_t; \ @@ -77,7 +77,7 @@ \ typedef struct { \ clist_##X##_node_t* last; \ - } clist_##X, clist_##X##_t; \ + } clist_##X; \ \ typedef struct { \ clist_##X##_node_t* const* _last; \ @@ -101,7 +101,7 @@ using_clist_types(void, int); STC_API size_t _clist_size(const clist_void* self); #define _clist_node(X, vp) c_container_of(vp, clist_##X##_node_t, value) -#define using_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ +#define using_clist_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ \ using_clist_types(X, Value); \ typedef RawValue clist_##X##_rawvalue_t; \ @@ -205,13 +205,13 @@ STC_API size_t _clist_size(const clist_void* self); STC_INLINE Value* \ clist_##X##_back(clist_##X* self) {return &self->last->value;} \ \ - _c_implement_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) + _c_implement_clist_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define _c_implement_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ +#define _c_implement_clist_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ \ STC_API void \ clist_##X##_del(clist_##X* self) { \ @@ -319,7 +319,7 @@ STC_API size_t _clist_size(const clist_void* self); clist_void_node_t* last = _clist_mergesort((clist_void_node_t *) self->last->next, clist_##X##_sort_compare); \ self->last = (clist_##X##_node_t *) last; \ } \ - typedef int clist_##X##_dud + typedef clist_##X clist_##X##_t #define _c_clist_insert_after(self, X, node, val) \ @@ -397,7 +397,7 @@ _clist_mergesort(clist_void_node_t *list, int (*cmp)(const void*, const void*)) } #else -#define _c_implement_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) +#define _c_implement_clist_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) #endif #endif diff --git a/stc/cmap.h b/stc/cmap.h index 923868aa..a263cce1 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -195,7 +195,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; uint32_t size, bucket_count; \ float max_load_factor; \ float shrink_limit_factor; \ - } ctype##_##X, ctype##_##X##_t; \ + } ctype##_##X; \ \ typedef struct { \ ctype##_##X##_value_t *val; \ @@ -437,7 +437,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; --self->size; \ } \ \ - typedef int ctype##_##X##_dud + typedef ctype##_##X ctype##_##X##_t /* https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ */ diff --git a/stc/cpqueue.h b/stc/cpqueue.h index e1c13d57..92121dfc 100644 --- a/stc/cpqueue.h +++ b/stc/cpqueue.h @@ -52,7 +52,7 @@ #define using_cpqueue(X, ctype, cmpOpr) /* cmpOpr: < or > */ \ \ - typedef ctype##_t cpqueue_##X, cpqueue_##X##_t; \ + typedef ctype##_t cpqueue_##X; \ typedef ctype##_value_t cpqueue_##X##_value_t; \ typedef ctype##_rawvalue_t cpqueue_##X##_rawvalue_t; \ typedef ctype##_input_t cpqueue_##X##_input_t; \ @@ -133,7 +133,7 @@ for (size_t i=0; iget); \ } \ } \ -\ STC_INLINE void \ csptr_##X##_reset(csptr_##X* self, csptr_##X##_value_t* p) { \ csptr_##X##_del(self); \ @@ -118,6 +120,27 @@ typedef long atomic_count_t; STC_INLINE csptr_##X##_value_t* \ csptr_##X##_get(csptr_##X x) {return x.get;} \ \ - typedef int csptr_##X##_dud + typedef csptr_##X csptr_##X##_t + + +#define using_csptr_4(X, Value, valueDestroy, valueCompare) \ + using_csptr_3(X, Value, valueDestroy); \ + STC_INLINE int \ + csptr_##X##_compare(csptr_##X* x, csptr_##X* y) { \ + return valueCompare(x->get, y->get); \ + } \ + typedef int csptr_##X##_dud4 + +#define using_csptr_5(X, Value, valueDestroy, valueCompare, hash) \ + using_csptr_4(X, Value, valueDestroy, valueCompare); \ + STC_INLINE int \ + csptr_##X##_equals(csptr_##X* x, csptr_##X* y) { \ + return valueCompare(x->get, y->get) == 0; \ + } \ + STC_INLINE uint32_t \ + csptr_##X##_hash(csptr_##X* self, size_t len) { \ + return hash(&self->get, len); \ + } \ + typedef int csptr_##X##_dud5 #endif diff --git a/stc/cstack.h b/stc/cstack.h index 37e8f3f3..ae37b859 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -49,7 +49,7 @@ #define using_cstack(X, ctype) \ \ - typedef ctype##_t cstack_##X, cstack_##X##_t; \ + typedef ctype##_t cstack_##X; \ typedef ctype##_value_t cstack_##X##_value_t; \ typedef ctype##_rawvalue_t cstack_##X##_rawvalue_t; \ typedef ctype##_input_t cstack_##X##_input_t; \ @@ -87,6 +87,6 @@ STC_INLINE cstack_##X##_value_t* \ cstack_##X##_itval(cstack_##X##_iter_t it) {return ctype##_itval(it);} \ \ - typedef int cstack_##X##_dud + typedef cstack_##X cstack_##X##_t #endif diff --git a/stc/cvec.h b/stc/cvec.h index 55ce3ef0..a5cd97ab 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -52,7 +52,7 @@ \ typedef struct { \ cvec_##X##_value_t* data; \ - } cvec_##X, cvec_##X##_t; \ + } cvec_##X; \ \ STC_INLINE cvec_##X \ cvec_##X##_init(void) {cvec_##X v = cvec_INIT; return v;} \ @@ -287,7 +287,7 @@ RawValue ry = valueToRaw(y); \ return valueCompareRaw(&rx, &ry); \ } \ - typedef int cvec_##taq##_dud + typedef cvec_##X cvec_##X##_t #else #define _c_implement_cvec_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) -- cgit v1.2.3