From eb85069b669e754836b9d4587ba03d3af1a5e975 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 26 Mar 2023 00:27:45 +0100 Subject: development branch for 4.2 Removed uses of c_auto and c_with in documentation examples and code examples. Still using c_defer a few places. Renamed c11/fmt.h to c11/print.h. Some additions in ccommon.h, e.g. c_const_cast(T, x). Improved docs. --- misc/examples/gauss2.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'misc/examples/gauss2.c') diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index c531e5e3..79397f0c 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -22,21 +22,22 @@ int main() stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct - c_auto (csmap_int, mhist) - { - c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); - csmap_int_insert(&mhist, index, 0).ref->second += 1; - } + csmap_int mhist = {0}; + cstr bar = {0}; + + c_forrange (N) { + int index = (int) round( stc64_normalf(&rng, &dist) ); + csmap_int_insert(&mhist, index, 0).ref->second += 1; + } - // Print the gaussian bar chart - c_auto (cstr, bar) - c_forpair (index, count, csmap_int, mhist) { - int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); - if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", *_.index, cstr_str(&bar)); - } + // Print the gaussian bar chart + c_forpair (index, count, csmap_int, mhist) { + int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); + if (n > 0) { + cstr_resize(&bar, n, '*'); + printf("%4d %s\n", *_.index, cstr_str(&bar)); } } + cstr_drop(&bar); + csmap_int_drop(&mhist); } -- cgit v1.2.3 From e35036deef4fc8f17cc9221e2e666dfdb832ba78 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 27 Mar 2023 19:57:09 +0200 Subject: More RAII cleanup in examples. Also removed gauss1.c and new_deq.c --- misc/benchmarks/various/prng_bench.cpp | 2 +- misc/examples/arc_demo.c | 2 +- misc/examples/astar.c | 2 +- misc/examples/books.c | 2 +- misc/examples/coread.c | 13 +++--- misc/examples/forfilter.c | 39 ++++++++--------- misc/examples/forloops.c | 23 ++++------ misc/examples/functor.c | 6 ++- misc/examples/gauss1.c | 58 ------------------------- misc/examples/gauss2.c | 12 +++--- misc/examples/inits.c | 2 +- misc/examples/mapmap.c | 15 +------ misc/examples/multimap.c | 2 +- misc/examples/new_deq.c | 46 -------------------- misc/examples/prime.c | 5 ++- misc/examples/printspan.c | 52 +++++++++++------------ misc/examples/rawptr_elements.c | 78 +++++++++++++++++----------------- misc/examples/regex2.c | 2 +- misc/examples/replace.c | 35 +++++++-------- misc/examples/shape.c | 38 ++++++++--------- misc/examples/sidebyside.cpp | 11 +++-- misc/examples/splitstr.c | 1 + 22 files changed, 164 insertions(+), 282 deletions(-) delete mode 100644 misc/examples/gauss1.c delete mode 100644 misc/examples/new_deq.c (limited to 'misc/examples/gauss2.c') diff --git a/misc/benchmarks/various/prng_bench.cpp b/misc/benchmarks/various/prng_bench.cpp index 6f4e0e47..be07f799 100644 --- a/misc/benchmarks/various/prng_bench.cpp +++ b/misc/benchmarks/various/prng_bench.cpp @@ -122,7 +122,7 @@ using namespace std; int main(void) { - enum {N = 2000000000}; + enum {N = 500000000}; uint16_t* recipient = new uint16_t[N]; static stc64_t rng; init_state(rng.state, 12345123); diff --git a/misc/examples/arc_demo.c b/misc/examples/arc_demo.c index 88555177..2339adbb 100644 --- a/misc/examples/arc_demo.c +++ b/misc/examples/arc_demo.c @@ -25,7 +25,7 @@ int main() const int years[] = {2021, 2012, 2022, 2015}; cvec_Arc vec = {0}; - c_forrange (i, c_ARRAYLEN(years)) + c_forrange (i, c_arraylen(years)) cvec_Arc_push(&vec, Arc_from(years[i])); printf("vec:"); diff --git a/misc/examples/astar.c b/misc/examples/astar.c index 6c850b08..7dd12d50 100644 --- a/misc/examples/astar.c +++ b/misc/examples/astar.c @@ -103,7 +103,7 @@ astar(cstr* maze, int width) { -1, 0, 0, width }, /* ~ ~ ~ ~ ~ ~ ~ */ { 1, 0, 0, width }, { -1, -1, 0, width }, { 0, -1, 0, width }, { 1, -1, 0, width }, }; - for (size_t i = 0; i < c_ARRAYLEN(deltas); i++) + for (size_t i = 0; i < c_arraylen(deltas); i++) { point delta = deltas[i]; point next = point_init(current.x + delta.x, current.y + delta.y, width); diff --git a/misc/examples/books.c b/misc/examples/books.c index 40fe877f..a62769b0 100644 --- a/misc/examples/books.c +++ b/misc/examples/books.c @@ -41,7 +41,7 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; - c_forrange (i, c_ARRAYLEN(to_find)) { + c_forrange (i, c_arraylen(to_find)) { const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); if (b) printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index c5f85e08..0a7f4816 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -20,17 +20,20 @@ bool file_nextline(struct file_nextline* U) while (cstr_getline(&U->line, U->fp)) cco_yield(true); - cco_final: // required label. + cco_final: // this label is required. printf("finish\n"); cstr_drop(&U->line); fclose(U->fp); cco_end(false); } -int main(void) { - struct file_nextline z = {__FILE__}; +int main(void) +{ + struct file_nextline it = {__FILE__}; int n = 0; - while (file_nextline(&z)) { - printf("%3d %s\n", ++n, cstr_str(&z.line)); + while (file_nextline(&it)) + { + printf("%3d %s\n", ++n, cstr_str(&it.line)); + //if (n == 10) cco_stop(&it); } } diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index fdf8be6f..0d72bd1b 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -28,17 +28,15 @@ void demo1(void) printf(" %d", *i.ref); puts(""); - int res, sum = 0; + int sum = 0; c_forfilter (i, IVec, vec, - c_flt_skipwhile(i, *i.ref != 80) && - c_flt_skip(i, 1) && - c_flt_skipwhile(i, *i.ref != 80) && - flt_isEven(i) && - flt_skipValue(i, 80) && - c_flt_take(i, 5) // short-circuit + c_flt_skipwhile(i, *i.ref != 80) && + c_flt_skip(i, 1) && + flt_isEven(i) && + flt_skipValue(i, 80) && + c_flt_take(i, 5) // short-circuit ){ - sum += res = flt_square(i); - printf(" %d", res); + sum += flt_square(i); } printf("\n sum: %d\n", sum); @@ -62,11 +60,13 @@ void demo2(void) { IVec vector = {0}; - c_forfilter (x, crange, crange_obj(INT64_MAX), - c_flt_skipwhile(x, *x.ref != 11) && - *x.ref % 2 != 0 && - c_flt_take(x, 5)) + c_forfilter (x, crange, crange_object(INT64_MAX), + c_flt_skipwhile(x, *x.ref != 11) && + (*x.ref % 2) != 0 && + c_flt_take(x, 5) + ){ IVec_push(&vector, (int)(*x.ref * *x.ref)); + } c_foreach (x, IVec, vector) printf(" %d", *x.ref); @@ -130,13 +130,14 @@ void demo5(void) crange R = crange_make(1963, INT32_MAX); c_forfilter (i, crange, R, - c_flt_skip(i,15) && - c_flt_skipwhile(i, flt_mid_decade(i)) && - c_flt_skip(i,30) && - flt_even(i) && - c_flt_take(i,5)) + c_flt_skip(i,15) && + c_flt_skipwhile(i, flt_mid_decade(i)) && + c_flt_skip(i,30) && + flt_even(i) && + c_flt_take(i,5) + ){ printf(" %lld", *i.ref); - + } puts(""); } diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c index 54b49485..82126456 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/forloops.c @@ -52,24 +52,17 @@ int main() puts("\n\nc_forpair:"); c_forpair (key, val, IMap, map) printf(" (%d %d)", *_.key, *_.val); - - puts("\n\nc_forfilter 1:"); - c_forfilter (i, IVec, vec, c_flt_take(i, 3)) - printf(" %d", *i.ref); - + #define isOdd(i) (*i.ref & 1) - puts("\n\nc_forfilter 2:"); - c_forfilter (i, IVec, vec, - c_flt_skipwhile(i, *i.ref != 65) && - c_flt_takewhile(i, *i.ref != 280) && - c_flt_skipwhile(i, isOdd(i)) && - isOdd(i) && - c_flt_skip(i, 2) && - c_flt_take(i, 2)) + puts("\n\nc_forfilter:"); + c_forfilter (i, IVec, vec, + isOdd(i) && + c_flt_skip(i, 4) && + c_flt_take(i, 4) + ){ printf(" %d", *i.ref); - puts(""); - // 189 + } IVec_drop(&vec); IMap_drop(&map); diff --git a/misc/examples/functor.c b/misc/examples/functor.c index 8952c710..f37e41d5 100644 --- a/misc/examples/functor.c +++ b/misc/examples/functor.c @@ -43,14 +43,16 @@ static bool int_lambda(const int* x, const int* y) { return (*x ^ 1) < (*y ^ 1); int main() { - const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_ARRAYLEN(data); + const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); printf("data: \t"); - c_forrange (i, n) printf("%d ", data[i]); + c_forrange (i, n) + printf("%d ", data[i]); puts(""); IPQueue q1 = {ipque_init(), int_less}; // Max priority queue IPQueue minq1 = {ipque_init(), int_greater}; // Min priority queue IPQueue q5 = {ipque_init(), int_lambda}; // Using lambda to compare elements. + c_forrange (i, n) ipque_push(&q1.Q, data[i]); print_queue("q1", q1); diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c deleted file mode 100644 index 67871aa9..00000000 --- a/misc/examples/gauss1.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include - -#include -#include - -// Declare int -> int hashmap. Uses typetag 'ii' for ints. -#define i_key int -#define i_val int -#define i_tag ii -#include - -// Declare int vector with entries from the cmap. -#define i_val cmap_ii_value -#define i_less(x, y) x->first < y->first -#define i_tag ii -#include - -int main() -{ - enum {N = 10000000}; - const double Mean = -12.0, StdDev = 6.0, Scale = 74; - - printf("Demo of gaussian / normal distribution of %d random samples\n", N); - - // Setup random engine with normal distribution. - uint64_t seed = (uint64_t)time(NULL); - stc64_t rng = stc64_new(seed); - stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); - - // Create and init histogram vec and map with defered destructors: - cvec_ii histvec = {0}; - cmap_ii histmap = {0}; - - c_forrange (N) { - int index = (int)round( stc64_normalf(&rng, &dist) ); - cmap_ii_insert(&histmap, index, 0).ref->second += 1; - } - - // Transfer map to vec and sort it by map keys. - c_foreach (i, cmap_ii, histmap) - cvec_ii_push(&histvec, (cmap_ii_value){i.ref->first, i.ref->second}); - - cvec_ii_sort(&histvec); - - // Print the gaussian bar chart - c_foreach (i, cvec_ii, histvec) { - int n = (int)(i.ref->second * StdDev * Scale * 2.5 / (double)N); - if (n > 0) { - printf("%4d ", i.ref->first); - c_forrange (n) printf("*"); - puts(""); - } - } - - cvec_ii_drop(&histvec); - cmap_ii_drop(&histmap); -} diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index 79397f0c..be514c12 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -6,7 +6,7 @@ // Declare int -> int sorted map. #define i_key int -#define i_val size_t +#define i_val int #include int main() @@ -22,16 +22,16 @@ int main() stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct - csmap_int mhist = {0}; + csmap_int hist = {0}; cstr bar = {0}; c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); - csmap_int_insert(&mhist, index, 0).ref->second += 1; + int index = (int)round( stc64_normalf(&rng, &dist) ); + csmap_int_insert(&hist, index, 0).ref->second += 1; } // Print the gaussian bar chart - c_forpair (index, count, csmap_int, mhist) { + c_forpair (index, count, csmap_int, hist) { int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); if (n > 0) { cstr_resize(&bar, n, '*'); @@ -39,5 +39,5 @@ int main() } } cstr_drop(&bar); - csmap_int_drop(&mhist); + csmap_int_drop(&hist); } diff --git a/misc/examples/inits.c b/misc/examples/inits.c index 7530dd08..81bcdd3e 100644 --- a/misc/examples/inits.c +++ b/misc/examples/inits.c @@ -40,7 +40,7 @@ int main(void) const float nums[] = {4.0f, 2.0f, 5.0f, 3.0f, 1.0f}; // PRIORITY QUEUE - c_forrange (i, c_ARRAYLEN(nums)) + c_forrange (i, c_arraylen(nums)) cpque_f_push(&floats, nums[i]); puts("\npop and show high priorites first:"); diff --git a/misc/examples/mapmap.c b/misc/examples/mapmap.c index 8fc307ab..668da5de 100644 --- a/misc/examples/mapmap.c +++ b/misc/examples/mapmap.c @@ -7,24 +7,13 @@ #define i_key_str // name #define i_val_str // email #define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override -#include +#include // Departments: std::map #define i_type Departments #define i_key_str // dep. name #define i_valclass People -// i_key_str implies: -// #define i_tag str -// #define i_key cstr -// #define i_keyclone cstr_clone -// #define i_keydrop cstr_drop -// #define i_cmp cstr_cmp -// #define i_hash cstr_hash -// i_valclass implies: -// #define i_val People -// #define i_valclone People_clone -// #define i_valdrop People_drop -#include +#include void add(Departments* deps, const char* name, const char* email, const char* dep) diff --git a/misc/examples/multimap.c b/misc/examples/multimap.c index 9c37db6c..d8981a81 100644 --- a/misc/examples/multimap.c +++ b/misc/examples/multimap.c @@ -71,7 +71,7 @@ int main() csmap_OL multimap = {0}; const clist_OL empty = clist_OL_init(); - for (size_t i = 0; i < c_ARRAYLEN(ol_data); ++i) + for (size_t i = 0; i < c_arraylen(ol_data); ++i) { struct OlympicsData* d = &ol_data[i]; OlympicLoc loc = {.year = d->year, diff --git a/misc/examples/new_deq.c b/misc/examples/new_deq.c deleted file mode 100644 index 467fd4e5..00000000 --- a/misc/examples/new_deq.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -forward_cdeq(cdeq_i32, int); -forward_cdeq(cdeq_pnt, struct Point); - -struct MyStruct { - cdeq_i32 intvec; - cdeq_pnt pntvec; -} typedef MyStruct; - - -#define i_val int -#define i_opt c_is_forward -#define i_tag i32 -#include - -struct Point { int x, y; } typedef Point; -int point_cmp(const Point* a, const Point* b) { - int c = a->x - b->x; - return c ? c : a->y - b->y; -} - -#define i_val Point -#define i_cmp point_cmp -#define i_opt c_is_forward -#define i_tag pnt -#include - - -int main() -{ - cdeq_pnt pvec = {0}; - - cdeq_pnt_push_back(&pvec, (Point){42, 14}); - cdeq_pnt_push_back(&pvec, (Point){32, 94}); - cdeq_pnt_push_front(&pvec, (Point){62, 81}); - - cdeq_pnt_sort(&pvec); - - c_foreach (i, cdeq_pnt, pvec) - printf(" (%d %d)", i.ref->x, i.ref->y); - puts(""); - - cdeq_pnt_drop(&pvec); -} diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 18bf3490..9ffb2f53 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -44,8 +44,9 @@ int main(void) puts("Show the last 50 primes using a temporary crange generator:"); crange R = crange_make(n - 1, 0, -2); c_forfilter (i, crange, R, - cbits_test(&primes, *i.ref>>1) && - c_flt_take(i, 50)) { + cbits_test(&primes, *i.ref>>1) && + c_flt_take(i, 50) + ){ printf("%lld ", *i.ref); if (c_flt_last(i) % 10 == 0) puts(""); } diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index 127b07b9..7459ac77 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -16,7 +16,8 @@ using_cspan(intspan, int, 1); void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); - c_foreach (e, intspan, container) printf(" %d", *e.ref); + c_foreach (e, intspan, container) + printf(" %d", *e.ref); puts(""); } @@ -31,31 +32,26 @@ int main() intspan sp2 = cspan_from_array(arr); printMe( (intspan)cspan_subspan(&sp2, 1, 4) ); - cvec_int vec; - cstack_int stk; - cdeq_int deq; - csset_str set; - c_defer( - cvec_int_drop(&vec), - cstack_int_drop(&stk), - cdeq_int_drop(&deq), - csset_str_drop(&set) - ) { - vec = c_make(cvec_int, {1, 2, 3, 4, 5}); - printMe( (intspan)cspan_from(&vec) ); - - printMe( sp2 ); - - stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7}); - printMe( (intspan)cspan_from(&stk) ); - - deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); - printMe( (intspan)cspan_from(&deq) ); - - set = c_make(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); - printf("%d:", (int)csset_str_size(&set)); - c_foreach (e, csset_str, set) - printf(" %s", cstr_str(e.ref)); - puts(""); - } + cvec_int vec = c_make(cvec_int, {1, 2, 3, 4, 5}); + printMe( (intspan)cspan_from(&vec) ); + + printMe( sp2 ); + + cstack_int stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7}); + printMe( (intspan)cspan_from(&stk) ); + + cdeq_int deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); + printMe( (intspan)cspan_from(&deq) ); + + csset_str set = c_make(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); + printf("%d:", (int)csset_str_size(&set)); + c_foreach (e, csset_str, set) + printf(" %s", cstr_str(e.ref)); + puts(""); + + // cleanup + cvec_int_drop(&vec); + cstack_int_drop(&stk); + cdeq_int_drop(&deq); + csset_str_drop(&set); } diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c index f46a7f5e..01bcdc44 100644 --- a/misc/examples/rawptr_elements.c +++ b/misc/examples/rawptr_elements.c @@ -2,58 +2,58 @@ #include #include -// Map of cstr => int64 pointers -typedef int64_t inttype; -// Do it without cbox: +// Create cmap of cstr => long* #define i_type SIPtrMap #define i_key_str -#define i_val inttype* -#define i_valraw inttype -#define i_valfrom(raw) c_new(inttype, raw) +#define i_val long* +#define i_valraw long +#define i_valfrom(raw) c_new(long, raw) #define i_valto(x) **x -#define i_valclone(x) c_new(inttype, *x) +#define i_valclone(x) c_new(long, *x) #define i_valdrop(x) c_free(*x) #include -// With cbox: +// Alternatively, using cbox: #define i_type IBox -#define i_val int -#include // +#define i_val long +#include // unique_ptr alike. +// cmap of cstr => IBox #define i_type SIBoxMap #define i_key_str -#define i_valboxed IBox +#define i_valboxed IBox // i_valboxed: use properties from IBox automatically #include int main() { - SIPtrMap map = {0}, m1; - SIBoxMap m2 = {0}; - c_defer( - SIPtrMap_drop(&map), - SIPtrMap_drop(&m1), - SIBoxMap_drop(&m2) - ){ - printf("\nMap with pointer elements:\n"); - SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1)); - SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2)); - - // Emplace: implicit key, val construction: - SIPtrMap_emplace(&map, "hello", 3); - SIPtrMap_emplace(&map, "goodbye", 4); - - m1 = SIPtrMap_clone(map); - - c_forpair (name, number, SIPtrMap, m1) - printf("%s: %" PRId64 "\n", cstr_str(_.name), **_.number); - - - puts("\nIBox map:"); - SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_make(123)); - SIBoxMap_emplace(&m2, "World", 999); - c_forpair (name, number, SIBoxMap, m2) - printf("%s: %d\n", cstr_str(_.name), *_.number->get); - puts(""); - } + // These have the same behaviour, except IBox has a get member: + SIPtrMap map1 = {0}; + SIBoxMap map2 = {0}; + + printf("\nMap cstr => long*:\n"); + SIPtrMap_insert(&map1, cstr_from("Test1"), c_new(long, 1)); + SIPtrMap_insert(&map1, cstr_from("Test2"), c_new(long, 2)); + + // Emplace implicitly creates cstr from const char* and an owned long* from long! + SIPtrMap_emplace(&map1, "Test3", 3); + SIPtrMap_emplace(&map1, "Test4", 4); + + c_forpair (name, number, SIPtrMap, map1) + printf("%s: %ld\n", cstr_str(_.name), **_.number); + + puts("\nMap cstr => IBox:"); + SIBoxMap_insert(&map2, cstr_from("Test1"), IBox_make(1)); + SIBoxMap_insert(&map2, cstr_from("Test2"), IBox_make(2)); + + // Emplace implicitly creates cstr from const char* and IBox from long! + SIBoxMap_emplace(&map2, "Test3", 3); + SIBoxMap_emplace(&map2, "Test4", 4); + + c_forpair (name, number, SIBoxMap, map2) + printf("%s: %ld\n", cstr_str(_.name), *_.number->get); + puts(""); + + SIPtrMap_drop(&map1); + SIBoxMap_drop(&map2); } diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c index 883dd112..3133f7c2 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regex2.c @@ -16,7 +16,7 @@ int main() }; cregex re = {0}; - c_forrange (i, c_ARRAYLEN(s)) + c_forrange (i, c_arraylen(s)) { int res = cregex_compile(&re, s[i].pattern); if (res < 0) { diff --git a/misc/examples/replace.c b/misc/examples/replace.c index c22c71ff..cf5b45cb 100644 --- a/misc/examples/replace.c +++ b/misc/examples/replace.c @@ -8,27 +8,28 @@ int main () // replace signatures used in the same order as described above: - // Ustring positions: 0123456789*123456789*12345 - cstr s = cstr_from(base); // "this is a test string." + // Ustring positions: 0123456789*123456789*12345 + cstr s = cstr_from(base); // "this is a test string." cstr m = cstr_clone(s); - c_defer (cstr_drop(&s), cstr_drop(&m)) { - cstr_append(&m, cstr_str(&m)); - cstr_append(&m, cstr_str(&m)); - printf("%s\n", cstr_str(&m)); - cstr_replace_at(&s, 9, 5, s2); // "this is an example string." (1) - printf("(1) %s\n", cstr_str(&s)); + cstr_append(&m, cstr_str(&m)); + cstr_append(&m, cstr_str(&m)); + printf("%s\n", cstr_str(&m)); - cstr_replace_at_sv(&s, 19, 6, c_sv(s3+7, 6)); // "this is an example phrase." (2) - printf("(2) %s\n", cstr_str(&s)); + cstr_replace_at(&s, 9, 5, s2); // "this is an example string." (1) + printf("(1) %s\n", cstr_str(&s)); - cstr_replace_at(&s, 8, 10, "just a"); // "this is just a phrase." (3) - printf("(3) %s\n", cstr_str(&s)); + cstr_replace_at_sv(&s, 19, 6, c_sv(s3+7, 6)); // "this is an example phrase." (2) + printf("(2) %s\n", cstr_str(&s)); - cstr_replace_at_sv(&s, 8, 6, c_sv("a shorty", 7)); // "this is a short phrase." (4) - printf("(4) %s\n", cstr_str(&s)); + cstr_replace_at(&s, 8, 10, "just a"); // "this is just a phrase." (3) + printf("(3) %s\n", cstr_str(&s)); - cstr_replace_at(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) - printf("(5) %s\n", cstr_str(&s)); - } + cstr_replace_at_sv(&s, 8, 6, c_sv("a shorty", 7)); // "this is a short phrase." (4) + printf("(4) %s\n", cstr_str(&s)); + + cstr_replace_at(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) + printf("(5) %s\n", cstr_str(&s)); + + c_drop(cstr, &s, &m); } diff --git a/misc/examples/shape.c b/misc/examples/shape.c index 2aabdcc2..e24f7fd7 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -139,23 +139,23 @@ void testShape(const Shape* shape) int main(void) { Shapes shapes = {0}; - c_defer (Shapes_drop(&shapes)) - { - Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); - Polygon* pol1 = c_new(Polygon, Polygon_init()); - Polygon* pol2 = c_new(Polygon, Polygon_init()); - - c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) - Polygon_addPoint(pol1, *i.ref); - - c_forlist (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) - Polygon_addPoint(pol2, *i.ref); - - Shapes_push(&shapes, &tri1->shape); - Shapes_push(&shapes, &pol1->shape); - Shapes_push(&shapes, &pol2->shape); - - c_foreach (i, Shapes, shapes) - testShape(*i.ref); - } + + Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); + Polygon* pol1 = c_new(Polygon, Polygon_init()); + Polygon* pol2 = c_new(Polygon, Polygon_init()); + + c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) + Polygon_addPoint(pol1, *i.ref); + + c_forlist (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) + Polygon_addPoint(pol2, *i.ref); + + Shapes_push(&shapes, &tri1->shape); + Shapes_push(&shapes, &pol1->shape); + Shapes_push(&shapes, &pol2->shape); + + c_foreach (i, Shapes, shapes) + testShape(*i.ref); + + Shapes_drop(&shapes); } diff --git a/misc/examples/sidebyside.cpp b/misc/examples/sidebyside.cpp index a817b5a5..a7c1008c 100644 --- a/misc/examples/sidebyside.cpp +++ b/misc/examples/sidebyside.cpp @@ -24,9 +24,8 @@ int main() { std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - - IIMap hist = {0}; - c_defer (IIMap_drop(&hist)) { + { + IIMap hist = {0}; IIMap_insert(&hist, 12, 100).ref->second += 1; IIMap_insert(&hist, 13, 100).ref->second += 1; IIMap_insert(&hist, 12, 100).ref->second += 1; @@ -34,6 +33,7 @@ int main() { c_foreach (i, IIMap, hist) printf("%d, %d\n", i.ref->first, i.ref->second); puts(""); + IIMap_drop(&hist); } // =================================================== { @@ -44,15 +44,14 @@ int main() { std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - - SIMap food = {0}; - c_defer (SIMap_drop(&food)) { + SIMap food = {0}; c_forlist (i, SIMap_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) SIMap_emplace(&food, i.ref->first, i.ref->second); c_foreach (i, SIMap, food) printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second); puts(""); + SIMap_drop(&food); } } diff --git a/misc/examples/splitstr.c b/misc/examples/splitstr.c index bf90b5d4..2bc6fc07 100644 --- a/misc/examples/splitstr.c +++ b/misc/examples/splitstr.c @@ -15,5 +15,6 @@ int main() cregex re = cregex_from("[^ ]+"); c_formatch (i, &re, " Hello World C99! ") printf("'%.*s'\n", c_SV(i.match[0])); + cregex_drop(&re); } -- cgit v1.2.3 From 59d74d181e44dd05a8570b42fc6284745e225664 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 28 Mar 2023 19:29:05 +0200 Subject: Example changes. Added crand.h possible replacement for crandom.h --- docs/ccommon_api.md | 2 +- include/stc/crand.h | 194 +++++++++++++++++++++++++++++++++++++++++++++++++ misc/examples/gauss2.c | 11 +-- misc/examples/prime.c | 5 +- misc/examples/shape.c | 19 ++--- 5 files changed, 211 insertions(+), 20 deletions(-) create mode 100644 include/stc/crand.h (limited to 'misc/examples/gauss2.c') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 53ba096a..46966fd9 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -92,7 +92,7 @@ Iterate containers with stop-criteria and chained range filtering. | `c_flt_skipwhile(it, predicate)` | Skip items until predicate is false | | `c_flt_takewhile(it, predicate)` | Take items until predicate is false | | `c_flt_count(it)` | Increment current and return value | -| `c_flt_last(it)` | Get value of last count/skip/take | +| `c_flt_last(it)` | Get value of last count/skip*/take* | ```c // Example: #include diff --git a/include/stc/crand.h b/include/stc/crand.h new file mode 100644 index 00000000..4ebc402d --- /dev/null +++ b/include/stc/crand.h @@ -0,0 +1,194 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "ccommon.h" + +#ifndef CRAND_H_INCLUDED +#define CRAND_H_INCLUDED +/* +// crand: Pseudo-random number generator +#include "stc/crand.h" + +int main() { + uint64_t seed = 123456789; + crand_t rng = crand_from(seed); + crand_unif_t dist1 = crand_unif_from(1, 6); + crandf_unif_t dist2 = crandf_unif_from(1.0, 10.0); + crandf_norm_t dist3 = crandf_norm_from(1.0, 10.0); + + uint64_t i = crand_r(&rng); + int64_t iu = crand_unif(&rng, &dist1); + double xu = crandf_unif(&rng, &dist2); + double xn = crandf_norm(&rng, &dist3); +} +*/ +#include +#include + +typedef struct crand { uint64_t state[5]; } crand_t; +typedef struct crand_unif { int64_t lower; uint64_t range, threshold; } crand_unif_t; +typedef struct crandf_unif { double lower, range; } crandf_unif_t; +typedef struct crandf_norm { double mean, stddev, next; int has_next; } crandf_norm_t; + +/* PRNG crand_t. + * Very fast PRNG suited for parallel usage with Weyl-sequence parameter. + * 320-bit state, 256 bit is mutable. + * Noticable faster than xoshiro and pcg, slighly slower than wyrand64 and + * Romu, but these have restricted capacity for larger parallel jobs or unknown minimum periods. + * crand_t supports 2^63 unique threads with a minimum 2^64 period lengths each. + * Passes all statistical tests, e.g PractRand and correlation tests, i.e. interleaved + * streams with one-bit diff state. Even the 16-bit version (LR=6, RS=5, LS=3) passes + * PractRand to multiple TB input. + */ + +/* Global crand_t PRNGs */ +STC_API void csrand(uint64_t seed); +STC_API uint64_t crand(void); +STC_API double crandf(void); + +/* Init crand_t prng with a seed */ +STC_API crand_t crand_from(uint64_t seed); + +/* Unbiased bounded uniform distribution. range [low, high] */ +STC_API crand_unif_t crand_unif_from(int64_t low, int64_t high); +STC_API int64_t crand_unif(crand_t* rng, crand_unif_t* dist); + +/* Normal distribution PRNG */ +STC_API double crandf_norm(crand_t* rng, crandf_norm_t* dist); + + +/* Main crand_t prng */ +STC_INLINE uint64_t crand_r(crand_t* rng) { + enum {LR=24, RS=11, LS=3}; uint64_t *s = rng->state; + const uint64_t out = (s[0] ^ (s[3] += s[4])) + s[1]; + s[0] = s[1] ^ (s[1] >> RS); + s[1] = s[2] + (s[2] << LS); + s[2] = ((s[2] << LR) | (s[2] >> (64 - LR))) + out; + return out; +} + +/* Float64 random number in range [0.0, 1.0). */ +STC_INLINE double crandf_r(crand_t* rng) { + union {uint64_t i; double f;} u = {0x3FF0000000000000U | (crand_r(rng) >> 12)}; + return u.f - 1.0; +} + +/* Float64 uniform distributed RNG, range [low, high). */ +STC_INLINE double crandf_unif(crand_t* rng, crandf_unif_t* dist) { + return crandf_r(rng)*dist->range + dist->lower; +} + +/* Init uniform distributed float64 RNG, range [low, high). */ +STC_INLINE crandf_unif_t crandf_unif_from(double low, double high) { + return c_LITERAL(crandf_unif_t){low, high - low}; +} + +/* Marsaglia polar method for gaussian/normal distribution, float64. */ +STC_INLINE crandf_norm_t crandf_norm_from(double mean, double stddev) { + return c_LITERAL(crandf_norm_t){mean, stddev, 0.0, 0}; +} + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) + +/* Global random() */ +static crand_t crand_global = {{ + 0x26aa069ea2fb1a4d, 0x70c72c95cd592d04, + 0x504f333d3aa0b359, 0x9e3779b97f4a7c15, + 0x6a09e667a754166b +}}; + +STC_DEF void csrand(uint64_t seed) { + crand_global = crand_from(seed); +} + +STC_DEF uint64_t crand(void) { + return crand_r(&crand_global); +} + +STC_DEF double crandf(void) { + return crandf_r(&crand_global); +} + +/* rng.state[4] must be odd */ +STC_DEF crand_t crand_from(uint64_t seed) { + crand_t rng = {{seed + 0x26aa069ea2fb1a4d, + seed*0x9e3779b97f4a7c15 + 0x70c72c95cd592d04, + seed + 0x504f333d3aa0b359, + seed*0x6a09e667a754166b, seed<<1 | 1}}; + return rng; +} + +/* Init unbiased uniform uint RNG with bounds [low, high] */ +STC_DEF crand_unif_t crand_unif_from(int64_t low, int64_t high) { + crand_unif_t dist = {low, (uint64_t) (high - low + 1)}; + dist.threshold = (uint64_t)(0 - dist.range) % dist.range; + return dist; +} + +#if defined(__SIZEOF_INT128__) + #define c_umul128(a, b, lo, hi) \ + do { __uint128_t _z = (__uint128_t)(a)*(b); \ + *(lo) = (uint64_t)_z, *(hi) = (uint64_t)(_z >> 64U); } while(0) +#elif defined(_MSC_VER) && defined(_WIN64) + #include + #define c_umul128(a, b, lo, hi) ((void)(*(lo) = _umul128(a, b, hi))) +#elif defined(__x86_64__) + #define c_umul128(a, b, lo, hi) \ + asm("mulq %3" : "=a"(*(lo)), "=d"(*(hi)) : "a"(a), "rm"(b)) +#endif + +/* Int uniform distributed RNG, range [low, high]. */ +STC_DEF int64_t crand_unif(crand_t* rng, crand_unif_t* d) { +#ifdef c_umul128 + uint64_t lo, hi; + do { c_umul128(crand_r(rng), d->range, &lo, &hi); } while (lo < d->threshold); + return d->lower + (int64_t)hi; +#else + uint64_t x, r; + do { x = crand_r(rng); r = x % d->range; } while (x - r > -d->range); + return d->lower + r; +#endif +} + +/* Normal distribution PRNG */ +STC_DEF double crandf_norm(crand_t* rng, crandf_norm_t* dist) { + double u1, u2, s, m; + if (dist->has_next++ & 1) + return dist->next * dist->stddev + dist->mean; + do { + u1 = 2.0 * crandf_r(rng) - 1.0; + u2 = 2.0 * crandf_r(rng) - 1.0; + s = u1*u1 + u2*u2; + } while (s >= 1.0 || s == 0.0); + m = sqrt(-2.0 * log(s) / s); + dist->next = u2 * m; + return (u1 * m) * dist->stddev + dist->mean; +} + +#endif +#endif +#undef i_opt +#undef i_static +#undef i_header +#undef i_implement +#undef i_extern diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index be514c12..ce29f786 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -11,14 +11,15 @@ int main() { - enum {N = 10000000}; - const double Mean = -12.0, StdDev = 6.0, Scale = 74; + enum {N = 5000000}; + uint64_t seed = (uint64_t)time(NULL); + stc64_t rng = stc64_new(seed); + const float Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f; printf("Demo of gaussian / normal distribution of %d random samples\n", N); + printf("Mean %f, StdDev %f\n", Mean, StdDev); // Setup random engine with normal distribution. - uint64_t seed = (uint64_t)time(NULL); - stc64_t rng = stc64_new(seed); stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct @@ -32,7 +33,7 @@ int main() // Print the gaussian bar chart c_forpair (index, count, csmap_int, hist) { - int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); + int n = (int)round((float)*_.count * StdDev * Scale * 2.5f / (float)N); if (n > 0) { cstr_resize(&bar, n, '*'); printf("%4d %s\n", *_.index, cstr_str(&bar)); diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 9ffb2f53..34d64f10 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -42,9 +42,8 @@ int main(void) puts("\n"); puts("Show the last 50 primes using a temporary crange generator:"); - crange R = crange_make(n - 1, 0, -2); - c_forfilter (i, crange, R, - cbits_test(&primes, *i.ref>>1) && + c_forfilter (i, crange, crange_object(n - 1, 0, -2), + cbits_test(&primes, *i.ref/2) && c_flt_take(i, 50) ){ printf("%lld ", *i.ref); diff --git a/misc/examples/shape.c b/misc/examples/shape.c index e24f7fd7..d7116039 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -4,7 +4,7 @@ #include #include -#define c_dyn_cast(T, s) \ +#define DYN_CAST(T, s) \ (&T##_api == (s)->api ? (T*)(s) : (T*)0) // Shape definition @@ -53,15 +53,14 @@ typedef struct { extern struct ShapeAPI Triangle_api; -Triangle Triangle_from(Point a, Point b, Point c) -{ - Triangle t = {.shape={.api=&Triangle_api}, .p={a, b, c}}; +Triangle Triangle_from(Point a, Point b, Point c) { + Triangle t = {{&Triangle_api}, {a, b, c}}; return t; } static void Triangle_draw(const Shape* shape) { - const Triangle* self = c_dyn_cast(Triangle, shape); + const Triangle* self = DYN_CAST(Triangle, shape); printf("Triangle : (%g,%g), (%g,%g), (%g,%g)\n", self->p[0].x, self->p[0].y, self->p[1].x, self->p[1].y, @@ -88,9 +87,8 @@ typedef struct { extern struct ShapeAPI Polygon_api; -Polygon Polygon_init(void) -{ - Polygon p = {.shape={.api=&Polygon_api}, .points=PointVec_init()}; +Polygon Polygon_init(void) { + Polygon p = {{&Polygon_api}, {0}}; return p; } @@ -101,15 +99,14 @@ void Polygon_addPoint(Polygon* self, Point p) static void Polygon_drop(Shape* shape) { - Polygon* self = c_dyn_cast(Polygon, shape); + Polygon* self = DYN_CAST(Polygon, shape); printf("poly destructed\n"); PointVec_drop(&self->points); - Shape_drop(shape); } static void Polygon_draw(const Shape* shape) { - const Polygon* self = c_dyn_cast(Polygon, shape); + const Polygon* self = DYN_CAST(Polygon, shape); printf("Polygon :"); c_foreach (i, PointVec, self->points) printf(" (%g,%g)", i.ref->x, i.ref->y); -- cgit v1.2.3 From 4f0ca428e332761666916477b22c3301044a85c6 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 28 Mar 2023 19:32:28 +0200 Subject: Fix gauss2.c --- misc/examples/gauss2.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'misc/examples/gauss2.c') diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index ce29f786..7fede5aa 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -14,7 +14,7 @@ int main() enum {N = 5000000}; uint64_t seed = (uint64_t)time(NULL); stc64_t rng = stc64_new(seed); - const float Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f; + const double Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f; printf("Demo of gaussian / normal distribution of %d random samples\n", N); printf("Mean %f, StdDev %f\n", Mean, StdDev); @@ -33,7 +33,7 @@ int main() // Print the gaussian bar chart c_forpair (index, count, csmap_int, hist) { - int n = (int)round((float)*_.count * StdDev * Scale * 2.5f / (float)N); + int n = (int)round((double)*_.count * StdDev * Scale * 2.5 / (double)N); if (n > 0) { cstr_resize(&bar, n, '*'); printf("%4d %s\n", *_.index, cstr_str(&bar)); -- cgit v1.2.3 From a0a290645828c88597efce80f6b0f5a958cefa89 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 30 Mar 2023 17:59:08 +0200 Subject: Added crand.h - Alternative API to crandom.h, which will be deprecated. --- README.md | 6 +-- docs/cpque_api.md | 8 +-- docs/crandom_api.md | 45 ++++++++--------- include/stc/clist.h | 4 +- include/stc/cqueue.h | 10 ++-- misc/benchmarks/picobench/picobench_cmap.cpp | 68 ++++++++++++------------- misc/benchmarks/picobench/picobench_csmap.cpp | 72 +++++++++++++-------------- misc/benchmarks/plotbench/cdeq_benchmark.cpp | 30 +++++------ misc/benchmarks/plotbench/clist_benchmark.cpp | 26 +++++----- misc/benchmarks/plotbench/cmap_benchmark.cpp | 34 ++++++------- misc/benchmarks/plotbench/cpque_benchmark.cpp | 32 ++++++------ misc/benchmarks/plotbench/csmap_benchmark.cpp | 34 ++++++------- misc/benchmarks/plotbench/cvec_benchmark.cpp | 22 ++++---- misc/benchmarks/shootout_hashmaps.cpp | 8 +-- misc/benchmarks/various/cbits_benchmark.cpp | 14 +++--- misc/benchmarks/various/prng_bench.cpp | 6 +-- misc/benchmarks/various/sso_bench.cpp | 14 +++--- misc/examples/birthday.c | 10 ++-- misc/examples/gauss2.c | 10 ++-- misc/examples/list.c | 7 ++- misc/examples/new_queue.c | 10 ++-- misc/examples/priority.c | 10 ++-- misc/examples/queue.c | 12 ++--- misc/examples/random.c | 16 +++--- 24 files changed, 249 insertions(+), 259 deletions(-) (limited to 'misc/examples/gauss2.c') diff --git a/README.md b/README.md index 75a5357e..b5f9ceb7 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Others ------ - [***ccommon*** - Generic safe macros and algorithms](docs/ccommon_api.md) - [***cregex*** - Regular expressions (extended from Rob Pike's regexp9)](docs/cregex_api.md) -- [***crandom*** - A novel very fast *PRNG* named **stc64**](docs/crandom_api.md) +- [***crand*** - A novel very fast *PRNG* named **stc64**](docs/crandom_api.md) - [***coption*** - getopt() alike command line args parser](docs/coption_api.md) Highlights @@ -343,7 +343,7 @@ once and if needed. Currently, define `i_extern` before including **clist** for It is possible to generate single headers by executing the python script `src/singleheader.py header-file > single`. Conveniently, `src\libstc.c` implements non-templated functions as shared symbols for **cstr**, **csview**, -**cbits** and **crandom**. When building in shared mode (-DSTC_HEADER), you may include this file in your project, +**cbits** and **crand**. When building in shared mode (-DSTC_HEADER), you may include this file in your project, or define your own as descibed above. ```c // stc_libs.c @@ -592,7 +592,7 @@ STC is generally very memory efficient. Type sizes: - Allows for `i_key*` template parameters instead of `i_val*` for all containers, not only for **cset** and **csset**. - Optimized *c_default_hash()*. Therefore *c_hash32()* and *c_hash64()* are removed (same speed). - Added *.._push()* and *.._emplace()* function to all containers to allow for more generic coding. -- Renamed global PRNGs *stc64_random()* and *stc64_srandom()* to *crandom()* and *csrandom()*. +- Renamed global PRNGs *stc64_random()* and *stc64_srandom()* to *crand()* and *csrand()*. - Added some examples and benchmarks for SSO and heterogenous lookup comparison with c++20 (string_bench_*.cpp). ## Brief summary of changes from version 2.x to 3.0 diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 1396dc1f..962ee162 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -60,7 +60,7 @@ i_val cpque_X_value_clone(i_val value); ## Example ```c -#include +#include #include #define i_val int64_t @@ -71,15 +71,15 @@ i_val cpque_X_value_clone(i_val value); int main() { intptr_t N = 10000000; - stc64_t rng = stc64_new(1234); - stc64_uniform_t dist = stc64_uniform_new(0, N * 10); + crand_t rng = crand_init(1234); + crand_unif_t dist = crand_unif_init(0, N * 10); // Define heap cpque_i heap = {0}; // Push ten million random numbers to priority queue. c_forrange (N) - cpque_i_push(&heap, stc64_uniform(&rng, &dist)); + cpque_i_push(&heap, crand_unif(&rng, &dist)); // Add some negative ones. int nums[] = {-231, -32, -873, -4, -343}; diff --git a/docs/crandom_api.md b/docs/crandom_api.md index e69cc539..7281b2d7 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -1,4 +1,4 @@ -# STC [crandom](../include/stc/crandom.h): Pseudo Random Number Generator +# STC [crand](../include/stc/crand.h): Pseudo Random Number Generator ![Random](pics/random.jpg) This features a *64-bit PRNG* named **stc64**, and can generate bounded uniform and normal @@ -33,45 +33,40 @@ xoshiro and pcg (Vigna/O'Neill) PRNGs: https://www.pcg-random.org/posts/on-vigna ## Header file -All crandom definitions and prototypes are available by including a single header file. +All crand definitions and prototypes are available by including a single header file. ```c -#include +#include ``` ## Methods ```c -void csrandom(uint64_t seed); // seed global stc64 prng -uint64_t crandom(void); // global stc64_rand(rng) -double crandomf(void); // global stc64_randf(rng) +void csrand(uint64_t seed); // seed global stc64 prng +uint64_t crand(void); // global crand_u64(rng) +double crandf(void); // global crand_f64(rng) -stc64_t stc64_new(uint64_t seed); // stc64_init(s) is deprecated -stc64_t stc64_with_seq(uint64_t seed, uint64_t seq); // with unique stream +crand_t crand_init(uint64_t seed); // stc64_init(s) is deprecated +uint64_t crand_u64(crand_t* rng); // range [0, 2^64 - 1] +double crand_f64(crand_t* rng); // range [0.0, 1.0) -uint64_t stc64_rand(stc64_t* rng); // range [0, 2^64 - 1] -double stc64_randf(stc64_t* rng); // range [0.0, 1.0) +crand_unif_t crand_unif_init(int64_t low, int64_t high); // uniform-distribution +int64_t crand_unif(crand_t* rng, crand_unif_t* dist); // range [low, high] -stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high); // uniform-distribution -int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist); // range [low, high] -stc64_uniformf_t stc64_uniformf_new(double lowf, double highf); -double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist); // range [lowf, highf) - -stc64_normalf_t stc64_normalf_new(double mean, double stddev); // normal-distribution -double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist); +crand_norm_t crand_norm_init(double mean, double stddev); // normal-distribution +double crand_norm(crand_t* rng, crand_norm_t* dist); ``` ## Types | Name | Type definition | Used to represent... | |:-------------------|:------------------------------------------|:-----------------------------| -| `stc64_t` | `struct {uint64_t state[4];}` | The PRNG engine type | -| `stc64_uniform_t` | `struct {int64_t lower; uint64_t range;}` | Integer uniform distribution | -| `stc64_uniformf_t` | `struct {double lower, range;}` | Real number uniform distr. | -| `stc64_normalf_t` | `struct {double mean, stddev;}` | Normal distribution type | +| `crand_t` | `struct {uint64_t state[4];}` | The PRNG engine type | +| `crand_unif_t` | `struct {int64_t lower; uint64_t range;}` | Integer uniform distribution | +| `crand_norm_t` | `struct {double mean, stddev;}` | Normal distribution type | ## Example ```c #include -#include +#include #include // Declare int -> int sorted map. Uses typetag 'i' for ints. @@ -89,13 +84,13 @@ int main() // Setup random engine with normal distribution. uint64_t seed = time(NULL); - stc64_t rng = stc64_new(seed); - stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); + crand_t rng = crand_init(seed); + crand_norm_t dist = crand_norm_init(Mean, StdDev); // Create histogram map csmap_i mhist = csmap_i_init(); c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); + int index = (int)round(crand_norm(&rng, &dist)); csmap_i_emplace(&mhist, index, 0).ref->second += 1; } diff --git a/include/stc/clist.h b/include/stc/clist.h index eb72b888..fa26fd65 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -26,7 +26,7 @@ it also support both push_back() and push_front(), unlike std::forward_list: #include - #include + #include #define i_key int64_t #define i_tag ix @@ -38,7 +38,7 @@ { int n; for (int i = 0; i < 1000000; ++i) // one million - clist_ix_push_back(&list, crandom() >> 32); + clist_ix_push_back(&list, crand() >> 32); n = 0; c_foreach (i, clist_ix, list) if (++n % 10000 == 0) printf("%8d: %10zu\n", n, *i.ref); diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 67909f8e..1934305a 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -22,7 +22,7 @@ */ // STC queue /* -#include +#include #include #define i_key int @@ -30,19 +30,19 @@ int main() { int n = 10000000; - stc64_t rng = stc64_new(1234); - stc64_uniform_t dist = stc64_uniform_new(0, n); + crand_t rng = crand_init(1234); + crand_unif_t dist = crand_unif_init(0, n); c_auto (cqueue_int, Q) { // Push ten million random numbers onto the queue. for (int i=0; i0; --i) { - int r = stc64_uniform(&rng, &dist); + int r = crand_unif(&rng, &dist); if (r & 1) ++n, cqueue_int_push(&Q, r); else diff --git a/misc/benchmarks/picobench/picobench_cmap.cpp b/misc/benchmarks/picobench/picobench_cmap.cpp index 3ffba5b9..bccbe70c 100644 --- a/misc/benchmarks/picobench/picobench_cmap.cpp +++ b/misc/benchmarks/picobench/picobench_cmap.cpp @@ -1,5 +1,5 @@ #define i_static -#include +#include #define i_static #include #include @@ -54,36 +54,36 @@ static void ins_and_erase_i(picobench::state& s) { MapInt map; map.max_load_factor((int)MaxLoadFactor100 / 100.0); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (s.iterations()) - map[crandom()]; + map[crand()]; map.clear(); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) - map[crandom()]; - csrandom(seed); + map[crand()]; + csrand(seed); c_forrange (s.iterations()) - map.erase(crandom()); + map.erase(crand()); s.set_result(map.size()); } /* static void ins_and_erase_cmap_i(picobench::state& s) { cmap_i map = cmap_i_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (s.iterations()) - cmap_i_insert(&map, crandom(), 0); + cmap_i_insert(&map, crand(), 0); cmap_i_clear(&map); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) - cmap_i_insert(&map, crandom(), 0); - csrandom(seed); + cmap_i_insert(&map, crand(), 0); + csrand(seed); c_forrange (s.iterations()) - cmap_i_erase(&map, crandom()); + cmap_i_erase(&map, crand()); s.set_result(cmap_i_size(&map)); cmap_i_drop(&map); } @@ -91,18 +91,18 @@ static void ins_and_erase_cmap_i(picobench::state& s) static void ins_and_erase_cmap_x(picobench::state& s) { cmap_x map = cmap_x_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (s.iterations()) - cmap_x_insert(&map, crandom(), 0); + cmap_x_insert(&map, crand(), 0); cmap_x_clear(&map); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) - cmap_x_insert(&map, crandom(), 0); - csrandom(seed); + cmap_x_insert(&map, crand(), 0); + csrand(seed); c_forrange (s.iterations()) - cmap_x_erase(&map, crandom()); + cmap_x_erase(&map, crand()); s.set_result(cmap_x_size(&map)); cmap_x_drop(&map); } @@ -124,11 +124,11 @@ static void ins_and_access_i(picobench::state& s) size_t result = 0; MapInt map; map.max_load_factor((int)MaxLoadFactor100 / 100.0); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (N1) - result += ++map[crandom() & mask]; + result += ++map[crand() & mask]; s.set_result(result); } @@ -137,11 +137,11 @@ static void ins_and_access_cmap_i(picobench::state& s) uint64_t mask = (1ull << s.arg()) - 1; size_t result = 0; cmap_i map = cmap_i_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (N1) - result += ++cmap_i_insert(&map, crandom() & mask, 0).ref->second; + result += ++cmap_i_insert(&map, crand() & mask, 0).ref->second; s.set_result(result); cmap_i_drop(&map); } @@ -158,7 +158,7 @@ PICOBENCH_SUITE("Map3"); static void randomize(char* str, size_t len) { for (size_t k=0; k < len; ++k) { - union {uint64_t i; char c[8];} r = {.i = crandom()}; + union {uint64_t i; char c[8];} r = {.i = crand()}; for (unsigned i=0; i<8 && ksecond; } // reset rng back to inital state - csrandom(seed); + csrand(seed); // measure erase then iterate whole map c_forrange (n, s.iterations()) { - cmap_x_erase(&map, crandom()); + cmap_x_erase(&map, crand()); if (!(n & K)) c_foreach (i, cmap_x, map) result += i.ref->second; } diff --git a/misc/benchmarks/picobench/picobench_csmap.cpp b/misc/benchmarks/picobench/picobench_csmap.cpp index 5caab6cc..a6a97b14 100644 --- a/misc/benchmarks/picobench/picobench_csmap.cpp +++ b/misc/benchmarks/picobench/picobench_csmap.cpp @@ -1,6 +1,6 @@ #include #define i_static -#include +#include #define i_static #include #include @@ -71,20 +71,20 @@ template static void insert_i(picobench::state& s) { MapInt map; - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (n, s.iterations()) - map.emplace(crandom() & 0xfffffff, n); + map.emplace(crand() & 0xfffffff, n); s.set_result(map.size()); } static void insert_csmap_i(picobench::state& s) { csmap_i map = csmap_i_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (n, s.iterations()) - csmap_i_insert(&map, crandom() & 0xfffffff, n); + csmap_i_insert(&map, crand() & 0xfffffff, n); s.set_result(csmap_i_size(&map)); csmap_i_drop(&map); } @@ -103,21 +103,21 @@ static void ins_and_erase_i(picobench::state& s) size_t result = 0; uint64_t mask = (1ull << s.arg()) - 1; MapInt map; - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (i, s.iterations()) - map.emplace(crandom() & mask, i); + map.emplace(crand() & mask, i); result = map.size(); map.clear(); - csrandom(seed); + csrand(seed); c_forrange (i, s.iterations()) - map[crandom() & mask] = i; + map[crand() & mask] = i; - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) - map.erase(crandom() & mask); + map.erase(crand() & mask); s.set_result(result); } @@ -126,21 +126,21 @@ static void ins_and_erase_csmap_i(picobench::state& s) size_t result = 0; uint64_t mask = (1ull << s.arg()) - 1; csmap_i map = csmap_i_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (i, s.iterations()) - csmap_i_insert(&map, crandom() & mask, i); + csmap_i_insert(&map, crand() & mask, i); result = csmap_i_size(&map); csmap_i_clear(&map); - csrandom(seed); + csrand(seed); c_forrange (i, s.iterations()) - csmap_i_insert_or_assign(&map, crandom() & mask, i); + csmap_i_insert_or_assign(&map, crand() & mask, i); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) - csmap_i_erase(&map, crandom() & mask); + csmap_i_erase(&map, crand() & mask); s.set_result(result); csmap_i_drop(&map); } @@ -158,12 +158,12 @@ static void ins_and_access_i(picobench::state& s) uint64_t mask = (1ull << s.arg()) - 1; size_t result = 0; MapInt map; - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (s.iterations()) { - result += ++map[crandom() & mask]; - auto it = map.find(crandom() & mask); + result += ++map[crand() & mask]; + auto it = map.find(crand() & mask); if (it != map.end()) map.erase(it->first); } s.set_result(result + map.size()); @@ -174,12 +174,12 @@ static void ins_and_access_csmap_i(picobench::state& s) uint64_t mask = (1ull << s.arg()) - 1; size_t result = 0; csmap_i map = csmap_i_init(); - csrandom(seed); + csrand(seed); picobench::scope scope(s); c_forrange (s.iterations()) { - result += ++csmap_i_insert(&map, crandom() & mask, 0).ref->second; - const csmap_i_value* val = csmap_i_get(&map, crandom() & mask); + result += ++csmap_i_insert(&map, crand() & mask, 0).ref->second; + const csmap_i_value* val = csmap_i_get(&map, crand() & mask); if (val) csmap_i_erase(&map, val->first); } s.set_result(result + csmap_i_size(&map)); @@ -194,7 +194,7 @@ PICOBENCH(ins_and_access_csmap_i).P; PICOBENCH_SUITE("Map4"); static void randomize(char* str, int len) { - union {uint64_t i; char c[8];} r = {.i = crandom()}; + union {uint64_t i; char c[8];} r = {.i = crand()}; for (int i = len - 7, j = 0; i < len; ++j, ++i) str[i] = (r.c[j] & 63) + 48; } @@ -207,12 +207,12 @@ static void ins_and_access_s(picobench::state& s) MapStr map; picobench::scope scope(s); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) { randomize(&str[0], str.size()); map.emplace(str, str); } - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) { randomize(&str[0], str.size()); result += map.erase(str); @@ -228,12 +228,12 @@ static void ins_and_access_csmap_s(picobench::state& s) csmap_str map = csmap_str_init(); picobench::scope scope(s); - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) { randomize(buf, s.arg()); csmap_str_emplace(&map, buf, buf); } - csrandom(seed); + csrand(seed); c_forrange (s.iterations()) { randomize(buf, s.arg()); result += csmap_str_erase(&map, buf); @@ -262,22 +262,22 @@ static void iterate_x(picobench::state& s) uint64_t K = (1ull << s.arg()) - 1; picobench::scope scope(s); - csrandom(seed); + csrand(seed); size_t result = 0; // measure insert then iterate whole map c_forrange (n, s.iterations()) { - map[crandom()] = n; + map[crand()] = n; if (!(n & K)) for (auto const& keyVal : map) result += keyVal.second; } // reset rng back to inital state - csrandom(seed); + csrand(seed); // measure erase then iterate whole map c_forrange (n, s.iterations()) { - map.erase(crandom()); + map.erase(crand()); if (!(n & K)) for (auto const& keyVal : map) result += keyVal.second; } @@ -290,22 +290,22 @@ static void iterate_csmap_x(picobench::state& s) uint64_t K = (1ull << s.arg()) - 1; picobench::scope scope(s); - csrandom(seed); + csrand(seed); size_t result = 0; // measure insert then iterate whole map c_forrange (n, s.iterations()) { - csmap_x_insert_or_assign(&map, crandom(), n); + csmap_x_insert_or_assign(&map, crand(), n); if (!(n & K)) c_foreach (i, csmap_x, map) result += i.ref->second; } // reset rng back to inital state - csrandom(seed); + csrand(seed); // measure erase then iterate whole map c_forrange (n, s.iterations()) { - csmap_x_erase(&map, crandom()); + csmap_x_erase(&map, crand()); if (!(n & K)) c_foreach (i, csmap_x, map) result += i.ref->second; } diff --git a/misc/benchmarks/plotbench/cdeq_benchmark.cpp b/misc/benchmarks/plotbench/cdeq_benchmark.cpp index 1259cc07..a8399ea8 100644 --- a/misc/benchmarks/plotbench/cdeq_benchmark.cpp +++ b/misc/benchmarks/plotbench/cdeq_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #ifdef __cplusplus #include @@ -28,10 +28,10 @@ Sample test_std_deque() { { s.test[INSERT].t1 = clock(); container con; - csrandom(seed); - c_forrange (N/3) con.push_front(crandom() & mask1); - c_forrange (N/3) {con.push_back(crandom() & mask1); con.pop_front();} - c_forrange (N/3) con.push_back(crandom() & mask1); + csrand(seed); + c_forrange (N/3) con.push_front(crand() & mask1); + c_forrange (N/3) {con.push_back(crand() & mask1); con.pop_front();} + c_forrange (N/3) con.push_back(crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = con.size(); s.test[ERASE].t1 = clock(); @@ -40,13 +40,13 @@ Sample test_std_deque() { s.test[ERASE].sum = con.size(); }{ container con; - csrandom(seed); - c_forrange (N) con.push_back(crandom() & mask2); + csrand(seed); + c_forrange (N) con.push_back(crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; // Iteration - not inherent find - skipping //container::iterator it; - //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crandom() & mask2)) != con.end()) sum += *it; + //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crand() & mask2)) != con.end()) sum += *it; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); @@ -72,10 +72,10 @@ Sample test_stc_deque() { s.test[INSERT].t1 = clock(); container con = cdeq_x_init(); //cdeq_x_reserve(&con, N); - csrandom(seed); - c_forrange (N/3) cdeq_x_push_front(&con, crandom() & mask1); - c_forrange (N/3) {cdeq_x_push_back(&con, crandom() & mask1); cdeq_x_pop_front(&con);} - c_forrange (N/3) cdeq_x_push_back(&con, crandom() & mask1); + csrand(seed); + c_forrange (N/3) cdeq_x_push_front(&con, crand() & mask1); + c_forrange (N/3) {cdeq_x_push_back(&con, crand() & mask1); cdeq_x_pop_front(&con);} + c_forrange (N/3) cdeq_x_push_back(&con, crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = cdeq_x_size(&con); s.test[ERASE].t1 = clock(); @@ -84,13 +84,13 @@ Sample test_stc_deque() { s.test[ERASE].sum = cdeq_x_size(&con); cdeq_x_drop(&con); }{ - csrandom(seed); + csrand(seed); container con = cdeq_x_init(); - c_forrange (N) cdeq_x_push_back(&con, crandom() & mask2); + c_forrange (N) cdeq_x_push_back(&con, crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; //cdeq_x_iter it, end = cdeq_x_end(&con); - //c_forrange (S) if ((it = cdeq_x_find(&con, crandom() & mask2)).ref != end.ref) sum += *it.ref; + //c_forrange (S) if ((it = cdeq_x_find(&con, crand() & mask2)).ref != end.ref) sum += *it.ref; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); diff --git a/misc/benchmarks/plotbench/clist_benchmark.cpp b/misc/benchmarks/plotbench/clist_benchmark.cpp index 04c8e8cd..46bd2793 100644 --- a/misc/benchmarks/plotbench/clist_benchmark.cpp +++ b/misc/benchmarks/plotbench/clist_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #ifdef __cplusplus #include @@ -28,9 +28,9 @@ Sample test_std_forward_list() { { s.test[INSERT].t1 = clock(); container con; - csrandom(seed); - c_forrange (N/2) con.push_front(crandom() & mask1); - c_forrange (N/2) con.push_front(crandom() & mask1); + csrand(seed); + c_forrange (N/2) con.push_front(crand() & mask1); + c_forrange (N/2) con.push_front(crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = 0; s.test[ERASE].t1 = clock(); @@ -39,13 +39,13 @@ Sample test_std_forward_list() { s.test[ERASE].sum = 0; }{ container con; - csrandom(seed); - c_forrange (N) con.push_front(crandom() & mask2); + csrand(seed); + c_forrange (N) con.push_front(crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; container::iterator it; // Iteration - not inherent find - skipping - //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crandom() & mask2)) != con.end()) sum += *it; + //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crand() & mask2)) != con.end()) sum += *it; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); @@ -70,9 +70,9 @@ Sample test_stc_forward_list() { { s.test[INSERT].t1 = clock(); container con = clist_x_init(); - csrandom(seed); - c_forrange (N/2) clist_x_push_front(&con, crandom() & mask1); - c_forrange (N/2) clist_x_push_back(&con, crandom() & mask1); + csrand(seed); + c_forrange (N/2) clist_x_push_front(&con, crand() & mask1); + c_forrange (N/2) clist_x_push_back(&con, crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = 0; s.test[ERASE].t1 = clock(); @@ -81,13 +81,13 @@ Sample test_stc_forward_list() { s.test[ERASE].sum = 0; clist_x_drop(&con); }{ - csrandom(seed); + csrand(seed); container con = clist_x_init(); - c_forrange (N) clist_x_push_front(&con, crandom() & mask2); + c_forrange (N) clist_x_push_front(&con, crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; //clist_x_iter it, end = clist_x_end(&con); - //c_forrange (S) if ((it = clist_x_find(&con, crandom() & mask2)).ref != end.ref) sum += *it.ref; + //c_forrange (S) if ((it = clist_x_find(&con, crand() & mask2)).ref != end.ref) sum += *it.ref; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); diff --git a/misc/benchmarks/plotbench/cmap_benchmark.cpp b/misc/benchmarks/plotbench/cmap_benchmark.cpp index 7a8f29d2..0582d162 100644 --- a/misc/benchmarks/plotbench/cmap_benchmark.cpp +++ b/misc/benchmarks/plotbench/cmap_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #ifdef __cplusplus #include @@ -26,28 +26,28 @@ Sample test_std_unordered_map() { typedef std::unordered_map container; Sample s = {"std,unordered_map"}; { - csrandom(seed); + csrand(seed); s.test[INSERT].t1 = clock(); container con; - c_forrange (i, N/2) con.emplace(crandom() & mask1, i); + c_forrange (i, N/2) con.emplace(crand() & mask1, i); c_forrange (i, N/2) con.emplace(i, i); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = con.size(); - csrandom(seed); + csrand(seed); s.test[ERASE].t1 = clock(); - c_forrange (N) con.erase(crandom() & mask1); + c_forrange (N) con.erase(crand() & mask1); s.test[ERASE].t2 = clock(); s.test[ERASE].sum = con.size(); }{ container con; - csrandom(seed); - c_forrange (i, N/2) con.emplace(crandom() & mask1, i); + csrand(seed); + c_forrange (i, N/2) con.emplace(crand() & mask1, i); c_forrange (i, N/2) con.emplace(i, i); - csrandom(seed); + csrand(seed); s.test[FIND].t1 = clock(); size_t sum = 0; container::iterator it; - c_forrange (N) if ((it = con.find(crandom() & mask1)) != con.end()) sum += it->second; + c_forrange (N) if ((it = con.find(crand() & mask1)) != con.end()) sum += it->second; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); @@ -70,30 +70,30 @@ Sample test_stc_unordered_map() { typedef cmap_x container; Sample s = {"STC,unordered_map"}; { - csrandom(seed); + csrand(seed); s.test[INSERT].t1 = clock(); container con = cmap_x_init(); - c_forrange (i, N/2) cmap_x_insert(&con, crandom() & mask1, i); + c_forrange (i, N/2) cmap_x_insert(&con, crand() & mask1, i); c_forrange (i, N/2) cmap_x_insert(&con, i, i); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = cmap_x_size(&con); - csrandom(seed); + csrand(seed); s.test[ERASE].t1 = clock(); - c_forrange (N) cmap_x_erase(&con, crandom() & mask1); + c_forrange (N) cmap_x_erase(&con, crand() & mask1); s.test[ERASE].t2 = clock(); s.test[ERASE].sum = cmap_x_size(&con); cmap_x_drop(&con); }{ container con = cmap_x_init(); - csrandom(seed); - c_forrange (i, N/2) cmap_x_insert(&con, crandom() & mask1, i); + csrand(seed); + c_forrange (i, N/2) cmap_x_insert(&con, crand() & mask1, i); c_forrange (i, N/2) cmap_x_insert(&con, i, i); - csrandom(seed); + csrand(seed); s.test[FIND].t1 = clock(); size_t sum = 0; const cmap_x_value* val; c_forrange (N) - if ((val = cmap_x_get(&con, crandom() & mask1))) + if ((val = cmap_x_get(&con, crand() & mask1))) sum += val->second; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; diff --git a/misc/benchmarks/plotbench/cpque_benchmark.cpp b/misc/benchmarks/plotbench/cpque_benchmark.cpp index a729c09f..da092b7f 100644 --- a/misc/benchmarks/plotbench/cpque_benchmark.cpp +++ b/misc/benchmarks/plotbench/cpque_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #define i_val float #define i_cmp -c_default_cmp @@ -11,19 +11,17 @@ #include static const uint32_t seed = 1234; +static const int N = 10000000; void std_test() { - stc64_t rng; - int N = 10000000; - std::priority_queue, std::greater> pq; - rng = stc64_new(seed); + csrand(seed); clock_t start = clock(); c_forrange (i, N) - pq.push((float) stc64_randf(&rng)*100000); + pq.push((float) crandf()*100000.0); - printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); + printf("Built priority queue: %f secs\n", (float)(clock() - start)/(float)CLOCKS_PER_SEC); printf("%g ", pq.top()); start = clock(); @@ -31,32 +29,30 @@ void std_test() pq.pop(); } - printf("\npopped PQ: %f secs\n\n", (clock() - start) / (float) CLOCKS_PER_SEC); + printf("\npopped PQ: %f secs\n\n", (float)(clock() - start)/(float)CLOCKS_PER_SEC); } void stc_test() { - stc64_t rng; - int N = 10000000, M = 10; + int N = 10000000; c_auto (cpque_f, pq) { - rng = stc64_new(seed); + csrand(seed); clock_t start = clock(); - c_forrange (i, N) - cpque_f_push(&pq, (float) stc64_randf(&rng)*100000); + c_forrange (i, N) { + cpque_f_push(&pq, (float) crandf()*100000); + } - printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); + printf("Built priority queue: %f secs\n", (float)(clock() - start)/(float)CLOCKS_PER_SEC); printf("%g ", *cpque_f_top(&pq)); - c_forrange (i, M) { + start = clock(); + c_forrange (i, N) { cpque_f_pop(&pq); } - start = clock(); - c_forrange (i, M, N) - cpque_f_pop(&pq); printf("\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); } } diff --git a/misc/benchmarks/plotbench/csmap_benchmark.cpp b/misc/benchmarks/plotbench/csmap_benchmark.cpp index 46bd695c..da3fc9cc 100644 --- a/misc/benchmarks/plotbench/csmap_benchmark.cpp +++ b/misc/benchmarks/plotbench/csmap_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #ifdef __cplusplus #include @@ -26,28 +26,28 @@ Sample test_std_map() { typedef std::map container; Sample s = {"std,map"}; { - csrandom(seed); + csrand(seed); s.test[INSERT].t1 = clock(); container con; - c_forrange (i, N/2) con.emplace(crandom() & mask1, i); + c_forrange (i, N/2) con.emplace(crand() & mask1, i); c_forrange (i, N/2) con.emplace(i, i); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = con.size(); - csrandom(seed); + csrand(seed); s.test[ERASE].t1 = clock(); - c_forrange (N) con.erase(crandom() & mask1); + c_forrange (N) con.erase(crand() & mask1); s.test[ERASE].t2 = clock(); s.test[ERASE].sum = con.size(); }{ container con; - csrandom(seed); - c_forrange (i, N/2) con.emplace(crandom() & mask1, i); + csrand(seed); + c_forrange (i, N/2) con.emplace(crand() & mask1, i); c_forrange (i, N/2) con.emplace(i, i); - csrandom(seed); + csrand(seed); s.test[FIND].t1 = clock(); size_t sum = 0; container::iterator it; - c_forrange (N) if ((it = con.find(crandom() & mask1)) != con.end()) sum += it->second; + c_forrange (N) if ((it = con.find(crand() & mask1)) != con.end()) sum += it->second; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); @@ -71,30 +71,30 @@ Sample test_stc_map() { typedef csmap_x container; Sample s = {"STC,map"}; { - csrandom(seed); + csrand(seed); s.test[INSERT].t1 = clock(); container con = csmap_x_init(); - c_forrange (i, N/2) csmap_x_insert(&con, crandom() & mask1, i); + c_forrange (i, N/2) csmap_x_insert(&con, crand() & mask1, i); c_forrange (i, N/2) csmap_x_insert(&con, i, i); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = csmap_x_size(&con); - csrandom(seed); + csrand(seed); s.test[ERASE].t1 = clock(); - c_forrange (N) csmap_x_erase(&con, crandom() & mask1); + c_forrange (N) csmap_x_erase(&con, crand() & mask1); s.test[ERASE].t2 = clock(); s.test[ERASE].sum = csmap_x_size(&con); csmap_x_drop(&con); }{ container con = csmap_x_init(); - csrandom(seed); - c_forrange (i, N/2) csmap_x_insert(&con, crandom() & mask1, i); + csrand(seed); + c_forrange (i, N/2) csmap_x_insert(&con, crand() & mask1, i); c_forrange (i, N/2) csmap_x_insert(&con, i, i); - csrandom(seed); + csrand(seed); s.test[FIND].t1 = clock(); size_t sum = 0; const csmap_x_value* val; c_forrange (N) - if ((val = csmap_x_get(&con, crandom() & mask1))) + if ((val = csmap_x_get(&con, crand() & mask1))) sum += val->second; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; diff --git a/misc/benchmarks/plotbench/cvec_benchmark.cpp b/misc/benchmarks/plotbench/cvec_benchmark.cpp index fe7e09fb..b605f4e6 100644 --- a/misc/benchmarks/plotbench/cvec_benchmark.cpp +++ b/misc/benchmarks/plotbench/cvec_benchmark.cpp @@ -1,7 +1,7 @@ #include #include #define i_static -#include +#include #ifdef __cplusplus #include @@ -28,8 +28,8 @@ Sample test_std_vector() { { s.test[INSERT].t1 = clock(); container con; - csrandom(seed); - c_forrange (N) con.push_back(crandom() & mask1); + csrand(seed); + c_forrange (N) con.push_back(crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = con.size(); s.test[ERASE].t1 = clock(); @@ -38,13 +38,13 @@ Sample test_std_vector() { s.test[ERASE].sum = con.size(); }{ container con; - csrandom(seed); - c_forrange (N) con.push_back(crandom() & mask2); + csrand(seed); + c_forrange (N) con.push_back(crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; //container::iterator it; // Iteration - not inherent find - skipping - //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crandom() & mask2)) != con.end()) sum += *it; + //c_forrange (S) if ((it = std::find(con.begin(), con.end(), crand() & mask2)) != con.end()) sum += *it; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); @@ -70,8 +70,8 @@ Sample test_stc_vector() { { s.test[INSERT].t1 = clock(); container con = cvec_x_init(); - csrandom(seed); - c_forrange (N) cvec_x_push_back(&con, crandom() & mask1); + csrand(seed); + c_forrange (N) cvec_x_push_back(&con, crand() & mask1); s.test[INSERT].t2 = clock(); s.test[INSERT].sum = cvec_x_size(&con); s.test[ERASE].t1 = clock(); @@ -80,13 +80,13 @@ Sample test_stc_vector() { s.test[ERASE].sum = cvec_x_size(&con); cvec_x_drop(&con); }{ - csrandom(seed); + csrand(seed); container con = cvec_x_init(); - c_forrange (N) cvec_x_push_back(&con, crandom() & mask2); + c_forrange (N) cvec_x_push_back(&con, crand() & mask2); s.test[FIND].t1 = clock(); size_t sum = 0; //cvec_x_iter it, end = cvec_x_end(&con); - //c_forrange (S) if ((it = cvec_x_find(&con, crandom() & mask2)).ref != end.ref) sum += *it.ref; + //c_forrange (S) if ((it = cvec_x_find(&con, crand() & mask2)).ref != end.ref) sum += *it.ref; s.test[FIND].t2 = clock(); s.test[FIND].sum = sum; s.test[ITER].t1 = clock(); diff --git a/misc/benchmarks/shootout_hashmaps.cpp b/misc/benchmarks/shootout_hashmaps.cpp index 947a35b4..bae9a42b 100644 --- a/misc/benchmarks/shootout_hashmaps.cpp +++ b/misc/benchmarks/shootout_hashmaps.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #define MAX_LOAD_FACTOR 85 @@ -40,8 +40,8 @@ KHASH_MAP_INIT_INT64(ii, IValue) #define i_max_load_factor MAX_LOAD_FACTOR / 100.0f #include -#define SEED(s) rng = stc64_new(s) -#define RAND(N) (stc64_rand(&rng) & (((uint64_t)1 << N) - 1)) +#define SEED(s) rng = crand_init(s) +#define RAND(N) (crand_u64(&rng) & (((uint64_t)1 << N) - 1)) #define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_##X##_init() #define CMAP_PUT(X, key, val) cmap_##X##_insert_or_assign(&map, key, val).ref->second @@ -314,7 +314,7 @@ int main(int argc, char* argv[]) unsigned keybits = argc >= 3 ? atoi(argv[2]) : DEFAULT_KEYBITS; unsigned n = n_mill * 1000000; unsigned N1 = n, N2 = n, N3 = n, N4 = n, N5 = n; - stc64_t rng; + crand_t rng; size_t seed = 123456; // time(NULL); printf("\nUnordered hash map shootout\n"); diff --git a/misc/benchmarks/various/cbits_benchmark.cpp b/misc/benchmarks/various/cbits_benchmark.cpp index dd709db1..1764f556 100644 --- a/misc/benchmarks/various/cbits_benchmark.cpp +++ b/misc/benchmarks/various/cbits_benchmark.cpp @@ -5,7 +5,7 @@ enum{ N=1<<22 }; // 4.2 mill. #define i_static -#include +#include #define i_type cbits #define i_len N #include @@ -39,12 +39,12 @@ int main(int argc, char **argv) one_sec_delay(); total = 0; - csrandom(seed); + csrand(seed); current_time = get_time_in_ms(); c_forrange (40 * N) { - uint64_t r = crandom(); + uint64_t r = crand(); bools[r & (N-1)] = r & 1<<29; } @@ -66,13 +66,13 @@ int main(int argc, char **argv) one_sec_delay(); total = 0; - csrandom(seed); + csrand(seed); current_time = get_time_in_ms(); bitset bits; c_forrange (40 * N) { - uint64_t r = crandom(); + uint64_t r = crand(); bits[r & (N-1)] = r & 1<<29; } @@ -92,13 +92,13 @@ int main(int argc, char **argv) one_sec_delay(); total = 0; - csrandom(seed); + csrand(seed); current_time = get_time_in_ms(); cbits bits2 = cbits_with_size(N, false); c_forrange (40 * N) { - uint64_t r = crandom(); + uint64_t r = crand(); cbits_set_value(&bits2, r & (N-1), r & 1<<29); } diff --git a/misc/benchmarks/various/prng_bench.cpp b/misc/benchmarks/various/prng_bench.cpp index be07f799..234e3805 100644 --- a/misc/benchmarks/various/prng_bench.cpp +++ b/misc/benchmarks/various/prng_bench.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include static inline uint64_t rotl64(const uint64_t x, const int k) { return (x << k) | (x >> (64 - k)); } @@ -124,7 +124,7 @@ int main(void) { enum {N = 500000000}; uint16_t* recipient = new uint16_t[N]; - static stc64_t rng; + static crand_t rng; init_state(rng.state, 12345123); std::mt19937 mt(12345123); @@ -187,7 +187,7 @@ int main(void) beg = clock(); for (size_t i = 0; i < N; i++) - recipient[i] = stc64_rand(&rng); + recipient[i] = crand_u64(&rng); end = clock(); cout << "stc64:\t\t" << (float(end - beg) / CLOCKS_PER_SEC) diff --git a/misc/benchmarks/various/sso_bench.cpp b/misc/benchmarks/various/sso_bench.cpp index 0fffef7a..993ff1bb 100644 --- a/misc/benchmarks/various/sso_bench.cpp +++ b/misc/benchmarks/various/sso_bench.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #define i_type StcVec @@ -30,7 +30,7 @@ static inline std::string randomString_STD(int strsize) { char* p = &s[0]; union { uint64_t u8; uint8_t b[8]; } r; for (int i = 0; i < strsize; ++i) { - if ((i & 7) == 0) r.u8 = crandom() & 0x3f3f3f3f3f3f3f3f; + if ((i & 7) == 0) r.u8 = crand() & 0x3f3f3f3f3f3f3f3f; p[i] = CHARS[r.b[i & 7]]; } return s; @@ -41,7 +41,7 @@ static inline cstr randomString_STC(int strsize) { char* p = cstr_data(&s); union { uint64_t u8; uint8_t b[8]; } r; for (int i = 0; i < strsize; ++i) { - if ((i & 7) == 0) r.u8 = crandom() & 0x3f3f3f3f3f3f3f3f; + if ((i & 7) == 0) r.u8 = crand() & 0x3f3f3f3f3f3f3f3f; p[i] = CHARS[r.b[i & 7]]; } return s; @@ -85,7 +85,7 @@ int main() { // VECTOR WITH STRINGS - csrandom(seed); + csrand(seed); sum = 0, n = 0; std::cerr << "\nstrsize\tmsecs\tstd::vector, size=" << BENCHMARK_SIZE << "\n"; for (int strsize = 1; strsize <= MAX_STRING_SIZE; strsize += 2) { @@ -95,7 +95,7 @@ int main() { } std::cout << "Avg:\t" << sum/n << '\n'; - csrandom(seed); + csrand(seed); sum = 0, n = 0; std::cerr << "\nstrsize\tmsecs\tcvec, size=" << BENCHMARK_SIZE << "\n"; for (int strsize = 1; strsize <= MAX_STRING_SIZE; strsize += 2) { @@ -108,7 +108,7 @@ int main() { // SORTED SET WITH STRINGS - csrandom(seed); + csrand(seed); sum = 0, n = 0; std::cerr << "\nstrsize\tmsecs\tstd::set, size=" << BENCHMARK_SIZE/16 << "\n"; for (int strsize = 1; strsize <= MAX_STRING_SIZE; strsize += 2) { @@ -118,7 +118,7 @@ int main() { } std::cout << "Avg:\t" << sum/n << '\n'; - csrandom(seed); + csrand(seed); sum = 0, n = 0; std::cerr << "\nstrsize\tmsecs\tcsset, size=" << BENCHMARK_SIZE/16 << "\n"; for (int strsize = 1; strsize <= MAX_STRING_SIZE; strsize += 2) { diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c index 2b52cc48..c301128a 100644 --- a/misc/examples/birthday.c +++ b/misc/examples/birthday.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #define i_tag ic #define i_key uint64_t @@ -17,11 +17,11 @@ static void test_repeats(void) const static uint64_t mask = (1ull << BITS) - 1; printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST); - stc64_t rng = stc64_new(seed); + crand_t rng = crand_init(seed); cmap_ic m = cmap_ic_with_capacity(N); c_forrange (i, N) { - uint64_t k = stc64_rand(&rng) & mask; + uint64_t k = crand_u64(&rng) & mask; int v = cmap_ic_insert(&m, k, 0).ref->second += 1; if (v > 1) printf("repeated value %" PRIu64 " (%d) at 2^%d\n", k, v, (int)log2((double)i)); @@ -38,12 +38,12 @@ void test_distribution(void) { enum {BITS = 26}; printf("distribution test: 2^%d values\n", BITS); - stc64_t rng = stc64_new(seed); + crand_t rng = crand_init(seed); const size_t N = 1ull << BITS ; cmap_x map = {0}; c_forrange (N) { - uint64_t k = stc64_rand(&rng); + uint64_t k = crand_u64(&rng); cmap_x_insert(&map, k & 0xf, 0).ref->second += 1; } diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index 7fede5aa..df709d03 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -1,7 +1,7 @@ #include #include -#include +#include #include // Declare int -> int sorted map. @@ -13,21 +13,21 @@ int main() { enum {N = 5000000}; uint64_t seed = (uint64_t)time(NULL); - stc64_t rng = stc64_new(seed); - const double Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f; + crand_t rng = crand_init(seed); + const double Mean = round(crand_f64(&rng)*98.f - 49.f), StdDev = crand_f64(&rng)*10.f + 1.f, Scale = 74.f; printf("Demo of gaussian / normal distribution of %d random samples\n", N); printf("Mean %f, StdDev %f\n", Mean, StdDev); // Setup random engine with normal distribution. - stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); + crand_norm_t dist = crand_norm_init(Mean, StdDev); // Create and init histogram map with defered destruct csmap_int hist = {0}; cstr bar = {0}; c_forrange (N) { - int index = (int)round( stc64_normalf(&rng, &dist) ); + int index = (int)round(crand_norm(&rng, &dist)); csmap_int_insert(&hist, index, 0).ref->second += 1; } diff --git a/misc/examples/list.c b/misc/examples/list.c index 9f0b2504..eb81067d 100644 --- a/misc/examples/list.c +++ b/misc/examples/list.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #define i_type DList #define i_val double @@ -11,11 +11,10 @@ int main() { const int n = 3000000; DList list = {0}; - stc64_t rng = stc64_new(1234567); - stc64_uniformf_t dist = stc64_uniformf_new(100.0f, n); + crand_t rng = crand_init(1234567); int m = 0; c_forrange (n) - DList_push_back(&list, stc64_uniformf(&rng, &dist)), ++m; + DList_push_back(&list, crand_f64(&rng)*n + 100), ++m; double sum = 0.0; printf("sumarize %d:\n", m); diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c index f7887ffb..044e44cb 100644 --- a/misc/examples/new_queue.c +++ b/misc/examples/new_queue.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -22,20 +22,20 @@ int point_cmp(const Point* a, const Point* b) { int main() { int n = 50000000; - stc64_t rng = stc64_new((uint64_t)time(NULL)); - stc64_uniform_t dist = stc64_uniform_new(0, n); + crand_t rng = crand_init((uint64_t)time(NULL)); + crand_unif_t dist = crand_unif_init(0, n); IQ Q = {0}; // Push 50'000'000 random numbers onto the queue. c_forrange (n) - IQ_push(&Q, (int)stc64_uniform(&rng, &dist)); + IQ_push(&Q, (int)crand_unif(&rng, &dist)); // Push or pop on the queue 50 million times printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q)); c_forrange (n) { - int r = (int)stc64_uniform(&rng, &dist); + int r = (int)crand_unif(&rng, &dist); if (r & 3) IQ_push(&Q, r); else diff --git a/misc/examples/priority.c b/misc/examples/priority.c index d3d0283e..95dd3183 100644 --- a/misc/examples/priority.c +++ b/misc/examples/priority.c @@ -1,7 +1,7 @@ #include #include -#include +#include #define i_val int64_t #define i_cmp -c_default_cmp // min-heap (increasing values) @@ -10,22 +10,22 @@ int main() { intptr_t N = 10000000; - stc64_t rng = stc64_new((uint64_t)time(NULL)); - stc64_uniform_t dist = stc64_uniform_new(0, N * 10); + crand_t rng = crand_init((uint64_t)time(NULL)); + crand_unif_t dist = crand_unif_init(0, N * 10); cpque_i heap = {0}; // Push ten million random numbers to priority queue printf("Push %" c_ZI " numbers\n", N); c_forrange (N) - cpque_i_push(&heap, stc64_uniform(&rng, &dist)); + cpque_i_push(&heap, crand_unif(&rng, &dist)); // push some negative numbers too. c_forlist (i, int, {-231, -32, -873, -4, -343}) cpque_i_push(&heap, *i.ref); c_forrange (N) - cpque_i_push(&heap, stc64_uniform(&rng, &dist)); + cpque_i_push(&heap, crand_unif(&rng, &dist)); puts("Extract the hundred smallest."); c_forrange (100) { diff --git a/misc/examples/queue.c b/misc/examples/queue.c index 0f387d52..83c18d09 100644 --- a/misc/examples/queue.c +++ b/misc/examples/queue.c @@ -1,4 +1,4 @@ -#include +#include #include #define i_val int @@ -7,20 +7,20 @@ int main() { int n = 100000000; - stc64_uniform_t dist; - stc64_t rng = stc64_new(1234); - dist = stc64_uniform_new(0, n); + crand_unif_t dist; + crand_t rng = crand_init(1234); + dist = crand_unif_init(0, n); cqueue_i queue = {0}; // Push ten million random numbers onto the queue. c_forrange (n) - cqueue_i_push(&queue, (int)stc64_uniform(&rng, &dist)); + cqueue_i_push(&queue, (int)crand_unif(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); c_forrange (n) { // forrange uses initial n only. - int r = (int)stc64_uniform(&rng, &dist); + int r = (int)crand_unif(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); else diff --git a/misc/examples/random.c b/misc/examples/random.c index e27279a0..ea9c483e 100644 --- a/misc/examples/random.c +++ b/misc/examples/random.c @@ -1,12 +1,12 @@ #include #include -#include +#include int main() { const size_t N = 1000000000; const uint64_t seed = (uint64_t)time(NULL), range = 1000000; - stc64_t rng = stc64_new(seed); + crand_t rng = crand_init(seed); int64_t sum; clock_t diff, before; @@ -15,28 +15,28 @@ int main() sum = 0; before = clock(); c_forrange (N) { - sum += (uint32_t)stc64_rand(&rng); + sum += (uint32_t)crand_u64(&rng); } diff = clock() - before; printf("full range\t\t: %f secs, %" c_ZI ", avg: %f\n", (float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N); - stc64_uniform_t dist1 = stc64_uniform_new(0, range); - rng = stc64_new(seed); + crand_unif_t dist1 = crand_unif_init(0, range); + rng = crand_init(seed); sum = 0; before = clock(); c_forrange (N) { - sum += stc64_uniform(&rng, &dist1); // unbiased + sum += crand_unif(&rng, &dist1); // unbiased } diff = clock() - before; printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZI ", avg: %f\n", range, (float)diff/CLOCKS_PER_SEC, N, (float)sum / (float)N); sum = 0; - rng = stc64_new(seed); + rng = crand_init(seed); before = clock(); c_forrange (N) { - sum += (int64_t)(stc64_rand(&rng) % (range + 1)); // biased + sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased } diff = clock() - before; printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZI ", avg: %f\n", -- cgit v1.2.3