introduce bitloops
This commit is contained in:
@@ -1,13 +0,0 @@
|
|||||||
package puzzle;
|
|
||||||
|
|
||||||
import gen.GenDict;
|
|
||||||
|
|
||||||
@GenDict(
|
|
||||||
packageName = "puzzle.dict950",
|
|
||||||
className = "DictData950",
|
|
||||||
scv = "/home/mike/dev/puzzle-generator/nl_score_hints_v4.csv",
|
|
||||||
simpleMax = 950,
|
|
||||||
minLen = 2,
|
|
||||||
maxLen = 8
|
|
||||||
)
|
|
||||||
public class DictMark { }
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package puzzle;
|
|
||||||
|
|
||||||
import gen.GenerateDict;
|
|
||||||
|
|
||||||
@GenerateDict(
|
|
||||||
packageName = "puzzle",
|
|
||||||
packageNameDict = "puzzle",
|
|
||||||
classNameDict = "DictData",
|
|
||||||
className = "LemmaData",
|
|
||||||
scv = "nl_score_hints_v4.csv",
|
|
||||||
words = {
|
|
||||||
"EEN", "NAAR", "IEDEREEN", "A", "C", "X", "TEST", "IN", "INE", "INER", "INEREN", "INERENA", "INERENAE",
|
|
||||||
"APPLE", "AXE", "ABC", "ABD", "AZ", "AB",
|
|
||||||
"AT", "CAT", "DOGS", "APPLY", "BANAN", "BANANA", "BANANAS", "BANANASS"
|
|
||||||
},
|
|
||||||
simpleMax=900,
|
|
||||||
minLen = 2,
|
|
||||||
maxLen = 8
|
|
||||||
)
|
|
||||||
public class DictMarker { }
|
|
||||||
@@ -251,8 +251,7 @@ public record Export() {
|
|||||||
return new ExportedPuzzle(grid.exportGrid(_ -> '#', '#'), new WordOut[0], 1, rewards);
|
return new ExportedPuzzle(grid.exportGrid(_ -> '#', '#'), new WordOut[0], 1, rewards);
|
||||||
}
|
}
|
||||||
|
|
||||||
var placed = Arrays.stream(slots).map(slot -> new Placed(slot.assign().w, slot.key(), Gridded.cellWalk((byte) slot.key(), slot.lo(), slot.hi()).toArray())).toArray(
|
var placed = Arrays.stream(slots).map(slot -> new Placed(slot.assign().w, slot.key(), Gridded.cellWalk((byte) slot.key(), slot.lo(), slot.hi()).toArray())).toArray(Placed[]::new);
|
||||||
Placed[]::new);
|
|
||||||
|
|
||||||
// 2) bounding box around all word cells + arrow cells, with 1-cell margin
|
// 2) bounding box around all word cells + arrow cells, with 1-cell margin
|
||||||
int minR = Integer.MAX_VALUE, minC = Integer.MAX_VALUE;
|
int minR = Integer.MAX_VALUE, minC = Integer.MAX_VALUE;
|
||||||
|
|||||||
@@ -114,7 +114,6 @@ public class Main {
|
|||||||
else rebuildIndex();
|
else rebuildIndex();
|
||||||
info("indexUpdated : " + INDEX_FILE);
|
info("indexUpdated : " + INDEX_FILE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
err("Failed to write: " + FILE_NAME);
|
err("Failed to write: " + FILE_NAME);
|
||||||
err("Reason : " + e.getMessage());
|
err("Reason : " + e.getMessage());
|
||||||
System.exit(2);
|
System.exit(2);
|
||||||
@@ -345,7 +344,6 @@ public class Main {
|
|||||||
try {
|
try {
|
||||||
return _attempt(rng, dict, opts);
|
return _attempt(rng, dict, opts);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
System.err.println("Failed to operate" + e.getMessage());
|
System.err.println("Failed to operate" + e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -445,7 +443,6 @@ public class Main {
|
|||||||
Files.writeString(indexPath, content, StandardCharsets.UTF_8);
|
Files.writeString(indexPath, content, StandardCharsets.UTF_8);
|
||||||
info("indexUpdated : " + indexPath);
|
info("indexUpdated : " + indexPath);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
err("Failed to update index.json: " + e.getMessage());
|
err("Failed to update index.json: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -486,12 +483,10 @@ public class Main {
|
|||||||
var pathInIndex = "/puzzles/" + filename;
|
var pathInIndex = "/puzzles/" + filename;
|
||||||
records.add(toIndexRecordJson(id, pathInIndex, date, theme, difficulty, createdAt));
|
records.add(toIndexRecordJson(id, pathInIndex, date, theme, difficulty, createdAt));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
err("Failed to read " + path + ": " + e.getMessage());
|
err("Failed to read " + path + ": " + e.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
err("Failed to list puzzles: " + e.getMessage());
|
err("Failed to list puzzles: " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -502,7 +497,6 @@ public class Main {
|
|||||||
Files.writeString(indexPath, content, StandardCharsets.UTF_8);
|
Files.writeString(indexPath, content, StandardCharsets.UTF_8);
|
||||||
info("Successfully rebuilt index.json with " + records.size() + " records.");
|
info("Successfully rebuilt index.json with " + records.size() + " records.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
err("Failed to write index.json: " + e.getMessage());
|
err("Failed to write index.json: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ import static puzzle.SwedishGenerator.*;
|
|||||||
public final class Masker {
|
public final class Masker {
|
||||||
|
|
||||||
public static final rci[] IT = Neighbors9x8.IT;
|
public static final rci[] IT = Neighbors9x8.IT;
|
||||||
|
public static final long[] PATH_LO = Neighbors9x8.PATH_LO;
|
||||||
|
public static final long[] PATH_HI = Neighbors9x8.PATH_HI;
|
||||||
|
public static final long MASK_LO = -1L;
|
||||||
|
public static final long MASK_HI = Neighbors9x8.MASK_HI;//(1L << (SIZE - 64)) - 1;
|
||||||
private final Rng rng;
|
private final Rng rng;
|
||||||
private final int[] stack;
|
private final int[] stack;
|
||||||
private final Clues cache;
|
private final Clues cache;
|
||||||
|
|||||||
@@ -23,12 +23,6 @@ public class Meta {
|
|||||||
|
|
||||||
static final ByteOrder ORDER = ByteOrder.BIG_ENDIAN;
|
static final ByteOrder ORDER = ByteOrder.BIG_ENDIAN;
|
||||||
|
|
||||||
static record ShardRec(String word, long w, int simpel, String[] clues) { }
|
|
||||||
|
|
||||||
static final Path projectRoot = Path.of("").toAbsolutePath().normalize(); // current working dir
|
|
||||||
//static final Path dir = projectRoot.resolve("src/main/resources/shards");
|
|
||||||
//static final Path shardData = dir.resolve("shard0.data");
|
|
||||||
//static final Path shardMap = dir.resolve("shard0.map");
|
|
||||||
static final Path dir = detectShardDir();
|
static final Path dir = detectShardDir();
|
||||||
static final Path shardData = dir.resolve("shard0.data");
|
static final Path shardData = dir.resolve("shard0.data");
|
||||||
static final Path shardMap = dir.resolve("shard0.map");
|
static final Path shardMap = dir.resolve("shard0.map");
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package puzzle;
|
package puzzle;
|
||||||
|
|
||||||
|
import gen.GenDict;
|
||||||
|
import gen.GenerateConst;
|
||||||
|
import gen.GenerateNeighbors;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
@@ -25,18 +28,24 @@ import static java.nio.charset.StandardCharsets.US_ASCII;
|
|||||||
* java SwedishGenerator [--seed N] [--pop N] [--gens N] [--tries N] [--words word-list.txt]
|
* java SwedishGenerator [--seed N] [--pop N] [--gens N] [--tries N] [--words word-list.txt]
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("ALL")
|
@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
|
||||||
|
)
|
||||||
public record SwedishGenerator() {
|
public record SwedishGenerator() {
|
||||||
|
|
||||||
public static final long X = 0L;
|
public static final long X = 0L;
|
||||||
public static final int LOG_EVERY_MS = 200;
|
|
||||||
public static final int BAR_LEN = 22;
|
|
||||||
public static final int C = Neighbors9x8.C;
|
public static final int C = Neighbors9x8.C;
|
||||||
public static final int R = Neighbors9x8.R;
|
public static final int R = Neighbors9x8.R;
|
||||||
public static final int SIZE = Neighbors9x8.SIZE;// ~18
|
public static final int SIZE = Neighbors9x8.SIZE;// ~18
|
||||||
public static final int SIZE_MIN_1 = Neighbors9x8.SIZE_MIN_1;// ~18
|
public static final int SIZE_MIN_1 = Neighbors9x8.SIZE_MIN_1;// ~18
|
||||||
public static final double SIZED = Neighbors9x8.SIZED;// ~18
|
public static final double SIZED = Neighbors9x8.SIZED;// ~18
|
||||||
public static final long MASK_LO = -1L;
|
|
||||||
public static final long MASK_HI = Neighbors9x8.MASK_HI;//(1L << (SIZE - 64)) - 1;
|
|
||||||
public static final int MAX_WORD_LENGTH = Neighbors9x8.R;
|
public static final int MAX_WORD_LENGTH = Neighbors9x8.R;
|
||||||
public static final int MAX_WORD_LENGTH_PLUS_ONE = MAX_WORD_LENGTH + 1;
|
public static final int MAX_WORD_LENGTH_PLUS_ONE = MAX_WORD_LENGTH + 1;
|
||||||
public static final int MIN_LEN = Neighbors9x8.MIN_LEN;//Config.MIN_LEN;
|
public static final int MIN_LEN = Neighbors9x8.MIN_LEN;//Config.MIN_LEN;
|
||||||
@@ -65,9 +74,6 @@ public record SwedishGenerator() {
|
|||||||
public static record FillResult(boolean ok, long nodes, long backtracks, int lastMRV, long elapsed, FillStats stats) { }
|
public static record FillResult(boolean ok, long nodes, long backtracks, int lastMRV, long elapsed, FillStats stats) { }
|
||||||
//@formatter:on
|
//@formatter:on
|
||||||
|
|
||||||
public static final long[] PATH_LO = Neighbors9x8.PATH_LO;
|
|
||||||
public static final long[] PATH_HI = Neighbors9x8.PATH_HI;
|
|
||||||
|
|
||||||
public static final class Rng {
|
public static final class Rng {
|
||||||
|
|
||||||
private int x;
|
private int x;
|
||||||
@@ -115,7 +121,6 @@ public record SwedishGenerator() {
|
|||||||
return new String(bytes, 0, bi, US_ASCII);
|
return new String(bytes, 0, bi, US_ASCII);
|
||||||
}
|
}
|
||||||
static int unpackIndex(long w) { return (int) (w >>> 40); }
|
static int unpackIndex(long w) { return (int) (w >>> 40); }
|
||||||
static int unpackShardIndex(long w) { return (int) (w >>> 43); }
|
|
||||||
static int unpackSize(long w) { return (int) (w >>> 40) & 7; }
|
static int unpackSize(long w) { return (int) (w >>> 40) & 7; }
|
||||||
static int unpackLetters(long w) { return (int) (w & LETTER_MASK); }
|
static int unpackLetters(long w) { return (int) (w & LETTER_MASK); }
|
||||||
static long pack43(long w) {
|
static long pack43(long w) {
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
// file: app/Trigger.java
|
|
||||||
package puzzle;
|
|
||||||
|
|
||||||
import gen.GenerateConst;
|
|
||||||
@GenerateConst(C = 9, R = 8, packageName = "precomp", className = "Const9x8")
|
|
||||||
public final class TriggerConstants { }
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package puzzle;
|
|
||||||
|
|
||||||
import gen.GenerateNeighbors;
|
|
||||||
@GenerateNeighbors(C = 4, R = 3, packageName = "precomp", className = "Neighbors4x3", MIN_LEN = 2)
|
|
||||||
public final class TriggerNeighbors4x3 { }
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// file: app/Trigger.java
|
|
||||||
package puzzle;
|
|
||||||
|
|
||||||
import gen.GenerateNeighbors;
|
|
||||||
@GenerateNeighbors(C = 9, R = 8, packageName = "precomp", className = "Neighbors9x8", MIN_LEN = 2)
|
|
||||||
public final class TriggerNeighbors9x8 { }
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
package puzzle;
|
|
||||||
|
|
||||||
import lombok.val;
|
|
||||||
import puzzle.DictJavaGeneratorMulti.CsvIndexService;
|
|
||||||
import puzzle.Meta.ShardRec;
|
|
||||||
import puzzle.SwedishGenerator.Lemma;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.StandardOpenOption;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
||||||
public class BuildClueAndSimpelIndex {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
val records = buildDict(Path.of("nl_score_hints_v4.csv"));
|
|
||||||
|
|
||||||
buildShard(records);
|
|
||||||
|
|
||||||
for (var qRaw : List.of("FIETS", "HUIS", "KIWI")) {
|
|
||||||
var w = Lemma.from(qRaw);
|
|
||||||
var i = Meta.findIndexInMapMmap(w);
|
|
||||||
|
|
||||||
System.out.println("\nQuery: " + qRaw + " (norm=" + qRaw + ") w=" + w + " -> i=" + i);
|
|
||||||
if (i >= 0) {
|
|
||||||
var rec = Meta.readRecord(w, i);
|
|
||||||
System.out.println(" simpel=" + rec.simpel());
|
|
||||||
System.out.println(" clues=" + Arrays.toString(rec.clues()));
|
|
||||||
} else {
|
|
||||||
System.out.println(" NOT FOUND");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\nFiles written to: " + Meta.dir);
|
|
||||||
System.out.println(" " + Meta.shardData);
|
|
||||||
System.out.println(" " + Meta.shardMap);
|
|
||||||
} // --- Demo main ---
|
|
||||||
// --- Build demo files: shard.data + shard.map ---
|
|
||||||
static void buildShard(List<ShardRec> records) throws IOException {
|
|
||||||
records = new ArrayList<>(records);
|
|
||||||
// map is sorted by w; record index i == positie in deze gesorteerde lijst
|
|
||||||
records.sort(Comparator.comparingLong(ShardRec::w));
|
|
||||||
|
|
||||||
var n = records.size();
|
|
||||||
List<byte[]> recBytes = new ArrayList<>(n);
|
|
||||||
var offsets = new int[n];
|
|
||||||
|
|
||||||
var off = 0;
|
|
||||||
for (var i = 0; i < n; i++) {
|
|
||||||
var r = records.get(i);
|
|
||||||
// schrijf het echte woord weg + simpel + JSON hints
|
|
||||||
var line = r.word() + "\t" + r.simpel() + "\t" + Meta.GSON.toJson(r.clues());
|
|
||||||
var bytes = line.getBytes(StandardCharsets.UTF_8);
|
|
||||||
recBytes.add(bytes);
|
|
||||||
offsets[i] = off;
|
|
||||||
|
|
||||||
off += bytes.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
var headerSize = 12L;
|
|
||||||
var tableSize = (long) n * 4L;
|
|
||||||
var dataStart = headerSize + tableSize;
|
|
||||||
|
|
||||||
try (var ch = FileChannel.open(Meta.shardData, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
|
|
||||||
|
|
||||||
var hdr = ByteBuffer.allocate(12).order(Meta.ORDER);
|
|
||||||
hdr.putInt(Meta.SHARD_MAGIC).putInt(Meta.VERSION).putInt(n);
|
|
||||||
hdr.flip();
|
|
||||||
ch.write(hdr);
|
|
||||||
|
|
||||||
var table = ByteBuffer.allocate(n * 4).order(Meta.ORDER);
|
|
||||||
for (var i = 0; i < n; i++) table.putInt(offsets[i]);
|
|
||||||
table.flip();
|
|
||||||
ch.write(table);
|
|
||||||
|
|
||||||
ch.position(dataStart);
|
|
||||||
for (var b : recBytes) ch.write(ByteBuffer.wrap(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
try (var ch = FileChannel.open(Meta.shardMap, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
|
|
||||||
|
|
||||||
var hdr = ByteBuffer.allocate(12).order(Meta.ORDER);
|
|
||||||
hdr.putInt(Meta.MAP_MAGIC).putInt(Meta.VERSION).putInt(n);
|
|
||||||
hdr.flip();
|
|
||||||
ch.write(hdr);
|
|
||||||
|
|
||||||
var keys = ByteBuffer.allocate(n * 8).order(Meta.ORDER);
|
|
||||||
for (var r : records) keys.putLong(r.w());
|
|
||||||
keys.flip();
|
|
||||||
ch.write(keys);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private static List<ShardRec> buildDict(Path wordsPath) throws IOException {
|
|
||||||
var recs = new ArrayList<ShardRec>();
|
|
||||||
try (var lines = Files.lines(wordsPath, StandardCharsets.UTF_8)) {
|
|
||||||
lines.forEach(line -> {
|
|
||||||
var parts = line.split(",", 4);
|
|
||||||
var word = parts[0].trim();
|
|
||||||
long w = SwedishGenerator.Lemma.from(word.getBytes(US_ASCII));
|
|
||||||
if (!word.equals(SwedishGenerator.Lemma.asWord(w, Export.BYTES.get()))) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
var rawClue = parts[3].trim();
|
|
||||||
if (rawClue.startsWith("\"") && rawClue.endsWith("\"")) {
|
|
||||||
rawClue = rawClue.substring(1, rawClue.length() - 1).replace("\"\"", "\"");
|
|
||||||
}
|
|
||||||
var clues = Meta.GSON.fromJson(rawClue, String[].class);
|
|
||||||
var simpel = Integer.parseInt(parts[CsvIndexService.SIMPEL_IDX].trim());
|
|
||||||
recs.add(new ShardRec(word, w, simpel, clues));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return recs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,62 +12,6 @@ import static java.nio.charset.StandardCharsets.US_ASCII;
|
|||||||
|
|
||||||
public final class DictJavaGeneratorMulti {
|
public final class DictJavaGeneratorMulti {
|
||||||
|
|
||||||
// Smaller = more files, but safer for javac/class limits.
|
|
||||||
private static final int WORDS_CHUNK = 8_192;
|
|
||||||
private static final int POS_CHUNK = 8_192;
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
var THRESS = 900;
|
|
||||||
var wordsFile = Path.of(args.length > 0 ? args[0] : "nl_score_hints_v4.csv");
|
|
||||||
var outDir = Path.of(args.length > 1 ? args[1] : "src/main/generated-sources/puzzle/dict" + THRESS);
|
|
||||||
var pkg = "puzzle.dict" + THRESS;
|
|
||||||
var builders = new HashMap<String, ShardBuilder>(16);
|
|
||||||
|
|
||||||
var dict = buildDict(wordsFile, builders, THRESS);
|
|
||||||
|
|
||||||
Files.createDirectories(outDir);
|
|
||||||
|
|
||||||
// Generate L2..L8
|
|
||||||
for (var L = 2; L <= 8; L++) {
|
|
||||||
var entry = dict.index()[L];
|
|
||||||
if (entry == null || entry.words() == null || entry.words().length == 0) {
|
|
||||||
throw new IllegalStateException("No words for length " + L);
|
|
||||||
}
|
|
||||||
writeLengthBundle(outDir, pkg, L, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Aggregator
|
|
||||||
writeAggregator(outDir, pkg, "DictData", dict.length(), THRESS);
|
|
||||||
System.out.println("Generated sources into: " + outDir.toAbsolutePath());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static String shardKey(long word) {
|
|
||||||
return "" + Lemma.unpackSize(word) + 1;
|
|
||||||
}
|
|
||||||
private static SwedishGenerator.Dict buildDict(Path wordsPath, HashMap<String, ShardBuilder> builders, int thress) throws IOException {
|
|
||||||
var map = new LongArrayList(100_000);
|
|
||||||
try (var lines = Files.lines(wordsPath, StandardCharsets.UTF_8)) {
|
|
||||||
lines.forEach(line -> {
|
|
||||||
var parts = line.split(",", 4);
|
|
||||||
var word = parts[0].trim();
|
|
||||||
var w = SwedishGenerator.Lemma.from(word.getBytes(US_ASCII));
|
|
||||||
if (!word.equals(SwedishGenerator.Lemma.asWord(w, Export.BYTES.get()))) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
var score = Integer.parseInt(parts[1].trim());
|
|
||||||
var simpel = Integer.parseInt(parts[CsvIndexService.SIMPEL_IDX].trim());
|
|
||||||
if (score < 1 || simpel > thress) {
|
|
||||||
if (Main.VERBOSE) System.err.println("Word too complex: " + line);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = shardKey(w);
|
|
||||||
var sb = builders.computeIfAbsent(key, k -> new ShardBuilder());
|
|
||||||
map.add(Lemma.pack(w, sb.addRecord()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return Dicts.makeDict(map.toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Dicts {
|
interface Dicts {
|
||||||
|
|
||||||
@@ -107,163 +51,6 @@ public final class DictJavaGeneratorMulti {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class ShardBuilder {
|
|
||||||
|
|
||||||
int c;
|
|
||||||
int addRecord() { return c++; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeAggregator(Path outDir, String pkg, String cls, int totalLen, int thress) throws IOException {
|
|
||||||
var out = outDir.resolve(cls + ".java");
|
|
||||||
try (var w = writer(out)) {
|
|
||||||
w.write("package " + pkg + ";\n\n");
|
|
||||||
w.write("public final class " + cls + " {\n");
|
|
||||||
w.write(" private " + cls + "() {}\n\n");
|
|
||||||
w.write(" public static final puzzle.SwedishGenerator.Dict DICT" + thress + " = build();\n\n");
|
|
||||||
w.write(" private static puzzle.SwedishGenerator.Dict build() {\n");
|
|
||||||
w.write(" puzzle.SwedishGenerator.DictEntry[] idx = new puzzle.SwedishGenerator.DictEntry[puzzle.SwedishGenerator.MAX_WORD_LENGTH_PLUS_ONE];\n");
|
|
||||||
for (var L = 2; L <= 8; L++) w.write(" idx[" + L + "] = DictDataL" + L + ".entry();\n");
|
|
||||||
w.write(" return new puzzle.SwedishGenerator.Dict(idx, " + totalLen + ");\n");
|
|
||||||
w.write(" }\n");
|
|
||||||
w.write("}\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeLengthBundle(Path outDir, String pkg, int L, SwedishGenerator.DictEntry e) throws IOException {
|
|
||||||
var words = e.words();
|
|
||||||
|
|
||||||
// flatten posBitsets: [rows][cols] -> flat[]
|
|
||||||
var bs = e.posBitsets();
|
|
||||||
var rows = bs.length;
|
|
||||||
var cols = bs[0].length;
|
|
||||||
var flat = new long[rows * cols];
|
|
||||||
var t = 0;
|
|
||||||
for (var r = 0; r < rows; r++) {
|
|
||||||
System.arraycopy(bs[r], 0, flat, t, cols);
|
|
||||||
t += cols;
|
|
||||||
}
|
|
||||||
|
|
||||||
var base = "DictDataL" + L;
|
|
||||||
|
|
||||||
// 1) chunk classes
|
|
||||||
var wChunks = writeChunkClasses(outDir, pkg, base + "W", words, WORDS_CHUNK);
|
|
||||||
var pChunks = writeChunkClasses(outDir, pkg, base + "P", flat, POS_CHUNK);
|
|
||||||
|
|
||||||
// 2) assembler class
|
|
||||||
writeLengthAssembler(outDir, pkg, base, L, rows, cols, words.length, flat.length, wChunks, pChunks);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes classes like Prefix0..PrefixN each with static long[] DATA. Returns chunk count. */
|
|
||||||
private static int writeChunkClasses(Path outDir, String pkg, String prefix, long[] data, int chunkSize) throws IOException {
|
|
||||||
var chunks = (data.length + chunkSize - 1) / chunkSize;
|
|
||||||
for (var ci = 0; ci < chunks; ci++) {
|
|
||||||
var from = ci * chunkSize;
|
|
||||||
var to = Math.min(data.length, from + chunkSize);
|
|
||||||
|
|
||||||
var out = outDir.resolve(prefix + ci + ".java");
|
|
||||||
try (var w = writer(out)) {
|
|
||||||
w.write("package " + pkg + ";\n\n");
|
|
||||||
w.write("public final class " + prefix + ci + " {\n");
|
|
||||||
w.write(" private " + prefix + ci + "() {}\n");
|
|
||||||
|
|
||||||
w.write(" public static long[] get() {\n");
|
|
||||||
w.write(" return new long[] { \n");
|
|
||||||
for (var i = from; i < to; i++) {
|
|
||||||
w.write(" " + toLongLiteral(data[i]) + (i + 1 < to ? "," : "") + "\n");
|
|
||||||
}
|
|
||||||
w.write(" };\n");
|
|
||||||
w.write(" }\n");
|
|
||||||
w.write("}\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return chunks;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeLengthAssembler(Path outDir, String pkg, String cls, int L,
|
|
||||||
int rows, int cols,
|
|
||||||
int wordsLen, int posLen,
|
|
||||||
int wChunks, int pChunks) throws IOException {
|
|
||||||
var out = outDir.resolve(cls + ".java");
|
|
||||||
try (var w = writer(out)) {
|
|
||||||
w.write("package " + pkg + ";\n\n");
|
|
||||||
w.write("public final class " + cls + " {\n");
|
|
||||||
w.write(" private " + cls + "() {}\n\n");
|
|
||||||
|
|
||||||
w.write(" static final int LEN = " + L + ";\n");
|
|
||||||
w.write(" static final int ROWS = " + rows + ";\n");
|
|
||||||
w.write(" static final int COLS = " + cols + ";\n");
|
|
||||||
w.write(" static final int WORDS_LEN = " + wordsLen + ";\n");
|
|
||||||
w.write(" static final int POS_LEN = " + posLen + ";\n\n");
|
|
||||||
|
|
||||||
// assemble words
|
|
||||||
w.write(" private static long[] words() {\n");
|
|
||||||
var wPrefix = "DictDataL" + L + "W";
|
|
||||||
if (wChunks == 1) {
|
|
||||||
w.write(" return " + wPrefix + "0.get();\n");
|
|
||||||
} else {
|
|
||||||
w.write(" long[] out = new long[WORDS_LEN];\n");
|
|
||||||
w.write(" int k = 0;\n");
|
|
||||||
for (var ci = 0; ci < wChunks; ci++) {
|
|
||||||
w.write(" k = copy(out, k, " + wPrefix + ci + ".get());\n");
|
|
||||||
}
|
|
||||||
w.write(" return out;\n");
|
|
||||||
}
|
|
||||||
w.write(" }\n\n");
|
|
||||||
|
|
||||||
// assemble pos
|
|
||||||
w.write(" private static long[] posFlat() {\n");
|
|
||||||
var pPrefix = "DictDataL" + L + "P";
|
|
||||||
if (pChunks == 1) {
|
|
||||||
w.write(" return " + pPrefix + "0.get();\n");
|
|
||||||
} else {
|
|
||||||
w.write(" long[] out = new long[POS_LEN];\n");
|
|
||||||
w.write(" int k = 0;\n");
|
|
||||||
for (var ci = 0; ci < pChunks; ci++) {
|
|
||||||
w.write(" k = copy(out, k, " + pPrefix + ci + ".get());\n");
|
|
||||||
}
|
|
||||||
w.write(" return out;\n");
|
|
||||||
}
|
|
||||||
w.write(" }\n\n");
|
|
||||||
|
|
||||||
// entry
|
|
||||||
w.write(" public static puzzle.SwedishGenerator.DictEntry entry() {\n");
|
|
||||||
w.write(" long[] wds = words();\n");
|
|
||||||
w.write(" long[] flat = posFlat();\n");
|
|
||||||
w.write(" long[][] pos = reshape(flat, ROWS, COLS);\n");
|
|
||||||
w.write(" return new puzzle.SwedishGenerator.DictEntry(wds, pos, wds.length, (wds.length + 63) >>> 6);\n");
|
|
||||||
w.write(" }\n\n");
|
|
||||||
|
|
||||||
// helpers
|
|
||||||
w.write(" private static int copy(long[] dst, int at, long[] src) {\n");
|
|
||||||
w.write(" System.arraycopy(src, 0, dst, at, src.length);\n");
|
|
||||||
w.write(" return at + src.length;\n");
|
|
||||||
w.write(" }\n\n");
|
|
||||||
|
|
||||||
w.write(" private static long[][] reshape(long[] flat, int rows, int cols) {\n");
|
|
||||||
w.write(" long[][] out = new long[rows][cols];\n");
|
|
||||||
w.write(" int k = 0;\n");
|
|
||||||
w.write(" for (int r = 0; r < rows; r++) {\n");
|
|
||||||
w.write(" System.arraycopy(flat, k, out[r], 0, cols);\n");
|
|
||||||
w.write(" k += cols;\n");
|
|
||||||
w.write(" }\n");
|
|
||||||
w.write(" return out;\n");
|
|
||||||
w.write(" }\n");
|
|
||||||
|
|
||||||
w.write("}\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static BufferedWriter writer(Path out) throws IOException {
|
|
||||||
return Files.newBufferedWriter(out, StandardCharsets.UTF_8,
|
|
||||||
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String toLongLiteral(long v) { return "0x" + Long.toUnsignedString(v, 16) + "L"; }
|
|
||||||
public static final class CsvIndexService {
|
|
||||||
|
|
||||||
static int SIMPEL_IDX = 2;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
record DictEntryDTO(LongArrayList words, IntListDTO[][] pos) {
|
record DictEntryDTO(LongArrayList words, IntListDTO[][] pos) {
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
package puzzle;
|
package puzzle;
|
||||||
|
|
||||||
import module java.base;
|
import module java.base;
|
||||||
|
import gen.GenerateDict;
|
||||||
|
import gen.GenerateNeighbors;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import precomp.Neighbors9x8.rci;
|
import precomp.Neighbors9x8.rci;
|
||||||
|
import puzzle.DictJavaGeneratorMulti.DictEntryDTO.IntListDTO;
|
||||||
import puzzle.Export.Clued;
|
import puzzle.Export.Clued;
|
||||||
import puzzle.Export.Gridded;
|
import puzzle.Export.Gridded;
|
||||||
import puzzle.DictJavaGeneratorMulti.DictEntryDTO.IntListDTO;
|
|
||||||
import puzzle.Export.LetterVisit.LetterAt;
|
import puzzle.Export.LetterVisit.LetterAt;
|
||||||
import puzzle.Masker.Clues;
|
import puzzle.Masker.Clues;
|
||||||
import puzzle.Masker.Slot;
|
import puzzle.Masker.Slot;
|
||||||
@@ -35,7 +37,20 @@ import static puzzle.LemmaData.INEREN;
|
|||||||
import static puzzle.LemmaData.INERENA;
|
import static puzzle.LemmaData.INERENA;
|
||||||
import static puzzle.LemmaData.INERENAE;
|
import static puzzle.LemmaData.INERENAE;
|
||||||
import static puzzle.SwedishGenerator.*;
|
import static puzzle.SwedishGenerator.*;
|
||||||
|
@GenerateNeighbors(C = 4, R = 3, packageName = "precomp", className = "Neighbors4x3", MIN_LEN = 2)
|
||||||
|
@GenerateDict(
|
||||||
|
packageName = "puzzle",
|
||||||
|
className = "LemmaData",
|
||||||
|
scv = "nl_score_hints_v4.csv",
|
||||||
|
words = {
|
||||||
|
"EEN", "NAAR", "IEDEREEN", "A", "C", "X", "TEST", "IN", "INE", "INER", "INEREN", "INERENA", "INERENAE",
|
||||||
|
"APPLE", "AXE", "ABC", "ABD", "AZ", "AB",
|
||||||
|
"AT", "CAT", "DOGS", "APPLY", "BANAN", "BANANA", "BANANAS", "BANANASS"
|
||||||
|
},
|
||||||
|
simpleMax=900,
|
||||||
|
minLen = 2,
|
||||||
|
maxLen = 8
|
||||||
|
)
|
||||||
public class SwedishGeneratorTest {
|
public class SwedishGeneratorTest {
|
||||||
|
|
||||||
public static final long WORD_A = Lemma.from("A");
|
public static final long WORD_A = Lemma.from("A");
|
||||||
|
|||||||
Reference in New Issue
Block a user