Libraryless. Click here for Pure Java version (5537L/32K).
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> implements Steppable { |
14 | // user-set |
15 | |
16 | CompressionRegime<A> regime; |
17 | L<Byte> inputData; |
18 | Steppable searcher; // main object that performs the search (optional) |
19 | |
20 | // public output variables |
21 | |
22 | new Best<Submission> best; |
23 | bool searcherDone; |
24 | |
25 | *(CompressionRegime<A> *regime) {} |
26 | *(CompressionRegime<A> *regime, LByte *inputData) {} |
27 | *(CompressionRegime<A> *regime, byte[] inputData) { this.inputData = byteArrayToList(inputData); } |
28 | |
29 | // can submit concurrently! |
30 | Submission submit(A compression) { ret _submit(compression); } |
31 | |
32 | // private (although Submission is returned) |
33 | |
34 | class Submission { |
35 | toString { |
36 | ret linesLL( |
37 | "Compressor " + (correct() ? "correct" : "incorrect"), |
38 | "Compression factor " + score(), |
39 | "Code:", |
40 | indentx(str(decompressor())), |
41 | "Code as bytes:", |
42 | indentx(bytesToHex(compressed())) |
43 | ); |
44 | } |
45 | |
46 | void setDecompressor(A decompressor) { |
47 | this.decompressor_cache = decompressor; |
48 | } |
49 | |
50 | LByte inputData() { ret inputData; } |
51 | byte[] inputDataAsArray() { ret byteListToArray(inputData); } |
52 | |
53 | simplyCached byte[] compressed() { |
54 | ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache); |
55 | } |
56 | |
57 | simplyCached A decompressor() { |
58 | ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache); |
59 | } |
60 | |
61 | int compressedSize() { ret l(compressed()); } |
62 | |
63 | simplyCached double score() { |
64 | ret doubleRatio(inputDataSize(), compressedSize()); |
65 | } |
66 | |
67 | bool decompressed; |
68 | O decompressedValue; |
69 | |
70 | O decompressed() { |
71 | if (!decompressed) { |
72 | decompressedValue = regime.runDecompressor(decompressor()); |
73 | set decompressed; |
74 | } |
75 | ret decompressedValue; |
76 | } |
77 | |
78 | simplyCached Bool correct() { |
79 | ret eq(toByteList(decompressed()), inputData()); |
80 | } |
81 | } |
82 | |
83 | int inputDataSize() { ret l(inputData); } |
84 | |
85 | Submission _submit(A compression) { |
86 | if (compression == null) null; |
87 | new Submission s; |
88 | s.setDecompressor(compression); |
89 | if (s.score() > best.score()) |
90 | if (!s.correct()) |
91 | warn("Compressor didn't verify"); |
92 | else |
93 | best.put(s, s.score()); |
94 | ret s; |
95 | } |
96 | |
97 | public bool step() { |
98 | if (searcherDone || searcher == null) false; |
99 | if (searcher.step()) true; |
100 | set searcherDone; |
101 | false; |
102 | } |
103 | } |
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: | #1033142 |
Snippet name: | CompressionSearch (unprobabilistic backup) |
Eternal ID of this version: | #1033142/3 |
Text MD5: | 273650fd195f08045bf044589dc47dba |
Transpilation MD5: | 5ce13462e327ee36ce9ab2d78ff36e34 |
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 19:06:13 |
Source code size: | 2946 bytes / 103 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 136 / 199 |
Version history: | 2 change(s) |
Referenced in: | [show references] |