diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-24 23:09:26 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-24 23:09:26 +0100 |
| commit | d45e1595ec1cffeab83ac6602b748250b66bea03 (patch) | |
| tree | 44b35714cdae22026fc5e15648e91684a2fbb154 /src/core/io_moves.h | |
| parent | ce3f1cc0ef9f46d70ab5387b1458e9098b40711d (diff) | |
| download | nissy-core-d45e1595ec1cffeab83ac6602b748250b66bea03.tar.gz nissy-core-d45e1595ec1cffeab83ac6602b748250b66bea03.zip | |
Big cleanup for appendsolution()
With this PR the appendsolution routine is extracted from the h48
solver and the new coordinate solver and made generic. This has
many advantages:
- less repetition (even if the two versions are different enough that
*for now* it was not a big deal)
- smaller h48/solve.h file, which is already a big beast
- easier to test the appendsolution() routine separately
Diffstat (limited to 'src/core/io_moves.h')
| -rw-r--r-- | src/core/io_moves.h | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/core/io_moves.h b/src/core/io_moves.h index fa91e8b..8c56a9f 100644 --- a/src/core/io_moves.h +++ b/src/core/io_moves.h | |||
| @@ -1,6 +1,26 @@ | |||
| 1 | STATIC uint8_t readmove(char); | 1 | STATIC uint8_t readmove(char); |
| 2 | STATIC int64_t readmoves(const char *, size_t n, uint8_t [n]); | ||
| 2 | STATIC uint8_t readmodifier(char); | 3 | STATIC uint8_t readmodifier(char); |
| 3 | STATIC int64_t writemoves(size_t n, uint8_t [n], size_t m, char [m]); | 4 | STATIC int64_t writemoves(size_t n, const uint8_t [n], size_t m, char [m]); |
| 5 | |||
| 6 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | ||
| 7 | RET_ERROR, ARG_ACTION) \ | ||
| 8 | const char *VAR_B; \ | ||
| 9 | uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ | ||
| 10 | for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ | ||
| 11 | while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ | ||
| 12 | VAR_B++; \ | ||
| 13 | if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ | ||
| 14 | break; \ | ||
| 15 | if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) { \ | ||
| 16 | LOG("Error: unknown move '%c'\n", *VAR_B); \ | ||
| 17 | return RET_ERROR; \ | ||
| 18 | } \ | ||
| 19 | if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ | ||
| 20 | VAR_B++; \ | ||
| 21 | ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ | ||
| 22 | ARG_ACTION \ | ||
| 23 | } | ||
| 4 | 24 | ||
| 5 | STATIC uint8_t | 25 | STATIC uint8_t |
| 6 | readmove(char c) | 26 | readmove(char c) |
| @@ -39,9 +59,22 @@ readmodifier(char c) | |||
| 39 | } | 59 | } |
| 40 | 60 | ||
| 41 | STATIC int64_t | 61 | STATIC int64_t |
| 62 | readmoves(const char *buf, size_t n, uint8_t ret[n]) | ||
| 63 | { | ||
| 64 | uint8_t m; | ||
| 65 | uint64_t c; | ||
| 66 | |||
| 67 | FOREACH_READMOVE(buf, m, c, n, NISSY_ERROR_INVALID_MOVES, | ||
| 68 | ret[c] = m; | ||
| 69 | ) | ||
| 70 | |||
| 71 | return (int64_t)c; | ||
| 72 | } | ||
| 73 | |||
| 74 | STATIC int64_t | ||
| 42 | writemoves( | 75 | writemoves( |
| 43 | size_t nmoves, | 76 | size_t nmoves, |
| 44 | uint8_t m[nmoves], | 77 | const uint8_t m[nmoves], |
| 45 | size_t buf_size, | 78 | size_t buf_size, |
| 46 | char buf[buf_size] | 79 | char buf[buf_size] |
| 47 | ) | 80 | ) |
| @@ -69,7 +102,9 @@ writemoves( | |||
| 69 | *b = ' '; | 102 | *b = ' '; |
| 70 | } | 103 | } |
| 71 | 104 | ||
| 72 | if (b != buf) | 105 | if (b == buf) |
| 106 | written = 1; /* Nothing written, only NULL-terminator */ | ||
| 107 | else | ||
| 73 | b--; /* Remove last space */ | 108 | b--; /* Remove last space */ |
| 74 | *b = '\0'; | 109 | *b = '\0'; |
| 75 | 110 | ||
