diff options
| -rw-r--r-- | src/solve_h48.h | 77 | ||||
| -rw-r--r-- | test/104_h48set/00_small.in | 6 | ||||
| -rw-r--r-- | test/104_h48set/00_small.out | 3 | ||||
| -rw-r--r-- | test/104_h48set/01_large.in | 153 | ||||
| -rw-r--r-- | test/104_h48set/01_large.out | 83 | ||||
| -rw-r--r-- | test/104_h48set/h48set_tests.c | 69 |
6 files changed, 391 insertions, 0 deletions
diff --git a/src/solve_h48.h b/src/solve_h48.h index 5f99d15..4bf9f78 100644 --- a/src/solve_h48.h +++ b/src/solve_h48.h | |||
| @@ -24,6 +24,13 @@ | |||
| 24 | #define MAX_SOLUTION_LENGTH 20 | 24 | #define MAX_SOLUTION_LENGTH 20 |
| 25 | 25 | ||
| 26 | typedef struct { | 26 | typedef struct { |
| 27 | int64_t n; | ||
| 28 | int64_t capacity; | ||
| 29 | int64_t mod; | ||
| 30 | int64_t *table; | ||
| 31 | } h48set_t; | ||
| 32 | |||
| 33 | typedef struct { | ||
| 27 | cube_t cube; | 34 | cube_t cube; |
| 28 | uint8_t depth; | 35 | uint8_t depth; |
| 29 | uint8_t maxdepth; | 36 | uint8_t maxdepth; |
| @@ -66,6 +73,13 @@ typedef struct { | |||
| 66 | char *s; | 73 | char *s; |
| 67 | } dfsarg_solveh48stats_t; | 74 | } dfsarg_solveh48stats_t; |
| 68 | 75 | ||
| 76 | _static void h48set_create(h48set_t *, int64_t, int64_t); | ||
| 77 | _static void h48set_clear(h48set_t *); | ||
| 78 | _static void h48set_destroy(h48set_t *); | ||
| 79 | _static_inline int64_t h48set_lookup(h48set_t *, int64_t); | ||
| 80 | _static_inline void h48set_insert(h48set_t *, int64_t); | ||
| 81 | _static_inline bool h48set_contains(h48set_t *, int64_t); | ||
| 82 | |||
| 69 | _static_inline int64_t coord_h48(cube_t, const uint32_t *, uint8_t); | 83 | _static_inline int64_t coord_h48(cube_t, const uint32_t *, uint8_t); |
| 70 | _static_inline int64_t coord_h48_edges(cube_t, int64_t, uint8_t, uint8_t); | 84 | _static_inline int64_t coord_h48_edges(cube_t, int64_t, uint8_t, uint8_t); |
| 71 | _static_inline cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); | 85 | _static_inline cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); |
| @@ -90,6 +104,69 @@ _static int64_t solve_h48(cube_t, int8_t, int8_t, int8_t, uint8_t, const void *, | |||
| 90 | _static int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); | 104 | _static int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); |
| 91 | _static int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); | 105 | _static int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); |
| 92 | 106 | ||
| 107 | _static void | ||
| 108 | h48set_create(h48set_t *set, int64_t capacity, int64_t mod) | ||
| 109 | { | ||
| 110 | set->capacity = capacity; | ||
| 111 | set->mod = mod; | ||
| 112 | |||
| 113 | set->table = malloc(set->capacity * sizeof(int64_t)); | ||
| 114 | h48set_clear(set); | ||
| 115 | } | ||
| 116 | |||
| 117 | _static void | ||
| 118 | h48set_clear(h48set_t *set) | ||
| 119 | { | ||
| 120 | int64_t i; | ||
| 121 | |||
| 122 | for (i = 0; i < set->capacity; i++) | ||
| 123 | set->table[i] = -1; | ||
| 124 | |||
| 125 | set->n = 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | _static void | ||
| 129 | h48set_destroy(h48set_t *set) | ||
| 130 | { | ||
| 131 | free(set->table); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* Returns the index in where x should be inserted, or -1 if x is in the set */ | ||
| 135 | _static_inline int64_t | ||
| 136 | h48set_lookup(h48set_t *set, int64_t x) | ||
| 137 | { | ||
| 138 | int64_t hash, i; | ||
| 139 | |||
| 140 | hash = ((x % set->capacity) * set->mod) % set->capacity; | ||
| 141 | for (i = hash; set->table[i] != -1; i = (i+1) % set->capacity) | ||
| 142 | if (set->table[i] == x) | ||
| 143 | return -1; | ||
| 144 | |||
| 145 | return i; | ||
| 146 | } | ||
| 147 | |||
| 148 | _static_inline void | ||
| 149 | h48set_insert(h48set_t *set, int64_t x) | ||
| 150 | { | ||
| 151 | int64_t i; | ||
| 152 | |||
| 153 | i = h48set_lookup(set, x); | ||
| 154 | if (i != -1) { | ||
| 155 | set->table[i] = x; | ||
| 156 | set->n++; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | _static_inline bool | ||
| 161 | h48set_contains(h48set_t *set, int64_t x) | ||
| 162 | { | ||
| 163 | int64_t i; | ||
| 164 | |||
| 165 | i = h48set_lookup(set, x); | ||
| 166 | |||
| 167 | return i == -1; | ||
| 168 | } | ||
| 169 | |||
| 93 | _static_inline int64_t | 170 | _static_inline int64_t |
| 94 | coord_h48(cube_t c, const uint32_t *cocsepdata, uint8_t h) | 171 | coord_h48(cube_t c, const uint32_t *cocsepdata, uint8_t h) |
| 95 | { | 172 | { |
diff --git a/test/104_h48set/00_small.in b/test/104_h48set/00_small.in new file mode 100644 index 0000000..d788d5b --- /dev/null +++ b/test/104_h48set/00_small.in | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | 11 | ||
| 2 | 7 | ||
| 3 | 3 | ||
| 4 | 34 | ||
| 5 | 45 | ||
| 6 | 34 | ||
diff --git a/test/104_h48set/00_small.out b/test/104_h48set/00_small.out new file mode 100644 index 0000000..29c08e5 --- /dev/null +++ b/test/104_h48set/00_small.out | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | 2 | ||
| 2 | 34 | ||
| 3 | 45 | ||
diff --git a/test/104_h48set/01_large.in b/test/104_h48set/01_large.in new file mode 100644 index 0000000..a70ef9a --- /dev/null +++ b/test/104_h48set/01_large.in | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | 307 | ||
| 2 | 293 | ||
| 3 | 150 | ||
| 4 | 100053 | ||
| 5 | 100045 | ||
| 6 | 100007 | ||
| 7 | 100007 | ||
| 8 | 100011 | ||
| 9 | 100072 | ||
| 10 | 100033 | ||
| 11 | 100015 | ||
| 12 | 100013 | ||
| 13 | 100068 | ||
| 14 | 100047 | ||
| 15 | 100046 | ||
| 16 | 100000 | ||
| 17 | 100076 | ||
| 18 | 100021 | ||
| 19 | 100019 | ||
| 20 | 100015 | ||
| 21 | 100049 | ||
| 22 | 100052 | ||
| 23 | 100071 | ||
| 24 | 100058 | ||
| 25 | 100009 | ||
| 26 | 100060 | ||
| 27 | 100030 | ||
| 28 | 100051 | ||
| 29 | 100069 | ||
| 30 | 100011 | ||
| 31 | 100051 | ||
| 32 | 100014 | ||
| 33 | 100031 | ||
| 34 | 100017 | ||
| 35 | 100076 | ||
| 36 | 100016 | ||
| 37 | 100030 | ||
| 38 | 100037 | ||
| 39 | 100011 | ||
| 40 | 100002 | ||
| 41 | 100029 | ||
| 42 | 100022 | ||
| 43 | 100038 | ||
| 44 | 100075 | ||
| 45 | 100066 | ||
| 46 | 100051 | ||
| 47 | 100020 | ||
| 48 | 100039 | ||
| 49 | 100029 | ||
| 50 | 100087 | ||
| 51 | 100099 | ||
| 52 | 100041 | ||
| 53 | 100042 | ||
| 54 | 100026 | ||
| 55 | 100060 | ||
| 56 | 100006 | ||
| 57 | 100010 | ||
| 58 | 100079 | ||
| 59 | 100046 | ||
| 60 | 100086 | ||
| 61 | 100010 | ||
| 62 | 100036 | ||
| 63 | 100047 | ||
| 64 | 100069 | ||
| 65 | 100041 | ||
| 66 | 100074 | ||
| 67 | 100090 | ||
| 68 | 100092 | ||
| 69 | 100020 | ||
| 70 | 100096 | ||
| 71 | 100046 | ||
| 72 | 100028 | ||
| 73 | 100072 | ||
| 74 | 100096 | ||
| 75 | 100025 | ||
| 76 | 100001 | ||
| 77 | 100067 | ||
| 78 | 100044 | ||
| 79 | 100063 | ||
| 80 | 100026 | ||
| 81 | 100062 | ||
| 82 | 100091 | ||
| 83 | 100012 | ||
| 84 | 100073 | ||
| 85 | 100051 | ||
| 86 | 100010 | ||
| 87 | 100096 | ||
| 88 | 100043 | ||
| 89 | 100025 | ||
| 90 | 100077 | ||
| 91 | 100089 | ||
| 92 | 100005 | ||
| 93 | 100024 | ||
| 94 | 100091 | ||
| 95 | 100046 | ||
| 96 | 100053 | ||
| 97 | 100058 | ||
| 98 | 100065 | ||
| 99 | 100051 | ||
| 100 | 100056 | ||
| 101 | 100025 | ||
| 102 | 100093 | ||
| 103 | 100088 | ||
| 104 | 100033 | ||
| 105 | 100073 | ||
| 106 | 100048 | ||
| 107 | 100059 | ||
| 108 | 100027 | ||
| 109 | 100045 | ||
| 110 | 100012 | ||
| 111 | 100059 | ||
| 112 | 100084 | ||
| 113 | 100017 | ||
| 114 | 100048 | ||
| 115 | 100004 | ||
| 116 | 100051 | ||
| 117 | 100016 | ||
| 118 | 100088 | ||
| 119 | 100033 | ||
| 120 | 100064 | ||
| 121 | 100060 | ||
| 122 | 100043 | ||
| 123 | 100084 | ||
| 124 | 100026 | ||
| 125 | 100051 | ||
| 126 | 100060 | ||
| 127 | 100069 | ||
| 128 | 100015 | ||
| 129 | 100014 | ||
| 130 | 100038 | ||
| 131 | 100068 | ||
| 132 | 100038 | ||
| 133 | 100097 | ||
| 134 | 100017 | ||
| 135 | 100013 | ||
| 136 | 100053 | ||
| 137 | 100019 | ||
| 138 | 100018 | ||
| 139 | 100081 | ||
| 140 | 100071 | ||
| 141 | 100025 | ||
| 142 | 100014 | ||
| 143 | 100016 | ||
| 144 | 100011 | ||
| 145 | 100069 | ||
| 146 | 100079 | ||
| 147 | 100063 | ||
| 148 | 100070 | ||
| 149 | 100072 | ||
| 150 | 100095 | ||
| 151 | 100042 | ||
| 152 | 100008 | ||
| 153 | 100022 | ||
diff --git a/test/104_h48set/01_large.out b/test/104_h48set/01_large.out new file mode 100644 index 0000000..6632336 --- /dev/null +++ b/test/104_h48set/01_large.out | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | 82 | ||
| 2 | 100000 | ||
| 3 | 100001 | ||
| 4 | 100002 | ||
| 5 | 100004 | ||
| 6 | 100005 | ||
| 7 | 100006 | ||
| 8 | 100007 | ||
| 9 | 100008 | ||
| 10 | 100009 | ||
| 11 | 100010 | ||
| 12 | 100011 | ||
| 13 | 100012 | ||
| 14 | 100013 | ||
| 15 | 100014 | ||
| 16 | 100015 | ||
| 17 | 100016 | ||
| 18 | 100017 | ||
| 19 | 100018 | ||
| 20 | 100019 | ||
| 21 | 100020 | ||
| 22 | 100021 | ||
| 23 | 100022 | ||
| 24 | 100024 | ||
| 25 | 100025 | ||
| 26 | 100026 | ||
| 27 | 100027 | ||
| 28 | 100028 | ||
| 29 | 100029 | ||
| 30 | 100030 | ||
| 31 | 100031 | ||
| 32 | 100033 | ||
| 33 | 100036 | ||
| 34 | 100037 | ||
| 35 | 100038 | ||
| 36 | 100039 | ||
| 37 | 100041 | ||
| 38 | 100042 | ||
| 39 | 100043 | ||
| 40 | 100044 | ||
| 41 | 100045 | ||
| 42 | 100046 | ||
| 43 | 100047 | ||
| 44 | 100048 | ||
| 45 | 100049 | ||
| 46 | 100051 | ||
| 47 | 100052 | ||
| 48 | 100053 | ||
| 49 | 100056 | ||
| 50 | 100058 | ||
| 51 | 100059 | ||
| 52 | 100060 | ||
| 53 | 100062 | ||
| 54 | 100063 | ||
| 55 | 100064 | ||
| 56 | 100065 | ||
| 57 | 100066 | ||
| 58 | 100067 | ||
| 59 | 100068 | ||
| 60 | 100069 | ||
| 61 | 100070 | ||
| 62 | 100071 | ||
| 63 | 100072 | ||
| 64 | 100073 | ||
| 65 | 100074 | ||
| 66 | 100075 | ||
| 67 | 100076 | ||
| 68 | 100077 | ||
| 69 | 100079 | ||
| 70 | 100081 | ||
| 71 | 100084 | ||
| 72 | 100086 | ||
| 73 | 100087 | ||
| 74 | 100088 | ||
| 75 | 100089 | ||
| 76 | 100090 | ||
| 77 | 100091 | ||
| 78 | 100092 | ||
| 79 | 100093 | ||
| 80 | 100095 | ||
| 81 | 100096 | ||
| 82 | 100097 | ||
| 83 | 100099 | ||
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 | } | ||
