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

44
LINES

< > BotCompany Repo | #1002528 // CondensedStrings2 (include)

JavaX fragment (include)

// stores all strings as UTF8 (typically half as big)
static class CondensedStrings2 extends AbstractList<S> implements RandomAccess {
  byte[] data;
  int[] indices;

  *() {} // only for serialisation
  
  *(L<S> l) {
    long totalLength = 0;
    for (S s : l) totalLength += l(toUtf8(s));
    int len = (int) totalLength;
    if (len != totalLength) fail("TOO BIG!!!!!! " + totalLength);
    data = new byte[len];
    int n = l(l);
    indices = new int[n];
    int idx = 0;
    for (int i = 0; i < n; i++) {
      S s = l.get(i);
      try {
        byte[] x = toUtf8(s);
        System.arraycopy(x, 0, data, idx, l(x));
        indices[i] = idx;
        idx += l(x);
      } catch (RuntimeException e) {
        print("idx=" + idx + ", s=" + s + ", i=" + i +", n=" + n + ", len=" + len);
        throw e;
      }
    }
  }
  
  public S get(int i) ctex {
    int idx1 = indices[i];
    int idx2 = i+1 < indices.length ? indices[i+1] : data.length;
    ret fastu_fromUtf8(data, idx1, idx2-idx1);
  }
  
  public int size() {
    ret indices.length;
  }
  
  int totalSize() {
    ret data.length;
  }
}

Author comment

Began life as a copy of #1002526

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002528
Snippet name: CondensedStrings2 (include)
Eternal ID of this version: #1002528/6
Text MD5: ef305b78b10a4da64ec508f356e43dfa
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-30 02:29:30
Source code size: 1150 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 613 / 1752
Version history: 5 change(s)
Referenced in: [show references]