introduce bitloops

This commit is contained in:
mike
2026-01-20 07:08:31 +01:00
parent 47ead135d3
commit a764f45041
83 changed files with 205106 additions and 175 deletions

View File

@@ -2,16 +2,11 @@ package puzzle;
import module java.base;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.Delegate;
import lombok.val;
import puzzle.Export.Gridded.Replacar.Cell;
import puzzle.Export.LetterVisit.LetterAt;
import puzzle.Masker.Clues;
import puzzle.SwedishGenerator.Dict;
import puzzle.SwedishGenerator.DictEntry;
import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Grid;
import puzzle.SwedishGenerator.Slotinfo;
@@ -35,12 +30,12 @@ import static puzzle.SwedishGenerator.X;
*/
public record Export() {
public static final ThreadLocal<byte[]> BYTES = ThreadLocal.withInitial(() -> new byte[SwedishGenerator.MAX_WORD_LENGTH]);
static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2;
static final byte CLUE_LEFT = 3;
static final byte CLUE_LEFT_TOP = 4;
public static final ThreadLocal<byte[]> BYTES = ThreadLocal.withInitial(() -> new byte[SwedishGenerator.MAX_WORD_LENGTH]);
static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2;
static final byte CLUE_LEFT = 3;
static final byte CLUE_LEFT_TOP = 4;
static final byte CLUE_RIGHT_TOP = 5;
static int HI(int in) { return in | 64; }
static char LETTER(int in) { return (char) (in | 64); }
@@ -240,8 +235,8 @@ public record Export() {
public record WordOut(String word, int[] cell, int startRow, int startCol, char direction, int arrowRow, int arrowCol, boolean isReversed, int complex, String[] clue) {
public WordOut(long l, int startRow, int startCol, char d, int arrowRow, int arrowCol, boolean isReversed, byte[] bytes) {
val meta = Meta.readRecord(Meta.shardKey(l), Lemma.unpackShardIndex(l));
public WordOut(Path shard,long l, int startRow, int startCol, char d, int arrowRow, int arrowCol, boolean isReversed, byte[] bytes) {
val meta = Meta.readRecord(shard, Lemma.unpackShardIndex(l));
this(Lemma.asWord(l, bytes), new int[]{ arrowRow, arrowCol, startRow, startCol }, startRow, startCol, d, arrowRow, arrowCol, isReversed,
meta.simpel(), meta.clues());
}
@@ -249,15 +244,27 @@ public record Export() {
public record ExportedPuzzle(String[] grid, WordOut[] words, int difficulty, Rewards rewards) { }
public record PuzzleResult(Clued clues, Gridded grid, Slotinfo[] slots, FillResult filled) {
public record PuzzleResult(Clued clues, Gridded grid, Slotinfo[] slots, FillResult filled, int thress) {
static public long calcSimpel(Slotinfo[] slots) {
public Path shardKey(long word) {
return shardKey(this.thress, word);
}
public static Path shardKey(int thress, long word) {
if (thress <= 0)
return Path.of("src/main/generated-sources/puzzle").resolve(Lemma.unpackSize(word) + 1 + ".idx");
else
return Path.of("src/main/generated-sources/puzzle/dict" + thress).resolve(Lemma.unpackSize(word) + 1 + ".idx");
}
public long calcSimpel( Slotinfo[] slots) {
return calcSimpel(thress, slots);
}
static public long calcSimpel(int thress, Slotinfo[] slots) {
int k = 0;
long simpel = 0L;
for (var n = 1; n < slots.length; n++) {
if (slots[n].assign().w != X) {
k++;
simpel += Meta.readRecord(Meta.shardKey(slots[n].assign().w), Lemma.unpackShardIndex(slots[n].assign().w)).simpel();
simpel += Meta.readRecord(shardKey(thress,slots[n].assign().w), Lemma.unpackShardIndex(slots[n].assign().w)).simpel();
}
}
simpel = k == 0 ? 0 : simpel / k;
@@ -304,6 +311,7 @@ public record Export() {
int MIN_R = minR, MIN_C = minC;
val bytes = BYTES.get();
var wordsOut = Arrays.stream(placed).map(p -> new WordOut(
shardKey( p.lemma),
p.lemma,
p.startRow() - MIN_R,
p.startCol() - MIN_C,
@@ -316,92 +324,4 @@ public record Export() {
}
}
interface Dicts {
static Dict makeDict(long[] wordz) {
var index = new DictEntryDTO[SwedishGenerator.MAX_WORD_LENGTH_PLUS_ONE];
Arrays.setAll(index, DictEntryDTO::new);
for (var lemma : wordz) {
var L = Lemma.unpackSize(lemma) + 1;//Lemma.unpackSize(lemma) + 2;
val entry = index[L];
val idx = entry.words().size();
val pos = entry.pos();
entry.words().add(lemma);
int i = 0;
for (long w = lemma & Lemma.LETTER_MASK; w != 0; w >>>= 5, i++) {
pos[i][(int) ((w & 31) - 1)].add(idx);
}
}
for (int i = 2; i < index.length; i++) if (index[i].words().size() <= 0) throw new RuntimeException("No words for length " + i);
return new Dict(Arrays.stream(index).map(i -> {
var words = i.words().toArray();
int numWords = words.length;
int numLongs = (numWords + 63) >>> 6;
var bitsets = new long[i.pos().length * 26][numLongs];
for (int p = 0; p < i.pos().length; p++) {
for (int l = 0; l < 26; l++) {
var list = i.pos()[p][l];
var bs = bitsets[p * 26 + l];
for (int k = 0; k < list.size(); k++) {
int wordIdx = list.data()[k];
bs[wordIdx >>> 6] |= (1L << (wordIdx & 63));
}
}
}
return new DictEntry(words, bitsets, words.length, (words.length + 63) >>> 6);
}).toArray(DictEntry[]::new),
Arrays.stream(index).mapToInt(i -> i.words().size()).sum());
}
}
record DictEntryDTO(LongArrayList words, IntListDTO[][] pos) {
public DictEntryDTO(int L) {
this(new LongArrayList(1024), new IntListDTO[L][26]);
for (var i = 0; i < L; i++) for (var j = 0; j < 26; j++) pos[i][j] = new IntListDTO();
}
}
@Getter
@Accessors(fluent = true)
@NoArgsConstructor
static final class IntListDTO {
int[] data = new int[8];
int size = 0;
public IntListDTO(int size) {
data = new int[size];
}
void add(int v) {
if (size >= data.length) data = Arrays.copyOf(data, data.length * 2);
data[size++] = v;
}
int[] toArray() { return Arrays.copyOf(data, size); }
}
static final class LongArrayList {
long[] a;
int size;
LongArrayList(int initialCapacity) {
if (initialCapacity < 0) throw new IllegalArgumentException();
a = new long[initialCapacity];
}
int size() { return size; }
void add(long v) {
if (size == a.length) grow();
a[size++] = v;
}
void grow() {
int newCap = a.length == 0 ? 1 : a.length * 2;
long[] n = new long[newCap];
System.arraycopy(a, 0, n, 0, size);
a = n;
}
long[] toArray() { return Arrays.copyOf(a, this.size); }
}
}