introduce bitloops

This commit is contained in:
mike
2026-01-14 08:24:53 +01:00
parent 352e8c79ca
commit aafa5d4d43
2 changed files with 23 additions and 25 deletions

View File

@@ -286,19 +286,18 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
} }
} }
static record Grid(byte[] g, long lo, long hi) { @AllArgsConstructor
static class Grid {
public Grid(byte[] g) { this(g, 0, 0); } final byte[] g;
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); } public long lo, hi;
public static int r(int offset) { return offset & 7; } public static int r(int offset) { return offset & 7; }
public static int c(int offset) { return offset >>> 3; } public static int c(int offset) { return offset >>> 3; }
static int offset(int r, int c) { return r | (c << 3); } static int offset(int r, int c) { return r | (c << 3); }
/// the pos will never target a clue /// the pos will never target a clue
public byte letter32At(int pos) { return g[pos]; } public byte letter32At(int pos) { return g[pos]; }
void setLetter(int idx, byte ch) { g[idx] = ch; } void setLetter(int idx, byte ch) { g[idx] = ch; }
void clearletter(int idx) { g[idx] = DASH; } void clearletter(int idx) { g[idx] = DASH; }
boolean isClue(int index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) != X : ((hi >>> (index & 63)) & 1L) != X; }
boolean notClue(int index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) == X : ((hi >>> (index & 63)) & 1L) == X; }
void undoPlace(long maskLo, long maskHi) { void undoPlace(long maskLo, long maskHi) {
for (long b = maskLo; b != 0; b &= b - 1) clearletter(Long.numberOfTrailingZeros(b)); for (long b = maskLo; b != 0; b &= b - 1) clearletter(Long.numberOfTrailingZeros(b));

View File

@@ -102,27 +102,27 @@ public class MainTest {
grid.setLetter(OFF_2_3, LETTER_Z); grid.setLetter(OFF_2_3, LETTER_Z);
Assertions.assertEquals(LETTER_A, grid.letter32At(OFF_0_0)); Assertions.assertEquals(LETTER_A, grid.letter32At(OFF_0_0));
Assertions.assertEquals(CLUE_UP, grid.letter32At(OFF_1_2)); Assertions.assertEquals(CLUE_UP, clues.digitAt(OFF_1_2));
Assertions.assertEquals(LETTER_Z, grid.letter32At(OFF_2_3)); Assertions.assertEquals(LETTER_Z, grid.letter32At(OFF_2_3));
Assertions.assertEquals(DASH, grid.letter32At(OFF_1_1)); Assertions.assertEquals(DASH, grid.letter32At(OFF_1_1));
// Test isLetterAt // Test isLetterAt
Assertions.assertTrue(grid.notClue(OFF_0_0)); Assertions.assertTrue(clues.notClue(OFF_0_0));
Assertions.assertFalse(grid.notClue(OFF_1_2)); Assertions.assertFalse(clues.notClue(OFF_1_2));
Assertions.assertTrue(grid.notClue(OFF_2_3)); Assertions.assertTrue(clues.notClue(OFF_2_3));
Assertions.assertFalse(grid.isClue(OFF_1_1)); Assertions.assertFalse(clues.isClue(OFF_1_1));
// Test isDigitAt // Test isDigitAt
Assertions.assertFalse(grid.isClue(0)); Assertions.assertFalse(clues.isClue(0));
Assertions.assertTrue(grid.isClue(OFF_1_2)); Assertions.assertTrue(clues.isClue(OFF_1_2));
Assertions.assertEquals(CLUE_UP, clues.digitAt(OFF_1_2)); Assertions.assertEquals(CLUE_UP, clues.digitAt(OFF_1_2));
Assertions.assertFalse(grid.isClue(OFF_2_3)); Assertions.assertFalse(clues.isClue(OFF_2_3));
Assertions.assertFalse(grid.isClue(OFF_1_1)); Assertions.assertFalse(clues.isClue(OFF_1_1));
// Test isLettercell // Test isLettercell
Assertions.assertTrue(grid.notClue(OFF_0_0)); // 'A' is letter Assertions.assertTrue(clues.notClue(OFF_0_0)); // 'A' is letter
Assertions.assertTrue(grid.isClue(OFF_1_2)); // digit Assertions.assertTrue(clues.isClue(OFF_1_2)); // digit
Assertions.assertTrue(grid.notClue(OFF_1_1)); // '#' is lettercell Assertions.assertTrue(clues.notClue(OFF_1_1)); // '#' is lettercell
} }
@Test @Test
public void testCluesDeepCopy() { public void testCluesDeepCopy() {
@@ -144,8 +144,7 @@ public class MainTest {
val idx = OFF_1_1; val idx = OFF_1_1;
var clues = Clues.createEmpty(); var clues = Clues.createEmpty();
clues.setClue(idx, CLUE_LEFT); clues.setClue(idx, CLUE_LEFT);
var grid = clues.toGrid(); Assertions.assertTrue(clues.isClue(idx));
Assertions.assertTrue(grid.isClue(idx));
} }
@Test @Test
void testMaskerCreation() { void testMaskerCreation() {
@@ -179,8 +178,8 @@ public class MainTest {
Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)"); Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)");
Assertions.assertEquals(18, filled.wordCount(), "Number of assigned words changed"); Assertions.assertEquals(18, filled.wordCount(), "Number of assigned words changed");
Assertions.assertEquals("SLEDE", Lemma.asWord(filled.clueMap()[282])); Assertions.assertEquals("SLEDE", Lemma.asWord(filled.clueMap()[282]));
Assertions.assertEquals(74732156493031040L, filled.grid().grid().lo()); Assertions.assertEquals(74732156493031040L, filled.grid().grid().lo);
Assertions.assertEquals(193L, filled.grid().grid().hi()); Assertions.assertEquals(193L, filled.grid().grid().hi);
var aa = new PuzzleResult(new Clued(mask), filled).exportFormatFromFilled(1, new Rewards(1, 1, 1)); var aa = new PuzzleResult(new Clued(mask), filled).exportFormatFromFilled(1, new Rewards(1, 1, 1));