Libraryless. Click here for Pure Java version (7289L/42K).
1 | // CompressionSearch |
2 | |
3 | // The terms are flying all over the place with this one |
4 | // (compressor, decompressor, compression, decompression, ...) |
5 | |
6 | // Basic idea: INPUT DATA (byte list) is compressed into a |
7 | // CUSTOM OBJECT (called the "COMPRESSION"), which, when run |
8 | // under a fitting COMPRESSION REGIME produces the INPUT DATA. |
9 | // |
10 | // Every compression can also be converted to and from a byte array |
11 | // (the compression regime does this). |
12 | |
13 | sclass CompressionSearch<A> extends Probabilistic { |
14 | // user-set |
15 | |
16 | settable CompressionRegime<A> regime; |
17 | settable L<Byte> inputData; |
18 | |
19 | // public output variables |
20 | |
21 | new Best<Submission> best; |
22 | |
23 | *(CompressionRegime<A> *regime) {} |
24 | *(CompressionRegime<A> *regime, LByte *inputData) {} |
25 | *(CompressionRegime<A> *regime, byte[] inputData) { this.inputData = byteArrayToList(inputData); } |
26 | |
27 | // can submit concurrently! |
28 | Submission submit(A compression) { ret _submit(compression); } |
29 | |
30 | // private (although Submission is returned) |
31 | |
32 | class Submission { |
33 | toString { |
34 | ret linesLL( |
35 | "Compressor " + (correct() ? "correct" : "incorrect"), |
36 | "Compression factor " + score(), |
37 | "Code:", |
38 | indentx(str(decompressor())), |
39 | "Code as bytes:", |
40 | indentx(bytesToHex(compressed())) |
41 | ); |
42 | } |
43 | |
44 | void setDecompressor(A decompressor) { |
45 | this.decompressor_cache = decompressor; |
46 | } |
47 | |
48 | LByte inputData() { ret inputData; } |
49 | byte[] inputDataAsArray() { ret byteListToArray(inputData); } |
50 | |
51 | simplyCached byte[] compressed() { |
52 | ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache); |
53 | } |
54 | |
55 | simplyCached A decompressor() { |
56 | ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache); |
57 | } |
58 | |
59 | int compressedSize() { ret l(compressed()); } |
60 | |
61 | simplyCached double score() { |
62 | ret doubleRatio(inputDataSize(), compressedSize()); |
63 | } |
64 | |
65 | bool decompressed; |
66 | O decompressedValue; |
67 | |
68 | O decompressed() { |
69 | if (!decompressed) { |
70 | decompressedValue = regime.runDecompressor(decompressor()); |
71 | set decompressed; |
72 | } |
73 | ret decompressedValue; |
74 | } |
75 | |
76 | simplyCached Bool correct() { |
77 | ret eq(toByteList(decompressed()), inputData()); |
78 | } |
79 | } |
80 | |
81 | int inputDataSize() { ret l(inputData); } |
82 | |
83 | Submission _submit(A compression) { |
84 | if (compression == null) null; |
85 | new Submission s; |
86 | s.setDecompressor(compression); |
87 | if (s.score() > best.score()) |
88 | if (!s.correct()) |
89 | warn("Compressor didn't verify"); |
90 | else |
91 | best.put(s, s.score()); |
92 | ret s; |
93 | } |
94 | |
95 | A bestCompression aka get() { |
96 | var s = best!; |
97 | ret s?.decompressor(); |
98 | } |
99 | |
100 | void addStrategy(AbstractCompressor compressor) { |
101 | if (compressor == null) ret; |
102 | initAction(compressor); |
103 | compressor.setSearch(this); |
104 | compressor.run(); |
105 | } |
106 | |
107 | run {} |
108 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033083 |
Snippet name: | CompressionSearch |
Eternal ID of this version: | #1033083/28 |
Text MD5: | f06b41656472bf41c68a9174738e38b9 |
Transpilation MD5: | 2d12e9916c4cfce9688ab5cc7d97b4fb |
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-15 20:03:25 |
Source code size: | 3000 bytes / 108 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 230 / 409 |
Version history: | 27 change(s) |
Referenced in: | [show references] |