From 1b46028f4636c78af40c37dbc55d76598996a5b7 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 2 Sep 2020 12:45:28 +0200 Subject: Updated pqueue_top() API. Added c_destroy() also working for cstr, cbitmap. --- examples/geek7.c | 2 +- examples/heap.c | 4 ++-- examples/inits.c | 2 +- examples/priority.c | 2 +- examples/replace.c | 2 +- stc/carray.h | 6 +++--- stc/cbitset.h | 2 +- stc/cdefs.h | 2 +- stc/cmap.h | 2 +- stc/cpqueue.h | 12 ++++++------ stc/cstr.h | 8 +------- 11 files changed, 19 insertions(+), 25 deletions(-) diff --git a/examples/geek7.c b/examples/geek7.c index 65af4486..2ff33223 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -68,7 +68,7 @@ void findElementsAfterDel(int arr[], int m, int del[], // Print top k elements in the min heap for (int i = 0; i < k; ++i) { - printf("%d ", *cpqueue_i_top(&heap)); + printf("%d ", cpqueue_i_top(heap)); // Pop the top element cpqueue_i_pop(&heap); diff --git a/examples/heap.c b/examples/heap.c index 82b351fd..0ff6e059 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -24,7 +24,7 @@ int main() printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); for (int i=0; i 0) { - printf("%.1f ", *cpqueue_f_top(&floats)); + printf("%.1f ", cpqueue_f_top(floats)); cpqueue_f_pop(&floats); } puts("\n"); diff --git a/examples/priority.c b/examples/priority.c index 2451d2d2..0204779d 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -28,7 +28,7 @@ int main() { // Extract the hundred smallest. for (int i=0; i<100; ++i) { - printf("%zd ", *cpqueue_i_top(&heap)); + printf("%zd ", cpqueue_i_top(heap)); cpqueue_i_pop(&heap); } cpqueue_i_destroy(&heap); diff --git a/examples/replace.c b/examples/replace.c index b31715c1..6e5fe024 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -29,5 +29,5 @@ int main () cstr_replace(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) printf("(5) %s\n", s.str); - cstr_mdestroy(&s, &m); + c_destroy(cstr, &s, &m); } diff --git a/stc/carray.h b/stc/carray.h index 387b5cc9..64d385a4 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -92,17 +92,17 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { #define declare_carray_3(tag, Value, valueDestroy) \ - typedef struct { \ + typedef struct carray1##tag { \ Value *data; \ size_t _xdim; \ } carray1##tag; \ \ - typedef struct { \ + typedef struct carray2##tag { \ Value *data; \ size_t _xdim, _yxdim; \ } carray2##tag; \ \ - typedef struct { \ + typedef struct carray3##tag { \ Value *data; \ size_t _xdim, _yxdim, _zdim; \ } carray3##tag; \ diff --git a/stc/cbitset.h b/stc/cbitset.h index 4736f329..31dd3f18 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -46,7 +46,7 @@ int main() { #include #include "cstr.h" -typedef struct { uint64_t* _arr; size_t size; } cbitset_t; +typedef struct cbitset { uint64_t* _arr; size_t size; } cbitset_t; #define cbitset_init {NULL, 0} diff --git a/stc/cdefs.h b/stc/cdefs.h index 1aebec02..f97d84be 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -86,7 +86,7 @@ prefix##_push_n(&container, __arr, sizeof(__arr)/sizeof(__arr[0])); \ } #define c_destroy(prefix, ...) do { \ - prefix* __arr[] = {__VA_ARGS__}; \ + struct prefix* __arr[] = {__VA_ARGS__}; \ for (size_t i=0; i #include @@ -38,7 +38,7 @@ cpqueue_f_push(&queue, crandom_uniform_f32(&gen, dist)); // Extract the 100 smallest. for (int i=0; i<100; ++i) { - printf("%f ", *cpqueue_f_top(&queue)); + printf("%f ", cpqueue_f_top(queue)); cpqueue_f_pop(&queue); } cpqueue_f_destroy(&queue); @@ -59,17 +59,17 @@ typedef type##_##tag##_input_t cpqueue_##tag##_input_t; \ STC_INLINE cpqueue_##tag \ cpqueue_##tag##_init() {return type##_##tag##_init();} \ STC_INLINE size_t \ -cpqueue_##tag##_size(cpqueue_##tag q) {return type##_##tag##_size(q);} \ +cpqueue_##tag##_size(cpqueue_##tag pq) {return type##_##tag##_size(pq);} \ STC_INLINE bool \ -cpqueue_##tag##_empty(cpqueue_##tag q) {return type##_##tag##_empty(q);} \ +cpqueue_##tag##_empty(cpqueue_##tag pq) {return type##_##tag##_empty(pq);} \ STC_INLINE void \ cpqueue_##tag##_destroy(cpqueue_##tag* self) {type##_##tag##_destroy(self);} \ STC_API void \ cpqueue_##tag##_build(cpqueue_##tag* self); \ STC_API void \ cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i); \ -STC_INLINE cpqueue_##tag##_value_t* \ -cpqueue_##tag##_top(cpqueue_##tag* self) {return &self->data[0];} \ +STC_INLINE cpqueue_##tag##_value_t \ +cpqueue_##tag##_top(cpqueue_##tag pq) {return pq.data[0];} \ STC_INLINE void \ cpqueue_##tag##_pop(cpqueue_##tag* self) {cpqueue_##tag##_erase(self, 0);} \ STC_API void \ diff --git a/stc/cstr.h b/stc/cstr.h index 6688f683..bc8e485d 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -30,7 +30,7 @@ #include "cdefs.h" -typedef struct cstr_t { +typedef struct cstr { char* str; } cstr_t; typedef struct { @@ -46,12 +46,6 @@ static cstr_t cstr_init = {(char* ) &_cstr_nullrep[2]}; #define cstr_front(s) (s).str[0] #define cstr_back(s) (s).str[_cstr_size(s) - 1] /* may have side effect */ #define cstr_npos ((size_t) (-1)) -/* destroy multiple strings: */ -#define cstr_mdestroy(...) do { \ - cstr_t *__objs[] = {__VA_ARGS__}; \ - for (size_t i=0; i