Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde2e6f9

Browse files
committed
improved minifying of acceptors
1 parent0735bb6 commitde2e6f9

File tree

3 files changed

+35
-150
lines changed

3 files changed

+35
-150
lines changed

‎src/main/java/com/indoqa/fsa/character/CharAcceptorBuilder.java‎

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919
importstaticcom.indoqa.fsa.character.CharDataAccessor.*;
2020

2121
importjava.io.*;
22-
importjava.util.Arrays;
23-
importjava.util.HashMap;
24-
importjava.util.Map;
25-
importjava.util.Map.Entry;
26-
importjava.util.TreeMap;
22+
importjava.util.*;
23+
importjava.util.concurrent.ConcurrentHashMap;
2724
importjava.util.function.Consumer;
2825

2926
importcom.indoqa.fsa.AcceptorBuilder;
30-
importcom.indoqa.fsa.utils.IntList;
31-
importcom.indoqa.fsa.utils.NodeData;
3227

3328
publicclassCharAcceptorBuilderimplementsAcceptorBuilder {
3429

@@ -90,16 +85,12 @@ public static CharAcceptor read(InputStream inputStream) throws IOException {
9085
privatestaticStringgetKey(char[]node) {
9186
StringBuilderstringBuilder =newStringBuilder();
9287

93-
if (node.length >0) {
94-
stringBuilder.append(node[0]);
95-
}
96-
97-
if (node.length >CharDataAccessor.NODE_SIZE) {
98-
stringBuilder.append(node[CharDataAccessor.NODE_SIZE]);
99-
}
88+
for (inti =0;i <10;i++) {
89+
if (node.length <=CharDataAccessor.NODE_SIZE *i) {
90+
break;
91+
}
10092

101-
if (node.length >CharDataAccessor.NODE_SIZE *2) {
102-
stringBuilder.append(node[CharDataAccessor.NODE_SIZE *2]);
93+
stringBuilder.append(node[CharDataAccessor.NODE_SIZE *i]);
10394
}
10495

10596
stringBuilder.append('_');
@@ -115,10 +106,6 @@ public void addAcceptedInput(CharSequence... value) {
115106
}
116107
}
117108

118-
publicvoidaddAcceptedInput(CharSequencevalue) {
119-
this.addAcceptedInput(value,0,value.length());
120-
}
121-
122109
publicvoidaddAcceptedInput(CharSequencevalue,intstart,intlength) {
123110
if (this.minified ||this.remapped) {
124111
thrownewIllegalStateException("The data have already been minified / remapped.");
@@ -181,6 +168,10 @@ public void write(OutputStream outputStream) throws IOException {
181168
dataOutputStream.flush();
182169
}
183170

171+
privatevoidaddAcceptedInput(CharSequencevalue) {
172+
this.addAcceptedInput(value,0,value.length());
173+
}
174+
184175
privatevoidaddArc(intnode,charlabel,inttarget,booleanterminal) {
185176
char[]oldNodeData =this.nodes[node];
186177

@@ -238,12 +229,11 @@ private boolean applyReplacements(char[] nodeData, Map<Integer, Integer> replace
238229
for (inti =0;i <nodeData.length;i +=CharDataAccessor.NODE_SIZE) {
239230
inttarget =getTarget(nodeData,i);
240231

241-
if (!replacements.containsKey(target)) {
232+
Integerreplacement =replacements.get(target);
233+
if (replacement ==null) {
242234
continue;
243235
}
244236

245-
intreplacement =replacements.get(target);
246-
247237
booleanisTerminal =isTerminal(nodeData,i);
248238
booleanisLast =isLast(nodeData,i);
249239

@@ -257,17 +247,23 @@ private boolean applyReplacements(char[] nodeData, Map<Integer, Integer> replace
257247
returnresult;
258248
}
259249

260-
privatevoidapplyReplacements(Map<Integer,Integer>replacements) {
250+
privateSet<String>applyReplacements(Map<Integer,Integer>replacements) {
261251
this.sendMessage("Applying " +replacements.size() +" replacements");
262252

253+
Set<String>result =newHashSet<>();
254+
263255
for (inti =0;i <this.nodeCount;i++) {
264256
char[]node =this.nodes[i];
265257
if (node ==null) {
266258
continue;
267259
}
268260

269-
this.applyReplacements(node,replacements);
261+
if (this.applyReplacements(node,replacements)) {
262+
result.add(getKey(node));
263+
}
270264
}
265+
266+
returnresult;
271267
}
272268

273269
privatechar[]buildData() {
@@ -286,8 +282,8 @@ private char[] buildData() {
286282
returndata;
287283
}
288284

289-
privateMap<String,IntList>buildGroups() {
290-
Map<String,IntList>result =newTreeMap<>();
285+
privateMap<String,List<Integer>>buildGroups() {
286+
Map<String,List<Integer>>result =newTreeMap<>();
291287

292288
for (inti =0;i <this.nodeCount;i++) {
293289
char[]node =this.nodes[i];
@@ -297,9 +293,9 @@ private Map<String, IntList> buildGroups() {
297293

298294
Stringkey =getKey(node);
299295

300-
IntListindexes =result.get(key);
296+
List<Integer>indexes =result.get(key);
301297
if (indexes ==null) {
302-
indexes =newIntList();
298+
indexes =newArrayList<>();
303299
result.put(key,indexes);
304300
}
305301

@@ -309,22 +305,22 @@ private Map<String, IntList> buildGroups() {
309305
returnresult;
310306
}
311307

312-
privateMap<Integer,Integer>findReplacements(IntListgroup) {
308+
privateMap<Integer,Integer>findReplacements(List<Integer>group) {
313309
Map<Integer,Integer>result =newHashMap<>();
314310

315-
Map<NodeData,Integer>hashes =newHashMap<>(group.size());
311+
Map<String,Integer>hashes =newConcurrentHashMap<>(group.size());
316312

317313
for (inti =0;i <group.size();i++) {
318-
inteachIndex =group.get(i);
314+
IntegereachIndex =group.get(i);
319315
char[]node =this.nodes[eachIndex];
320316
if (node ==null) {
321317
continue;
322318
}
323319

324-
Integerprevious =hashes.putIfAbsent(newNodeData(node),eachIndex);
325-
320+
Integerprevious =hashes.putIfAbsent(newString(node),eachIndex);
326321
if (previous !=null) {
327322
result.put(eachIndex,previous);
323+
this.nodes[eachIndex] =null;
328324
}
329325
}
330326

@@ -339,31 +335,22 @@ private void minify() {
339335
this.sendMessage("Minifying " +this.nodeCount +" nodes ...");
340336

341337
Map<Integer,Integer>replacements =this.replaceEndNodes();
342-
this.applyReplacements(replacements);
338+
Set<String>changedGroups =this.applyReplacements(replacements);
343339

344-
Map<String,IntList>groups =this.buildGroups();
340+
Map<String,List<Integer>>groups =this.buildGroups();
345341

346342
while (true) {
347343
replacements.clear();
348344

349-
for (IntListeachGroup :groups.values()) {
350-
if (eachGroup.size() <2) {
351-
continue;
352-
}
353-
354-
Map<Integer,Integer>groupReplacements =this.findReplacements(eachGroup);
355-
for (Entry<Integer,Integer>eachReplacement :groupReplacements.entrySet()) {
356-
this.nodes[eachReplacement.getKey()] =null;
357-
}
358-
359-
replacements.putAll(groupReplacements);
345+
for (StringeachChangedGroup :changedGroups) {
346+
replacements.putAll(this.findReplacements(groups.get(eachChangedGroup)));
360347
}
361348

362349
if (replacements.isEmpty()) {
363350
break;
364351
}
365352

366-
this.applyReplacements(replacements);
353+
changedGroups =this.applyReplacements(replacements);
367354
}
368355

369356
this.minified =true;

‎src/main/java/com/indoqa/fsa/utils/IntList.java‎

Lines changed: 0 additions & 53 deletions
This file was deleted.

‎src/main/java/com/indoqa/fsa/utils/NodeData.java‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp