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

44
LINES

< > BotCompany Repo | #1002525 // Compact String List Test (v1, char array with indices, developing)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (579L/5K/15K).

!752

static class CondensedStrings {
  char[] data;
  int[] indices;
  
  *(L<S> l) {
    long totalLength = 0;
    for (S s : l) totalLength += l(s);
    int len = (int) totalLength;
    if (len != totalLength) fail("TOO BIG!!!!!! " + totalLength);
    data = new char[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 {
        s.getChars(0, l(s), data, idx);
        indices[i] = idx;
        idx += l(s);
      } catch (RuntimeException e) {
        print("idx=" + idx + ", s=" + s + ", i=" + i +", n=" + n + ", len=" + len);
        throw e;
      }
    }
  }
  
  S get(int i) {
    int idx1 = indices[i];
    int idx2 = i+1 < indices.length ? indices[i+1] : data.length;
    ret new String(data, idx1, idx2-idx1);
  }
  
  int size() {
    ret indices.length;
  }
}

p {
  CondensedStrings cs = new CondensedStrings(litlist("hello", "world!"));
  print(quote(cs.get(0)));
  print(quote(cs.get(1)));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002525
Snippet name: Compact String List Test (v1, char array with indices, developing)
Eternal ID of this version: #1002525/1
Text MD5: 6e35d679e5e0e138811e045707db5103
Transpilation MD5: b6d62cc7ec9cfb578034224b7c5f4d84
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-30 23:18:06
Source code size: 1034 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 572 / 584
Referenced in: [show references]