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).

1  
sclass JE_CompressEachElementIndividually<El> extends AbstractCompressor_AnyType<IJavaExpr, L<El>> {
2  
  settable IF0<AbstractCompressor_AnyType<IJavaExpr<El>, El>> elementCompressionStrategy;
3  
  new L<CompressionSearch_AnyType<IJavaExpr<El>, El>> searches;
4  
  settable IF1<El, IJavaExpr> makeLiteral; // required
5  
  new BitSet compressedElementsBitSet;
6  
  // TODO: akas for variables
7  
  
8  
  selfType onCompressionFail(IF1<El, IJavaExpr> f) { makeLiteral = f; this; }
9  
  
10  
  settable bool debug;
11  
12  
  // internal
13  
  
14  
  new L<IJavaExpr> renderedElements;
15  
  IJavaExpr renderedCode;
16  
  new BitSet dirtyElements;
17  
  
18  
  int size() { ret l(inputData()); }
19  
  int nNotLiteral() { ret countPred(searches, search -> search.has()); }
20  
  
21  
  scaffolded IJavaExpr getRenderedElement(int i) {
22  
    if (contains(dirtyElements, i) || _get(renderedElements, i) == null) {
23  
      remove(dirtyElements, i);
24  
      //print("Rendering element " + (i+1) + "/" + size());
25  
      var e = renderElement(i);
26  
      listSet(renderedElements, i, e);
27  
      //print("  => " + e);
28  
    }
29  
    ret renderedElements.get(i);
30  
  }
31  
    
32  
  IJavaExpr renderElement(int i) {
33  
    var search = _get(searches, i);
34  
    IJavaExpr comp = search?!;
35  
    if (comp != null) {
36  
      synchronized(compressedElementsBitSet) { compressedElementsBitSet.set(i); }
37  
      ret comp;
38  
    }
39  
    
40  
    // no "good" compression found, return literal
41  
    IJavaExpr java = makeLiteral.get(_get(inputData(), i));
42  
    metaSet(java, gaveUp := this);
43  
    ret java;
44  
  }
45  
  
46  
  bool dirty() { ret nempty(dirtyElements) || l(renderedElements) < size(); }
47  
  
48  
  public void flush {
49  
    if (!dirty()) ret;
50  
    submit(code());
51  
  }
52  
53  
  L<IJavaExpr> codeForElements() {
54  
    ret countIteratorToList getRenderedElement(size());
55  
  }
56  
  
57  
  // get current best compressed representation
58  
  IJavaExpr code() {
59  
    if (dirty() || renderedCode == null) {
60  
      L<IJavaExpr> els = codeForElements();
61  
      int iNull = indexOf(els, null);
62  
      if (iNull >= 0)
63  
        fail("Null element: " + iNull + "/" + l(els));
64  
      renderedCode = javaExpr(
65  
           functionCall_list ll(els),
66  
        -> mapQuickEval_javaExpr(els));
67  
    }
68  
    ret renderedCode;
69  
  }
70  
71  
  // start compressors  
72  
  public void step0 {
73  
    if (elementCompressionStrategy == null) ret;
74  
    
75  
    probabilisticForEach(scheduler(), 1.0, 0.5, indexAsSideValue(inputData()), ivf1WithToString("Compress element", data -> {
76  
      int i = data.sideValue();
77  
      var subSearch = new CompressionSearch_AnyType<IJavaExpr<El>, El>(assertNotNull(+childRegime()), data!, 1 /* TODO */);
78  
      subSearch.onNewBest(-> set(dirtyElements, i));
79  
      listSet(searches, i, subSearch);
80  
      subSearch.addStrategy(elementCompressionStrategy!);
81  
    }));
82  
  }
83  
  
84  
  S stats() {
85  
    ret renderVars(+nNotLiteral() + " of " + size());
86  
  }
87  
  
88  
  // might as well work (reuse compression regime for children)
89  
  swappable CompressionRegime_AnyType<IJavaExpr<El>> childRegime() {
90  
    ret (CompressionRegime_AnyType) regime();
91  
  }
92  
93  
  bool isElementCompressed(int i) {  
94  
    synchronized(compressedElementsBitSet) {
95  
      ret compressedElementsBitSet.get(i);
96  
    }
97  
  }
98  
}

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: 395 / 736
Version history: 82 change(s)
Referenced in: [show references]