-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_hash_table.c
84 lines (63 loc) · 2.11 KB
/
test_hash_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-FileCopyrightText: © 2020 Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: BSL-1.0
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "phash_table.h"
#include "instrument.h"
// NB: using gms_hash_sdbm_64() doesn't make a difference w.r.t
// hash table space usage,
// i.e. bit-distribution doesn't seem to change when truncating the
// 64 bit hash value vs. truncating during the computation of the 32 bit hash
static uint32_t hash_instrument(const void *p, uint32_t i, uint32_t param)
{
const Instrument *x = p;
return gms_hash_sdbm_32(x[i].isin, 12, param);
}
static uint32_t hash_ins_str(const void *p, uint32_t i, uint32_t param)
{
(void)i;
const char *isin = p;
return gms_hash_sdbm_32(isin, 12, param);
}
int main(int argc, char **argv)
{
assert(argc > 1);
const char *filename = argv[1];
size_t n = 0;
Instrument *xs = get_instruments(filename, &n);
assert(xs);
Instrument *end = xs + n;
(void)end;
//for (Instrument *p = xs; p != end; ++p) {
// printf("%s\n", p->isin);
//}
printf("%zu instruments\n", n);
Gms_Phash_Table h;
int r = gms_phash_table_build(&h, xs, n, hash_instrument);
if (r) {
fprintf(stderr, "Hash table build failed: %d\n", r);
free(xs);
return 1;
}
printf("Bucket table size: %" PRIu32 " slots (%zu bytes), Index table size: %"
PRIu32 " slots (%zu bytes), Total: %zu bytes\n",
h.bkt_table_n, sizeof h.bkt_table[0] * h.bkt_table_n,
h.idx_table_n, sizeof h.idx_table[0] * h.idx_table_n,
sizeof h.bkt_table[0] * h.bkt_table_n + sizeof h.idx_table[0] * h.idx_table_n
);
for (Instrument *p = xs; p != end; ++p) {
uint32_t i = gms_phash_table_lookup(&h, p->isin, hash_ins_str);
if (memcmp(p->isin, xs[i].isin, 12)) {
printf("Mismatch: expected %s vs. %s (i: %" PRIu32 ")\n",
p->isin, xs[i].isin, i);
}
}
gms_phash_table_free(&h);
free(xs);
return 0;
}