introduce bitloops
This commit is contained in:
@@ -35,24 +35,24 @@ public final class Masker {
|
||||
return findOffendingClue(c) == -1;
|
||||
}
|
||||
|
||||
public int findOffendingClue(Clues grid) {
|
||||
if (((grid.xlo & grid.rlo) & grid.lo) != X) return numberOfTrailingZeros((grid.xlo & grid.rlo) & grid.lo);
|
||||
if (((grid.xhi & grid.rhi) & grid.hi) != X) return 64 | numberOfTrailingZeros((grid.xhi & grid.rhi) & grid.hi);
|
||||
public int findOffendingClue(Clues c) {
|
||||
if (((c.xlo & c.rlo) & c.lo) != X) return numberOfTrailingZeros((c.xlo & c.rlo) & c.lo);
|
||||
if (((c.xhi & c.rhi) & c.hi) != X) return 64 | numberOfTrailingZeros((c.xhi & c.rhi) & c.hi);
|
||||
|
||||
var num = 0;
|
||||
for (var bits = grid.lo; bits != X; bits &= bits - 1) activeCIdx[num++] = numberOfTrailingZeros(bits);
|
||||
for (var bits = grid.hi; bits != X; bits &= bits - 1) activeCIdx[num++] = 64 | numberOfTrailingZeros(bits);
|
||||
for (var bits = c.lo; bits != X; bits &= bits - 1) activeCIdx[num++] = numberOfTrailingZeros(bits);
|
||||
for (var bits = c.hi; bits != X; bits &= bits - 1) activeCIdx[num++] = 64 | numberOfTrailingZeros(bits);
|
||||
|
||||
if (num == 0) return -1;
|
||||
|
||||
var start = rng.randint0_SIZE() % num;
|
||||
var n = 0;
|
||||
for (var i = 0; i < num; i++) {
|
||||
var idx = activeCIdx[(start + i) % num];
|
||||
int dir = grid.getDir(idx);
|
||||
var idx = activeCIdx[(start + i) % num];
|
||||
int dir = c.getDir(idx);
|
||||
var key = Slot.packSlotKey(idx, dir);
|
||||
long sLo = PATH_LO[key], sHi = PATH_HI[key];
|
||||
long hLo = sLo & grid.lo, hHi = sHi & grid.hi;
|
||||
long hLo = sLo & c.lo, hHi = sHi & c.hi;
|
||||
if (Slotinfo.increasing(key)) {
|
||||
if (hLo != X) {
|
||||
sLo &= (1L << numberOfTrailingZeros(hLo)) - 1;
|
||||
@@ -121,26 +121,26 @@ public final class Masker {
|
||||
if (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= MIN_LEN)
|
||||
visitor.visit(key, rayLo, rayHi);
|
||||
}
|
||||
private static boolean validSlotRev(long lo, long hi, int min, int key) {
|
||||
private static boolean validSlotRev(long lo, long hi, int key) {
|
||||
var rayLo = PATH_LO[key];
|
||||
var rayHi = PATH_HI[key];
|
||||
// only consider clue cells
|
||||
var hitsLo = rayLo & lo;
|
||||
var hitsHi = rayHi & hi;
|
||||
|
||||
if (hitsHi != X) return (Long.bitCount(rayHi & -(1L << 63 - numberOfLeadingZeros(hitsHi) << 1)) >= min);
|
||||
else if (hitsLo != X) return (Long.bitCount(rayLo & -(1L << 63 - numberOfLeadingZeros(hitsLo) << 1)) + Long.bitCount(rayHi) >= min);
|
||||
else return (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= min);
|
||||
if (hitsHi != X) return (Long.bitCount(rayHi & -(1L << 63 - numberOfLeadingZeros(hitsHi) << 1)) >= SwedishGenerator.MIN_LEN);
|
||||
else if (hitsLo != X) return (Long.bitCount(rayLo & -(1L << 63 - numberOfLeadingZeros(hitsLo) << 1)) + Long.bitCount(rayHi) >= SwedishGenerator.MIN_LEN);
|
||||
else return (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= SwedishGenerator.MIN_LEN);
|
||||
}
|
||||
private static boolean validSlot(long lo, long hi, int min, int key) {
|
||||
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;
|
||||
|
||||
if (hitsLo != X) return (Long.bitCount(rayLo & ((1L << numberOfTrailingZeros(hitsLo)) - 1)) >= min);
|
||||
else if (hitsHi != X) return (Long.bitCount(rayLo) + Long.bitCount(rayHi & ((1L << numberOfTrailingZeros(hitsHi)) - 1)) >= min);
|
||||
else return (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= min);
|
||||
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];
|
||||
@@ -198,10 +198,6 @@ public final class Masker {
|
||||
long lo_cl = grid.lo, hi_cl = grid.hi;
|
||||
var penalty = (((long) Math.abs(grid.clueCount() - clueSize)) * 16000L);
|
||||
var hasSlots = false;
|
||||
/* if (!isValid(grid, 2)) {
|
||||
throw new RuntimeException("Invalid grid configuration for mask fitness calculation");
|
||||
//return 1_000_000_000L;
|
||||
}*/
|
||||
|
||||
var numClues = 0;
|
||||
for (var bits = lo_cl; bits != X; bits &= bits - 1) {
|
||||
@@ -653,8 +649,8 @@ public final class Masker {
|
||||
public static Clues createEmpty() { return new Clues(0, 0, 0, 0, 0, 0, 0, 0); }
|
||||
|
||||
public boolean hasRoomForClue(int key) {
|
||||
if (Slotinfo.increasing(key)) if (!validSlot(lo, hi, MIN_LEN, key)) return false;
|
||||
return validSlotRev(lo, hi, MIN_LEN, key);
|
||||
if (Slotinfo.increasing(key)) if (!validSlot(lo, hi, key)) return false;
|
||||
return validSlotRev(lo, hi, key);
|
||||
}
|
||||
|
||||
public void setClueLo(long mask, byte idx) {
|
||||
|
||||
Reference in New Issue
Block a user