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

Commit23ccec2

Browse files
committed
feat: shrink limit
1 parent6c5a242 commit23ccec2

File tree

5 files changed

+89
-43
lines changed

5 files changed

+89
-43
lines changed

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</parent>
2727

2828
<artifactId>indoqa-fsa</artifactId>
29-
<version>0.3.1-SNAPSHOT</version>
29+
<version>0.4.0-SNAPSHOT</version>
3030

3131
<name>Indoqa FSA</name>
3232
<description>Finite State Automata</description>

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

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importstaticcom.indoqa.fsa.character.CharDataAccessor.*;
2020

2121
importjava.io.*;
22+
importjava.text.NumberFormat;
2223
importjava.util.*;
2324
importjava.util.Map.Entry;
2425
importjava.util.function.Consumer;
@@ -29,12 +30,14 @@ public class CharAcceptorBuilder implements AcceptorBuilder {
2930

3031
publicstaticfinalintFILE_VERSION =2;
3132
publicstaticfinalintDEFAULT_CAPACITY_INCREMENT =16 *1024;
33+
publicstaticfinalintDEFAULT_SHRINK_LIMIT =1_000;
3234

3335
privatefinalbooleancaseSensitive;
3436

3537
privatechar[][]nodes =newchar[0][];
3638
privateintnodeCount;
37-
privateintcapacityIncrement;
39+
privatefinalintcapacityIncrement;
40+
privatefinalintshrinkLimit;
3841

3942
privateReplacementsreplacements;
4043

@@ -45,14 +48,15 @@ public class CharAcceptorBuilder implements AcceptorBuilder {
4548
privateintrequiredLength;
4649

4750
publicCharAcceptorBuilder(booleancaseSensitive) {
48-
this(caseSensitive,DEFAULT_CAPACITY_INCREMENT);
51+
this(caseSensitive,DEFAULT_CAPACITY_INCREMENT,DEFAULT_SHRINK_LIMIT);
4952
}
5053

51-
publicCharAcceptorBuilder(booleancaseSensitive,intcapacityIncrement) {
54+
publicCharAcceptorBuilder(booleancaseSensitive,intcapacityIncrement,intshrinkLimit) {
5255
super();
5356

5457
this.caseSensitive =caseSensitive;
5558
this.capacityIncrement =capacityIncrement;
59+
this.shrinkLimit =shrinkLimit;
5660
this.addNode();
5761
}
5862

@@ -88,6 +92,10 @@ public static CharAcceptor read(InputStream inputStream) throws IOException {
8892
returnnewCharAcceptor(data,caseSensitive);
8993
}
9094

95+
privatestaticStringformatNumber(longnumber) {
96+
returnNumberFormat.getInstance(Locale.ENGLISH).format(number);
97+
}
98+
9199
privatestaticStringgetKey(char[]node) {
92100
StringBuilderstringBuilder =newStringBuilder();
93101

@@ -231,26 +239,6 @@ private void addNode() {
231239
this.nodeCount++;
232240
}
233241

234-
privateSet<String>applyReplacements() {
235-
this.sendMessage("Applying " +this.replacements.getCount() +" replacements");
236-
237-
Set<String>result =newHashSet<>();
238-
239-
for (inti =0;i <this.nodeCount;i++) {
240-
char[]node =this.nodes[i];
241-
if (node ==null) {
242-
continue;
243-
}
244-
245-
booleanupdated =this.applyReplacements(node);
246-
if (updated) {
247-
result.add(getKey(node));
248-
}
249-
}
250-
251-
returnresult;
252-
}
253-
254242
privatebooleanapplyReplacements(char[]nodeData) {
255243
booleanresult =false;
256244

@@ -275,6 +263,25 @@ private boolean applyReplacements(char[] nodeData) {
275263
returnresult;
276264
}
277265

266+
privatevoidapplyReplacements(Set<String>changedGroups) {
267+
this.sendMessage("Applying " +formatNumber(this.replacements.getCount()) +" replacements");
268+
if (changedGroups !=null) {
269+
changedGroups.clear();
270+
}
271+
272+
for (inti =0;i <this.nodeCount;i++) {
273+
char[]node =this.nodes[i];
274+
if (node ==null) {
275+
continue;
276+
}
277+
278+
booleanupdated =this.applyReplacements(node);
279+
if (updated &&changedGroups !=null) {
280+
changedGroups.add(getKey(node));
281+
}
282+
}
283+
}
284+
278285
privatechar[]buildData() {
279286
char[]data =newchar[this.requiredLength];
280287
intoffset =0;
@@ -317,7 +324,6 @@ private Map<String, List<NodeReference>> buildGroups() {
317324
}
318325
}
319326

320-
this.sendMessage("Built " +result.size() +" groups");
321327
returnresult;
322328
}
323329

@@ -353,17 +359,37 @@ private void findReplacements(List<NodeReference> group) {
353359
}
354360
}
355361

362+
privatelonggetSize() {
363+
longlength =0;
364+
365+
for (inti =0;i <this.nodeCount;i++) {
366+
char[]node =this.nodes[i];
367+
if (node ==null) {
368+
continue;
369+
}
370+
371+
if (node.length !=0) {
372+
length +=node.length;
373+
}
374+
}
375+
376+
returnlength;
377+
}
378+
356379
privatevoidminify() {
357380
if (this.minified) {
358381
return;
359382
}
360383

361-
this.sendMessage("Minifying " +this.nodeCount +" nodes");
384+
longstart =System.currentTimeMillis();
385+
longpreviousSize =this.getSize();
386+
this.sendMessage("Minifying " +formatNumber(this.nodeCount) +" nodes");
362387

363388
Map<String,List<NodeReference>>groups =this.buildGroups();
364-
this.replacements.clear();
389+
Set<String>changedGroups =newHashSet<>();
390+
365391
this.findEndNodeReplacements();
366-
Set<String>changedGroups =this.applyReplacements();
392+
this.applyReplacements(changedGroups);
367393

368394
while (true) {
369395
this.replacements.clear();
@@ -377,13 +403,32 @@ private void minify() {
377403
this.findReplacements(group);
378404
}
379405

380-
if (this.replacements.getCount() ==0) {
406+
if (this.replacements.isEmpty()) {
381407
break;
382408
}
383409

384-
changedGroups =this.applyReplacements();
410+
this.applyReplacements(changedGroups);
411+
412+
longsize =this.getSize();
413+
longshrunk =previousSize -size;
414+
longminShrink =previousSize /this.shrinkLimit;
415+
this.sendMessage(
416+
"Shrunk size by " +formatNumber(shrunk) +" (from " +formatNumber(previousSize) +" to " +formatNumber(size) +"), "
417+
+formatNumber((System.currentTimeMillis() -start) /1_000) +" seconds");
418+
419+
if (shrunk <minShrink) {
420+
this.sendMessage(
421+
"Shrinking step smaller than limit (" +formatNumber(shrunk) +" < " +formatNumber(minShrink)
422+
+"). Aborting ...");
423+
break;
424+
}
425+
426+
previousSize =size;
385427
}
386428

429+
longduration =System.currentTimeMillis() -start;
430+
this.sendMessage("Minified in " +formatNumber(duration /1_000) +" seconds");
431+
387432
this.minified =true;
388433
}
389434

@@ -409,9 +454,9 @@ private void remap() {
409454
}
410455
}
411456

412-
this.sendMessage("RequiredLength:" +this.requiredLength);
457+
this.sendMessage("Required length" +formatNumber(this.requiredLength));
413458

414-
this.applyReplacements();
459+
this.applyReplacements((Set<String>)null);
415460

416461
this.remapped =true;
417462
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public CharTransducerBuilder(boolean caseSensitive) {
3838
}
3939

4040
publicCharTransducerBuilder(booleancaseSensitive,charseparator) {
41-
this(caseSensitive,separator,CharAcceptorBuilder.DEFAULT_CAPACITY_INCREMENT);
41+
this(caseSensitive,separator,CharAcceptorBuilder.DEFAULT_CAPACITY_INCREMENT,CharAcceptorBuilder.DEFAULT_SHRINK_LIMIT);
4242
}
4343

44-
publicCharTransducerBuilder(booleancaseSensitive,charseparator,intcapacityIncrement) {
44+
publicCharTransducerBuilder(booleancaseSensitive,charseparator,intcapacityIncrement,intshrinkLimit) {
4545
super();
4646

47-
this.acceptorBuilder =newCharAcceptorBuilder(caseSensitive,capacityIncrement);
47+
this.acceptorBuilder =newCharAcceptorBuilder(caseSensitive,capacityIncrement,shrinkLimit);
4848
this.separator =separator;
4949
}
5050

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ public NodeReference(char[] data, int index) {
3131

3232
@Override
3333
publicintcompareTo(NodeReferenceother) {
34-
for (inti =0;i <this.data.length;i++) {
35-
if (other.data.length <=i) {
36-
return1;
37-
}
34+
if (other.data.length <this.data.length) {
35+
return1;
36+
}
3837

38+
if (other.data.length >this.data.length) {
39+
return -1;
40+
}
41+
42+
for (inti =0;i <this.data.length;i++) {
3943
if (this.data[i] <other.data[i]) {
4044
return -1;
4145
}
@@ -45,10 +49,6 @@ public int compareTo(NodeReference other) {
4549
}
4650
}
4751

48-
if (other.data.length >this.data.length) {
49-
return -1;
50-
}
51-
5252
returnInteger.compare(this.index,other.index);
5353
}
5454

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Replacements {
2525

2626
publicReplacements(intcapacity) {
2727
this.targets =newint[capacity];
28+
this.clear();
2829
}
2930

3031
publicvoidclear() {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp