From f857d8215a266673e25356779f740100fe362025 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 25 Nov 2021 22:14:11 +0100 Subject: Updated int params constness. Updated random.c example. --- examples/crandom_ex.c | 51 --------------------------------------- examples/random.c | 43 +++++++++++++-------------------- include/stc/cdeq.h | 2 +- include/stc/cmap.h | 4 ++-- include/stc/csmap.h | 2 +- include/stc/cstr.h | 66 +++++++++++++++++++++++++-------------------------- include/stc/cvec.h | 4 ++-- 7 files changed, 56 insertions(+), 116 deletions(-) delete mode 100644 examples/crandom_ex.c diff --git a/examples/crandom_ex.c b/examples/crandom_ex.c deleted file mode 100644 index 1d1691af..00000000 --- a/examples/crandom_ex.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include -#include - -int main() -{ - enum {R = 30}; - const size_t N = 1000000000; - uint64_t seed = 1234; // time(NULL); - stc64_t rng = stc64_init(seed); - - uint64_t sum = 0; - - stc64_normalf_t dist2 = stc64_normalf_init((float)R / 2.0, (float)R / 6.0); - size_t N2 = 10000000; - int hist[R] = {0}; - sum = 0; - c_forrange (N2) { - int n = round((stc64_normalf(&rng, &dist2) + 0.5)); - sum += n; - if (n >= 0 && n < R) ++hist[n]; - } - cstr bar = cstr_init(); - c_forrange (i, int, R) { - cstr_resize(&bar, hist[i] * 25ull * R / N2, '*'); - printf("%3d %s\n", i, bar.str); - } - - clock_t diff, before; - - sum = 0; - before = clock(); - c_forrange (N) { - sum += stc64_rand(&rng); - } - diff = clock() - before; - printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - - stc64_uniform_t dist1 = stc64_uniform_init(0, 1000); - sum = 0; - before = clock(); - c_forrange (N) { - sum += stc64_uniform(&rng, &dist1); - } - diff = clock() - before; - printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - - cstr_del(&bar); -} \ No newline at end of file diff --git a/examples/random.c b/examples/random.c index b3b01533..d4a12a27 100644 --- a/examples/random.c +++ b/examples/random.c @@ -1,51 +1,42 @@ #include #include -#include #include -#include int main() { - enum {R = 30}; - const size_t N = 1000000000; - uint64_t seed = 1234; // time(NULL); + const size_t N = 5000000000; + const uint64_t seed = time(NULL), range = 1000000; stc64_t rng = stc64_init(seed); - uint64_t sum = 0; + uint64_t sum; + clock_t diff, before; - stc64_normalf_t dist2 = stc64_normalf_init((float)R / 2.0, (float)R / 6.0); - size_t N2 = 10000000; - int hist[R] = {0}; + printf("Compare speed of full and unbiased ranged random numbers...\n"); sum = 0; - c_forrange (N2) { - int n = (int) round((stc64_normalf(&rng, &dist2) + 0.5)); - sum += n; - if (n >= 0 && n < R) ++hist[n]; - } - cstr bar = cstr_init(); - c_forrange (i, int, R) { - cstr_resize(&bar, hist[i] * 25ull * R / N2, '*'); - printf("%3d %s\n", i, bar.str); + before = clock(); + c_forrange (N) { + sum += (uint32_t) stc64_rand(&rng); } + diff = clock() - before; + printf("full range\t\t: %f secs, %zu, avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - clock_t diff, before; - + stc64_uniform_t dist1 = stc64_uniform_init(0, range); + rng = stc64_init(seed); sum = 0; before = clock(); c_forrange (N) { - sum += stc64_rand(&rng); + sum += stc64_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("unbiased 0-%zu\t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - stc64_uniform_t dist1 = stc64_uniform_init(0, 1000); sum = 0; + rng = stc64_init(seed); before = clock(); c_forrange (N) { - sum += stc64_uniform(&rng, &dist1); + sum += stc64_rand(&rng) % (range + 1); // biased } diff = clock() - before; - printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); + printf("biased 0-%zu \t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - cstr_del(&bar); } \ No newline at end of file diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 6475b431..3e32beb6 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -46,7 +46,7 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API void _cx_memb(_del)(_cx_self* self); STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); -STC_API bool _cx_memb(_expand_right_half_)(_cx_self* self, const size_t idx, const size_t n); +STC_API bool _cx_memb(_expand_right_half_)(_cx_self* self, size_t idx, size_t n); #ifndef i_queue STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, i_valraw raw); diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 5feff0c3..979f1b44 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -87,11 +87,11 @@ typedef cx_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_rawvalue; -STC_API _cx_self _cx_memb(_with_capacity)(const size_t cap); +STC_API _cx_self _cx_memb(_with_capacity)(size_t cap); STC_API _cx_self _cx_memb(_clone)(_cx_self map); STC_API void _cx_memb(_del)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t capacity); +STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t capacity); STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr); STC_API _cx_result _cx_memb(_insert_entry_)(_cx_self* self, i_keyraw rkey); STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val); diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 4e024c63..279bdf6d 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -96,7 +96,7 @@ typedef cx_SET_ONLY( i_keyraw ) STC_API _cx_self _cx_memb(_init)(void); STC_API _cx_self _cx_memb(_clone)(_cx_self tree); STC_API void _cx_memb(_del)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t cap); +STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, i_keyraw rkey, _cx_iter* out); STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, i_keyraw rkey); STC_API _cx_value* _cx_memb(_front)(const _cx_self* self); diff --git a/include/stc/cstr.h b/include/stc/cstr.h index d07b3ce3..7cb92685 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -96,17 +96,17 @@ STC_INLINE void cstr_push_back(cstr* self, char value) { cstr_append_n(self, &value, 1); } STC_INLINE void cstr_pop_back(cstr* self) { self->str[ --_cstr_rep(self)->size ] = '\0'; } -STC_INLINE void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n) +STC_INLINE void cstr_insert_n(cstr* self, const size_t pos, const char* str, const size_t n) { cstr_replace_n(self, pos, 0, str, n); } -STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str) +STC_INLINE void cstr_insert(cstr* self, const size_t pos, const char* str) { cstr_replace_n(self, pos, 0, str, strlen(str)); } -STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s) +STC_INLINE void cstr_insert_s(cstr* self, const size_t pos, cstr s) { cstr_replace_n(self, pos, 0, s.str, _cstr_rep(&s)->size); } -STC_INLINE void cstr_replace(cstr* self, size_t pos, size_t len, const char* str) +STC_INLINE void cstr_replace(cstr* self, const size_t pos, const size_t len, const char* str) { cstr_replace_n(self, pos, len, str, strlen(str)); } -STC_INLINE void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s) +STC_INLINE void cstr_replace_s(cstr* self, const size_t pos, const size_t len, cstr s) { cstr_replace_n(self, pos, len, s.str, _cstr_rep(&s)->size); } -STC_INLINE void cstr_erase(cstr* self, size_t pos) +STC_INLINE void cstr_erase(cstr* self, const size_t pos) { cstr_erase_n(self, pos, 1); } STC_INLINE char* cstr_front(cstr* self) { return self->str; } STC_INLINE char* cstr_back(cstr* self) @@ -126,14 +126,14 @@ STC_INLINE bool cstr_getline(cstr *self, FILE *stream) { return cstr_getdelim(self, '\n', stream); } STC_INLINE cstr -cstr_with_capacity(size_t cap) { +cstr_with_capacity(const size_t cap) { cstr s = cstr_null; cstr_reserve(&s, cap); return s; } STC_INLINE cstr -cstr_with_size(size_t len, char fill) { +cstr_with_size(const size_t len, const char fill) { cstr s = cstr_null; cstr_resize(&s, len, fill); return s; @@ -162,7 +162,7 @@ cstr_starts_with(cstr s, const char* sub) { STC_INLINE bool cstr_ends_with(cstr s, const char* sub) { - size_t n = strlen(sub), sz = _cstr_rep(&s)->size; + const size_t n = strlen(sub), sz = _cstr_rep(&s)->size; return n <= sz && !memcmp(s.str + sz - n, sub, n); } @@ -179,9 +179,9 @@ STC_LIBRARY_ONLY( static struct cstr_rep _cstr_nullrep = {0, 0, {0}}; const cstr cstr_null = {_cstr_nullrep.str}; ) STC_DEF size_t -cstr_reserve(cstr* self, size_t cap) { +cstr_reserve(cstr* self, const size_t cap) { struct cstr_rep* rep = _cstr_rep(self); - size_t oldcap = rep->cap; + const size_t oldcap = rep->cap; if (cap > oldcap) { rep = (struct cstr_rep*) c_realloc(oldcap ? rep : NULL, _cstr_opt_mem(cap)); self->str = rep->str; @@ -192,15 +192,15 @@ cstr_reserve(cstr* self, size_t cap) { } STC_DEF void -cstr_resize(cstr* self, size_t len, char fill) { - size_t n = _cstr_rep(self)->size; +cstr_resize(cstr* self, const size_t len, const char fill) { + const size_t n = _cstr_rep(self)->size; cstr_reserve(self, len); if (len > n) memset(self->str + n, fill, len - n); if (len | n) self->str[_cstr_rep(self)->size = len] = '\0'; } STC_DEF cstr -cstr_from_n(const char* str, size_t n) { +cstr_from_n(const char* str, const size_t n) { if (n == 0) return cstr_null; struct cstr_rep* rep = (struct cstr_rep*) c_malloc(_cstr_opt_mem(n)); cstr s = {(char *) memcpy(rep->str, str, n)}; @@ -254,7 +254,7 @@ cstr_assign_fmt(cstr* self, const char* fmt, ...) { } STC_DEF cstr* -cstr_assign_n(cstr* self, const char* str, size_t n) { +cstr_assign_n(cstr* self, const char* str, const size_t n) { if (n || _cstr_rep(self)->cap) { cstr_reserve(self, n); memmove(self->str, str, n); @@ -264,11 +264,11 @@ cstr_assign_n(cstr* self, const char* str, size_t n) { } STC_DEF cstr* -cstr_append_n(cstr* self, const char* str, size_t n) { +cstr_append_n(cstr* self, const char* str, const size_t n) { if (n == 0) return self; - size_t oldlen = _cstr_rep(self)->size, newlen = oldlen + n; + const size_t oldlen = _cstr_rep(self)->size, newlen = oldlen + n; if (newlen > _cstr_rep(self)->cap) { - size_t off = (size_t) (str - self->str); /* handle self append */ + const size_t off = (size_t) (str - self->str); /* handle self append */ cstr_reserve(self, (oldlen*3 >> 1) + n); if (off <= oldlen) str = self->str + off; } @@ -277,10 +277,10 @@ cstr_append_n(cstr* self, const char* str, size_t n) { return self; } -STC_INLINE void _cstr_internal_move(cstr* self, size_t pos1, size_t pos2) { +STC_INLINE void _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { if (pos1 == pos2) return; - size_t len = _cstr_rep(self)->size, newlen = len + pos2 - pos1; + const size_t len = _cstr_rep(self)->size, newlen = len + pos2 - pos1; if (newlen > _cstr_rep(self)->cap) cstr_reserve(self, (len*3 >> 1) + pos2 - pos1); memmove(&self->str[pos2], &self->str[pos1], len - pos1); @@ -288,8 +288,8 @@ STC_INLINE void _cstr_internal_move(cstr* self, size_t pos1, size_t pos2) { } STC_DEF void -cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) { - size_t sz = cstr_size(*self); +cstr_replace_n(cstr* self, const size_t pos, size_t len, const char* str, const size_t n) { + const size_t sz = cstr_size(*self); if (len > sz - pos) len = sz - pos; c_autobuf (xstr, char, n) { memcpy(xstr, str, n); @@ -299,14 +299,14 @@ cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) { } STC_DEF cstr -cstr_from_replace_all(const char* str, size_t str_len, - const char* find, size_t find_len, - const char* repl, size_t repl_len) { +cstr_from_replace_all(const char* str, const size_t str_len, + const char* find, const size_t find_len, + const char* repl, const size_t repl_len) { cstr out = cstr_null; - size_t from = 0, pos; char* res; + size_t from = 0; char* res; if (find_len) while ((res = c_strnstrn(str + from, find, str_len - from, find_len))) { - pos = res - str; + const size_t pos = res - str; cstr_append_n(&out, str + from, pos - from); cstr_append_n(&out, repl, repl_len); from = pos + find_len; @@ -322,8 +322,8 @@ cstr_replace_all(cstr* self, const char* find, const char* repl) { } STC_DEF void -cstr_erase_n(cstr* self, size_t pos, size_t n) { - size_t len = _cstr_rep(self)->size; +cstr_erase_n(cstr* self, const size_t pos, size_t n) { + const size_t len = _cstr_rep(self)->size; if (n > len - pos) n = len - pos; if (len) { memmove(&self->str[pos], &self->str[pos + n], len - (pos + n)); @@ -332,7 +332,7 @@ cstr_erase_n(cstr* self, size_t pos, size_t n) { } STC_DEF bool -cstr_getdelim(cstr *self, int delim, FILE *fp) { +cstr_getdelim(cstr *self, const int delim, FILE *fp) { size_t pos = 0, cap = _cstr_rep(self)->cap; int c = fgetc(fp); if (c == EOF) @@ -356,9 +356,9 @@ cstr_find(cstr s, const char* needle) { } STC_DEF size_t -cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax) { +cstr_find_n(cstr s, const char* needle, const size_t pos, const size_t nmax) { if (pos > _cstr_rep(&s)->size) return cstr_npos; - size_t nlen = strlen(needle); + const size_t nlen = strlen(needle); char* res = c_strnstrn(s.str + pos, needle, _cstr_rep(&s)->size - pos, nmax < nlen ? nmax : nlen); return res ? res - s.str : cstr_npos; } @@ -371,7 +371,7 @@ c_strncasecmp(const char* s1, const char* s2, size_t nmax) { } STC_DEF char* -c_strnstrn(const char *s, const char *needle, size_t slen, size_t nlen) { +c_strnstrn(const char *s, const char *needle, size_t slen, const size_t nlen) { if (!nlen) return (char *)s; if (nlen > slen) return NULL; slen -= nlen; diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 5e9a9877..25c82765 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -82,8 +82,8 @@ STC_API _cx_self _cx_memb(_init)(void); STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API void _cx_memb(_del)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t cap); -STC_API bool _cx_memb(_resize)(_cx_self* self, const size_t size, i_val fill_val); +STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); +STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_val fill_val); #ifndef i_cmp_none STC_API int _cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw raw); -- cgit v1.2.3