introduce bitloops
This commit is contained in:
@@ -8,6 +8,7 @@ import puzzle.Export.Dicts;
|
||||
import puzzle.Export.Gridded;
|
||||
import puzzle.Export.IntListDTO;
|
||||
import puzzle.Export.LetterVisit.LetterAt;
|
||||
import puzzle.Masker.Clues;
|
||||
import puzzle.Masker.Slot;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -27,74 +28,81 @@ public class SwedishGeneratorTest {
|
||||
public static Context get() { return CTX.get(); }
|
||||
}
|
||||
|
||||
static final long TEST = Lemma.from("TEST");
|
||||
static final long TEST = Lemma.from("TEST");
|
||||
static final long IN = Lemma.from("IN");
|
||||
static final long INER = Lemma.from("INER");
|
||||
static final long INEREN = Lemma.from("INEREN");
|
||||
static final long INERENA = Lemma.from("INERENA");
|
||||
static final long INERENAE = Lemma.from("INERENAE");
|
||||
static final long APPLE = Lemma.from("APPLE");
|
||||
static final long EXE = Lemma.from("AXE");
|
||||
static final long ABC = Lemma.from("ABC");
|
||||
static final long ABD = Lemma.from("ABD");
|
||||
static final long AZ = Lemma.from("AZ");
|
||||
static final long AB = Lemma.from("AB");
|
||||
static final long[] WORDS = new long[]{
|
||||
Lemma.from("AT"),
|
||||
Lemma.from("CAT"),
|
||||
Lemma.from("DOGS"),
|
||||
Lemma.from("APPLE"),
|
||||
APPLE,
|
||||
Lemma.from("APPLY"),
|
||||
Lemma.from("BANAN"),
|
||||
Lemma.from("BANANA"),
|
||||
Lemma.from("BANANAS"),
|
||||
Lemma.from("BANANASS") // length 8
|
||||
};
|
||||
static final long l2a = Lemma.from("IN");
|
||||
static final long l4a = Lemma.from("INER");
|
||||
static final long l6a = Lemma.from("INEREN");
|
||||
static final long l7a = Lemma.from("INERENA");
|
||||
static final long l8a = Lemma.from("INERENAE");
|
||||
static final long l1 = Lemma.from("APPLE");
|
||||
static final long l2 = Lemma.from("AXE");
|
||||
|
||||
static final long[] WORDS2 = new long[]{ Lemma.from("IN"),
|
||||
Lemma.from("APPLE"),
|
||||
Lemma.from("APPLY"),
|
||||
Lemma.from("BANAN"),
|
||||
Lemma.from("INE"),
|
||||
Lemma.from("INER"),
|
||||
Lemma.from("INEREN"),
|
||||
Lemma.from("INERENA"),
|
||||
Lemma.from("INERENAE") };
|
||||
static final long ABC = Lemma.from("ABC");
|
||||
static final long ABD = Lemma.from("ABD");
|
||||
static final long AZ = Lemma.from("AZ");
|
||||
static final long AB = Lemma.from("AB");
|
||||
static final byte LETTER_A = ((byte) 'A') & 31;
|
||||
static final byte LETTER_B = ((byte) 'B') & 31;
|
||||
static final byte LETTER_C = ((byte) 'C') & 31;
|
||||
static final byte LETTER_X = ((byte) 'X') & 31;
|
||||
static final byte LETTER_Z = ((byte) 'Z') & 31;
|
||||
static final byte CLUE_DOWN = 0;
|
||||
static final byte CLUE_RIGHT = 1;
|
||||
static final byte CLUE_UP = 2;
|
||||
static final byte CLUE_LEFT = 3;
|
||||
static final long[] WORDS2 = new long[]{ IN,
|
||||
APPLE,
|
||||
Lemma.from("APPLY"),
|
||||
Lemma.from("BANAN"),
|
||||
Lemma.from("INE"),
|
||||
INER,
|
||||
INEREN,
|
||||
INERENA,
|
||||
INERENAE };
|
||||
|
||||
static final int OFF_1_0 = Grid.offset(1, 0);
|
||||
static final int OFF_1_1 = Grid.offset(1, 1);
|
||||
static final int OFF_1_2 = Grid.offset(1, 2);
|
||||
static final int OFF_1_3 = Grid.offset(1, 3);
|
||||
static final int OFF_1_4 = Grid.offset(1, 4);
|
||||
static final int OFF_1_5 = Grid.offset(1, 5);
|
||||
static final int OFF_2_1 = Grid.offset(2, 1);
|
||||
static final int OFF_2_3 = Grid.offset(2, 3);
|
||||
static final int OFF_2_2 = Grid.offset(2, 2);
|
||||
static final int OFF_2_4 = Grid.offset(2, 4);
|
||||
static final int OFF_0_0 = Grid.offset(0, 0);
|
||||
static final int OFF_0_4 = Grid.offset(0, 4);
|
||||
static final int OFF_0_5 = Grid.offset(0, 5);
|
||||
static final int OFF_0_1 = Grid.offset(0, 1);
|
||||
static final int OFF_0_2 = Grid.offset(0, 2);
|
||||
static final int OFF_0_3 = Grid.offset(0, 3);
|
||||
static final int OFF_2_0 = Grid.offset(2, 0);
|
||||
static final int OFF_2_5 = Grid.offset(2, 5);
|
||||
static final int OFF_3_5 = Grid.offset(3, 5);
|
||||
static final int OFF_4_5 = Grid.offset(4, 5);
|
||||
static final int OFF_3_0 = Grid.offset(3, 0);
|
||||
static final int OFF_3_1 = Grid.offset(3, 1);
|
||||
static final int OFF_3_2 = Grid.offset(3, 2);
|
||||
static final int OFF_3_3 = Grid.offset(3, 3);
|
||||
static final int OFF_3_4 = Grid.offset(3, 4);
|
||||
static final byte LETTER_A = ((byte) 'A') & 31;
|
||||
static final byte LETTER_P = ((byte) 'P') & 31;
|
||||
static final byte LETTER_L = ((byte) 'L') & 31;
|
||||
static final byte LETTER_B = ((byte) 'B') & 31;
|
||||
static final byte LETTER_C = ((byte) 'C') & 31;
|
||||
static final byte LETTER_E = ((byte) 'E') & 31;
|
||||
static final byte LETTER_I = ((byte) 'I') & 31;
|
||||
static final byte LETTER_N = ((byte) 'N') & 31;
|
||||
static final byte LETTER_X = ((byte) 'X') & 31;
|
||||
static final byte LETTER_R = ((byte) 'R') & 31;
|
||||
static final byte LETTER_Z = ((byte) 'Z') & 31;
|
||||
static final byte CLUE_DOWN = 0;
|
||||
static final byte CLUE_RIGHT = 1;
|
||||
static final byte CLUE_UP = 2;
|
||||
static final byte CLUE_LEFT = 3;
|
||||
|
||||
static final int OFF_1_0 = Masker.offset(1, 0);
|
||||
static final int OFF_1_1 = Masker.offset(1, 1);
|
||||
static final int OFF_1_2 = Masker.offset(1, 2);
|
||||
static final int OFF_1_3 = Masker.offset(1, 3);
|
||||
static final int OFF_1_4 = Masker.offset(1, 4);
|
||||
static final int OFF_1_5 = Masker.offset(1, 5);
|
||||
static final int OFF_2_1 = Masker.offset(2, 1);
|
||||
static final int OFF_2_3 = Masker.offset(2, 3);
|
||||
static final int OFF_2_2 = Masker.offset(2, 2);
|
||||
static final int OFF_2_4 = Masker.offset(2, 4);
|
||||
static final int OFF_0_0 = Masker.offset(0, 0);
|
||||
static final int OFF_0_4 = Masker.offset(0, 4);
|
||||
static final int OFF_0_5 = Masker.offset(0, 5);
|
||||
static final int OFF_0_1 = Masker.offset(0, 1);
|
||||
static final int OFF_0_2 = Masker.offset(0, 2);
|
||||
static final int OFF_0_3 = Masker.offset(0, 3);
|
||||
static final int OFF_2_0 = Masker.offset(2, 0);
|
||||
static final int OFF_2_5 = Masker.offset(2, 5);
|
||||
static final int OFF_3_5 = Masker.offset(3, 5);
|
||||
static final int OFF_4_5 = Masker.offset(4, 5);
|
||||
static final int OFF_3_0 = Masker.offset(3, 0);
|
||||
static final int OFF_3_1 = Masker.offset(3, 1);
|
||||
static final int OFF_3_2 = Masker.offset(3, 2);
|
||||
static final int OFF_3_3 = Masker.offset(3, 3);
|
||||
static final int OFF_3_4 = Masker.offset(3, 4);
|
||||
static final byte D_BYTE_2 = CLUE_RIGHT;
|
||||
|
||||
enum Idx {
|
||||
@@ -139,9 +147,9 @@ public class SwedishGeneratorTest {
|
||||
}
|
||||
@Test
|
||||
void testPatternForSlotAllLetters() {
|
||||
var grid = new Gridded(createEmpty());
|
||||
var key = Slot.packSlotKey(OFF_0_0, CLUE_RIGHT);
|
||||
val clues = Masker.Clues.createEmpty();
|
||||
var grid = new Gridded(clues);
|
||||
clues.setClueLo(IDX_0_0.lo, CLUE_RIGHT);
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, key, (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3), 0L, ABC);
|
||||
val map = grid.stream(clues).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
@@ -192,13 +200,21 @@ public class SwedishGeneratorTest {
|
||||
var f = rng.nextFloat();
|
||||
assertTrue(f >= 0.0 && f <= 1.0);
|
||||
}
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGrid() {
|
||||
var grid = new Gridded(createEmpty());
|
||||
var empty = Clues.createEmpty();
|
||||
var grid = new Gridded(empty);
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_0_0, 0, Lemma.from("A"));
|
||||
val arr = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
val arr = grid.stream(empty).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
assertEquals(1, arr.size());
|
||||
assertEquals(LETTER_A, arr.get(OFF_0_0));
|
||||
}
|
||||
@@ -217,11 +233,18 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testLemmaAndDict() {
|
||||
Assertions.assertEquals(Lemma.packShiftIn("APPLE".getBytes(StandardCharsets.US_ASCII)), Lemma.unpackLetters(l1));
|
||||
assertEquals(4, Lemma.unpackSize(l1));
|
||||
assertEquals(LETTER_A, Lemma.byteAt(l1, 0));
|
||||
Assertions.assertEquals(Lemma.packShiftIn("APPLE".getBytes(StandardCharsets.US_ASCII)), Lemma.unpackLetters(APPLE));
|
||||
assertEquals(4, Lemma.unpackSize(APPLE));
|
||||
assertEquals(LETTER_I, Lemma.byteAt(INERENAE, 0));
|
||||
assertEquals(LETTER_N, Lemma.byteAt(INERENAE, 1));
|
||||
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 2));
|
||||
assertEquals(LETTER_R, Lemma.byteAt(INERENAE, 3));
|
||||
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 4));
|
||||
assertEquals(LETTER_N, Lemma.byteAt(INERENAE, 5));
|
||||
assertEquals(LETTER_A, Lemma.byteAt(INERENAE, 6));
|
||||
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 7));
|
||||
|
||||
var dict = Dicts.makeDict(new long[]{ l1, l2, l2a, l4a, l6a, l7a, l8a });
|
||||
var dict = Dicts.makeDict(new long[]{ APPLE, EXE, IN, INER, INEREN, INERENA, INERENAE });
|
||||
|
||||
assertEquals(1, dict.index()[3].words().length);
|
||||
assertEquals(1, dict.index()[5].words().length);
|
||||
@@ -251,13 +274,13 @@ public class SwedishGeneratorTest {
|
||||
assertEquals(OFF_2_3, Slot.clueIndex(key));
|
||||
assertEquals(CLUE_DOWN, Slot.dir(key));
|
||||
assertFalse(Slot.horiz(key));
|
||||
var cells = Gridded.walk((byte) key, lo, 0L).toArray();
|
||||
assertEquals(2, SwedishGenerator.IT[cells[0]].r());
|
||||
assertEquals(3, SwedishGenerator.IT[cells[1]].r());
|
||||
assertEquals(4, SwedishGenerator.IT[cells[2]].r());
|
||||
assertEquals(5, SwedishGenerator.IT[cells[0]].c());
|
||||
assertEquals(5, SwedishGenerator.IT[cells[1]].c());
|
||||
assertEquals(5, SwedishGenerator.IT[cells[2]].c());
|
||||
var cells = Gridded.cellWalk((byte) key, lo, 0L).toArray();
|
||||
assertEquals(2, Masker.IT[cells[0]].r());
|
||||
assertEquals(3, Masker.IT[cells[1]].r());
|
||||
assertEquals(4, Masker.IT[cells[2]].r());
|
||||
assertEquals(5, Masker.IT[cells[0]].c());
|
||||
assertEquals(5, Masker.IT[cells[1]].c());
|
||||
assertEquals(5, Masker.IT[cells[2]].c());
|
||||
|
||||
assertTrue(Slot.horiz(CLUE_RIGHT)); // right
|
||||
assertFalse(Slot.horiz(CLUE_DOWN)); // down
|
||||
@@ -335,7 +358,8 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testPlaceWord() {
|
||||
var grid = new Gridded(createEmpty());
|
||||
var empty = Clues.createEmpty();
|
||||
var grid = new Gridded(empty);
|
||||
// Slot at OFF_0_0 length 3, horizontal (right)
|
||||
var key = Slot.packSlotKey(0, CLUE_RIGHT);
|
||||
var lo = (1L << OFF_0_0) | (1L << OFF_0_1) | (1L << OFF_0_2);
|
||||
@@ -344,7 +368,8 @@ public class SwedishGeneratorTest {
|
||||
|
||||
// 1. Successful placement in empty grid
|
||||
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
|
||||
var map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
|
||||
var map = grid.stream(empty).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
assertEquals(3, map.size());
|
||||
assertEquals(LETTER_A, map.get(OFF_0_0));
|
||||
assertEquals(LETTER_B, map.get(OFF_0_1));
|
||||
@@ -362,7 +387,7 @@ public class SwedishGeneratorTest {
|
||||
assertEquals(LETTER_C, map.get(OFF_0_2));
|
||||
|
||||
// 4. Partial placement then conflict (rollback)
|
||||
grid = new Gridded(createEmpty());
|
||||
grid = new Gridded(Clues.createEmpty());
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_0_2, 0, Lemma.from("X")); // Conflict at the end
|
||||
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
|
||||
map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
@@ -372,7 +397,8 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testBacktrackingHelpers() {
|
||||
var grid = new Gridded(createEmpty());
|
||||
var clues = Clues.createEmpty();
|
||||
var grid = new Gridded(clues);
|
||||
// Slot at 0,1 length 2
|
||||
var key = Slot.packSlotKey(0, CLUE_RIGHT);
|
||||
var lo = (1L << OFF_0_1) | (1L << OFF_0_2);
|
||||
@@ -381,7 +407,8 @@ public class SwedishGeneratorTest {
|
||||
val top = grid.grid().hi;
|
||||
var placed = GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, 0L, w);
|
||||
assertTrue(placed);
|
||||
var map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
|
||||
var map = grid.stream(clues).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
|
||||
assertEquals(2, map.size());
|
||||
assertEquals(LETTER_A, map.get(OFF_0_1));
|
||||
assertEquals(LETTER_Z, map.get(OFF_0_2));
|
||||
|
||||
Reference in New Issue
Block a user