introduce bitloops

This commit is contained in:
mike
2026-01-14 07:50:35 +01:00
parent c529ce90c7
commit bcad930bfc
3 changed files with 10 additions and 11 deletions

View File

@@ -120,7 +120,6 @@ public record Export() {
}
public String renderHuman() {
return String.join("\n", exportGrid(_ -> ' ', '#'));
}
public boolean notClue(int c) { return grid.notClue(c); }
@FunctionalInterface

View File

@@ -263,11 +263,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var grid = new Grid(new byte[SIZE], lo, hi);
for (var l = lo; l != X; l &= l - 1) {
int idx = Long.numberOfTrailingZeros(l);
grid.g[idx] = digitAt(idx);
grid.setLetter(idx, digitAt(idx));
}
for (var h = hi; h != X; h &= h - 1) {
int idx = 64 | Long.numberOfTrailingZeros(h);
grid.g[idx] = digitAt(idx);
grid.setLetter(idx, digitAt(idx));
}
return grid;
}
@@ -293,6 +293,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
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); }
/// the pos will never target a clue
public byte letter32At(int pos) { return g[pos]; }
void setLetter(int idx, byte ch) { g[idx] = ch; }
void clearletter(int idx) { g[idx] = DASH; }
@@ -842,7 +843,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
static int[] candidateInfoForPattern(long[] res, long pattern, long[][] posBitsets, int numLongs) {
boolean first = true;
boolean first = true;
for (long p = pattern; p != 0; ) {
int combined = (int) (p & 0xFF);
@@ -877,9 +878,8 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return indices;
}
static int candidateCountForPattern(final long[] res, final long pattern, final DictEntry entry, final int numLongs) {
val posBitsets = entry.posBitsets;
boolean first = true;
static int candidateCountForPattern(final long[] res, final long pattern, final long[][] posBitsets, final int numLongs) {
boolean first = true;
for (long p = pattern; p != 0; ) {
int combined = (int) (p & 0xFF);
@@ -963,7 +963,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
if (assigned[s.key] != X) continue;
var pattern = patternForSlot(grid, s);
var index = dictIndex[s.length()];
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index, index.numlong);
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index.posBitsets, index.numlong);
if (count == 0) {
current = PICK_NOT_DONE;

View File

@@ -409,13 +409,13 @@ public class SwedishGeneratorTest {
var ctx = Context.get();
var pattern = packPattern("APP");
assertEquals(2, candidateCountForPattern(ctx.bitset(), pattern, entry5, entry5.numlong()));
assertEquals(2, candidateCountForPattern(ctx.bitset(), pattern, entry5.posBitsets(), entry5.numlong()));
pattern = packPattern("BAN");
assertEquals(1, candidateCountForPattern(ctx.bitset(), pattern, entry5, entry5.numlong()));
assertEquals(1, candidateCountForPattern(ctx.bitset(), pattern, entry5.posBitsets(), entry5.numlong()));
pattern = packPattern("CAT");
assertEquals(0, candidateCountForPattern(ctx.bitset(), pattern, entry5, entry5.numlong()));
assertEquals(0, candidateCountForPattern(ctx.bitset(), pattern, entry5.posBitsets(), entry5.numlong()));
}
@Test