introduce bitloops

This commit is contained in:
mike
2026-01-12 08:43:13 +01:00
parent 9c1be77d76
commit fdd1c76bae
5 changed files with 38 additions and 39 deletions

View File

@@ -46,7 +46,7 @@ public record Export() {
for (var r = 0; r < R; r++) {
if (r > 0) sb.append('\n');
for (var c = 0; c < C; c++) {
sb.append(grid.isDigitAt(Grid.offset(r, c)) ? ' ' : (char) grid.byteAt(Grid.offset(r, c)));
sb.append(grid.isLetterSet(Grid.offset(r, c)) ? (char) grid.byteAt(Grid.offset(r, c)) : ' ');
}
}
return sb.toString();

View File

@@ -4,7 +4,6 @@ import lombok.Getter;
import lombok.val;
import precomp.Neighbors9x8;
import precomp.Neighbors9x8.nbrs_16;
import precomp.Neighbors9x8.nbrs_8;
import precomp.Neighbors9x8.rci;
import puzzle.Export.Bit;
import puzzle.Export.Bit1029;
@@ -172,27 +171,27 @@ public record SwedishGenerator(Rng rng) {
this.lo = lo;
this.hi = hi;
}
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); }
int digitAt(int index) { return g[index] & 7; }
public static int r(int offset) { return offset & 7; }
public static int c(int offset) { return offset >>> 3; }
static int offset(int r, int c) { return r | (c << 3); }
Grid deepCopyGrid() { return new Grid(g.clone(), lo, hi); }
public byte byteAt(int pos) { return g[pos]; }
void setByteAt(int idx, byte ch) { g[idx] = ch; }
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); }
int digitAt(int index) { return g[index] & 7; }
public static int r(int offset) { return offset & 7; }
public static int c(int offset) { return offset >>> 3; }
static int offset(int r, int c) { return r | (c << 3); }
Grid deepCopyGrid() { return new Grid(g.clone(), lo, hi); }
public byte byteAt(int pos) { return g[pos]; }
void setClue(int idx, byte ch) {
g[idx] = ch;
if ((idx & 64) == 0) lo |= (1L << idx);
else hi |= (1L << (idx & 63));
}
void clearletter(int idx) { g[idx] = DASH; }
void setLetter(int idx, byte ch) { g[idx] = ch; }
void clearletter(int idx) { g[idx] = DASH; }
void clearClue(int idx) {
g[idx] = DASH;
if ((idx & 64) == 0) lo &= ~(1L << idx);
else hi &= ~(1L << (idx & 63));
}
static boolean isDigit(byte b) { return (b & B48) == B48; }
boolean isDigitAt(int index) { return isDigit(g[index]); }
boolean isClue(long index) {
if ((index & 64) == 0) return ((lo >> index) & 1L) != X;
return ((hi >> (index & 63)) & 1L) != X;
@@ -655,7 +654,7 @@ public record SwedishGenerator(Rng rng) {
ch = Lemma.byteAt(w, i);
if (cur == DASH) {
mask |= (1 << i);
grid.setByteAt(idx, ch);
grid.setLetter(idx, ch);
} else if (cur != ch) {
for (var j = 0; j < i; j++) {
if ((mask & (1 << j)) != 0) {

View File

@@ -35,10 +35,10 @@ public class ExportFormatTest {
clueMap.put(key, lemma.word());
// Manually fill the grid letters for "TEST" at (0,1), (0,2), (0,3), (0,4)
grid.setByteAt(Grid.offset(0, 1), (byte) 'T');
grid.setByteAt(Grid.offset(0, 2), (byte) 'E');
grid.setByteAt(Grid.offset(0, 3), (byte) 'S');
grid.setByteAt(Grid.offset(0, 4), (byte) 'T');
grid.setLetter(Grid.offset(0, 1), (byte) 'T');
grid.setLetter(Grid.offset(0, 2), (byte) 'E');
grid.setLetter(Grid.offset(0, 3), (byte) 'S');
grid.setLetter(Grid.offset(0, 4), (byte) 'T');
// Terminate thGrid.offset(e slot at) (0,5) with another digit to avoid it extending to MAX_WORD_LENGTH
grid.setClue(Grid.offset(0, 5), (byte) '1');

View File

@@ -22,8 +22,8 @@ public class MainTest {
// Set up digits on the grid to create slots.
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
grid.setClue(0, (byte) '2');
grid.setByteAt(Grid.offset(0, 1), (byte) 'A');
grid.setByteAt(Grid.offset(0, 2), (byte) 'B');
grid.setLetter(Grid.offset(0, 1), (byte) 'A');
grid.setLetter(Grid.offset(0, 2), (byte) 'B');
var slots = extractSlots(grid);
assertEquals(1, slots.size());
@@ -77,9 +77,9 @@ public class MainTest {
var grid = Grid.createEmpty();
// Test set/get
grid.setByteAt(Grid.offset(0, 0), (byte) 'A');
grid.setLetter(Grid.offset(0, 0), (byte) 'A');
grid.setClue(Grid.offset(1, 2), (byte) '5');
grid.setByteAt(Grid.offset(2, 3), (byte) 'Z');
grid.setLetter(Grid.offset(2, 3), (byte) 'Z');
Assertions.assertEquals((byte) 'A', grid.byteAt(Grid.offset(0, 0)));
Assertions.assertEquals((byte) '5', grid.byteAt(Grid.offset(1, 2)));
@@ -93,30 +93,30 @@ public class MainTest {
Assertions.assertFalse(grid.isLetterSet(Grid.offset(1, 1)));
// Test isDigitAt
Assertions.assertFalse(grid.isDigitAt(0));
Assertions.assertTrue(grid.isDigitAt(Grid.offset(1, 2)));
Assertions.assertFalse(grid.isClue(0));
Assertions.assertTrue(grid.isClue(Grid.offset(1, 2)));
Assertions.assertEquals(5, grid.digitAt(Grid.offset(1, 2)));
Assertions.assertFalse(grid.isDigitAt(Grid.offset(2, 3)));
Assertions.assertFalse(grid.isDigitAt(Grid.offset(1, 1)));
Assertions.assertFalse(grid.isClue(Grid.offset(2, 3)));
Assertions.assertFalse(grid.isClue(Grid.offset(1, 1)));
// Test isLettercell
Assertions.assertTrue(grid.isLetterAt(Grid.offset(0, 0))); // 'A' is letter
Assertions.assertFalse(grid.isLetterAt(Grid.offset(1, 2))); // '5' is digit
Assertions.assertTrue(grid.isLetterAt(Grid.offset(1, 1))); // '#' is lettercell
Assertions.assertTrue(grid.notClue(Grid.offset(0, 0))); // 'A' is letter
Assertions.assertTrue(grid.isClue(Grid.offset(1, 2))); // '5' is digit
Assertions.assertTrue(grid.notClue(Grid.offset(1, 1))); // '#' is lettercell
}
@Test
public void testGridDeepCopy() {
var grid = Grid.createEmpty();
grid.setByteAt(Grid.offset(0, 0), (byte) 'A');
grid.setByteAt(Grid.offset(0, 1), (byte) 'B');
grid.setByteAt(Grid.offset(1, 0), (byte) 'C');
grid.setByteAt(Grid.offset(1, 1), (byte) 'D');
grid.setLetter(Grid.offset(0, 0), (byte) 'A');
grid.setLetter(Grid.offset(0, 1), (byte) 'B');
grid.setLetter(Grid.offset(1, 0), (byte) 'C');
grid.setLetter(Grid.offset(1, 1), (byte) 'D');
var copy = grid.deepCopyGrid();
Assertions.assertEquals((byte) 'A', copy.byteAt(0));
copy.setByteAt(0, (byte) 'X');
copy.setLetter(0, (byte) 'X');
Assertions.assertEquals((byte) 'X', copy.byteAt(0));
Assertions.assertEquals((byte) 'A', grid.byteAt(0)); // Original should be unchanged
}
@@ -126,7 +126,7 @@ public class MainTest {
var grid = Grid.createEmpty();
val idx = Grid.offset(1, 1);
grid.setClue(idx, (byte) '1');
Assertions.assertTrue(grid.isDigitAt(idx));
Assertions.assertTrue(grid.isClue(idx));
}
@Test
public void testAttempt() {

View File

@@ -68,21 +68,21 @@ public class SwedishGeneratorTest {
@Test
void testGrid() {
var grid = Grid.createEmpty();
grid.setByteAt(0, (byte) 'A');
grid.setLetter(0, (byte) 'A');
grid.setClue(Grid.offset(0, 1), (byte) '1');
assertEquals('A', grid.byteAt(0));
assertEquals(1, grid.digitAt(Grid.offset(0, 1)));
assertTrue(grid.isLetterAt(0));
assertFalse(grid.isDigitAt(0));
assertTrue(grid.isDigitAt(Grid.offset(0, 1)));
assertFalse(grid.isClue(0));
assertTrue(grid.isClue(Grid.offset(0, 1)));
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
assertTrue(grid.isLetterAt(0));
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
var copy = grid.deepCopyGrid();
assertEquals('A', copy.byteAt(0));
copy.setByteAt(0, (byte) 'B');
copy.setLetter(0, (byte) 'B');
assertEquals('B', copy.byteAt(0));
assertEquals('A', grid.byteAt(0));
}
@@ -333,7 +333,7 @@ public class SwedishGeneratorTest {
// 4. Partial placement then conflict (rollback)
grid = Grid.createEmpty();
grid.setByteAt(Grid.offset(0, 2), (byte) 'X'); // Conflict at the end
grid.setLetter(Grid.offset(0, 2), (byte) 'X'); // Conflict at the end
assertFalse(placeWord(grid, s, w1, undoBuffer, 3));
// Verify grid is still empty (except for 'X')
assertEquals(DASH, grid.byteAt(Grid.offset(0, 0)));