introduce bitloops
This commit is contained in:
@@ -2,6 +2,8 @@ package puzzle;
|
||||
|
||||
import module java.base;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.Accessors;
|
||||
import lombok.val;
|
||||
import precomp.Neighbors9x8;
|
||||
import precomp.Neighbors9x8.rci;
|
||||
@@ -15,11 +17,11 @@ public final class Masker {
|
||||
private final Rng rng;
|
||||
private final int[] stack;
|
||||
private final Clues cache;
|
||||
private final int[] activeCIdx = new int[SwedishGenerator.SIZE];
|
||||
private final long[] activeSLo = new long[SwedishGenerator.SIZE];
|
||||
private final long[] activeSHi = new long[SwedishGenerator.SIZE];
|
||||
private final long[] adjLo = new long[SwedishGenerator.SIZE];
|
||||
private final long[] adjHi = new long[SwedishGenerator.SIZE];
|
||||
private final int[] activeCIdx = new int[Neighbors9x8.SIZE];
|
||||
private final long[] activeSLo = new long[Neighbors9x8.SIZE];
|
||||
private final long[] activeSHi = new long[Neighbors9x8.SIZE];
|
||||
private final long[] adjLo = new long[Neighbors9x8.SIZE];
|
||||
private final long[] adjHi = new long[Neighbors9x8.SIZE];
|
||||
private final int[] rCount = new int[8];
|
||||
private final int[] cCount = new int[9];
|
||||
private static final long[] NBR_LO = Neighbors9x8.NBR_LO;
|
||||
@@ -133,20 +135,20 @@ public final class Masker {
|
||||
else return (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= SwedishGenerator.MIN_LEN);
|
||||
}
|
||||
private static boolean validSlot(long lo, long hi, int key) {
|
||||
var rayLo = PATH_LO[key];
|
||||
var rayHi = PATH_HI[key];
|
||||
var hitsLo = rayLo & lo;
|
||||
var hitsHi = rayHi & hi;
|
||||
var rayLo = PATH_LO[key];
|
||||
var rayHi = PATH_HI[key];
|
||||
var hitsLo = rayLo & lo;
|
||||
var hitsHi = rayHi & hi;
|
||||
|
||||
if (hitsLo != X) return (Long.bitCount(rayLo & ((1L << numberOfTrailingZeros(hitsLo)) - 1)) >= SwedishGenerator.MIN_LEN);
|
||||
else if (hitsHi != X) return (Long.bitCount(rayLo) + Long.bitCount(rayHi & ((1L << numberOfTrailingZeros(hitsHi)) - 1)) >= SwedishGenerator.MIN_LEN);
|
||||
else return (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= SwedishGenerator.MIN_LEN);
|
||||
}
|
||||
private static void processSlot(Clues c, SlotVisitor visitor, int key) {
|
||||
var rayLo = PATH_LO[key];
|
||||
var rayHi = PATH_HI[key];
|
||||
var hitsLo = rayLo & c.lo;
|
||||
var hitsHi = rayHi & c.hi;
|
||||
var rayLo = PATH_LO[key];
|
||||
var rayHi = PATH_HI[key];
|
||||
var hitsLo = rayLo & c.lo;
|
||||
var hitsHi = rayHi & c.hi;
|
||||
|
||||
if (hitsLo != X) {
|
||||
var stop = 1L << numberOfTrailingZeros(hitsLo);
|
||||
@@ -159,9 +161,9 @@ public final class Masker {
|
||||
if (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= MIN_LEN)
|
||||
visitor.visit(key, rayLo, rayHi);
|
||||
}
|
||||
public static Slot[] extractSlots(Clues grid, DictEntry[] index) {
|
||||
var slots = new ArrayList<Slot>(grid.clueCount());
|
||||
grid.forEachSlot((key, lo, hi) -> slots.add(Slot.from(key, lo, hi, index[Slot.length(lo, hi)])));
|
||||
public static Slot[] extractSlots(Clues c, DictEntry[] index) {
|
||||
var slots = new ArrayList<Slot>(c.clueCount());
|
||||
c.forEachSlot((key, lo, hi) -> slots.add(Slot.from(key, lo, hi, index[Slot.length(lo, hi)])));
|
||||
return slots.toArray(Slot[]::new);
|
||||
}
|
||||
public static Slotinfo[] slots(Clues mask, DictEntry[] index) {
|
||||
@@ -193,19 +195,19 @@ public final class Masker {
|
||||
|
||||
public long maskFitness(final Clues grid, int clueSize) {
|
||||
|
||||
long cHLo = 0L, cHHi = 0L, cVLo = 0L, cVHi = 0L;
|
||||
long cHLo2 = 0L, cHHi2 = 0L, cVLo2 = 0L, cVHi2 = 0L;
|
||||
long cHLo = 0L, cHHi = 0L, cVLo = 0L, cVHi = 0L;
|
||||
long cHLo2 = 0L, cHHi2 = 0L, cVLo2 = 0L, cVHi2 = 0L;
|
||||
long lo_cl = grid.lo, hi_cl = grid.hi;
|
||||
var penalty = (((long) Math.abs(grid.clueCount() - clueSize)) * 16000L);
|
||||
var hasSlots = false;
|
||||
|
||||
var numClues = 0;
|
||||
for (var bits = lo_cl; bits != X; bits &= bits - 1) {
|
||||
var lsb = bits & -bits;
|
||||
var clueIdx = numberOfTrailingZeros(lsb);
|
||||
var lsb = bits & -bits;
|
||||
var clueIdx = numberOfTrailingZeros(lsb);
|
||||
int dir = grid.getDir(clueIdx);
|
||||
var key = Slot.packSlotKey(clueIdx, dir);
|
||||
long rLo = PATH_LO[key], rHi = PATH_HI[key];
|
||||
var key = Slot.packSlotKey(clueIdx, dir);
|
||||
long rLo = PATH_LO[key], rHi = PATH_HI[key];
|
||||
long hLo = rLo & lo_cl, hHi = rHi & hi_cl;
|
||||
if (Slotinfo.increasing(key)) {
|
||||
if (hLo != X) {
|
||||
@@ -245,11 +247,11 @@ public final class Masker {
|
||||
} else penalty += 25000;
|
||||
}
|
||||
for (var bits = hi_cl; bits != X; bits &= bits - 1) {
|
||||
var lsb = bits & -bits;
|
||||
var clueIdx = numberOfTrailingZeros(lsb);
|
||||
var lsb = bits & -bits;
|
||||
var clueIdx = numberOfTrailingZeros(lsb);
|
||||
int dir = grid.getDir(64 | clueIdx);
|
||||
var key = Slot.packSlotKey(64 | clueIdx, dir);
|
||||
long rLo = PATH_LO[key], rHi = PATH_HI[key];
|
||||
var key = Slot.packSlotKey(64 | clueIdx, dir);
|
||||
long rLo = PATH_LO[key], rHi = PATH_HI[key];
|
||||
long hLo = rLo & lo_cl, hHi = rHi & hi_cl;
|
||||
if (Slotinfo.increasing(key)) {
|
||||
if (hLo != X) {
|
||||
@@ -588,9 +590,9 @@ public final class Masker {
|
||||
var childCount = 0;
|
||||
for (var k = 0; k < offspring; k++) {
|
||||
if (Thread.currentThread().isInterrupted()) break;
|
||||
var p1 = rng.rand(pop);
|
||||
var p2 = rng.rand(pop);
|
||||
var child = crossover(p1.grid, p2.grid);
|
||||
var p1 = rng.rand(pop);
|
||||
var p2 = rng.rand(pop);
|
||||
var child = crossover(p1.grid, p2.grid);
|
||||
children[k] = new GridAndFit(hillclimb(child, clueSize, 70));
|
||||
childCount++;
|
||||
}
|
||||
@@ -641,11 +643,14 @@ public final class Masker {
|
||||
}
|
||||
//@formatter:off
|
||||
@FunctionalInterface public interface SlotVisitor { void visit(int key, long lo, long hi); }
|
||||
sealed interface BitPop permits Clues { long hi(); long lo(); }
|
||||
//@formatter:on
|
||||
@AllArgsConstructor
|
||||
public static class Clues {
|
||||
@Accessors(fluent = true)
|
||||
public static final class Clues
|
||||
implements BitPop {
|
||||
|
||||
long lo, hi, vlo, vhi, rlo, rhi, xlo, xhi;
|
||||
@Getter long lo, hi, vlo, vhi, rlo, rhi, xlo, xhi;
|
||||
public static Clues createEmpty() { return new Clues(0, 0, 0, 0, 0, 0, 0, 0); }
|
||||
|
||||
public boolean hasRoomForClue(int key) {
|
||||
|
||||
Reference in New Issue
Block a user