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

98
LINES

< > BotCompany Repo | #1033140 // JE_CompressEachElementIndividually - works on a list

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

Libraryless. Click here for Pure Java version (8640L/51K).

sclass JE_CompressEachElementIndividually<El> extends AbstractCompressor_AnyType<IJavaExpr, L<El>> {
  settable IF0<AbstractCompressor_AnyType<IJavaExpr<El>, El>> elementCompressionStrategy;
  new L<CompressionSearch_AnyType<IJavaExpr<El>, El>> searches;
  settable IF1<El, IJavaExpr> makeLiteral; // required
  new BitSet compressedElementsBitSet;
  // TODO: akas for variables
  
  selfType onCompressionFail(IF1<El, IJavaExpr> f) { makeLiteral = f; this; }
  
  settable bool debug;

  // internal
  
  new L<IJavaExpr> renderedElements;
  IJavaExpr renderedCode;
  new BitSet dirtyElements;
  
  int size() { ret l(inputData()); }
  int nNotLiteral() { ret countPred(searches, search -> search.has()); }
  
  scaffolded IJavaExpr getRenderedElement(int i) {
    if (contains(dirtyElements, i) || _get(renderedElements, i) == null) {
      remove(dirtyElements, i);
      //print("Rendering element " + (i+1) + "/" + size());
      var e = renderElement(i);
      listSet(renderedElements, i, e);
      //print("  => " + e);
    }
    ret renderedElements.get(i);
  }
    
  IJavaExpr renderElement(int i) {
    var search = _get(searches, i);
    IJavaExpr comp = search?!;
    if (comp != null) {
      synchronized(compressedElementsBitSet) { compressedElementsBitSet.set(i); }
      ret comp;
    }
    
    // no "good" compression found, return literal
    IJavaExpr java = makeLiteral.get(_get(inputData(), i));
    metaSet(java, gaveUp := this);
    ret java;
  }
  
  bool dirty() { ret nempty(dirtyElements) || l(renderedElements) < size(); }
  
  public void flush {
    if (!dirty()) ret;
    submit(code());
  }

  L<IJavaExpr> codeForElements() {
    ret countIteratorToList getRenderedElement(size());
  }
  
  // get current best compressed representation
  IJavaExpr code() {
    if (dirty() || renderedCode == null) {
      L<IJavaExpr> els = codeForElements();
      int iNull = indexOf(els, null);
      if (iNull >= 0)
        fail("Null element: " + iNull + "/" + l(els));
      renderedCode = javaExpr(
           functionCall_list ll(els),
        -> mapQuickEval_javaExpr(els));
    }
    ret renderedCode;
  }

  // start compressors  
  public void step0 {
    if (elementCompressionStrategy == null) ret;
    
    probabilisticForEach(scheduler(), 1.0, 0.5, indexAsSideValue(inputData()), ivf1WithToString("Compress element", data -> {
      int i = data.sideValue();
      var subSearch = new CompressionSearch_AnyType<IJavaExpr<El>, El>(assertNotNull(+childRegime()), data!, 1 /* TODO */);
      subSearch.onNewBest(-> set(dirtyElements, i));
      listSet(searches, i, subSearch);
      subSearch.addStrategy(elementCompressionStrategy!);
    }));
  }
  
  S stats() {
    ret renderVars(+nNotLiteral() + " of " + size());
  }
  
  // might as well work (reuse compression regime for children)
  swappable CompressionRegime_AnyType<IJavaExpr<El>> childRegime() {
    ret (CompressionRegime_AnyType) regime();
  }

  bool isElementCompressed(int i) {  
    synchronized(compressedElementsBitSet) {
      ret compressedElementsBitSet.get(i);
    }
  }
}

Author comment

Began life as a copy of #1033095

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033140
Snippet name: JE_CompressEachElementIndividually - works on a list
Eternal ID of this version: #1033140/83
Text MD5: 5f7154540c2b66763a898f5464c57eac
Transpilation MD5: 6fdd2fc5a02d83dff30dedb7c27d8c87
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-30 18:06:02
Source code size: 3182 bytes / 98 lines
Pitched / IR pitched: No / No
Views / Downloads: 394 / 735
Version history: 82 change(s)
Referenced in: #1033150 - AbstractCompressor
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)