introduce bitloops
This commit is contained in:
@@ -10,7 +10,6 @@ import lombok.val;
|
|||||||
import precomp.Neighbors9x8;
|
import precomp.Neighbors9x8;
|
||||||
import precomp.Neighbors9x8.rci;
|
import precomp.Neighbors9x8.rci;
|
||||||
import puzzle.Export.Bit1029;
|
import puzzle.Export.Bit1029;
|
||||||
import puzzle.Export.ClueAt;
|
|
||||||
import puzzle.Export.DictEntryDTO;
|
import puzzle.Export.DictEntryDTO;
|
||||||
import puzzle.Export.Gridded;
|
import puzzle.Export.Gridded;
|
||||||
import puzzle.Export.Strings;
|
import puzzle.Export.Strings;
|
||||||
@@ -732,7 +731,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
}
|
}
|
||||||
return best.grid;
|
return best.grid;
|
||||||
}
|
}
|
||||||
static long patternForSlot2(final long glo, final long ghi, final byte[] g, final int key, final long lo, final long hi) {
|
static long patternForSlot(final long glo, final long ghi, final byte[] g, final int key, final long lo, final long hi) {
|
||||||
if (((lo & glo) | (hi & ghi)) == X) return 0;
|
if (((lo & glo) | (hi & ghi)) == X) return 0;
|
||||||
long p = 0;
|
long p = 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
@@ -763,36 +762,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
static long patternForSlot(final long glo, final long ghi, final byte[] g, final int key, final long lo, final long hi) {
|
|
||||||
if (((lo & glo) | (hi & ghi)) == X) return 0;
|
|
||||||
long p = 0;
|
|
||||||
if (Slot.increasing(key)) {
|
|
||||||
for (long b = lo & glo; b != X; b &= b - 1) {
|
|
||||||
int idx = numberOfTrailingZeros(b);
|
|
||||||
int i = bitCount(lo & ((1L << idx) - 1));
|
|
||||||
p |= ((long) (i * 26 + g[idx])) << (i << 3);
|
|
||||||
}
|
|
||||||
int offset = bitCount(lo);
|
|
||||||
for (long b = hi & ghi; b != X; b &= b - 1) {
|
|
||||||
int idx = numberOfTrailingZeros(b);
|
|
||||||
int i = offset + bitCount(hi & ((1L << idx) - 1));
|
|
||||||
p |= ((long) (i * 26 + g[64 | idx])) << (i << 3);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int offset = bitCount(hi);
|
|
||||||
for (long b = hi & ghi; b != X; b &= b - 1) {
|
|
||||||
int idx = numberOfTrailingZeros(b);
|
|
||||||
int i = bitCount(hi & ~((1L << idx) | ((1L << idx) - 1)));
|
|
||||||
p |= ((long) (i * 26 + g[64 | idx])) << (i << 3);
|
|
||||||
}
|
|
||||||
for (long b = lo & glo; b != X; b &= b - 1) {
|
|
||||||
int idx = numberOfTrailingZeros(b);
|
|
||||||
int i = offset + bitCount(lo & ~((1L << idx) | ((1L << idx) - 1)));
|
|
||||||
p |= ((long) (i * 26 + g[idx])) << (i << 3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
static int slotScore(byte[] count, long lo, long hi) {
|
static int slotScore(byte[] count, long lo, long hi) {
|
||||||
int cross = 0;
|
int cross = 0;
|
||||||
for (long b = lo; b != X; b &= b - 1) cross += (count[numberOfTrailingZeros(b)] - 1);
|
for (long b = lo; b != X; b &= b - 1) cross += (count[numberOfTrailingZeros(b)] - 1);
|
||||||
@@ -866,11 +835,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
|
|
||||||
int[] indices = new int[count];
|
int[] indices = new int[count];
|
||||||
for (int k = 0, ki = 0; k < numLongs; k++) {
|
for (int k = 0, ki = 0; k < numLongs; k++) {
|
||||||
long w = res[k];
|
for (long w = res[k]; w != X; w &= w - 1) indices[ki++] = (k << 6) | numberOfTrailingZeros(w);
|
||||||
while (w != X) {
|
|
||||||
indices[ki++] = (k << 6) | numberOfTrailingZeros(w);
|
|
||||||
w &= w - 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return indices;
|
return indices;
|
||||||
@@ -977,7 +942,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
for (int i = 0, count, count2 = -1, bestScore = -1, n = TOTAL; i < n; i++) {
|
for (int i = 0, count, count2 = -1, bestScore = -1, n = TOTAL; i < n; i++) {
|
||||||
var s = slots[i];
|
var s = slots[i];
|
||||||
if (s.assign.w != X) continue;
|
if (s.assign.w != X) continue;
|
||||||
var pattern = patternForSlot2(glo, ghi, g, s.key, s.lo, s.hi);
|
var pattern = patternForSlot(glo, ghi, g, s.key, s.lo, s.hi);
|
||||||
var index = s.entry;
|
var index = s.entry;
|
||||||
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index.posBitsets, index.numlong);
|
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index.posBitsets, index.numlong);
|
||||||
|
|
||||||
@@ -999,7 +964,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
current = PICK_DONE;
|
current = PICK_DONE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var pattern = patternForSlot2(glo, ghi, g, best.key, best.lo, best.hi);
|
var pattern = patternForSlot(glo, ghi, g, best.key, best.lo, best.hi);
|
||||||
var index = best.entry;
|
var index = best.entry;
|
||||||
current = CARRIER;
|
current = CARRIER;
|
||||||
current.slot = best;
|
current.slot = best;
|
||||||
@@ -1071,7 +1036,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
|||||||
low = glo;
|
low = glo;
|
||||||
top = ghi;
|
top = ghi;
|
||||||
if (!placeWord(k, slo, shi, w)) continue;
|
if (!placeWord(k, slo, shi, w)) continue;
|
||||||
|
|
||||||
Bit1029.set(used, lemIdx);
|
Bit1029.set(used, lemIdx);
|
||||||
s.assign.w = w;
|
s.assign.w = w;
|
||||||
if (backtrack(depth + 1)) return true;
|
if (backtrack(depth + 1)) return true;
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ public class SwedishGeneratorTest {
|
|||||||
placeWord(grid, grid.g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_2_0, 0, Lemma.from(0, "C"));
|
placeWord(grid, grid.g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_2_0, 0, Lemma.from(0, "C"));
|
||||||
var key = Slot.packSlotKey(OFF_1_0, CLUE_RIGHT);
|
var key = Slot.packSlotKey(OFF_1_0, CLUE_RIGHT);
|
||||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, key, 7L, 0L);
|
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, key, 7L, 0L);
|
||||||
assertEquals(1L | (55L << 16), pattern);
|
assertEquals(14081L, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user