introduce bitloops

This commit is contained in:
mike
2026-01-17 01:30:50 +01:00
parent 4585c1f2eb
commit a2134f0dce
3 changed files with 27 additions and 22 deletions

View File

@@ -389,7 +389,9 @@ public class Main {
var mask = swe.generateMask(opts.clueSize, opts.pop, opts.gens, opts.offspring);
if (mask == null) return null;
val multiThreaded = Thread.currentThread().getName().contains("pool");
var filled = fillMask(rng, extractSlots(mask, dict.index()), mask.toGrid(), multiThreaded);
var slots = extractSlots(mask, dict.index());
val slotInfo = scoreSlots(new int[slots.length], slots);
var filled = fillMask(rng, slotInfo, mask.toGrid(), multiThreaded);
TOTAL_NODES.addAndGet(filled.stats().nodes);
TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks);

View File

@@ -78,9 +78,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
@AllArgsConstructor
static class Pick {
Slot slot;
int[] indices;
int count;
Slotinfo slot;
int[] indices;
int count;
}
// 0b11
@@ -335,6 +335,10 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
}
static record Slotinfo(int key, long lo, long hi, int score, DictEntry entry) {
}
static record Slot(int key, long lo, long hi, DictEntry entry) {
static final int BIT_FOR_DIR = 2;
@@ -874,8 +878,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return count;
}
static void scoreSlots(int[] slotScores, Slot[] slots) {
val count = new byte[SIZE];
static Slotinfo[] scoreSlots(int[] slotScores, Slot[] slots) {
val count = new byte[SIZE];
Slotinfo[] slotInfo = new Slotinfo[slots.length];
for (var s : slots) {
for (long b = s.lo; b != X; b &= b - 1) count[numberOfTrailingZeros(b)]++;
for (long b = s.hi; b != X; b &= b - 1) count[64 | numberOfTrailingZeros(b)]++;
@@ -883,27 +888,23 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (int i = 0; i < slots.length; i++) {
var slot = slots[i];
slotScores[i] = slotScore(count, slot.lo, slot.hi);
slotInfo[i] = new Slotinfo(slot.key, slot.lo, slot.hi, slotScores[i], slot.entry);
}
return slotInfo;
}
public static FillResult fillMask(final Rng rng,final Slot[] slots,final Grid mask, final boolean multiThreaded) {
public static FillResult fillMask(final Rng rng, final Slotinfo[] slots,
final Grid grid,
final boolean multiThreaded) {
val NO_LOG = (!Main.VERBOSE || multiThreaded);
val grid = mask;
val used = new long[2048];
val assigned = new long[CLUE_INDEX_MAX_SIZE];
val bitset = new long[2500];
val g = grid.g;
val TOTAL = slots.length;
val slotScores = new int[TOTAL];
scoreSlots(slotScores, slots);
val t0 = System.currentTimeMillis();
val TOTAL = slots.length;
val t0 = System.currentTimeMillis();
val CARRIER = new Pick(null, null, 0);
class Solver {
private final Pick CARRIER = new Pick(null, null, 0);
long nodes;
long backtracks;
int lastMRV;
@@ -963,7 +964,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return true;
}
void chooseMRV() {
Slot best = null;
Slotinfo best = null;
for (int i = 0, count, count2 = -1, bestScore = -1, n = TOTAL; i < n; i++) {
var s = slots[i];
if (assigned[s.key] != X) continue;
@@ -977,9 +978,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
if (best == null
|| count < count2
|| (count == count2 && slotScores[i] > bestScore)) {
|| (count == count2 && s.score > bestScore)) {
best = s;
bestScore = slotScores[i];
bestScore = s.score;
count2 = count;
if (count <= 1) break;
}