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

108
LINES

< > BotCompany Repo | #1033156 // CompressionSearch - backup 2

JavaX fragment (include)

// 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 {}
}

Author comment

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: #1033156
Snippet name: CompressionSearch - backup 2
Eternal ID of this version: #1033156/1
Text MD5: f06b41656472bf41c68a9174738e38b9
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:00:56
Source code size: 3000 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 144 / 161
Referenced in: