From 62d87e063318cc4c842b1b2d8c184f48aeaf6659 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 19 May 2025 17:45:14 +0200 Subject: Refactored checkdata Relevant changes include: - Changed the signature of nissy_checkdata(). - Removed expected_distribution.h from tools; this data is now included in each solver's src/ code. - Removed distribution check for cocsep; may add back later. --- src/solvers/coord/checkdata.h | 65 ++++++++++++++++++++++++++++++++++++++++ src/solvers/coord/coord.h | 1 + src/solvers/coord/dr.h | 16 ++++++++++ src/solvers/coord/dreo.h | 14 +++++++++ src/solvers/coord/eo.h | 11 +++++++ src/solvers/coord/gendata.h | 2 +- src/solvers/coord/solve.h | 3 +- src/solvers/coord/types_macros.h | 2 ++ src/solvers/coord/utils.h | 28 ++++++++--------- 9 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 src/solvers/coord/checkdata.h (limited to 'src/solvers/coord') diff --git a/src/solvers/coord/checkdata.h b/src/solvers/coord/checkdata.h new file mode 100644 index 0000000..81394c7 --- /dev/null +++ b/src/solvers/coord/checkdata.h @@ -0,0 +1,65 @@ +STATIC long long checkdata_coord( + const char *, unsigned long long n, const unsigned char [n]); + +STATIC long long +checkdata_coord( + const char *solver, + unsigned long long data_size, + const unsigned char data[data_size] +) +{ + coord_t *coord; + const unsigned char *table; + tableinfo_t info; + int64_t err; + uint64_t actual_distribution[INFO_DISTRIBUTION_LEN]; + + if ((size_t)data % 8 != 0) { + LOG("[checkdata] Error: buffer is not 8-byte aligned\n"); + return NISSY_ERROR_DATA; + } + + parse_coord_and_axis(solver, &coord, NULL); + if (coord == NULL) { + LOG("[cehckdata] Unknown coordinate solver '%s'\n", solver); + return NISSY_ERROR_DATA; + } + + table = data + INFOSIZE; + err = readtableinfo(data_size, data, &info); + if (err != NISSY_OK) { + LOG("[checkdata] Data is corrupt\n"); + return err; + } + + if (info.type != TABLETYPE_PRUNING) { + LOG("[checkdata] Skipping '%s'\n", info.solver); + table += info.next; + err = readtableinfo_n(data_size, data, 2, &info); + if (err != NISSY_OK) { + LOG("[checkdata] Data is corrupt\n"); + return err; + } + } + + LOG("[checkdata] Checking distribution for '%s' from " + "table preamble\n", info.solver); + if (!distribution_equal(coord->pruning_distribution, + info.distribution, coord->pruning_max)) { + LOG("[checkdata] Distribution from the table preamble does " + "not match the expected one\n"); + return NISSY_ERROR_DATA; + } + + LOG("\n[checkdata] Checking distribution for '%s' from " + "actual table\n", info.solver); + getdistribution(table, actual_distribution, &info); + if (!distribution_equal(coord->pruning_distribution, + actual_distribution, coord->pruning_max)) { + LOG("[checkdata] Distribution from the actual table does " + "not match the expected one\n"); + return NISSY_ERROR_DATA; + } + + return NISSY_OK; +} diff --git a/src/solvers/coord/coord.h b/src/solvers/coord/coord.h index 2f24035..da93439 100644 --- a/src/solvers/coord/coord.h +++ b/src/solvers/coord/coord.h @@ -6,4 +6,5 @@ #include "list.h" #include "utils.h" #include "gendata.h" +#include "checkdata.h" #include "solve.h" diff --git a/src/solvers/coord/dr.h b/src/solvers/coord/dr.h index 607cc39..d986742 100644 --- a/src/solvers/coord/dr.h +++ b/src/solvers/coord/dr.h @@ -32,6 +32,22 @@ STATIC coord_t coordinate_dr = { }, .is_admissible = &solution_lastqt_cw, .is_solvable = &is_eoco_solvable, + .pruning_distribution = { + [0] = 1, + [1] = 1, + [2] = 5, + [3] = 44, + [4] = 487, + [5] = 5841, + [6] = 68364, + [7] = 776568, + [8] = 7950748, + [9] = 52098876, + [10] = 76236234, + [11] = 3771112, + [12] = 129, + }, + .pruning_max = 12, .sym = { .classes = DREOESEP_CLASSES, .max = DREOESEP_MAX, diff --git a/src/solvers/coord/dreo.h b/src/solvers/coord/dreo.h index 1cce4e2..f6fa3e9 100644 --- a/src/solvers/coord/dreo.h +++ b/src/solvers/coord/dreo.h @@ -27,6 +27,20 @@ STATIC coord_t coordinate_dreo = { }, .is_admissible = &solution_lastqt_cw, .is_solvable = &is_dreo_solvable, + .pruning_distribution = { + [0] = 1, + [1] = 1, + [2] = 4, + [3] = 22, + [4] = 160, + [5] = 1286, + [6] = 8550, + [7] = 42152, + [8] = 90748, + [9] = 33466, + [10] = 757, + }, + .pruning_max = 10, .sym = { .classes = DRESEP_CLASSES, .max = COMB_12_4, diff --git a/src/solvers/coord/eo.h b/src/solvers/coord/eo.h index 51dfb29..7735319 100644 --- a/src/solvers/coord/eo.h +++ b/src/solvers/coord/eo.h @@ -20,6 +20,17 @@ STATIC coord_t coordinate_eo = { }, .is_admissible = &solution_lastqt_cw, .is_solvable = &is_eo_even, + .pruning_distribution = { + [0] = 1, + [1] = 2, + [2] = 25, + [3] = 202, + [4] = 620, + [5] = 900, + [6] = 285, + [7] = 13, + }, + .pruning_max = 7, .sym = {0}, }; diff --git a/src/solvers/coord/gendata.h b/src/solvers/coord/gendata.h index 1b65f49..0cc012e 100644 --- a/src/solvers/coord/gendata.h +++ b/src/solvers/coord/gendata.h @@ -22,7 +22,7 @@ gendata_coord_dispatch( { coord_t *coord; - parse_coord_and_axis(strlen(coordstr), coordstr, &coord, NULL); + parse_coord_and_axis(coordstr, &coord, NULL); if (coord == NULL) { LOG("Error: could not parse coordinate '%s'\n", coordstr); diff --git a/src/solvers/coord/solve.h b/src/solvers/coord/solve.h index df0f6ff..659040c 100644 --- a/src/solvers/coord/solve.h +++ b/src/solvers/coord/solve.h @@ -222,8 +222,7 @@ solve_coord_dispatch( coord_t *coord; uint8_t axis; - parse_coord_and_axis( - strlen(coord_and_axis), coord_and_axis, &coord, &axis); + parse_coord_and_axis(coord_and_axis, &coord, &axis); if (coord == NULL) { LOG("Error: could not parse coordinate from '%s'\n", diff --git a/src/solvers/coord/types_macros.h b/src/solvers/coord/types_macros.h index a5ac7f3..0c7a3a2 100644 --- a/src/solvers/coord/types_macros.h +++ b/src/solvers/coord/types_macros.h @@ -26,6 +26,8 @@ typedef struct { uint8_t axistrans[3]; bool (*is_admissible)(const solution_moves_t[static 1]); bool (*is_solvable)(cube_t); + uint64_t pruning_distribution[INFO_DISTRIBUTION_LEN]; + uint8_t pruning_max; struct { size_t classes; uint64_t max; diff --git a/src/solvers/coord/utils.h b/src/solvers/coord/utils.h index aa4d74c..6066049 100644 --- a/src/solvers/coord/utils.h +++ b/src/solvers/coord/utils.h @@ -1,7 +1,6 @@ STATIC coord_t *parse_coord(size_t n, const char [n]); -STATIC uint8_t parse_axis(size_t n, const char [n]); -STATIC void parse_coord_and_axis( - size_t n, const char [n], coord_t **, uint8_t *); +STATIC uint8_t parse_axis(const char [static 2]); +STATIC void parse_coord_and_axis(const char *, coord_t **, uint8_t *); STATIC long long dataid_coord(const char *, char [static NISSY_SIZE_DATAID]); STATIC coord_t * @@ -17,13 +16,13 @@ parse_coord(size_t n, const char coord[n]) } STATIC uint8_t -parse_axis(size_t n, const char axis[n]) +parse_axis(const char axis[static 2]) { - if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) { + if (!strcmp(axis, "UD") || !strcmp(axis, "DU")) { return AXIS_UD; - } else if (!strncmp(axis, "RL", n) || !strncmp(axis, "LR", n)) { + } else if (!strcmp(axis, "RL") || !strcmp(axis, "LR")) { return AXIS_RL; - } else if (!strncmp(axis, "FB", n) || !strncmp(axis, "BF", n)) { + } else if (!strcmp(axis, "FB") || !strcmp(axis, "BF")) { return AXIS_FB; } @@ -32,25 +31,22 @@ parse_axis(size_t n, const char axis[n]) STATIC void parse_coord_and_axis( - size_t n, - const char str[n], + const char *str, coord_t **coord, uint8_t *axis ) { - const char *s; size_t i; - s = str + 6; - for (i = 0; i < n; i++) - if (s[i] == '_') + for (i = 6; i < strlen(str); i++) + if (str[i] == '_') break; if (coord != NULL) - *coord = parse_coord(i, s); + *coord = parse_coord(i-6, str+6); if (axis != NULL) - *axis = i == n ? UINT8_ERROR : parse_axis(n-i-1, s+i+1); + *axis = i == strlen(str) ? UINT8_ERROR : parse_axis(str+i+1); } STATIC long long @@ -58,7 +54,7 @@ dataid_coord(const char *ca, char dataid[static NISSY_SIZE_DATAID]) { coord_t *c; - parse_coord_and_axis(strlen(ca), ca, &c, NULL); + parse_coord_and_axis(ca, &c, NULL); if (c == NULL) { LOG("Error: cannot parse coordinate from '%s'\n", ca); -- cgit v1.3