introduce bitloops

This commit is contained in:
mike
2026-01-14 11:03:28 +01:00
parent 66bd8193ef
commit c1706e1bf7
5 changed files with 42 additions and 65 deletions

View File

@@ -120,7 +120,7 @@ public record Export() {
var offset = Grid.offset(r, c);
if (clues.isClue(offset))
sb.append((char) (48 | clues.digitAt(offset)));
else if (grid.lisLetterAtLo(offset))
else if (grid.lisLetterAt(offset))
sb.append((char) (64 | grid.letter32At(offset)));
else
sb.append(' ');

View File

@@ -303,30 +303,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
g[idx] = ch;
}
void setLetter(int idx, byte ch) {
if ((idx & 64) == 0)
setLetterLo(idx, ch);
else
setLetterHi(idx, ch);
}
private void clearletterLo(int idx) {
g[idx] = DASH;
lo &= ~(1L << idx);
}
private void clearletterHi(int idx) {
g[idx] = DASH;
hi &= ~(1L << (idx & 63));
}
void undoPlace(long maskLo, long maskHi) {
lo &= ~maskLo;
hi &= ~maskHi;
/*for (long b = maskLo; b != 0; b &= b - 1) clearletterLo(Long.numberOfTrailingZeros(b));
for (long b = maskHi; b != 0; b &= b - 1) clearletterHi(64 | Long.numberOfTrailingZeros(b));*/
}
}
static record DictEntry(long[] words, long[][] posBitsets, int length, int numlong) { }
@@ -799,7 +775,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (long b = s.hi; b != 0; b &= b - 1) cross += (count[64 | Long.numberOfTrailingZeros(b)] - 1);
return cross * 10 + s.length();
}
static boolean placeWord(Grid grid, Slot s, long w, long[] undoBuffer, int offset) {
static boolean placeWord(Grid grid, Slot s, long w) {
if (s.increasing()) {
for (long b = s.lo & grid.lo; b != 0; b &= b - 1) {
int idx = Long.numberOfTrailingZeros(b);
@@ -824,8 +800,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
grid.lo |= maskLo;
grid.hi |= maskHi;
}
undoBuffer[offset << 1] = maskLo;
undoBuffer[(offset << 1) | 1] = maskHi;
} else {
int bcHi = Long.bitCount(s.hi);
for (long b = s.hi & grid.hi; b != 0; b &= b - 1) {
@@ -850,8 +824,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
grid.lo |= maskLo;
grid.hi |= maskHi;
}
undoBuffer[offset << 1] = maskLo;
undoBuffer[(offset << 1) | 1] = maskHi;
}
return true;
}
@@ -932,7 +904,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
val used = new Bit1029();
val assigned = new long[CLUE_INDEX_MAX_SIZE];
val bitset = new long[2500];
val undo = new long[64];
val TOTAL = slots.length;
val slotScores = new int[TOTAL];
@@ -1042,8 +1013,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var w = entry.words[idx];
var lemIdx = Lemma.unpackIndex(w);
if (used.get(lemIdx)) continue;
if (!placeWord(grid, s, w, undo, depth)) continue;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, s, w)) continue;
used.set(lemIdx);
assigned[k] = w;
@@ -1052,7 +1024,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
assigned[k] = X;
used.clear(lemIdx);
grid.undoPlace(undo[depth << 1], undo[(depth << 1) | 1]);
//grid.undoPlace(undo[depth << 1], undo[(depth << 1) | 1]);
grid.lo = low;
grid.hi = top;
}
backtracks++;
return false;
@@ -1067,8 +1041,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var w = entry.words[idxInArray];
var lemIdx = Lemma.unpackIndex(w);
if (used.get(lemIdx)) continue;
if (!placeWord(grid, s, w, undo, depth)) continue;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, s, w)) continue;
used.set(lemIdx);
assigned[k] = w;
@@ -1077,7 +1052,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
assigned[k] = X;
used.clear(lemIdx);
grid.undoPlace(undo[depth << 1], undo[(depth << 1) | 1]);
grid.lo = low;
grid.hi = top;
//grid.undoPlace(undo[depth << 1], undo[(depth << 1) | 1]);
}
backtracks++;