diff options
| author | Tyge Løvset <[email protected]> | 2020-07-24 13:07:33 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-24 13:07:33 +0200 |
| commit | 54e135b6453fed2cd210bd9b738eb69fdde93a88 (patch) | |
| tree | a9a30c72b5de09ce6cee644c712365446f439e58 | |
| parent | eda4377e0bea66d23f584b7660a7617f8bea4bf0 (diff) | |
| download | STC-modified-54e135b6453fed2cd210bd9b738eb69fdde93a88.tar.gz STC-modified-54e135b6453fed2cd210bd9b738eb69fdde93a88.zip | |
Added <container>_<tag>_init variables; Fixed prime.c and list.c examples.
| -rw-r--r-- | examples/list.c | 11 | ||||
| -rw-r--r-- | examples/prime.c | 16 | ||||
| -rw-r--r-- | stc/cdefs.h | 4 | ||||
| -rw-r--r-- | stc/clist.h | 7 | ||||
| -rw-r--r-- | stc/cmap.h | 6 | ||||
| -rw-r--r-- | stc/cvec.h | 5 |
6 files changed, 31 insertions, 18 deletions
diff --git a/examples/list.c b/examples/list.c index 81569260..53b8711f 100644 --- a/examples/list.c +++ b/examples/list.c @@ -5,10 +5,7 @@ declare_CList(ix, uint64_t);
int main() {
- CList_ix list = clist_ix_from((uint64_t[]) {10, 20, 30, 40}, 4);
- c_foreach (i, clist_ix, list) printf("%zu ", i.item->value);
- puts("");
-
+ CList_ix list = clist_init;
crandom32_t pcg = crandom32_init(time(NULL));
int n;
for (int i=0; i<10000000; ++i) // ten million
@@ -22,5 +19,11 @@ int main() { puts("sorted");
c_foreach (i, clist_ix, list)
if (n--) printf("%8d: %10zu\n", 100 - n, i.item->value); else break;
+
+ clist_ix_clear(&list);
+ c_push(&list, clist_ix, c_items(10, 20, 30, 40, 50));
+ c_foreach (i, clist_ix, list) printf("%zu ", i.item->value);
+ puts("");
+
clist_ix_destroy(&list);
}
\ No newline at end of file diff --git a/examples/prime.c b/examples/prime.c index 803c772a..04f37e9a 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -3,20 +3,20 @@ #include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <stc/cbitvec.h>
+#include <stc/cbitset.h>
static inline void sieveOfEratosthenes(size_t n)
{
- CBitVec prime = cbitvec_make(n + 1, true);
+ CBitset prime = cbitset_make(n + 1, true);
printf("computing primes up to %zu\n", n);
- cbitvec_unset(&prime, 0);
- cbitvec_unset(&prime, 1);
+ cbitset_reset(&prime, 0);
+ cbitset_reset(&prime, 1);
for (size_t i = 2; i <= n; ++i) {
// If prime[i] is not changed, then it is a prime
- if (cbitvec_value(&prime, i) && i*i <= n) {
+ if (cbitset_test(prime, i) && i*i <= n) {
for (size_t j = i*i; j <= n; j += i) {
- cbitvec_unset(&prime, j);
+ cbitset_reset(&prime, j);
}
}
}
@@ -24,10 +24,10 @@ static inline void sieveOfEratosthenes(size_t n) // Print all prime numbers
size_t count = 0;
for (size_t i = 1; i <= n; ++i)
- if (cbitvec_value(&prime, i)) ++count; // printf("%zu\n", i);
+ if (cbitset_test(prime, i)) ++count; // printf("%zu\n", i);
printf("number of primes: %zu\n", count);
- cbitvec_destroy(&prime);
+ cbitset_destroy(&prime);
}
int main(void)
diff --git a/stc/cdefs.h b/stc/cdefs.h index 753e1b47..b33b9186 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -38,8 +38,12 @@ #if defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define STC_API extern
+#define STC_VARDECL extern
+#define STC_VARDEF
#else
#define STC_API STC_INLINE
+#define STC_VARDECL static
+#define STC_VARDEF static
#endif
/* Macro overloading feature support: https://rextester.com/ONP80107 */
diff --git a/stc/clist.h b/stc/clist.h index 41067286..85c2d9b9 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -91,10 +91,11 @@ \
declare_CListTypes(tag, Value); \
\
- STC_INLINE CList_##tag \
- clist_##tag##_init(void) {return clist_init;} \
+ STC_VARDECL CList_##tag clist_##tag##_init; \
STC_API void \
clist_##tag##_destroy(CList_##tag* self); \
+ STC_INLINE void \
+ clist_##tag##_clear(CList_##tag* self) {clist_##tag##_destroy(self);} \
STC_API void \
clist_##tag##_pushBack(CList_##tag* self, Value value); \
STC_API void \
@@ -149,6 +150,8 @@ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
+ STC_VARDEF CList_##tag clist_##tag##_init = clist_init; \
+ \
STC_API void \
clist_##tag##_destroy(CList_##tag* self) { \
while (self->last) \
@@ -158,8 +158,7 @@ typedef struct { \ uint8_t* _hx; \
} CType##Iter_##tag, ctype##_##tag##_iter_t; \
\
-STC_INLINE CType##_##tag \
-ctype##_##tag##_init(void) {return cmap_init;} \
+STC_VARDECL CType##_##tag ctype##_##tag##_init; \
STC_API CType##_##tag \
ctype##_##tag##_make(size_t initialSize); \
STC_API void \
@@ -201,6 +200,9 @@ typedef Value CType##Value_##tag, ctype##_##tag##_value_t #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
+ \
+STC_VARDEF CType##_##tag ctype##_##tag##_init = cmap_init; \
+ \
STC_API CType##_##tag \
ctype##_##tag##_make(size_t initialSize) { \
CType##_##tag h = ctype##_init; \
@@ -49,8 +49,7 @@ typedef struct CVec_##tag { \ Value* data; \
} CVec_##tag; \
\
-STC_INLINE CVec_##tag \
-cvec_##tag##_init(void) {return cvec_init;} \
+STC_VARDECL CVec_##tag cvec_##tag##_init; \
STC_API CVec_##tag \
cvec_##tag##_make(size_t size, Value null); \
STC_API void \
@@ -112,6 +111,8 @@ typedef RawValue CVecRawValue_##tag #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define implement_CVec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
+STC_VARDEF CVec_##tag cvec_##tag##_init = cvec_init; \
+ \
STC_API CVec_##tag \
cvec_##tag##_make(size_t size, Value null) { \
CVec_##tag vec = cvec_init; \
|
