introduce bitloops
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
package puzzle;
|
||||
|
||||
import module java.base;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import lombok.val;
|
||||
import puzzle.Export.Dicts;
|
||||
import puzzle.Export.IntListDTO;
|
||||
import puzzle.DictJavaGeneratorMulti.DictEntryDTO.IntListDTO;
|
||||
import puzzle.SwedishGenerator.Dict;
|
||||
import puzzle.SwedishGenerator.DictEntry;
|
||||
import puzzle.SwedishGenerator.Lemma;
|
||||
import static java.nio.charset.StandardCharsets.US_ASCII;
|
||||
import static puzzle.SwedishGenerator.THRESS;
|
||||
|
||||
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 {
|
||||
Path wordsFile = Path.of(args.length > 0 ? args[0] : "nl_score_hints_v3.csv");
|
||||
Path outDir = Path.of(args.length > 1 ? args[1] : "src/main/generated-sources/puzzle");
|
||||
String pkg = "puzzle";
|
||||
Path outDir = Path.of(args.length > 1 ? args[1] : "src/main/generated-sources/puzzle/dict" + THRESS);
|
||||
String pkg = "puzzle.dict" + THRESS;
|
||||
HashMap<Path, ShardBuilder> builders = new HashMap<Path, ShardBuilder>(16);
|
||||
|
||||
SwedishGenerator.Dict dict = buildDict(wordsFile, builders);
|
||||
@@ -33,10 +40,16 @@ public final class DictJavaGeneratorMulti {
|
||||
// Aggregator
|
||||
writeAggregator(outDir, pkg, "DictData", dict.length());
|
||||
System.out.println("Generated sources into: " + outDir.toAbsolutePath());
|
||||
builders.forEach(DictJavaGeneratorMulti::writeIndexedShard);
|
||||
|
||||
}
|
||||
static final Path[] SHARDS = IntStream.range(0, 10).mapToObj(sId -> Path.of("src/main/generated-sources/puzzle/dict"+THRESS).resolve(sId + ".idx")).toArray(
|
||||
Path[]::new);
|
||||
static Path shardKey(long word) {
|
||||
return SHARDS[Lemma.unpackSize(word) + 1];
|
||||
}
|
||||
|
||||
private static SwedishGenerator.Dict buildDict(Path wordsPath, HashMap<Path, ShardBuilder> builders) throws IOException {
|
||||
var map = new Export.LongArrayList(100_000);
|
||||
var map = new LongArrayList(100_000);
|
||||
try (var lines = Files.lines(wordsPath, StandardCharsets.UTF_8)) {
|
||||
lines.forEach(line -> {
|
||||
CsvIndexService.lineToLemma(line, w -> {
|
||||
@@ -50,7 +63,7 @@ public final class DictJavaGeneratorMulti {
|
||||
String recStr = word + "\t" + simpel + "\t" + json + "\n";
|
||||
byte[] rec = recStr.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
var key = Meta.shardKey(w);
|
||||
var key = shardKey(w) ;
|
||||
ShardBuilder sb = builders.computeIfAbsent(key, k -> new ShardBuilder());
|
||||
try {
|
||||
map.add(Lemma.pack(w, sb.addRecord(rec)));
|
||||
@@ -64,7 +77,43 @@ public final class DictJavaGeneratorMulti {
|
||||
return Dicts.makeDict(map.toArray());
|
||||
}
|
||||
|
||||
static final int VERSION = 1;
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
static final class ShardBuilder {
|
||||
|
||||
@@ -79,7 +128,8 @@ public final class DictJavaGeneratorMulti {
|
||||
}
|
||||
}
|
||||
|
||||
static void writeIndexedShard(Path out, ShardBuilder sb) throws IOException {
|
||||
static final int VERSION = 1;
|
||||
static void writeIndexedShard(Path out, ShardBuilder sb) {
|
||||
int n = sb.offsets.size();
|
||||
int[] offs = sb.offsets.toArray();
|
||||
byte[] data = sb.data.toByteArray();
|
||||
@@ -101,6 +151,8 @@ public final class DictJavaGeneratorMulti {
|
||||
|
||||
// data
|
||||
ch.write(ByteBuffer.wrap(data));
|
||||
}catch (IOException e){
|
||||
throw new RuntimeException("Failed to write shard to " + out, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,11 +162,11 @@ public final class DictJavaGeneratorMulti {
|
||||
w.write("package " + pkg + ";\n\n");
|
||||
w.write("public final class " + cls + " {\n");
|
||||
w.write(" private " + cls + "() {}\n\n");
|
||||
w.write(" public static final SwedishGenerator.Dict DICT = build();\n\n");
|
||||
w.write(" private static SwedishGenerator.Dict build() {\n");
|
||||
w.write(" SwedishGenerator.DictEntry[] idx = new SwedishGenerator.DictEntry[SwedishGenerator.MAX_WORD_LENGTH_PLUS_ONE];\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 (int L = 2; L <= 8; L++) w.write(" idx[" + L + "] = DictDataL" + L + ".entry();\n");
|
||||
w.write(" return new SwedishGenerator.Dict(idx, " + totalLen + ");\n");
|
||||
w.write(" return new puzzle.SwedishGenerator.Dict(idx, " + totalLen + ");\n");
|
||||
w.write(" }\n");
|
||||
w.write("}\n");
|
||||
}
|
||||
@@ -217,11 +269,11 @@ public final class DictJavaGeneratorMulti {
|
||||
w.write(" }\n\n");
|
||||
|
||||
// entry
|
||||
w.write(" public static SwedishGenerator.DictEntry entry() {\n");
|
||||
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 SwedishGenerator.DictEntry(wds, pos, wds.length, (wds.length + 63) >>> 6);\n");
|
||||
w.write(" return new puzzle.SwedishGenerator.DictEntry(wds, pos, wds.length, (wds.length + 63) >>> 6);\n");
|
||||
w.write(" }\n\n");
|
||||
|
||||
// helpers
|
||||
@@ -252,4 +304,87 @@ public final class DictJavaGeneratorMulti {
|
||||
private static String toLongLiteral(long v) {
|
||||
return "0x" + Long.toUnsignedString(v, 16) + "L";
|
||||
}
|
||||
public static final class CsvIndexService {
|
||||
|
||||
static int SIMPEL_IDX = 3;
|
||||
|
||||
public static int lineToSimpel(String line) {
|
||||
var parts = line.split(",", 5);
|
||||
return Integer.parseInt(parts[SIMPEL_IDX].trim());
|
||||
}
|
||||
public static String[] lineToClue(String line) {
|
||||
if (line.isBlank()) throw new RuntimeException("Empty line");
|
||||
var parts = line.split(",", 5);
|
||||
var rawClue = parts[4].trim();
|
||||
if (rawClue.startsWith("\"") && rawClue.endsWith("\"")) {
|
||||
rawClue = rawClue.substring(1, rawClue.length() - 1).replace("\"\"", "\"");
|
||||
}
|
||||
return Meta.GSON.fromJson(rawClue, String[].class);
|
||||
}
|
||||
public static void lineToLemma(String line, LongConsumer ok) {
|
||||
if (line.isBlank()) {
|
||||
throw new RuntimeException("Empty line");
|
||||
}
|
||||
var parts = line.split(",", 5);
|
||||
var id = Integer.parseInt(parts[0].trim());
|
||||
var word = parts[1].trim();
|
||||
int score = Integer.parseInt(parts[2].trim());
|
||||
int simpel = Integer.parseInt(parts[3].trim());
|
||||
if (score < 1 || simpel>THRESS) {
|
||||
if (Main.VERBOSE) System.err.println("Word too complex: " + line);
|
||||
return;
|
||||
}
|
||||
ok.accept(Lemma.from(word.getBytes(US_ASCII)));
|
||||
}
|
||||
}
|
||||
|
||||
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); }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user