diff options
| -rw-r--r-- | docs/coroutine_api.md | 1 | ||||
| -rw-r--r-- | include/stc/cmap.h | 2 | ||||
| -rw-r--r-- | include/stc/coroutine.h | 4 | ||||
| -rw-r--r-- | include/stc/cpque.h | 2 | ||||
| -rw-r--r-- | include/stc/priv/template.h | 20 | ||||
| -rw-r--r-- | misc/examples/algorithms/random.c | 35 | ||||
| -rw-r--r-- | misc/examples/coroutines/coroutines.c | 2 | ||||
| -rw-r--r-- | misc/examples/coroutines/dining_philosophers.c | 6 | ||||
| -rw-r--r-- | misc/examples/mixed/demos.c | 6 |
9 files changed, 41 insertions, 37 deletions
diff --git a/docs/coroutine_api.md b/docs/coroutine_api.md index 6dc12123..b356dcf0 100644 --- a/docs/coroutine_api.md +++ b/docs/coroutine_api.md @@ -51,7 +51,6 @@ NB! ***cco_yield\*()*** / ***cco_await\*()*** may not be called from within a `s | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | | `void` | `cco_blocking_call(cocall) {}` | Run blocking until cocall is finished | -| `void` | `cco_blocking_call(cocall, int* outres) {}`| Run blocking until cocall is finished | | | `cco_blocking_task(task) {}` | Run blocking until task is finished | | | `cco_blocking_task(task, rt, STACKSZ) {}`| Run blocking until task is finished | | | Time functions: | | diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 2dd8cbe6..cd7430ba 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -69,6 +69,7 @@ struct chash_slot { uint8_t hashx; }; #define _i_SET_ONLY c_true #define _i_keyref(vp) (vp) #endif +#define _i_ishash #include "priv/template.h" #ifndef i_is_forward _cx_DEFTYPES(_c_chash_types, _cx_Self, i_key, i_val, _i_MAP_ONLY, _i_SET_ONLY); @@ -463,6 +464,7 @@ _cx_MEMB(_erase_entry)(_cx_Self* self, _cx_value* _val) { #undef i_max_load_factor #undef _i_isset #undef _i_ismap +#undef _i_ishash #undef _i_keyref #undef _i_MAP_ONLY #undef _i_SET_ONLY diff --git a/include/stc/coroutine.h b/include/stc/coroutine.h index 01a32fc2..0e592bae 100644 --- a/include/stc/coroutine.h +++ b/include/stc/coroutine.h @@ -101,9 +101,7 @@ typedef enum { } while (0) /* cco_blocking_call(): assumes coroutine returns a cco_result value (int) */ -#define cco_blocking_call(...) c_MACRO_OVERLOAD(cco_blocking_call, __VA_ARGS__) -#define cco_blocking_call_1(corocall) while ((corocall) != CCO_DONE) -#define cco_blocking_call_2(corocall, result) while ((*(result) = (corocall)) != CCO_DONE) +#define cco_blocking_call(corocall) while ((corocall) != CCO_DONE) #define cco_cleanup \ *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP diff --git a/include/stc/cpque.h b/include/stc/cpque.h index ca51eeff..520514ef 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -29,6 +29,7 @@ #endif #define _i_prefix cpque_ +#define _i_ispque #include "priv/template.h" #ifndef i_is_forward _cx_DEFTYPES(_c_cpque_types, _cx_Self, i_key); @@ -160,3 +161,4 @@ _cx_MEMB(_push)(_cx_Self* self, _cx_value value) { #endif #define CPQUE_H_INCLUDED #include "priv/template2.h" +#undef _i_ispque
\ No newline at end of file diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index ccdce718..0f4ac893 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -171,6 +171,15 @@ #endif #endif +#ifndef i_no_cmp + #if defined i_cmp || defined i_less || defined i_cmp_native + #define _i_has_cmp + #endif + #if defined i_eq || defined i_cmp_native + #define _i_has_eq + #endif +#endif + #if !defined i_key #error "No i_key or i_val defined" #elif defined i_keyraw ^ defined i_keyto @@ -179,6 +188,10 @@ #error "Both i_keyclone/i_valclone and i_keydrop/i_valdrop must be defined, if any" #elif defined i_from || defined i_drop #error "i_from / i_drop not supported. Define i_keyfrom/i_valfrom and/or i_keydrop/i_valdrop instead" +#elif defined i_keyraw && defined _i_ishash && !(defined i_hash && (defined _i_has_cmp || defined i_eq)) + #error "For cmap/cset, both i_hash and i_eq (or i_less or i_cmp) must be defined when i_keyraw is defined." +#elif defined i_keyraw && (defined _i_ismap || defined _i_isset || defined _i_ispque) && !defined _i_has_cmp + #error "For csmap/csset/cpque, i_cmp or i_less must be defined when i_keyraw is defined." #endif #ifndef i_tag @@ -203,13 +216,6 @@ #endif #ifndef i_no_cmp - #if defined i_cmp || defined i_less || defined i_cmp_native - #define _i_has_cmp - #endif - #if defined i_eq || defined i_cmp_native - #define _i_has_eq - #endif - // i_eq, i_less, i_cmp #if !defined i_eq && (defined i_cmp || defined i_less) #define i_eq(x, y) !(i_cmp(x, y)) diff --git a/misc/examples/algorithms/random.c b/misc/examples/algorithms/random.c index 9522c16d..e457d329 100644 --- a/misc/examples/algorithms/random.c +++ b/misc/examples/algorithms/random.c @@ -4,42 +4,39 @@ int main(void) { - const long long N = 10000000; - const uint64_t seed = (uint64_t)time(NULL), range = 1000000; + const long long N = 10000000, range = 1000000; + const uint64_t seed = (uint64_t)time(NULL); crand_t rng = crand_init(seed); - - long long sum; - clock_t diff, before; + clock_t t; printf("Compare speed of full and unbiased ranged random numbers...\n"); - sum = 0; - before = clock(); + long long sum = 0; + t = clock(); c_forrange (N) { - sum += (uint32_t)crand_u64(&rng); + sum += (int32_t)crand_u64(&rng); } - diff = clock() - before; + t = clock() - t; printf("full range\t\t: %f secs, %lld, avg: %f\n", - (double)diff/(double)CLOCKS_PER_SEC, N, (double)(sum/N)); + (double)t/CLOCKS_PER_SEC, N, (double)(sum/N)); crand_uniform_t dist1 = crand_uniform_init(0, range); rng = crand_init(seed); sum = 0; - before = clock(); + t = clock(); c_forrange (N) { sum += crand_uniform(&rng, &dist1); // unbiased } - diff = clock() - before; + t = clock() - t; printf("unbiased 0-%lld\t: %f secs, %lld, avg: %f\n", - (long long)range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); + range, (double)t/CLOCKS_PER_SEC, N, (double)(sum/N)); sum = 0; rng = crand_init(seed); - before = clock(); + t = clock(); c_forrange (N) { - sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased + sum += (int32_t)crand_u64(&rng) % (range + 1); // biased } - diff = clock() - before; - printf("biased 0-%" PRIu64 " \t: %f secs, %lld, avg: %f\n", - (long long)range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); - + t = clock() - t; + printf("biased 0-%lld \t: %f secs, %lld, avg: %f\n", + range, (double)t/CLOCKS_PER_SEC, N, (double)(sum/N)); } diff --git a/misc/examples/coroutines/coroutines.c b/misc/examples/coroutines/coroutines.c index e642a823..faeb71f6 100644 --- a/misc/examples/coroutines/coroutines.c +++ b/misc/examples/coroutines/coroutines.c @@ -103,7 +103,7 @@ int main(void) struct combined c = {.prm={.count=8}, .fib={14}}; int res; - cco_blocking_call(combined(&c), &res) { + cco_blocking_call(res = combined(&c)) { if (res == CCO_YIELD) printf("Prime(%d)=%lld, Fib(%d)=%lld\n", c.prm.idx, c.prm.result, diff --git a/misc/examples/coroutines/dining_philosophers.c b/misc/examples/coroutines/dining_philosophers.c index f7edf815..e917c303 100644 --- a/misc/examples/coroutines/dining_philosophers.c +++ b/misc/examples/coroutines/dining_philosophers.c @@ -39,7 +39,7 @@ int philosopher(struct Philosopher* p) printf("Philosopher %d is hungry...\n", p->id); cco_await_sem(p->left_fork); cco_await_sem(p->right_fork); - + duration = 0.5 + crandf(); printf("Philosopher %d is eating for %.0f minutes...\n", p->id, duration*10); cco_await_timer(&p->tm, duration); @@ -94,10 +94,10 @@ int main(void) cco_timer tm = cco_timer_from(15.0); // seconds csrand((uint64_t)time(NULL)); - while (!cco_done(&dine)) { + cco_blocking_call(dining(&dine)) + { if (cco_timer_expired(&tm)) cco_stop(&dine); - dining(&dine); // resume cco_sleep(0.001); ++n; } diff --git a/misc/examples/mixed/demos.c b/misc/examples/mixed/demos.c index 1a604d9f..7f5091fd 100644 --- a/misc/examples/mixed/demos.c +++ b/misc/examples/mixed/demos.c @@ -28,7 +28,7 @@ void stringdemo1(void) cstr_drop(&cs); } -#define i_key int64_t +#define i_key long long #define i_tag ix #include <stc/cvec.h> @@ -39,14 +39,14 @@ void vectordemo1(void) for (int i = 10; i <= 100; i += 10) cvec_ix_push(&bignums, i * i); - printf("erase - %d: %" PRIu64 "\n", 3, bignums.data[3]); + printf("erase - %d: %lld\n", 3, bignums.data[3]); cvec_ix_erase_n(&bignums, 3, 1); // erase index 3 cvec_ix_pop(&bignums); // erase the last cvec_ix_erase_n(&bignums, 0, 1); // erase the first for (int i = 0; i < cvec_ix_size(&bignums); ++i) { - printf("%d: %" PRIu64 "\n", i, bignums.data[i]); + printf("%d: %lld\n", i, bignums.data[i]); } cvec_ix_drop(&bignums); |
