diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/hashmaps/birthday.c | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
Diffstat (limited to 'misc/examples/hashmaps/birthday.c')
| -rw-r--r-- | misc/examples/hashmaps/birthday.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/misc/examples/hashmaps/birthday.c b/misc/examples/hashmaps/birthday.c new file mode 100644 index 00000000..4742cb45 --- /dev/null +++ b/misc/examples/hashmaps/birthday.c @@ -0,0 +1,68 @@ +#include <math.h> +#include <stdio.h> +#include <time.h> +#include <stc/crand.h> + +#define i_tag ic +#define i_key uint64_t +#define i_val int +#include <stc/cmap.h> + +static uint64_t seed = 12345; + +static void test_repeats(void) +{ + enum {BITS = 46, BITS_TEST = BITS/2 + 2}; + static const uint64_t N = 1ull << BITS_TEST; + static const uint64_t mask = (1ull << BITS) - 1; + + printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST); + crand_t rng = crand_init(seed); + + cmap_ic m = cmap_ic_with_capacity(N); + c_forrange (i, N) { + 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)); + } + cmap_ic_drop(&m); +} + +#define i_key uint32_t +#define i_val uint64_t +#define i_tag x +#include <stc/cmap.h> + +void test_distribution(void) +{ + enum {BITS = 26}; + printf("distribution test: 2^%d values\n", BITS); + crand_t rng = crand_init(seed); + const size_t N = 1ull << BITS ; + + cmap_x map = {0}; + c_forrange (N) { + uint64_t k = crand_u64(&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; + + 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(void) +{ + seed = (uint64_t)time(NULL); + test_distribution(); + test_repeats(); +} |
