summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/birthday.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-26 00:27:45 +0100
committerTyge Løvset <[email protected]>2023-03-26 00:27:45 +0100
commiteb85069b669e754836b9d4587ba03d3af1a5e975 (patch)
tree45c9a0d3fe40c59a8b33ae8ecd2e7aa78bef6240 /misc/examples/birthday.c
parente8be14dfc894eeac859f0287d4d5b4f4745c0585 (diff)
downloadSTC-modified-eb85069b669e754836b9d4587ba03d3af1a5e975.tar.gz
STC-modified-eb85069b669e754836b9d4587ba03d3af1a5e975.zip
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.
Diffstat (limited to 'misc/examples/birthday.c')
-rw-r--r--misc/examples/birthday.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c
index cb627ef8..2b52cc48 100644
--- a/misc/examples/birthday.c
+++ b/misc/examples/birthday.c
@@ -18,16 +18,15 @@ static void test_repeats(void)
printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST);
stc64_t rng = stc64_new(seed);
- c_auto (cmap_ic, m)
- {
- cmap_ic_reserve(&m, N);
- c_forrange (i, N) {
- uint64_t k = stc64_rand(&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));
- }
+
+ cmap_ic m = cmap_ic_with_capacity(N);
+ c_forrange (i, N) {
+ uint64_t k = stc64_rand(&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));
}
+ cmap_ic_drop(&m);
}
#define i_key uint32_t
@@ -42,22 +41,23 @@ void test_distribution(void)
stc64_t rng = stc64_new(seed);
const size_t N = 1ull << BITS ;
- c_auto (cmap_x, map) {
- c_forrange (N) {
- uint64_t k = stc64_rand(&rng);
- cmap_x_insert(&map, k & 0xf, 0).ref->second += 1;
- }
+ cmap_x map = {0};
+ c_forrange (N) {
+ uint64_t k = stc64_rand(&rng);
+ cmap_x_insert(&map, k & 0xf, 0).ref->second += 1;
+ }
- uint64_t sum = 0;
- c_foreach (i, cmap_x, map) sum += i.ref->second;
- sum /= (uint64_t)map.size;
+ uint64_t sum = 0;
+ c_foreach (i, cmap_x, map) sum += i.ref->second;
+ sum /= (uint64_t)map.size;
- c_foreach (i, cmap_x, map) {
- printf("%4" PRIu32 ": %" PRIu64 " - %" PRIu64 ": %11.8f\n",
- i.ref->first, i.ref->second, sum,
- (1.0 - (double)i.ref->second / (double)sum));
- }
+ c_foreach (i, cmap_x, map) {
+ printf("%4" PRIu32 ": %" PRIu64 " - %" PRIu64 ": %11.8f\n",
+ i.ref->first, i.ref->second, sum,
+ (1.0 - (double)i.ref->second / (double)sum));
}
+
+ cmap_x_drop(&map);
}
int main()