From 992de12cc081e7dde16c3c231f83bfa77c67825a Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 31 Jul 2025 09:47:57 +0200 Subject: Added move sequence comparison function --- src/core/core_types.h | 9 +++++++ src/core/moves.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/nissy.c | 12 +++++++++ src/nissy.h | 22 ++++++++++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/core/core_types.h b/src/core/core_types.h index 2ad8938..19a9f42 100644 --- a/src/core/core_types.h +++ b/src/core/core_types.h @@ -1,4 +1,13 @@ +#define MOVES_STRUCT_MAXLEN 1000 + typedef struct { cube_t cube; uint8_t orientation; } oriented_cube_t; + +typedef struct { + size_t nnormal; + size_t ninverse; + uint8_t normal[MOVES_STRUCT_MAXLEN]; + uint8_t inverse[MOVES_STRUCT_MAXLEN]; +} moves_struct_t; diff --git a/src/core/moves.h b/src/core/moves.h index d3ff4a3..9c8cc90 100644 --- a/src/core/moves.h +++ b/src/core/moves.h @@ -3,8 +3,12 @@ STATIC uint8_t readmove(char); STATIC int64_t readmoves(const char *, - size_t, size_t, uint64_t *, uint64_t *, uint8_t *, uint8_t *); + size_t, size_t, size_t *, size_t *, uint8_t *, uint8_t *); +STATIC int64_t readmoves_struct(const char *, moves_struct_t [static 1]); STATIC int64_t countmoves(const char *); +STATIC bool moves_struct_equal( + const moves_struct_t [static 1], const moves_struct_t [static 1]); +STATIC long long comparemoves(const char *, const char *); STATIC uint8_t readmodifier(char); STATIC int64_t writemoves(size_t, const uint8_t *, size_t, char *); @@ -124,8 +128,8 @@ readmoves( const char *buf, size_t nsize, size_t invsize, - uint64_t *n, - uint64_t *i, + size_t *n, + size_t *i, uint8_t *normal, uint8_t *inverse ) @@ -153,6 +157,13 @@ readmoves( return (int64_t)c; } +STATIC int64_t +readmoves_struct(const char *moves, moves_struct_t ret[static 1]) +{ + return readmoves(moves, MOVES_STRUCT_MAXLEN, MOVES_STRUCT_MAXLEN, + &ret->nnormal, &ret->ninverse, ret->normal, ret->inverse); +} + STATIC int64_t countmoves(const char *buf) { @@ -168,6 +179,58 @@ countmoves(const char *buf) return count; } +STATIC bool +moves_struct_equal( + const moves_struct_t ms1[static 1], + const moves_struct_t ms2[static 1] +) +{ + size_t i; + + if (ms1->nnormal != ms2->nnormal || ms1->ninverse != ms2->ninverse) + return false; + + for (i = 0; i < ms1->nnormal; i++) + if (ms1->normal[i] != ms2->normal[i]) + return false; + + for (i = 0; i < ms1->ninverse; i++) + if (ms1->inverse[i] != ms2->inverse[i]) + return false; + + return true; +} + +STATIC long long +comparemoves(const char *moves1, const char *moves2) +{ + int64_t err; + moves_struct_t ms1, ms2; + + if ((err = readmoves_struct(moves1, &ms1)) < 0) + return err; + sortparallel_moves(ms1.nnormal, ms1.normal); + sortparallel_moves(ms1.ninverse, ms1.inverse); + + if ((err = readmoves_struct(moves2, &ms2)) < 0) + return err; + sortparallel_moves(ms2.nnormal, ms2.normal); + sortparallel_moves(ms2.ninverse, ms2.inverse); + + if (moves_struct_equal(&ms1, &ms2)) + return NISSY_COMPARE_MOVES_EQUAL; + + /* + TODO: more types of move comparison + - up to moving rotations around + - up to rotation + - up transformation (including mirror or not including it) + - ... + */ + + return NISSY_COMPARE_MOVES_DIFFERENT; +} + STATIC int64_t writemoves( size_t nmoves, diff --git a/src/nissy.c b/src/nissy.c index 93a4841..4b5d476 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -353,6 +353,18 @@ nissy_countmoves( return countmoves(moves); } +long long +nissy_comparemoves( + const char *moves1, + const char *moves2 +) +{ + if (moves1 == NULL || moves2 == NULL) + return NISSY_ERROR_NULL_POINTER; + + return comparemoves(moves1, moves2); +} + long long nissy_setlogger( void (*log)(const char *, void *), diff --git a/src/nissy.h b/src/nissy.h index 0c1d18d..38fe656 100644 --- a/src/nissy.h +++ b/src/nissy.h @@ -42,6 +42,10 @@ for example 'rotation UF' or 'mirrored BL'. #define NISSY_STATUS_STOP 1 #define NISSY_STATUS_PAUSE 2 +/* Possible results of move sequence comparison */ +#define NISSY_COMPARE_MOVES_EQUAL 0 +#define NISSY_COMPARE_MOVES_DIFFERENT 99 + /* The solved cube */ #define NISSY_SOLVED_CUBE "ABCDEFGH=ABCDEFGHIJKL=A" @@ -374,6 +378,24 @@ nissy_countmoves( const char *moves ); +/* +Parameters: + moves1 - The first sequence of moves to compare. + moves2 - The second sequence of moves to compare. + +Return values: + NISSY_ERROR_INVALID_MOVES - One of the given moves sequences is invalid. + NISSY_ERROR_NULL_POINTER - One of the arguments is NULL. + NISSY_COMPARE_MOVES_EQUAL - The two moves sequences are indentical, up + to swapping parallel moves. + NISSY_COMPARE_MOVES_DIFFERENT - The two moves sequences are different. +*/ +long long +nissy_comparemoves( + const char *moves1, + const char *moves2 +); + /* Set a global logger function used by this library. Setting the logger to NULL disables logging. -- cgit v1.3