introduce bitloops
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package puzzle;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import puzzle.SwedishGenerator.Lemma;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -11,8 +12,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
@UtilityClass
|
||||
public class Meta {
|
||||
|
||||
static final Gson GSON = new Gson();
|
||||
@@ -27,14 +27,14 @@ public class Meta {
|
||||
static final Path shardData = dir.resolve("shard0.data");
|
||||
static final Path shardMap = dir.resolve("shard0.map");
|
||||
|
||||
private static Path detectShardDir() {
|
||||
private Path detectShardDir() {
|
||||
// 1) optioneel override
|
||||
String p = System.getProperty("puzzle.shards.dir");
|
||||
if (p != null && !p.isBlank()) return Path.of(p).toAbsolutePath().normalize();
|
||||
|
||||
// 2) default: naast classes output (CLASS_OUTPUT/shards)
|
||||
try {
|
||||
var url = Meta.class.getProtectionDomain().getCodeSource().getLocation(); // classes dir (niet jar)
|
||||
var url = Meta.class.getProtectionDomain().getCodeSource().getLocation(); // classes dir (niet jar)
|
||||
Path classes = Path.of(url.toURI());
|
||||
return classes.resolve("shards");
|
||||
} catch (Exception e) {
|
||||
@@ -43,7 +43,7 @@ public class Meta {
|
||||
}
|
||||
}
|
||||
// --- Lookup: w -> i using mmap ---
|
||||
static int findIndexInMapMmap(long target) throws IOException {
|
||||
private int findIndexInMapMmap(long target) throws IOException {
|
||||
try (var ch = FileChannel.open(Meta.shardMap, StandardOpenOption.READ)) {
|
||||
var mbb = (MappedByteBuffer) ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()).order(ORDER);
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Meta {
|
||||
}
|
||||
|
||||
// --- Read record i from shard.data (your format) ---
|
||||
static ShardLem readRecord(long w, int i) throws IOException {
|
||||
private ShardLem readRecord(long w, int i) throws IOException {
|
||||
try (var ch = FileChannel.open(Meta.shardData, StandardOpenOption.READ)) {
|
||||
var hdr = ByteBuffer.allocate(12).order(ORDER);
|
||||
ch.read(hdr);
|
||||
@@ -97,11 +97,11 @@ public class Meta {
|
||||
|
||||
var simpel = Integer.parseInt(parts[1]);
|
||||
var clues = GSON.fromJson(parts[2], String[].class);
|
||||
return new ShardLem(w, simpel, clues);
|
||||
return new ShardLem(w, simpel, i, clues);
|
||||
}
|
||||
}
|
||||
|
||||
static int readIntAt(FileChannel ch, long pos) throws IOException {
|
||||
private int readIntAt(FileChannel ch, long pos) throws IOException {
|
||||
var b = ByteBuffer.allocate(4).order(ORDER);
|
||||
ch.position(pos);
|
||||
ch.read(b);
|
||||
@@ -109,23 +109,18 @@ public class Meta {
|
||||
return b.getInt();
|
||||
}
|
||||
|
||||
// --- Demo main ---
|
||||
public static ShardLem lookup(long w) {
|
||||
public record ShardLem(long w, int simpel, int mmap, String[] clues) { }
|
||||
|
||||
public ShardLem lookupSilent(long w) {
|
||||
try {
|
||||
var i = findIndexInMapMmap(Lemma.pack43(w));
|
||||
System.out.println("\nQuery: w=" + w + " -> i=" + i);
|
||||
var i = findIndexInMapMmap(Lemma.packLetterAndLengthBits(w));
|
||||
if (i >= 0) {
|
||||
var rec = readRecord(w, i);
|
||||
System.out.println(" simpel=" + rec.simpel());
|
||||
System.out.println(" clues=" + Arrays.toString(rec.clues()));
|
||||
return rec;
|
||||
return readRecord(w, i);
|
||||
} else {
|
||||
System.out.println(" NOT FOUND");
|
||||
throw new RuntimeException("NOT FOUND");
|
||||
throw new RuntimeException("NOT FOUND{w=" + w + ", text=" + Lemma.asWord(w, new byte[8]) + "}");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public record ShardLem(long w, int simpel, String[] clues) { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user