From 3c00d164e02342d6c36f69ee0faa105ebb3b6494 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 8 Dec 2020 10:01:21 +0100 Subject: - Added clone() to cvec and clist - Renamed cstr_erase(s, pos, n) to cstr_erase_at(s, pos, n) - Changed cvec_erase_at(v, pos) to cvec_erase_at(v, pos, n). --- docs/crandom_api.md | 2 +- examples/demos.c | 15 +++++++++------ stc/clist.h | 9 +++++++++ stc/cstr.h | 4 ++-- stc/cvec.h | 41 ++++++++++++++++++++++++----------------- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/docs/crandom_api.md b/docs/crandom_api.md index e2db0d30..9af69b9e 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -52,7 +52,7 @@ All cstr definitions and prototypes may be included in your C source file by inc ``` `1-2)` PRNG 64-bit engine initializers. `3)` Integer generator, range \[0, 2^64). `4)` Double RNG with range \[0, 1). `5-6)` Uniform integer RNG with range \[*low*, *high*]. `7-8)` Uniform double RNG with range \[*low*, *high*). -`9-10)` Normal-distributed double RNG were 99.7% of the values are within the range [*mean*-*stddev\*3*, *mean*+*stddev\*3*]. +`9-10)` Normal-distributed double RNG were around 68% of the values are within the range [*mean* - *stddev, *mean* + *stddev*]. The method `crand_i64(crand_rng64_t* rng)` is an extremely fast PRNG suited for parallel usage, featuring a Weyl-sequence as part of the state. It is faster than *sfc64*, *wyhash64*, *pcg*, and the *xoroshiro* diff --git a/examples/demos.c b/examples/demos.c index 7e8ef094..0355d32f 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -14,7 +14,7 @@ void stringdemo1() cstr_insert(&cs, 3, "-two"); printf("%s.\n", cs.str); - cstr_erase(&cs, 7, 5); // -nine + cstr_erase_at(&cs, 7, 5); // -nine printf("%s.\n", cs.str); cstr_replace(&cs, cstr_find(cs, "seven"), 5, "four"); @@ -41,14 +41,17 @@ void vectordemo1() printf("\nVECTORDEMO1\n"); cvec_ix bignums = cvec_inits; // = (cvec_ix) cvec_inits; if initializing after declaration. cvec_ix_reserve(&bignums, 100); - for (size_t i = 0; i<=100; ++i) - cvec_ix_push_back(&bignums, i * i * i); + for (size_t i = 10; i <= 100; i += 10) + cvec_ix_push_back(&bignums, i * i); - printf("erase - %d: %zu\n", 100, bignums.data[100]); - cvec_ix_pop_back(&bignums); // erase the last + printf("erase - %d: %zu\n", 3, bignums.data[3]); + cvec_ix_erase_at(&bignums, 3, 1); // erase index 3 + + cvec_ix_pop_back(&bignums); // erase the last + cvec_ix_erase_at(&bignums, 0, 1); // erase the first for (size_t i = 0; i < cvec_size(bignums); ++i) { - if (i >= 90) printf("%zu: %zu\n", i, bignums.data[i]); + printf("%zu: %zu\n", i, bignums.data[i]); } cvec_ix_del(&bignums); } diff --git a/stc/clist.h b/stc/clist.h index 22074a61..768de474 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -118,6 +118,8 @@ STC_API size_t _clist_size(const clist_void* self); \ STC_API void \ clist_##X##_del(clist_##X* self); \ + STC_API clist_##X \ + clist_##X##_clone(clist_##X list); \ STC_INLINE void \ clist_##X##_clear(clist_##X* self) {clist_##X##_del(self);} \ \ @@ -213,6 +215,13 @@ STC_API size_t _clist_size(const clist_void* self); #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) #define _c_implement_clist_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ \ + STC_DEF clist_##X \ + clist_##X##_clone(clist_##X list) { \ + clist_##X out = clist_inits; \ + c_foreach_3 (i, clist_##X, list) \ + clist_##X##_emplace_back(&out, valueToRaw(i.val)); \ + return out; \ + } \ STC_DEF void \ clist_##X##_del(clist_##X* self) { \ while (self->last) _clist_##X##_erase_after(self, self->last); \ diff --git a/stc/cstr.h b/stc/cstr.h index 4a3ed92f..fa577f1a 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -56,7 +56,7 @@ cstr_append_n(cstr_t* self, const char* str, size_t len); STC_API void cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n); STC_API void -cstr_erase(cstr_t* self, size_t pos, size_t n); +cstr_erase_at(cstr_t* self, size_t pos, size_t n); STC_API bool cstr_getdelim(cstr_t *self, int delim, FILE *stream); STC_API size_t @@ -370,7 +370,7 @@ cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n) } STC_DEF void -cstr_erase(cstr_t* self, size_t pos, size_t n) { +cstr_erase_at(cstr_t* self, size_t pos, size_t n) { size_t len = cstr_size(*self); if (len) { memmove(&self->str[pos], &self->str[pos + n], len - (pos + n)); diff --git a/stc/cvec.h b/stc/cvec.h index d6c66751..edb855db 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -88,6 +88,8 @@ return x; \ } \ \ + STC_API cvec_##X \ + cvec_##X##_clone(cvec_##X vec); \ STC_API void \ cvec_##X##_push_n(cvec_##X *self, const cvec_##X##_input_t in[], size_t size); \ STC_API void \ @@ -137,12 +139,8 @@ return cvec_##X##_erase_range_p(self, pos.val, pos.val + 1); \ } \ STC_INLINE cvec_##X##_iter_t \ - cvec_##X##_erase_at(cvec_##X* self, size_t idx) { \ - return cvec_##X##_erase_range_p(self, self->data + idx, self->data + idx + 1); \ - } \ - STC_INLINE cvec_##X##_iter_t \ - cvec_##X##_erase_range_i(cvec_##X* self, size_t ifirst, size_t ifinish) { \ - return cvec_##X##_erase_range_p(self, self->data + ifirst, self->data + ifinish); \ + cvec_##X##_erase_at(cvec_##X* self, size_t idx, size_t n) { \ + return cvec_##X##_erase_range_p(self, self->data + idx, self->data + idx + n); \ } \ \ STC_API cvec_##X##_iter_t \ @@ -238,21 +236,30 @@ cvec_##X##_reserve(self, 4 + len * 3 / 2); \ self->data[_cvec_size(self)++] = value; \ } \ +\ + STC_DEF cvec_##X \ + cvec_##X##_clone(cvec_##X vec) { \ + size_t len = cvec_size(vec); \ + cvec_##X out = cvec_##X##_with_capacity(len); \ + _cvec_size(&out) = len; \ + const cvec_##X##_value_t* p = vec.data; \ + for (size_t i=0; idata, size = cvec_size(*self); \ - c_withbuffer (buf, cvec_##X##_value_t, len) { \ - for (size_t i=0; i cvec_capacity(*self)) \ - cvec_##X##_reserve(self, 4 + (size + len) * 3 / 2); \ - pos = self->data + idx; \ - memmove(pos + len, pos, (size - idx) * sizeof(Value)); \ - memcpy(pos, buf, len * sizeof(Value)); \ - _cvec_size(self) += len; \ - } \ - cvec_##X##_iter_t it = {pos}; return it; \ + if (size + len > cvec_capacity(*self)) \ + cvec_##X##_reserve(self, 4 + (size + len) * 3 / 2); \ + _cvec_size(self) += len; \ + pos = self->data + idx; \ + cvec_##X##_iter_t it = {pos}; \ + memmove(pos + len, pos, (size - idx) * sizeof(Value)); \ + while (first != finish) \ + *pos++ = valueFromRaw(valueToRaw(first++)); \ + return it; \ } \ \ STC_DEF cvec_##X##_iter_t \ -- cgit v1.2.3