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

103
LINES

< > BotCompany Repo | #1033142 // CompressionSearch (unprobabilistic backup)

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

Libraryless. Click here for Pure Java version (5537L/32K).

// 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> implements Steppable {
  // user-set
  
  CompressionRegime<A> regime;
  L<Byte> inputData;
  Steppable searcher; // main object that performs the search (optional)
  
  // public output variables
  
  new Best<Submission> best;
  bool searcherDone;
  
  *(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;
  }
  
  public bool step() {
    if (searcherDone || searcher == null) false;
    if (searcher.step()) true;
    set searcherDone;
    false;
  }
}

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: #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: 137 / 201
Version history: 2 change(s)
Referenced in: