diff options
| author | Tyge Løvset <[email protected]> | 2020-09-02 12:45:28 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-02 12:45:28 +0200 |
| commit | 1b46028f4636c78af40c37dbc55d76598996a5b7 (patch) | |
| tree | ef5156aa345465dedbb9f887e077345477f862b6 | |
| parent | d3bb64882aa00c9c1b3056d74796b59fc2180f0e (diff) | |
| download | STC-modified-1b46028f4636c78af40c37dbc55d76598996a5b7.tar.gz STC-modified-1b46028f4636c78af40c37dbc55d76598996a5b7.zip | |
Updated pqueue_top() API. Added c_destroy() also working for cstr, cbitmap.
| -rw-r--r-- | examples/geek7.c | 2 | ||||
| -rw-r--r-- | examples/heap.c | 4 | ||||
| -rw-r--r-- | examples/inits.c | 2 | ||||
| -rw-r--r-- | examples/priority.c | 2 | ||||
| -rw-r--r-- | examples/replace.c | 2 | ||||
| -rw-r--r-- | stc/carray.h | 6 | ||||
| -rw-r--r-- | stc/cbitset.h | 2 | ||||
| -rw-r--r-- | stc/cdefs.h | 2 | ||||
| -rw-r--r-- | stc/cmap.h | 2 | ||||
| -rw-r--r-- | stc/cpqueue.h | 12 | ||||
| -rw-r--r-- | 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<M; ++i)
- printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
+ printf("%.0f ", cpqueue_f_top(pq)), cpqueue_f_pop(&pq);
start = clock();
for (int i=M; i<N; ++i)
@@ -38,7 +38,7 @@ int main() printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
+ printf("%.0f ", cpqueue_f_top(pq)), cpqueue_f_pop(&pq);
puts("");
cpqueue_f_destroy(&pq);
diff --git a/examples/inits.c b/examples/inits.c index ca916577..3d7490d5 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -36,7 +36,7 @@ int main(void) { // sorted:
while (cvec_size(floats) > 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 <assert.h>
#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<sizeof(__arr)/sizeof(__arr[0]); ++i) \
prefix##_destroy(__arr[i]); \
} while (0)
@@ -165,7 +165,7 @@ typedef struct { \ typedef RawKey ctype##_##tag##_rawkey_t; \
typedef RawValue ctype##_##tag##_rawvalue_t; \
\
-typedef struct { \
+typedef struct ctype##_##tag { \
ctype##_##tag##_entry_t* table; \
uint8_t* _hashx; \
uint32_t size, bucket_count; \
diff --git a/stc/cpqueue.h b/stc/cpqueue.h index 77dd8e62..15fd980a 100644 --- a/stc/cpqueue.h +++ b/stc/cpqueue.h @@ -21,7 +21,7 @@ * SOFTWARE.
*/
-/* Priority Queue using cvec as heap.
+/* Priority Queue using heap, with adapter class (normally cvec).
#include <stc/crandom.h>
#include <stc/cpqueue.h>
@@ -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 \
@@ -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<sizeof(__objs)/sizeof(__objs[0]); ++i) \
- cstr_destroy(__objs[i]); \
-} while (0)
STC_API cstr_t
cstr_make_n(const char* str, size_t len);
|
