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

37
LINES

< > BotCompany Repo | #1013600 // splitIterator - split an iterator into parallel iterators with dynamic buffering

JavaX fragment (include)

// iterators are not thread-safe
static <A> L<IterableIterator<A>> splitIterator(final int n, final Iterator<A> it) {
  class SplitIterator {
    final int[] positions = repIntAsArray(n, -1);
    new CircularArrayList<A> buffer;
    int first;
    bool done;
  
    L<IterableIterator<A>> iterators() {
      new L<IterableIterator<A>> l;
      for _i to n: {
        final int i = _i;
        l.add((IterableIterator) iteratorFF(func {
          positions[i]++;
          updateBuffer();
          int idx = positions[i]-first;
          ret idx >= l(buffer) ? endMarker() : buffer.get(idx);
        }));
      }
      ret l;
    }
    
    int last() { ret first+l(buffer); }
    
    void updateBuffer {
      int min = min(positions), max = max(positions);
      while (min > first) { buffer.remove(0); ++first; }
      while (!done && max >= last()) {
        if (!it.hasNext())
          done = true;
        else
          buffer.add(it.next());
      }
    }
  }
  ret new SplitIterator().iterators();
}

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: #1013600
Snippet name: splitIterator - split an iterator into parallel iterators with dynamic buffering
Eternal ID of this version: #1013600/13
Text MD5: 5679d53cd7a64bc35079495a2f3a6e58
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-11 14:21:31
Source code size: 1047 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 531 / 545
Version history: 12 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)