diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-07-13 08:03:20 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-07-13 08:03:20 +0200 |
| commit | 3b313daec1614d8a3288b8e20a06dc912a59bda8 (patch) | |
| tree | 9b4ade371c2c021c905f8abe38503f7f910d3764 /test/104_h48set/h48set_tests.c | |
| parent | 4abb81c230bfcd599ccceb2b1bcbdcadbd859537 (diff) | |
| download | nissy-core-3b313daec1614d8a3288b8e20a06dc912a59bda8.tar.gz nissy-core-3b313daec1614d8a3288b8e20a06dc912a59bda8.zip | |
Added a set implementation for h48 table generation
Diffstat (limited to 'test/104_h48set/h48set_tests.c')
| -rw-r--r-- | test/104_h48set/h48set_tests.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/test/104_h48set/h48set_tests.c b/test/104_h48set/h48set_tests.c new file mode 100644 index 0000000..97811da --- /dev/null +++ b/test/104_h48set/h48set_tests.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #include "../test.h" | ||
| 2 | |||
| 3 | char str[STRLENMAX]; | ||
| 4 | |||
| 5 | typedef struct { | ||
| 6 | int64_t n; | ||
| 7 | int64_t capacity; | ||
| 8 | int64_t mod; | ||
| 9 | int64_t *table; | ||
| 10 | } h48set_t; | ||
| 11 | |||
| 12 | void h48set_create(h48set_t *, int64_t, int64_t); | ||
| 13 | void h48set_clear(h48set_t *); | ||
| 14 | void h48set_destroy(h48set_t *); | ||
| 15 | int64_t h48set_lookup(h48set_t *, int64_t); | ||
| 16 | void h48set_insert(h48set_t *, int64_t); | ||
| 17 | bool h48set_contains(h48set_t *, int64_t); | ||
| 18 | |||
| 19 | int compare(const void *x, const void *y) { | ||
| 20 | int64_t a = *(int64_t *)x; | ||
| 21 | int64_t b = *(int64_t *)y; | ||
| 22 | |||
| 23 | if (a > b) return 1; | ||
| 24 | if (a == b) return 0; | ||
| 25 | return -1; | ||
| 26 | } | ||
| 27 | |||
| 28 | int64_t readl(void) { | ||
| 29 | fgets(str, STRLENMAX, stdin); | ||
| 30 | return atoll(str); | ||
| 31 | } | ||
| 32 | |||
| 33 | void run(void) { | ||
| 34 | bool f; | ||
| 35 | h48set_t set; | ||
| 36 | int64_t n, i, j, capacity, mod, *a, u; | ||
| 37 | |||
| 38 | capacity = readl(); | ||
| 39 | mod = readl(); | ||
| 40 | n = readl(); | ||
| 41 | |||
| 42 | a = malloc(n * sizeof(int64_t)); | ||
| 43 | for (i = 0; i < n; i++) | ||
| 44 | a[i] = readl(); | ||
| 45 | |||
| 46 | /* Count unique elements */ | ||
| 47 | u = 0; | ||
| 48 | for (i = 0; i < n; i++) { | ||
| 49 | for (j = 0, f = true; j < i; j++) | ||
| 50 | f = f && a[i] != a[j]; | ||
| 51 | u += f; | ||
| 52 | } | ||
| 53 | |||
| 54 | h48set_create(&set, capacity, mod); | ||
| 55 | for (i = 0; i < n; i++) | ||
| 56 | h48set_insert(&set, a[i]); | ||
| 57 | |||
| 58 | for (i = 0, j = 0; i < set.capacity; i++) | ||
| 59 | if (set.table[i] != -1) | ||
| 60 | a[j++] = set.table[i]; | ||
| 61 | qsort(a, j, sizeof(int64_t), compare); | ||
| 62 | |||
| 63 | printf("%" PRId64 "\n", set.n); | ||
| 64 | for (i = 0; i < j; i++) | ||
| 65 | printf("%" PRId64 "\n", a[i]); | ||
| 66 | |||
| 67 | h48set_destroy(&set); | ||
| 68 | free(a); | ||
| 69 | } | ||
