introduce bitloops
This commit is contained in:
@@ -2,24 +2,22 @@ package puzzle;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.val;
|
||||
import puzzle.Export.Clued;
|
||||
import puzzle.Export.Gridded;
|
||||
import precomp.Neighbors9x8;
|
||||
import precomp.Neighbors9x8.rci;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static java.lang.Long.*;
|
||||
import static puzzle.SwedishGenerator.*;
|
||||
|
||||
public final class Masker {
|
||||
|
||||
private final Rng rng;
|
||||
private final int[] stack;
|
||||
private final Clues cache;
|
||||
|
||||
public static final rci[] IT = Neighbors9x8.IT;
|
||||
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];
|
||||
@@ -42,8 +40,8 @@ public final class Masker {
|
||||
for (int dc1 = -2; dc1 <= 2; dc1++)
|
||||
for (int dc2 = -2; dc2 <= 2; dc2++) {
|
||||
val ti = IT[i];
|
||||
MUTATE_RI[i][k++] = Grid.offset(SwedishGenerator.clamp(ti.r() + dr1 + dr2, 0, R - 1),
|
||||
SwedishGenerator.clamp(ti.c() + dc1 + dc2, 0, C - 1));
|
||||
MUTATE_RI[i][k++] = offset(clamp(ti.r() + dr1 + dr2, 0, R - 1),
|
||||
clamp(ti.c() + dc1 + dc2, 0, C - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,6 +68,27 @@ 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) {
|
||||
long rayLo = PATH_LO[key];
|
||||
long rayHi = PATH_HI[key];
|
||||
// only consider clue cells
|
||||
long hitsLo = rayLo & lo;
|
||||
long 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);
|
||||
}
|
||||
private static boolean validSlot(long lo, long hi, int min, int key) {
|
||||
long rayLo = PATH_LO[key];
|
||||
long rayHi = PATH_HI[key];
|
||||
long hitsLo = rayLo & lo;
|
||||
long 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);
|
||||
}
|
||||
private static void processSlot(Clues c, SlotVisitor visitor, int key) {
|
||||
long rayLo = PATH_LO[key];
|
||||
long rayHi = PATH_HI[key];
|
||||
@@ -79,10 +98,9 @@ public final class Masker {
|
||||
if (hitsLo != X) {
|
||||
long stop = 1L << numberOfTrailingZeros(hitsLo);
|
||||
rayLo &= (stop - 1);
|
||||
rayHi = 0; // any hi is beyond the stop
|
||||
rayHi = 0;
|
||||
} else if (hitsHi != X) {
|
||||
long stop = 1L << numberOfTrailingZeros(hitsHi);
|
||||
// keep all lo (lo indices are < any hi index), but cut hi below stop
|
||||
rayHi &= (stop - 1);
|
||||
}
|
||||
if (Long.bitCount(rayLo) + Long.bitCount(rayHi) >= MIN_LEN)
|
||||
@@ -116,6 +134,8 @@ public final class Masker {
|
||||
for (long b = hi; b != X; b &= b - 1) cross += (count[64 | numberOfTrailingZeros(b)] - 1);
|
||||
return cross * 10 + Slot.length(lo, hi);
|
||||
}
|
||||
public static int clamp(int x, int a, int b) { return Math.max(a, Math.min(b, x)); }
|
||||
public static int offset(int r, int c) { return r | (c << 3); }
|
||||
|
||||
public long maskFitness(final Clues grid, int clueSize) {
|
||||
|
||||
@@ -216,7 +236,8 @@ public final class Masker {
|
||||
|
||||
// Connectiviteitscheck
|
||||
for (int i = 0; i < numClues; i++) {
|
||||
adjLo[i] = 0; adjHi[i] = 0;
|
||||
adjLo[i] = 0;
|
||||
adjHi[i] = 0;
|
||||
}
|
||||
for (int i = 0; i < numClues; i++) {
|
||||
for (int j = i + 1; j < numClues; j++) {
|
||||
@@ -240,22 +261,22 @@ public final class Masker {
|
||||
stack[0] = 0;
|
||||
int sp = 1;
|
||||
while (sp > 0) {
|
||||
int cur = stack[--sp];
|
||||
int cur = stack[--sp];
|
||||
long nLo = adjLo[cur] & ~reachedLo;
|
||||
long nHi = adjHi[cur] & ~reachedHi;
|
||||
while (nLo != 0) {
|
||||
long lsb = nLo & -nLo;
|
||||
int idx = numberOfTrailingZeros(lsb);
|
||||
reachedLo |= lsb;
|
||||
int idx = numberOfTrailingZeros(lsb);
|
||||
reachedLo |= lsb;
|
||||
stack[sp++] = idx;
|
||||
nLo &= ~lsb;
|
||||
nLo &= ~lsb;
|
||||
}
|
||||
while (nHi != 0) {
|
||||
long lsb = nHi & -nHi;
|
||||
int idx = 64 | numberOfTrailingZeros(lsb);
|
||||
reachedHi |= lsb;
|
||||
int idx = 64 | numberOfTrailingZeros(lsb);
|
||||
reachedHi |= lsb;
|
||||
stack[sp++] = idx;
|
||||
nHi &= ~lsb;
|
||||
nHi &= ~lsb;
|
||||
}
|
||||
}
|
||||
int count = bitCount(reachedLo) + bitCount(reachedHi);
|
||||
@@ -501,29 +522,13 @@ public final class Masker {
|
||||
return best.grid;
|
||||
}//@formatter:off
|
||||
@FunctionalInterface public interface SlotVisitor { void visit(int key, long lo, long hi); }
|
||||
//@formatter:on
|
||||
@AllArgsConstructor
|
||||
public static class Clues {
|
||||
|
||||
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 static Clued parse(String s) {
|
||||
var c = createEmpty();
|
||||
var lines = s.split("\n");
|
||||
for (int r = 0; r < Math.min(lines.length, R); r++) {
|
||||
var line = lines[r];
|
||||
for (int col = 0; col < Math.min(line.length(), C); col++) {
|
||||
char ch = line.charAt(col);
|
||||
if (ch >= '0' && ch <= '4') {
|
||||
int idx = Grid.offset(r, col);
|
||||
byte dir = (byte) (ch - '0');
|
||||
if ((idx & 64) == 0) c.setClueLo(1L << idx, dir);
|
||||
else c.setClueHi(1L << (idx & 63), dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Clued(c);
|
||||
}
|
||||
|
||||
public boolean cluelessLo(int idx) {
|
||||
if (!isClueLo(idx)) return false;
|
||||
clearClueLo(~(1L << idx));
|
||||
@@ -536,10 +541,8 @@ public final class Masker {
|
||||
}
|
||||
public boolean hasRoomForClue(int key, long packed) {
|
||||
if (packed == X || !notClue(packed & 0x7FL) || !notClue((packed >>> 7) & 0x7FL)) return false;
|
||||
final boolean[] res = {false};
|
||||
if (Slotinfo.increasing(key)) processSlot(this, (k, lo, hi) -> res[0] = true, key);
|
||||
else processSlotRev(this, (k, lo, hi) -> res[0] = true, key);
|
||||
return res[0];
|
||||
if (Slotinfo.increasing(key)) if (!validSlot(lo, hi, MIN_LEN, key)) return false;
|
||||
return validSlotRev(lo, hi, MIN_LEN, key);
|
||||
}
|
||||
|
||||
public void setClueLo(long mask, byte idx) {
|
||||
@@ -586,16 +589,18 @@ public final class Masker {
|
||||
}
|
||||
public Grid toGrid() { return new Grid(new byte[SwedishGenerator.SIZE], lo, hi); }
|
||||
public boolean isValid(int minLen) {
|
||||
class ValidationVisitor implements SlotVisitor {
|
||||
boolean ok = true;
|
||||
@Override
|
||||
public void visit(int key, long lo, long hi) {
|
||||
if (bitCount(lo) + bitCount(hi) < minLen) ok = false;
|
||||
}
|
||||
}
|
||||
ValidationVisitor v = new ValidationVisitor();
|
||||
forEachSlot(v);
|
||||
return v.ok;
|
||||
for (var l = lo & ~xlo & ~rlo & vlo; l != X; l &= l - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(numberOfTrailingZeros(l), 1))) return false;
|
||||
for (var l = lo & ~xlo & ~rlo & ~vlo; l != X; l &= l - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(numberOfTrailingZeros(l), 0))) return false;
|
||||
for (var l = lo & ~xlo & rlo & ~vlo; l != X; l &= l - 1) if (!validSlotRev(lo, hi, minLen, Slot.packSlotKey(numberOfTrailingZeros(l), 2))) return false;
|
||||
for (var l = lo & ~xlo & rlo & vlo; l != X; l &= l - 1) if (!validSlotRev(lo, hi, minLen, Slot.packSlotKey(numberOfTrailingZeros(l), 3))) return false;
|
||||
for (var l = lo & xlo & ~rlo & ~vlo; l != X; l &= l - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(numberOfTrailingZeros(l), 4))) return false;
|
||||
|
||||
for (var h = hi & ~xhi & ~rhi & vhi; h != X; h &= h - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(64 | numberOfTrailingZeros(h), 1))) return false;
|
||||
for (var h = hi & ~xhi & ~rhi & ~vhi; h != X; h &= h - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(64 | numberOfTrailingZeros(h), 0))) return false;
|
||||
for (var h = hi & ~xhi & rhi & ~vhi; h != X; h &= h - 1) if (!validSlotRev(lo, hi, minLen, Slot.packSlotKey((64 | numberOfTrailingZeros(h)), 2))) return false;
|
||||
for (var h = hi & ~xhi & rhi & vhi; h != X; h &= h - 1) if (!validSlotRev(lo, hi, minLen, Slot.packSlotKey((64 | numberOfTrailingZeros(h)), 3))) return false;
|
||||
for (var h = hi & xhi & ~rhi & ~vhi; h != X; h &= h - 1) if (!validSlot(lo, hi, minLen, Slot.packSlotKey(64 | numberOfTrailingZeros(h), 4))) return false;
|
||||
return true;
|
||||
}
|
||||
public void forEachSlot(SlotVisitor visitor) {
|
||||
for (var l = lo & ~xlo & ~rlo & vlo; l != X; l &= l - 1) processSlot(this, visitor, Slot.packSlotKey(numberOfTrailingZeros(l), 1));
|
||||
@@ -603,7 +608,7 @@ public final class Masker {
|
||||
for (var l = lo & ~xlo & rlo & ~vlo; l != X; l &= l - 1) processSlotRev(this, visitor, Slot.packSlotKey(numberOfTrailingZeros(l), 2));
|
||||
for (var l = lo & ~xlo & rlo & vlo; l != X; l &= l - 1) processSlotRev(this, visitor, Slot.packSlotKey(numberOfTrailingZeros(l), 3));
|
||||
for (var l = lo & xlo & ~rlo & ~vlo; l != X; l &= l - 1) processSlot(this, visitor, Slot.packSlotKey(numberOfTrailingZeros(l), 4));
|
||||
|
||||
|
||||
for (var h = hi & ~xhi & ~rhi & vhi; h != X; h &= h - 1) processSlot(this, visitor, Slot.packSlotKey(64 | numberOfTrailingZeros(h), 1));
|
||||
for (var h = hi & ~xhi & ~rhi & ~vhi; h != X; h &= h - 1) processSlot(this, visitor, Slot.packSlotKey(64 | numberOfTrailingZeros(h), 0));
|
||||
for (var h = hi & ~xhi & rhi & ~vhi; h != X; h &= h - 1) processSlotRev(this, visitor, Slot.packSlotKey((64 | numberOfTrailingZeros(h)), 2));
|
||||
@@ -640,11 +645,10 @@ public final class Masker {
|
||||
|
||||
static final int BIT_FOR_DIR = 3;
|
||||
public 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 bitCount(lo) + bitCount(hi); }
|
||||
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
|
||||
public static int dir(int key) { return key & 7; }
|
||||
public IntStream walk() { return Gridded.walk((byte) key, lo, hi); }
|
||||
public static boolean horiz(int d) { return (d & 1) != 0 && (d & 4) == 0; }
|
||||
public static int packSlotKey(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
|
||||
public static int length(long lo, long hi) { return bitCount(lo) + bitCount(hi); }
|
||||
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
|
||||
public static int dir(int key) { return key & 7; }
|
||||
public static boolean horiz(int d) { return (d & 1) != 0 && (d & 4) == 0; }
|
||||
public static int packSlotKey(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user