Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

108
LINES

< > BotCompany Repo | #1033083 // CompressionSearch

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (7289L/42K).

// CompressionSearch

// The terms are flying all over the place with this one
// (compressor, decompressor, compression, decompression, ...)

// Basic idea: INPUT DATA (byte list) is compressed into a
//   CUSTOM OBJECT (called the "COMPRESSION"), which, when run
//     under a fitting COMPRESSION REGIME produces the INPUT DATA.
//
//   Every compression can also be converted to and from a byte array
//     (the compression regime does this).

sclass CompressionSearch<A> extends Probabilistic {
  // user-set
  
  settable CompressionRegime<A> regime;
  settable L<Byte> inputData;

  // public output variables
  
  new Best<Submission> best;

  *(CompressionRegime<A> *regime) {}
  *(CompressionRegime<A> *regime, LByte *inputData) {}
  *(CompressionRegime<A> *regime, byte[] inputData) { this.inputData = byteArrayToList(inputData); }
  
  // can submit concurrently!
  Submission submit(A compression) { ret _submit(compression); }
  
  // private (although Submission is returned)
  
  class Submission {
    toString {
      ret linesLL(
        "Compressor " + (correct() ? "correct" : "incorrect"),
        "Compression factor " + score(),
        "Code:",
        indentx(str(decompressor())),
        "Code as bytes:",
        indentx(bytesToHex(compressed()))
      );
    }
    
    void setDecompressor(A decompressor) {
      this.decompressor_cache = decompressor;
    }
    
    LByte inputData() { ret inputData; }
    byte[] inputDataAsArray() { ret byteListToArray(inputData); }
    
    simplyCached byte[] compressed() {
      ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache);
    }
    
    simplyCached A decompressor() {
      ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache);
    }
    
    int compressedSize() { ret l(compressed()); }
    
    simplyCached double score() {
      ret doubleRatio(inputDataSize(), compressedSize());
    }
    
    bool decompressed;
    O decompressedValue;
    
    O decompressed() {
      if (!decompressed) {
        decompressedValue = regime.runDecompressor(decompressor());
        set decompressed;
      }
      ret decompressedValue;
    }
    
    simplyCached Bool correct() {
      ret eq(toByteList(decompressed()), inputData());
    }
  }
  
  int inputDataSize() { ret l(inputData); }
  
  Submission _submit(A compression) {
    if (compression == null) null;
    new Submission s;
    s.setDecompressor(compression);
    if (s.score() > best.score())
      if (!s.correct())
        warn("Compressor didn't verify");
      else
        best.put(s, s.score());
    ret s;
  }
  
  A bestCompression aka get() {
    var s = best!;
    ret s?.decompressor();
  }
  
  void addStrategy(AbstractCompressor compressor) {
    if (compressor == null) ret;
    initAction(compressor);
    compressor.setSearch(this);
    compressor.run();
  }
  
  run {}
}

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: 228 / 405
Version history: 27 change(s)
Referenced in: [show references]