introduce bitloops

This commit is contained in:
mike
2026-01-14 08:36:31 +01:00
parent 6e2ecae082
commit bd52bd0ef0
5 changed files with 24 additions and 29 deletions

View File

@@ -109,7 +109,7 @@ public record Export() {
} }
} }
//public boolean isLetterSet(int idx) { return isLetter(g[idx]); } //public boolean isLetterSet(int idx) { return isLetter(g[idx]); }
char NOT_CLUE_NOT_LETTER_TO(byte b, char fallback) { return isLetter(b) ? (char) b : fallback; } char NOT_CLUE_NOT_LETTER_TO(byte b, char fallback) { return isLetter(b) ? (char) (64 | b) : fallback; }
String gridToString(Clues clues) { String gridToString(Clues clues) {
var sb = new StringBuilder(); var sb = new StringBuilder();
for (var r = 0; r < R; r++) { for (var r = 0; r < R; r++) {
@@ -119,7 +119,7 @@ public record Export() {
if (clues.isClue(offset)) if (clues.isClue(offset))
sb.append((char) (48 | clues.digitAt(offset))); sb.append((char) (48 | clues.digitAt(offset)));
else else
sb.append((char) grid.letter32At(offset)); sb.append((char) (64 | grid.letter32At(offset)));
} }
} }
return sb.toString(); return sb.toString();
@@ -230,7 +230,7 @@ public record Export() {
for (var p : placed) { for (var p : placed) {
for (var c : p.cells) { for (var c : p.cells) {
if (inBounds(c) && mask.notClue(c)) { if (inBounds(c) && mask.notClue(c)) {
letterAt.put(c, (char) g.letter32At(c)); letterAt.put(c, (char) (64 | g.letter32At(c)));
} }
} }
} }

View File

@@ -259,18 +259,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (int i = 0; i < SIZE; i++) if (digitAt(i) == b.digitAt(i)) same++; for (int i = 0; i < SIZE; i++) if (digitAt(i) == b.digitAt(i)) same++;
return same / SIZED;*/ return same / SIZED;*/
} }
public Grid toGrid() { public Grid toGrid() { return new Grid(new byte[SIZE], lo, hi); }
var grid = new Grid(new byte[SIZE], lo, hi);
for (var l = lo; l != X; l &= l - 1) {
int idx = Long.numberOfTrailingZeros(l);
grid.setLetter(idx, digitAt(idx));
}
for (var h = hi; h != X; h &= h - 1) {
int idx = 64 | Long.numberOfTrailingZeros(h);
grid.setLetter(idx, digitAt(idx));
}
return grid;
}
public void forEachSlot(SlotVisitor visitor) { public void forEachSlot(SlotVisitor visitor) {
for (var l = lo; l != X; l &= l - 1) processSlot(this, visitor, Long.numberOfTrailingZeros(l)); for (var l = lo; l != X; l &= l - 1) processSlot(this, visitor, Long.numberOfTrailingZeros(l));
for (var h = hi; h != X; h &= h - 1) processSlot(this, visitor, 64 | Long.numberOfTrailingZeros(h)); for (var h = hi; h != X; h &= h - 1) processSlot(this, visitor, 64 | Long.numberOfTrailingZeros(h));

View File

@@ -15,6 +15,9 @@ import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static puzzle.MainTest.*;
import static puzzle.MainTest.LETTER_E;
import static puzzle.MainTest.LETTER_T;
import static puzzle.SwedishGenerator.*; import static puzzle.SwedishGenerator.*;
public class ExportFormatTest { public class ExportFormatTest {
@@ -42,10 +45,10 @@ public class ExportFormatTest {
clueMap[key] = SwedishGeneratorTest.TEST; clueMap[key] = SwedishGeneratorTest.TEST;
// Manually fill the grid letters for "TEST" at (0,1), (0,2), (0,3), (0,4) // Manually fill the grid letters for "TEST" at (0,1), (0,2), (0,3), (0,4)
grid.setLetter(Grid.offset(0, 1), (byte) 'T'); grid.setLetter(Grid.offset(0, 1), LETTER_T);
grid.setLetter(Grid.offset(0, 2), (byte) 'E'); grid.setLetter(Grid.offset(0, 2), LETTER_E);
grid.setLetter(Grid.offset(0, 3), (byte) 'S'); grid.setLetter(Grid.offset(0, 3), LETTER_S);
grid.setLetter(Grid.offset(0, 4), (byte) 'T'); grid.setLetter(Grid.offset(0, 4), LETTER_T);
var fillResult = new FillResult(true, new Gridded(grid), clueMap, new FillStats(0, 0, 0, 0)); var fillResult = new FillResult(true, new Gridded(grid), clueMap, new FillStats(0, 0, 0, 0));
var puzzleResult = new PuzzleResult(new Clued(clues), fillResult); var puzzleResult = new PuzzleResult(new Clued(clues), fillResult);

View File

@@ -21,6 +21,9 @@ public class MainTest {
static final byte LETTER_A = (byte) 'A'; static final byte LETTER_A = (byte) 'A';
static final byte LETTER_B = (byte) 'B'; static final byte LETTER_B = (byte) 'B';
static final byte LETTER_Z = (byte) 'Z'; static final byte LETTER_Z = (byte) 'Z';
static final byte LETTER_T = (byte) 'T';
static final byte LETTER_E = (byte) 'E';
static final byte LETTER_S = (byte) 'S';
static final byte CLUE_DOWN = 0; static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1; static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2; static final byte CLUE_UP = 2;

View File

@@ -132,7 +132,7 @@ public class SwedishGeneratorTest {
void testGrid() { void testGrid() {
var grid = createEmpty(); var grid = createEmpty();
grid.setLetter(OFF_0_0, LETTER_A); grid.setLetter(OFF_0_0, LETTER_A);
assertEquals('A', grid.letter32At(OFF_0_0)); assertEquals(LETTER_A, grid.letter32At(OFF_0_0));
} }
@Test @Test
@@ -333,9 +333,9 @@ public class SwedishGeneratorTest {
// 1. Successful placement in empty grid // 1. Successful placement in empty grid
assertTrue(placeWord(grid, s, w1, undoBuffer, 0)); assertTrue(placeWord(grid, s, w1, undoBuffer, 0));
assertEquals('A', grid.letter32At(OFF_0_0)); assertEquals(LETTER_A, grid.letter32At(OFF_0_0));
assertEquals('B', grid.letter32At(OFF_0_1)); assertEquals(LETTER_B, grid.letter32At(OFF_0_1));
assertEquals('C', grid.letter32At(OFF_0_2)); assertEquals(LETTER_C, grid.letter32At(OFF_0_2));
assertEquals(lo, undoBuffer[0]); assertEquals(lo, undoBuffer[0]);
// 2. Successful placement with partial overlap (same characters) // 2. Successful placement with partial overlap (same characters)
@@ -346,9 +346,9 @@ public class SwedishGeneratorTest {
var w2 = ABD; var w2 = ABD;
assertFalse(placeWord(grid, s, w2, undoBuffer, 2)); assertFalse(placeWord(grid, s, w2, undoBuffer, 2));
// Verify grid is unchanged (still "ABC") // Verify grid is unchanged (still "ABC")
assertEquals('A', grid.letter32At(OFF_0_0)); assertEquals(LETTER_A, grid.letter32At(OFF_0_0));
assertEquals('B', grid.letter32At(OFF_0_1)); assertEquals(LETTER_B, grid.letter32At(OFF_0_1));
assertEquals('C', grid.letter32At(OFF_0_2)); assertEquals(LETTER_C, grid.letter32At(OFF_0_2));
// 4. Partial placement then conflict (rollback) // 4. Partial placement then conflict (rollback)
grid = createEmpty(); grid = createEmpty();
@@ -357,7 +357,7 @@ public class SwedishGeneratorTest {
// Verify grid is still empty (except for 'X') // Verify grid is still empty (except for 'X')
assertEquals(DASH, grid.letter32At(OFF_0_0)); assertEquals(DASH, grid.letter32At(OFF_0_0));
assertEquals(DASH, grid.letter32At(OFF_0_1)); assertEquals(DASH, grid.letter32At(OFF_0_1));
assertEquals('X', grid.letter32At(OFF_0_2)); assertEquals(LETTER_X, grid.letter32At(OFF_0_2));
} }
@Test @Test
@@ -372,8 +372,8 @@ public class SwedishGeneratorTest {
var placed = placeWord(grid, s, w, undoBuffer, 0); var placed = placeWord(grid, s, w, undoBuffer, 0);
assertTrue(placed); assertTrue(placed);
assertEquals('A', grid.letter32At(OFF_0_1)); assertEquals(LETTER_A, grid.letter32At(OFF_0_1));
assertEquals('Z', grid.letter32At(OFF_0_2)); assertEquals(LETTER_Z, grid.letter32At(OFF_0_2));
assertEquals(lo, undoBuffer[0]); assertEquals(lo, undoBuffer[0]);
grid.undoPlace(undoBuffer[0], undoBuffer[1]); grid.undoPlace(undoBuffer[0], undoBuffer[1]);