introduce bitloops

This commit is contained in:
mike
2026-01-12 22:22:19 +01:00
parent 88a61e6f4d
commit a9b4dfb422
4 changed files with 148 additions and 119 deletions

View File

@@ -166,7 +166,7 @@ public record SwedishGenerator(Rng rng) {
this.hi = hi;
}
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); }
int digitAt(int index) { return g[index] & 7; }
int digitAt(int index) { return g[index]; }
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); }
@@ -326,9 +326,8 @@ public record SwedishGenerator(Rng rng) {
}
private static void processSlot(Grid grid, SlotVisitor visitor, int idx) {
int d = grid.digitAt(idx); // 1..4
int di = d - 1; // 0..3
int key = (idx << 2) | di;
int d = grid.digitAt(idx); // 0..3
int key = (idx << 2) | d;
long rayLo = PATH_LO[key];
long rayHi = PATH_HI[key];
@@ -339,7 +338,7 @@ public record SwedishGenerator(Rng rng) {
// slice ray to stop before first clue, depending on direction monotonicity
// right/down => increasing indices; up/left => decreasing indices
boolean increasing = Slot.increasing(d);
boolean increasing = Slot.increasing(d + 1);
if (increasing) {
// first clue is lowest index among hits (lo first, then hi)
@@ -367,7 +366,7 @@ public record SwedishGenerator(Rng rng) {
}
if ((rayLo | rayHi) != 0) {
visitor.visit(Slot.packSlotDir(idx, d), rayLo, rayHi);
visitor.visit(Slot.packSlotDir(idx, d + 1), rayLo, rayHi);
}
}
@@ -391,11 +390,10 @@ public record SwedishGenerator(Rng rng) {
for (long bits = (i == 0 ? lo_cl : hi_cl); bits != X; bits &= bits - 1) {
int clueIdx = i | Long.numberOfTrailingZeros(bits);
int d = grid.digitAt(clueIdx);
int di = d - 1;
int key = (clueIdx << 2) | di;
int key = (clueIdx << 2) | d;
long rLo = PATH_LO[key], rHi = PATH_HI[key];
long hLo = rLo & lo_cl, hHi = rHi & hi_cl;
if (Slot.increasing(d)) {
if (Slot.increasing(d + 1)) {
if (hLo != 0) {
rLo &= ((1L << Long.numberOfTrailingZeros(hLo)) - 1);
rHi = 0;
@@ -412,7 +410,7 @@ public record SwedishGenerator(Rng rng) {
}
if ((rLo | rHi) != 0) {
hasSlots = true;
if (Slot.horiz(d)) covH.or(rLo, rHi);
if (Slot.horiz(d + 1)) covH.or(rLo, rHi);
else covV.or(rLo, rHi);
if ((Long.bitCount(rLo) + Long.bitCount(rHi)) < MIN_LEN) penalty += 8000;
} else {
@@ -508,7 +506,7 @@ public record SwedishGenerator(Rng rng) {
if (g.isClue(idx)) continue;
int d_idx = rng.randint2bit();
if (g.hasRoomForClue(OFFSETS_D_IDX[d_idx | idx << 2])) {
g.setClue(idx, (byte) (1 + (d_idx | 48)));
g.setClue(idx, (byte) d_idx);
placed++;
}
}
@@ -523,7 +521,7 @@ public record SwedishGenerator(Rng rng) {
if (!g.clueless(ri)) {
int d_idx = rng.randint2bit();
val packed = OFFSETS_D_IDX[d_idx | ri << 2];
if (g.hasRoomForClue(packed)) g.setClue(ri, (byte) (1 + (d_idx | 48)));
if (g.hasRoomForClue(packed)) g.setClue(ri, (byte) d_idx);
}
}
return g;
@@ -557,7 +555,7 @@ public record SwedishGenerator(Rng rng) {
for (var hi = out.hi; hi != X; hi &= hi - 1L) clearClues(out, 64 + Long.numberOfTrailingZeros(hi));
return out;
}
public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS_D_IDX[(out.digitAt(idx) - 1) | (idx << 2)])) out.clearClue(idx); }
public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS_D_IDX[(out.digitAt(idx) ) | (idx << 2)])) out.clearClue(idx); }
Grid hillclimb(Grid start, int limit) {
var best = start;