Libraryless. Click here for Pure Java version (7966L/47K).
1 | // A = compressor type (e.g. s), Data = data type |
2 | persistable sclass CompressionSearch_AnyType<A, Data> extends Probabilistic { |
3 | replace Regime with CompressionRegime_AnyType<A>. |
4 | |
5 | // user-set |
6 | |
7 | settable Regime regime; |
8 | gettable Data inputData; |
9 | settable long inputSize; // in bytes |
10 | settable bool doFullTesting; // go full javaEval [VERY SLOW] |
11 | |
12 | event onNewBest; |
13 | event onDisqualified(Submission s); |
14 | |
15 | // public output variables |
16 | |
17 | new Best<Submission> best; |
18 | new L<AbstractCompressor_AnyType> strategies; |
19 | |
20 | *() { init(); } |
21 | *(Regime *regime) { init(); } |
22 | *(Regime *regime, Data *inputData, long *inputSize) { init(); } |
23 | |
24 | void init { |
25 | best.onChange = r newBest; |
26 | } |
27 | |
28 | // can submit concurrently! |
29 | Submission submit(A compression, O notes default null) { |
30 | ret _submit(compression, notes); |
31 | } |
32 | |
33 | // private (although Submission is returned) |
34 | |
35 | class Submission { |
36 | settable O notes; |
37 | |
38 | toString { |
39 | if (!correct()) ret linesLL("BAD"); |
40 | |
41 | ret linesLL( |
42 | (isTrue(isFullChecked()) ? "FULLCHECKED" |
43 | : " ") + " " + ifloor(score()*100), |
44 | "Exact compression ratio: " + scoreRatio(), |
45 | "Code:", |
46 | indentx(shorten(500, str(decompressor()))), |
47 | "Code as bytes:", |
48 | indentx(shorten(500, bytesToHex(compressed()))) |
49 | ); |
50 | } |
51 | |
52 | void setDecompressor(A decompressor) { |
53 | this.decompressor_cache = decompressor; |
54 | } |
55 | |
56 | Data inputData() { ret inputData; } |
57 | |
58 | simplyCached byte[] compressed() { |
59 | ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache); |
60 | } |
61 | |
62 | simplyCached A decompressor() { |
63 | ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache); |
64 | } |
65 | |
66 | simplyCached S decompressor_str() { |
67 | ret strOrNull(decompressor()); |
68 | } |
69 | |
70 | int compressedSize aka nBytes() { ret l(compressed()); } |
71 | |
72 | simplyCached Ratio scoreRatio() { |
73 | ret Ratio(inputSize(), compressedSize()); |
74 | } |
75 | |
76 | simplyCached double score() { |
77 | ret scoreRatio()!; |
78 | } |
79 | |
80 | bool decompressed; |
81 | O decompressedValue; |
82 | |
83 | O decompressed() { |
84 | if (!decompressed) { |
85 | decompressedValue = regime.runDecompressor(decompressor()); |
86 | set decompressed; |
87 | } |
88 | ret decompressedValue; |
89 | } |
90 | |
91 | bool checkUnder(Regime regime) { |
92 | ret checkResult(regime.runDecompressor(decompressor())); |
93 | } |
94 | |
95 | simplyCached TripleBool isFullChecked() { |
96 | if (!isTrue(correct())) false; |
97 | |
98 | var fullRegime = fullRegime(); |
99 | if (fullRegime == null) true; |
100 | ret checkUnder(fullRegime); |
101 | } |
102 | |
103 | bool checkResult(O decompressed) { |
104 | ret eq(toByteList(decompressed), inputData()); |
105 | } |
106 | |
107 | simplyCached Bool correct() { |
108 | ret eq(toByteList(decompressed()), inputData()); |
109 | } |
110 | } // end of Submission |
111 | |
112 | Submission _submit(A compression, O notes) { |
113 | if (compression == null) null; |
114 | print("Received submission"); |
115 | print("Code: " + takeFirst(500, str(compression))); |
116 | new Submission s; |
117 | s.notes(notes); |
118 | print("Notes: " + takeFirst(500, str(notes))); |
119 | s.setDecompressor(compression); |
120 | print("Size: " + nBytes(s.nBytes())); |
121 | if (s.score() > best.score()) |
122 | if (!s.correct()) { |
123 | warn("Compressor didn't verify"); |
124 | disqualified(s); |
125 | } else { |
126 | print("Score: " + s.score()); |
127 | best.put(s, s.score(); |
128 | } |
129 | ret s; |
130 | } |
131 | |
132 | Submission bestSubmission() { |
133 | methodForEach flush(strategies); |
134 | ret best!; |
135 | } |
136 | |
137 | A bestCompression aka get() { |
138 | var s = bestSubmission(); |
139 | ret s?.decompressor(); |
140 | } |
141 | |
142 | bool has() { ret bestSubmission() != null; } |
143 | |
144 | void addStrategy(AbstractCompressor_AnyType<A, Data> compressor) { |
145 | if (compressor == null) ret; |
146 | initAction(compressor); |
147 | compressor.setSearch(this); |
148 | strategies.add(compressor); |
149 | compressor.step0(); |
150 | } |
151 | |
152 | swappable CompressionRegime_AnyType fullRegime() { |
153 | if (doFullTesting) |
154 | ret regime.fullRegime(); |
155 | null; |
156 | } |
157 | |
158 | // actually not needed, addStrategy schedules a strategy immediately |
159 | public void step0 {} |
160 | } |
Began life as a copy of #1033083
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033158 |
Snippet name: | CompressionSearch_AnyType - receives and tests the submissions |
Eternal ID of this version: | #1033158/37 |
Text MD5: | 4f8991c2a5dd3b3ad2f4952ee8cc21ec |
Transpilation MD5: | d78d1ab93224b8674ab08b263520948d |
Author: | stefan |
Category: | javax / recognition by compression |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-27 00:00:41 |
Source code size: | 4291 bytes / 160 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 252 / 467 |
Version history: | 36 change(s) |
Referenced in: | [show references] |