Skip to content

Commit 7c2d8fd

Browse files
committed
more generated code
1 parent 578121c commit 7c2d8fd

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

src/test/java/ch/digitalfondue/jfiveparse/DoubleArrayTrie.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
// from https://medium.com/@lchang1994/deep-dive-dat-double-array-trie-f51e5e5f006c
1010
public class DoubleArrayTrie {
11-
private int[] check;
12-
private int[] base;
11+
int[] check;
12+
int[] base;
1313

1414
public DoubleArrayTrie() {
1515
check = new int[128];
@@ -37,10 +37,6 @@ private void expand(int index) {
3737
base = Arrays.copyOf(base, newSize);
3838
}
3939

40-
public int charId(char c) {
41-
return c >= 38 && c <= 122 ? entitiesCharIdx[c - 38] : -1;
42-
}
43-
4440
public void insert(String word) {
4541
int node = 1;
4642
for (int i = 0; i < word.length(); i++) {
@@ -163,8 +159,8 @@ private List<Integer> getChildrenOfOffset(int nodeIdx, int baseOffset) {
163159
if (baseOffset == 0) {
164160
return children;
165161
}
166-
// 'a'-'z' maps to 1..26
167-
for (int c = 1; c <= 26; c++) {
162+
// the chars map to 1 up to MAX_CHAR_ID
163+
for (int c = 1; c <= MAX_CHAR_ID; c++) {
168164
int idx = baseOffset + c;
169165
if (idx < check.length && check[idx] == nodeIdx) {
170166
children.add(c);
@@ -212,8 +208,12 @@ void check() {
212208
}
213209

214210

211+
// generated code
215212

216-
213+
private static final int MAX_CHAR_ID = 62;
214+
public int charId(char c) {
215+
return c >= 38 && c <= 122 ? entitiesCharIdx[c - 38] : -1;
216+
}
217217
private static final int[] entitiesCharIdx = new int[85];
218218
static {
219219
Arrays.fill(entitiesCharIdx, -1);

src/test/java/ch/digitalfondue/jfiveparse/GenerateEntities.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.gson.GsonBuilder;
1919
import com.google.gson.reflect.TypeToken;
20+
import org.junit.jupiter.api.Disabled;
2021
import org.junit.jupiter.api.Test;
2122

2223
import java.io.ByteArrayOutputStream;
@@ -41,12 +42,11 @@ private static class EntityValues {
4142

4243
// this generates the supporting array for mapping a character to a unique id
4344
// for the double array trie
45+
// step 1
4446
@Test
47+
@Disabled
4548
void generateCharactersMappingArray() throws IOException {
46-
Type type = (new TypeToken<Map<String, EntityValues>>() {
47-
}).getType();
48-
String json = Files.readString(Paths.get("src/test/resources/entities.json"));
49-
Map<String, EntityValues> m = new GsonBuilder().create().fromJson(json, type);
49+
Map<String, EntityValues> m = getEntitiesMap();
5050

5151
var allUsedChars = new TreeSet<Integer>();
5252
for (String key: m.keySet()) {
@@ -56,6 +56,7 @@ void generateCharactersMappingArray() throws IOException {
5656
var firstChar = allChars.get(0);
5757
var lastChar = allChars.get(allChars.size() - 1);
5858
// mapping function
59+
System.out.println("private static final int MAX_CHAR_ID = " + allChars.size() + ";");
5960
System.out.println("public int charId(char c) {");
6061
System.out.printf(" return c >= %d && c <= %d ? entitiesCharIdx[c - %d] : -1;\n", firstChar, lastChar, firstChar);
6162
System.out.println("}");
@@ -70,14 +71,31 @@ void generateCharactersMappingArray() throws IOException {
7071
}
7172
System.out.println("}");
7273
//
73-
System.out.println(allUsedChars.first() + " " + allUsedChars.last() + ";");
7474
}
7575

76-
public static void main(String[] args) throws IOException {
76+
private static Map<String, EntityValues> getEntitiesMap() throws IOException {
7777
Type type = (new TypeToken<Map<String, EntityValues>>() {
7878
}).getType();
7979
String json = Files.readString(Paths.get("src/test/resources/entities.json"));
8080
Map<String, EntityValues> m = new GsonBuilder().create().fromJson(json, type);
81+
return m;
82+
}
83+
84+
// step 2
85+
@Test
86+
@Disabled
87+
void pregenerateSupportArray() throws IOException {
88+
var entities = getEntitiesMap();
89+
var dat = new DoubleArrayTrie();
90+
for(String key : entities.keySet()) {
91+
dat.insert(key);
92+
}
93+
System.out.println(Arrays.toString(dat.base));
94+
System.out.println(Arrays.toString(dat.check));
95+
}
96+
97+
public static void main(String[] args) throws IOException {
98+
Map<String, EntityValues> m = getEntitiesMap();
8199

82100

83101
// we can iterate for making a and array for mapping char -> id!

0 commit comments

Comments
 (0)