introduce bitloops

This commit is contained in:
mike
2026-01-22 18:47:04 +01:00
parent a659bd5162
commit 2295a7d97c
71 changed files with 254 additions and 205151 deletions

View File

@@ -1,13 +1,14 @@
package puzzle;
import gen.GenDict;
import gen.GenerateConst;
import gen.GenerateNeighbors;
import anno.ConstGen;
import anno.GenerateNeighbor;
import anno.GenerateNeighbors;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.val;
import precomp.Neighbors9x8;
import static java.lang.Long.*;
import static java.lang.Long.bitCount;
import static java.lang.Long.numberOfLeadingZeros;
import static java.lang.Long.numberOfTrailingZeros;
import static java.nio.charset.StandardCharsets.US_ASCII;
@@ -27,24 +28,21 @@ import static java.nio.charset.StandardCharsets.US_ASCII;
* javac SwedishGenerator.java
* java SwedishGenerator [--seed N] [--pop N] [--gens N] [--tries N] [--words word-list.txt]
*/
@SuppressWarnings("ALL")
@GenerateNeighbors(C = 9, R = 8, packageName = "precomp", className = "Neighbors9x8", MIN_LEN = 2)
@GenerateConst(C = 9, R = 8, packageName = "precomp", className = "Const9x8")
@GenDict(
packageName = "puzzle.dict950",
className = "DictData950",
scv = "/home/mike/dev/puzzle-generator/nl_score_hints_v4.csv",
simpleMax = 950,
minLen = 2,
maxLen = 8
)
@ConstGen(C = 9, R = 8, packageName = "precomp", className = "Const9x8")
@GenerateNeighbors({
@GenerateNeighbor(C = 9, R = 8, packageName = "precomp", className = "Neighbors9x8", MIN_LEN = 2),
@GenerateNeighbor(C = 4, R = 3, packageName = "precomp", className = "Neighbors4x3", MIN_LEN = 2)
})
/*@GenerateShapedCopies(
shapes = { "precomp.Neighbors9x8", "precomp.Neighbors4x3" }
)*/
public record SwedishGenerator() {
public static final long X = 0L;
public static final int SIZE = Neighbors9x8.SIZE;
public static final int MAX_TRIES_PER_SLOT = 500;// MAX_TRIES_PER_SLOT;
public static final long RANGE_0_SIZE = Neighbors9x8.RANGE_0_SIZE;
public static final long RANGE_0_624 = Neighbors9x8.RANGE_0_624;
public static final long X = 0L;
public static final int SIZE = Neighbors9x8.SIZE;
public static final int MAX_TRIES_PER_SLOT = 500;// MAX_TRIES_PER_SLOT;
public static final long RANGE_0_SIZE = Neighbors9x8.RANGE_0_SIZE;
public static final long RANGE_0_624 = Neighbors9x8.RANGE_0_624;
public static boolean isLo(int n) { return (n & 64) == 0; }
interface Bit1029 {
@@ -57,12 +55,12 @@ public record SwedishGenerator() {
}
//@formatter:off
public static record Dict(DictEntry[] index, int length) { }
public static record DictEntry(long[] words, long[][] posBitsets, int length, int numlong) { }
public record Dict(DictEntry[] index, int length) { }
public record DictEntry(long[] words, long[][] posBitsets, int length, int numlong) { }
@AllArgsConstructor @NoArgsConstructor static final class Assign { long w; }
public static final class FillStats { public double simplicity; }
@AllArgsConstructor public static final class Grid { public final byte[] g; public long lo, hi; }
public static record FillResult(boolean ok, long nodes, long backtracks, int lastMRV, long elapsed, FillStats stats) { }
public record FillResult(boolean ok, long nodes, long backtracks, int lastMRV, long elapsed, FillStats stats) { }
//@formatter:on
public static final class Rng {
@@ -91,10 +89,10 @@ public record SwedishGenerator() {
public int biasedIndexPow3(int N) { return (int) (((Math.min(nextU32(), Math.min(nextU32(), nextU32())) & 0xFFFFFFFFL) * (long) N) >>> 32); }
}
public static interface Lemma {
public interface Lemma {
static final long LETTER_MASK = (1L << 40) - 1; // low 40 bits
static final long INDEX_MASK = (1L << 43) - 1; // 24 bits
long LETTER_MASK = (1L << 40) - 1; // low 40 bits
long LETTER_AND_LENGTH_MASK = (1L << 43) - 1;
static long from(byte[] word) { return packShiftIn(word) | ((long) (word.length - 1) << 40); }
static long pack(long w, int shardIndex) { return w | (((long) shardIndex) << 43) | ((long) length0(w)) << 40; }
@@ -103,23 +101,21 @@ public record SwedishGenerator() {
for (int i = b.length - 1; i >= 0; i--) w = (w << 5) | ((long) b[i] & 31);
return w;
}
static public long from(String word) { return packShiftIn(word.getBytes(US_ASCII)) | ((long) (word.length() - 1) << 40); }
static long from(String word) { return packShiftIn(word.getBytes(US_ASCII)) | ((long) (word.length() - 1) << 40); }
static byte byteAt(long word, int idx) { return (byte) ((word >>> (idx * 5)) & 0b11111L); }
static int length0(long word) { return ((63 - numberOfLeadingZeros(word & LETTER_MASK)) / 5); }
public static String asWord(long word, byte[] bytes) {
static String asWord(long word, byte[] bytes) {
int bi = 0;
for (long w = word & LETTER_MASK; w != 0; w >>>= 5) bytes[bi++] = (byte) ((w & 31) | 64);
return new String(bytes, 0, bi, US_ASCII);
}
static int unpackIndex(long w) { return (int) (w >>> 40); }
static int unpackSize(long w) { return (int) (w >>> 40) & 7; }
static int unpackLetters(long w) { return (int) (w & LETTER_MASK); }
static long pack43(long w) {
return w & INDEX_MASK;
}
static int unpackIndex(long w) { return (int) (w >>> 40); }
static int unpackSize(long w) { return (int) (w >>> 40) & 7; }
static int unpackLetters(long w) { return (int) (w & LETTER_MASK); }
static long packLetterAndLengthBits(long w) { return w & LETTER_AND_LENGTH_MASK; }
}
public static record Slotinfo(int key, long lo, long hi, int score, Assign assign, DictEntry entry, int minL) {
public record Slotinfo(int key, long lo, long hi, int score, Assign assign, DictEntry entry, int minL) {
public static int wordCount(int k, Slotinfo[] arr) {
for (var n = 1; n < arr.length; n++) if (arr[n].assign.w != X) k++;
@@ -289,12 +285,11 @@ public record SwedishGenerator() {
val words = s.entry.words;
long low, top;
if (info != null && info.length > 0) {
var idxs = info;
var L = idxs.length;
var L = info.length;
var tries = Math.min(MAX_TRIES_PER_SLOT, L);
for (var t = 0; t < tries; t++) {
var w = words[idxs[rng.biasedIndexPow3(L - 1)]];
var w = words[info[rng.biasedIndexPow3(L - 1)]];
var lemIdx = Lemma.unpackIndex(w);
if (Bit1029.get(used, lemIdx)) continue;
low = glo;