diff options
| author | Tyge Løvset <[email protected]> | 2020-09-25 10:27:07 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-25 10:27:07 +0200 |
| commit | c2ab107bc1bcd196b10b1d5f130a1a32449f7773 (patch) | |
| tree | d7b64a12ac6afad3880319a86f5b009f696054ba | |
| parent | 3be928828630984f4ff2c5cf352ace1366c52341 (diff) | |
| download | STC-modified-c2ab107bc1bcd196b10b1d5f130a1a32449f7773.tar.gz STC-modified-c2ab107bc1bcd196b10b1d5f130a1a32449f7773.zip | |
Added cptr.h and good examples for cptr and csptr (share_ptr.c). Some internal refactor.
| -rw-r--r-- | examples/ptr.c | 34 | ||||
| -rw-r--r-- | examples/share_ptr.c | 61 | ||||
| -rw-r--r-- | examples/shared_ptr.c | 48 | ||||
| -rw-r--r-- | stc/carray.h | 4 | ||||
| -rw-r--r-- | stc/cdefs.h | 1 | ||||
| -rw-r--r-- | stc/clist.h | 20 | ||||
| -rw-r--r-- | stc/cmap.h | 4 | ||||
| -rw-r--r-- | stc/cpqueue.h | 4 | ||||
| -rw-r--r-- | stc/cqueue.h | 4 | ||||
| -rw-r--r-- | stc/csptr.h | 33 | ||||
| -rw-r--r-- | stc/cstack.h | 4 | ||||
| -rw-r--r-- | stc/cvec.h | 4 |
12 files changed, 145 insertions, 76 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 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
@@ -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; i<size; ++i) cpqueue_##X##_push(self, in[i]); \
} \
\
- typedef int cpqueue_##X##_dud
+ typedef cpqueue_##X cpqueue_##X##_t
#else
#define implement_cpqueue(X, ctype, cmpOpr)
diff --git a/stc/cqueue.h b/stc/cqueue.h index 9ccb69f1..42f2be67 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -60,7 +60,7 @@ #define using_cqueue(X, ctype) \
\
- typedef ctype##_t cqueue_##X, cqueue_##X##_t; \
+ typedef ctype##_t cqueue_##X; \
typedef ctype##_value_t cqueue_##X##_value_t; \
typedef ctype##_rawvalue_t cqueue_##X##_rawvalue_t; \
typedef ctype##_input_t cqueue_##X##_input_t; \
@@ -100,6 +100,6 @@ STC_INLINE cqueue_##X##_value_t* \
cqueue_##X##_itval(cqueue_##X##_iter_t it) {return ctype##_itval(it);} \
\
- typedef int cqueue_##X##_dud
+ typedef cqueue_##X cqueue_##X##_t
#endif
diff --git a/stc/csptr.h b/stc/csptr.h index 406ab74a..82f8b3f8 100644 --- a/stc/csptr.h +++ b/stc/csptr.h @@ -82,18 +82,21 @@ typedef long atomic_count_t; }
#endif
+#define using_csptr(...) c_MACRO_OVERLOAD(using_csptr, __VA_ARGS__)
-#define using_csptr(X, Value, valueDestroy) \
+#define using_csptr_2(X, Value) \
+ using_csptr_3(X, Value, c_default_del)
+
+#define using_csptr_3(X, Value, valueDestroy) \
typedef Value csptr_##X##_value_t; \
typedef struct { csptr_##X##_value_t* get; atomic_count_t* use_count; } csptr_##X, csptr_##X##_t; \
\
STC_INLINE csptr_##X \
csptr_##X##_make(csptr_##X##_value_t* p) { \
csptr_##X ptr = {p}; \
- if (p) *(ptr.use_count = c_new(atomic_count_t)) = 1; \
+ if (p) *(ptr.use_count = c_new_1(atomic_count_t)) = 1; \
return ptr; \
} \
-\
STC_INLINE csptr_##X \
csptr_##X##_share(csptr_##X ptr) { \
if (ptr.get) atomic_increment(ptr.use_count); \
@@ -108,7 +111,6 @@ typedef long atomic_count_t; c_free(self->get); \
} \
} \
-\
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
@@ -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)
|
