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

39
LINES

< > BotCompany Repo | #1035860 // ConcatOnDemandList

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

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

// still efficient when used to build a list recursively
persistable sclass ConcatOnDemandList<A> extends RandomAccessAbstractList<A> {
  new LL<A> parts;
  L<A> completeList;
  
  *(L<A>... parts) { fOr (part : parts) addPart(part); }
  
  void addPart(L<A> part) { parts.add(part); }
  
  public int size() { ret completeList().size(); }
  public A get(int i) { ret completeList().get(i); }
  
  L<A> completeList() {
    if (parts != null) {
      new L<A> out;
      for (part : parts)
        collect(part, out);
      parts = null;
      completeList = out;
    }
    ret completeList;
  }
  
  void collect(L<A> part, L<A> out) {
    if (part == null) ret;
    if (part cast ConcatOnDemandList)
      part.collectInto(out);
    else
      out.addAll(part);
  }
  
  void collectInto(L<A> out) {
    if (parts != null)
      for (part : parts)
        collect(part, out);
    else
      out.addAll(completeList);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035860
Snippet name: ConcatOnDemandList
Eternal ID of this version: #1035860/9
Text MD5: dc8d62b783030d610843868cf7119196
Transpilation MD5: 7de169259f23bd7d88c308bbb4c5fd91
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-03 07:01:04
Source code size: 962 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 136 / 230
Version history: 8 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)