introduce bitloops
This commit is contained in:
@@ -46,7 +46,7 @@ public record Export() {
|
|||||||
for (var r = 0; r < R; r++) {
|
for (var r = 0; r < R; r++) {
|
||||||
if (r > 0) sb.append('\n');
|
if (r > 0) sb.append('\n');
|
||||||
for (var c = 0; c < C; c++) {
|
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();
|
return sb.toString();
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import lombok.Getter;
|
|||||||
import lombok.val;
|
import lombok.val;
|
||||||
import precomp.Neighbors9x8;
|
import precomp.Neighbors9x8;
|
||||||
import precomp.Neighbors9x8.nbrs_16;
|
import precomp.Neighbors9x8.nbrs_16;
|
||||||
import precomp.Neighbors9x8.nbrs_8;
|
|
||||||
import precomp.Neighbors9x8.rci;
|
import precomp.Neighbors9x8.rci;
|
||||||
import puzzle.Export.Bit;
|
import puzzle.Export.Bit;
|
||||||
import puzzle.Export.Bit1029;
|
import puzzle.Export.Bit1029;
|
||||||
@@ -179,12 +178,13 @@ public record SwedishGenerator(Rng rng) {
|
|||||||
static int offset(int r, int c) { return r | (c << 3); }
|
static int offset(int r, int c) { return r | (c << 3); }
|
||||||
Grid deepCopyGrid() { return new Grid(g.clone(), lo, hi); }
|
Grid deepCopyGrid() { return new Grid(g.clone(), lo, hi); }
|
||||||
public byte byteAt(int pos) { return g[pos]; }
|
public byte byteAt(int pos) { return g[pos]; }
|
||||||
void setByteAt(int idx, byte ch) { g[idx] = ch; }
|
|
||||||
void setClue(int idx, byte ch) {
|
void setClue(int idx, byte ch) {
|
||||||
g[idx] = ch;
|
g[idx] = ch;
|
||||||
if ((idx & 64) == 0) lo |= (1L << idx);
|
if ((idx & 64) == 0) lo |= (1L << idx);
|
||||||
else hi |= (1L << (idx & 63));
|
else hi |= (1L << (idx & 63));
|
||||||
}
|
}
|
||||||
|
void setLetter(int idx, byte ch) { g[idx] = ch; }
|
||||||
void clearletter(int idx) { g[idx] = DASH; }
|
void clearletter(int idx) { g[idx] = DASH; }
|
||||||
void clearClue(int idx) {
|
void clearClue(int idx) {
|
||||||
g[idx] = DASH;
|
g[idx] = DASH;
|
||||||
@@ -192,7 +192,6 @@ public record SwedishGenerator(Rng rng) {
|
|||||||
else hi &= ~(1L << (idx & 63));
|
else hi &= ~(1L << (idx & 63));
|
||||||
}
|
}
|
||||||
static boolean isDigit(byte b) { return (b & B48) == B48; }
|
static boolean isDigit(byte b) { return (b & B48) == B48; }
|
||||||
boolean isDigitAt(int index) { return isDigit(g[index]); }
|
|
||||||
boolean isClue(long index) {
|
boolean isClue(long index) {
|
||||||
if ((index & 64) == 0) return ((lo >> index) & 1L) != X;
|
if ((index & 64) == 0) return ((lo >> index) & 1L) != X;
|
||||||
return ((hi >> (index & 63)) & 1L) != X;
|
return ((hi >> (index & 63)) & 1L) != X;
|
||||||
@@ -655,7 +654,7 @@ public record SwedishGenerator(Rng rng) {
|
|||||||
ch = Lemma.byteAt(w, i);
|
ch = Lemma.byteAt(w, i);
|
||||||
if (cur == DASH) {
|
if (cur == DASH) {
|
||||||
mask |= (1 << i);
|
mask |= (1 << i);
|
||||||
grid.setByteAt(idx, ch);
|
grid.setLetter(idx, ch);
|
||||||
} else if (cur != ch) {
|
} else if (cur != ch) {
|
||||||
for (var j = 0; j < i; j++) {
|
for (var j = 0; j < i; j++) {
|
||||||
if ((mask & (1 << j)) != 0) {
|
if ((mask & (1 << j)) != 0) {
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ public class ExportFormatTest {
|
|||||||
clueMap.put(key, lemma.word());
|
clueMap.put(key, lemma.word());
|
||||||
|
|
||||||
// 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.setByteAt(Grid.offset(0, 1), (byte) 'T');
|
grid.setLetter(Grid.offset(0, 1), (byte) 'T');
|
||||||
grid.setByteAt(Grid.offset(0, 2), (byte) 'E');
|
grid.setLetter(Grid.offset(0, 2), (byte) 'E');
|
||||||
grid.setByteAt(Grid.offset(0, 3), (byte) 'S');
|
grid.setLetter(Grid.offset(0, 3), (byte) 'S');
|
||||||
grid.setByteAt(Grid.offset(0, 4), (byte) 'T');
|
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
|
// 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');
|
grid.setClue(Grid.offset(0, 5), (byte) '1');
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ public class MainTest {
|
|||||||
// Set up digits on the grid to create slots.
|
// Set up digits on the grid to create slots.
|
||||||
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
|
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
|
||||||
grid.setClue(0, (byte) '2');
|
grid.setClue(0, (byte) '2');
|
||||||
grid.setByteAt(Grid.offset(0, 1), (byte) 'A');
|
grid.setLetter(Grid.offset(0, 1), (byte) 'A');
|
||||||
grid.setByteAt(Grid.offset(0, 2), (byte) 'B');
|
grid.setLetter(Grid.offset(0, 2), (byte) 'B');
|
||||||
|
|
||||||
var slots = extractSlots(grid);
|
var slots = extractSlots(grid);
|
||||||
assertEquals(1, slots.size());
|
assertEquals(1, slots.size());
|
||||||
@@ -77,9 +77,9 @@ public class MainTest {
|
|||||||
var grid = Grid.createEmpty();
|
var grid = Grid.createEmpty();
|
||||||
|
|
||||||
// Test set/get
|
// 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.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) 'A', grid.byteAt(Grid.offset(0, 0)));
|
||||||
Assertions.assertEquals((byte) '5', grid.byteAt(Grid.offset(1, 2)));
|
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)));
|
Assertions.assertFalse(grid.isLetterSet(Grid.offset(1, 1)));
|
||||||
|
|
||||||
// Test isDigitAt
|
// Test isDigitAt
|
||||||
Assertions.assertFalse(grid.isDigitAt(0));
|
Assertions.assertFalse(grid.isClue(0));
|
||||||
Assertions.assertTrue(grid.isDigitAt(Grid.offset(1, 2)));
|
Assertions.assertTrue(grid.isClue(Grid.offset(1, 2)));
|
||||||
Assertions.assertEquals(5, grid.digitAt(Grid.offset(1, 2)));
|
Assertions.assertEquals(5, grid.digitAt(Grid.offset(1, 2)));
|
||||||
Assertions.assertFalse(grid.isDigitAt(Grid.offset(2, 3)));
|
Assertions.assertFalse(grid.isClue(Grid.offset(2, 3)));
|
||||||
Assertions.assertFalse(grid.isDigitAt(Grid.offset(1, 1)));
|
Assertions.assertFalse(grid.isClue(Grid.offset(1, 1)));
|
||||||
|
|
||||||
// Test isLettercell
|
// Test isLettercell
|
||||||
Assertions.assertTrue(grid.isLetterAt(Grid.offset(0, 0))); // 'A' is letter
|
Assertions.assertTrue(grid.notClue(Grid.offset(0, 0))); // 'A' is letter
|
||||||
Assertions.assertFalse(grid.isLetterAt(Grid.offset(1, 2))); // '5' is digit
|
Assertions.assertTrue(grid.isClue(Grid.offset(1, 2))); // '5' is digit
|
||||||
Assertions.assertTrue(grid.isLetterAt(Grid.offset(1, 1))); // '#' is lettercell
|
Assertions.assertTrue(grid.notClue(Grid.offset(1, 1))); // '#' is lettercell
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGridDeepCopy() {
|
public void testGridDeepCopy() {
|
||||||
var grid = Grid.createEmpty();
|
var grid = Grid.createEmpty();
|
||||||
grid.setByteAt(Grid.offset(0, 0), (byte) 'A');
|
grid.setLetter(Grid.offset(0, 0), (byte) 'A');
|
||||||
grid.setByteAt(Grid.offset(0, 1), (byte) 'B');
|
grid.setLetter(Grid.offset(0, 1), (byte) 'B');
|
||||||
grid.setByteAt(Grid.offset(1, 0), (byte) 'C');
|
grid.setLetter(Grid.offset(1, 0), (byte) 'C');
|
||||||
grid.setByteAt(Grid.offset(1, 1), (byte) 'D');
|
grid.setLetter(Grid.offset(1, 1), (byte) 'D');
|
||||||
|
|
||||||
var copy = grid.deepCopyGrid();
|
var copy = grid.deepCopyGrid();
|
||||||
Assertions.assertEquals((byte) 'A', copy.byteAt(0));
|
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) 'X', copy.byteAt(0));
|
||||||
Assertions.assertEquals((byte) 'A', grid.byteAt(0)); // Original should be unchanged
|
Assertions.assertEquals((byte) 'A', grid.byteAt(0)); // Original should be unchanged
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,7 @@ public class MainTest {
|
|||||||
var grid = Grid.createEmpty();
|
var grid = Grid.createEmpty();
|
||||||
val idx = Grid.offset(1, 1);
|
val idx = Grid.offset(1, 1);
|
||||||
grid.setClue(idx, (byte) '1');
|
grid.setClue(idx, (byte) '1');
|
||||||
Assertions.assertTrue(grid.isDigitAt(idx));
|
Assertions.assertTrue(grid.isClue(idx));
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testAttempt() {
|
public void testAttempt() {
|
||||||
|
|||||||
@@ -68,21 +68,21 @@ public class SwedishGeneratorTest {
|
|||||||
@Test
|
@Test
|
||||||
void testGrid() {
|
void testGrid() {
|
||||||
var grid = Grid.createEmpty();
|
var grid = Grid.createEmpty();
|
||||||
grid.setByteAt(0, (byte) 'A');
|
grid.setLetter(0, (byte) 'A');
|
||||||
grid.setClue(Grid.offset(0, 1), (byte) '1');
|
grid.setClue(Grid.offset(0, 1), (byte) '1');
|
||||||
|
|
||||||
assertEquals('A', grid.byteAt(0));
|
assertEquals('A', grid.byteAt(0));
|
||||||
assertEquals(1, grid.digitAt(Grid.offset(0, 1)));
|
assertEquals(1, grid.digitAt(Grid.offset(0, 1)));
|
||||||
assertTrue(grid.isLetterAt(0));
|
assertTrue(grid.isLetterAt(0));
|
||||||
assertFalse(grid.isDigitAt(0));
|
assertFalse(grid.isClue(0));
|
||||||
assertTrue(grid.isDigitAt(Grid.offset(0, 1)));
|
assertTrue(grid.isClue(Grid.offset(0, 1)));
|
||||||
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
|
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
|
||||||
assertTrue(grid.isLetterAt(0));
|
assertTrue(grid.isLetterAt(0));
|
||||||
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
|
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
|
||||||
|
|
||||||
var copy = grid.deepCopyGrid();
|
var copy = grid.deepCopyGrid();
|
||||||
assertEquals('A', copy.byteAt(0));
|
assertEquals('A', copy.byteAt(0));
|
||||||
copy.setByteAt(0, (byte) 'B');
|
copy.setLetter(0, (byte) 'B');
|
||||||
assertEquals('B', copy.byteAt(0));
|
assertEquals('B', copy.byteAt(0));
|
||||||
assertEquals('A', grid.byteAt(0));
|
assertEquals('A', grid.byteAt(0));
|
||||||
}
|
}
|
||||||
@@ -333,7 +333,7 @@ public class SwedishGeneratorTest {
|
|||||||
|
|
||||||
// 4. Partial placement then conflict (rollback)
|
// 4. Partial placement then conflict (rollback)
|
||||||
grid = Grid.createEmpty();
|
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));
|
assertFalse(placeWord(grid, s, w1, undoBuffer, 3));
|
||||||
// Verify grid is still empty (except for 'X')
|
// Verify grid is still empty (except for 'X')
|
||||||
assertEquals(DASH, grid.byteAt(Grid.offset(0, 0)));
|
assertEquals(DASH, grid.byteAt(Grid.offset(0, 0)));
|
||||||
|
|||||||
Reference in New Issue
Block a user