introduce bitloops

This commit is contained in:
mike
2026-01-14 12:28:07 +01:00
parent 6afe675a9d
commit dfb4679da8
5 changed files with 49 additions and 44 deletions

View File

@@ -383,18 +383,17 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
}
static record Slot(int key, long lo, long hi) {
static record Slot(int key, long lo, long hi, DictEntry entry) {
static final int BIT_FOR_DIR = 2;
static Slot from(int key, long lo, long hi) { return new Slot(key, lo, hi); }
public int length() { return Long.bitCount(lo) + Long.bitCount(hi); }
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
public static int dir(int key) { return key & 3; }
public static boolean increasing(int dir) { return (dir & 2) == 0; }
public IntStream walk() { return Gridded.walk((byte) key, lo, hi); }
public static boolean horiz(int d) { return (d & 1) != 0; }
public static int packSlotDir(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
static Slot from(int key, long lo, long hi, DictEntry entry) { return new Slot(key, lo, hi, entry); }
public static int length(long lo, long hi) { return Long.bitCount(lo) + Long.bitCount(hi); }
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
public static int dir(int key) { return key & 3; }
public static boolean increasing(int dir) { return (dir & 2) == 0; }
public IntStream walk() { return Gridded.walk((byte) key, lo, hi); }
public static boolean horiz(int d) { return (d & 1) != 0; }
public static int packSlotDir(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
}
private static void processSlot(Clues grid, SlotVisitor visitor, int idx) {
@@ -438,10 +437,10 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
visitor.visit(key, rayLo, rayHi);
}
static Slot[] extractSlots(Clues grid) {
static Slot[] extractSlots(Clues grid, DictEntry[] index) {
var slots = new Slot[grid.clueCount()];
int[] N = new int[]{ 0 };
grid.forEachSlot((key, lo, hi) -> slots[N[0]++] = Slot.from(key, lo, hi));
grid.forEachSlot((key, lo, hi) -> slots[N[0]++] = Slot.from(key, lo, hi, index[Slot.length(lo, hi)]));
return slots;
}
@@ -764,11 +763,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
return p;
}
static int slotScore(byte[] count, Slot s) {
static int slotScore(byte[] count, long lo, long hi) {
int cross = 0;
for (long b = s.lo; b != 0; b &= b - 1) cross += (count[Long.numberOfTrailingZeros(b)] - 1);
for (long b = s.hi; b != 0; b &= b - 1) cross += (count[64 | Long.numberOfTrailingZeros(b)] - 1);
return cross * 10 + s.length();
for (long b = lo; b != 0; b &= b - 1) cross += (count[Long.numberOfTrailingZeros(b)] - 1);
for (long b = hi; b != 0; b &= b - 1) cross += (count[64 | Long.numberOfTrailingZeros(b)] - 1);
return cross * 10 + Slot.length(lo, hi);
}
static boolean placeWord(Grid grid, final int key, final long lo, final long hi, final long w) {
final long glo = grid.lo, ghi = grid.hi;
@@ -890,10 +889,13 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (long b = s.lo; b != 0; b &= b - 1) count[Long.numberOfTrailingZeros(b)]++;
for (long b = s.hi; b != 0; b &= b - 1) count[64 | Long.numberOfTrailingZeros(b)]++;
}
for (int i = 0; i < slots.length; i++) slotScores[i] = slotScore(count, slots[i]);
for (int i = 0; i < slots.length; i++) {
var slot = slots[i];
slotScores[i] = slotScore(count, slot.lo, slot.hi);
}
}
public static FillResult fillMask(Rng rng, Slot[] slots, Grid mask, DictEntry[] dictIndex) {
public static FillResult fillMask(Rng rng, Slot[] slots, Grid mask) {
val multiThreaded = Thread.currentThread().getName().contains("pool");
val NO_LOG = (!Main.VERBOSE || multiThreaded);
val grid = mask;
@@ -943,7 +945,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var s = slots[i];
if (assigned[s.key] != X) continue;
var pattern = patternForSlot(grid, s.key, s.lo, s.hi);
var index = dictIndex[s.length()];
var index = s.entry;
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index.posBitsets, index.numlong);
if (count == 0) {
@@ -965,7 +967,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return;
}
var pattern = patternForSlot(grid, best.key, best.lo, best.hi);
var index = dictIndex[best.length()];
var index = best.entry;
current = CARRIER;
current.slot = best;
current.count = index.length;
@@ -992,10 +994,12 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
lastMRV = pick.count;
if (!NO_LOG) renderProgress();
val s = pick.slot;
val k = s.key;
val entry = dictIndex[s.length()];
val s = pick.slot;
val k = s.key;
val slo = s.lo;
val shi = s.hi;
val entry = s.entry;
long low, top;
if (info != null && info.length > 0) {
var idxs = info;
var L = idxs.length;
@@ -1009,9 +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;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, k, s.lo, s.hi, w)) continue;
low = grid.lo;
top = grid.hi;
if (!placeWord(grid, k, slo, shi, w)) continue;
used.set(lemIdx);
assigned[k] = w;
@@ -1036,9 +1040,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;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, k, s.lo, s.hi, w)) continue;
low = grid.lo;
top = grid.hi;
if (!placeWord(grid, k, slo, shi, w)) continue;
used.set(lemIdx);
assigned[k] = w;