1 | // iterators are not thread-safe |
2 | static <A> L<IterableIterator<A>> splitIterator(final int n, final Iterator<A> it) {
|
3 | class SplitIterator {
|
4 | final int[] positions = repIntAsArray(n, -1); |
5 | new CircularArrayList<A> buffer; |
6 | int first; |
7 | bool done; |
8 | |
9 | L<IterableIterator<A>> iterators() {
|
10 | new L<IterableIterator<A>> l; |
11 | for _i to n: {
|
12 | final int i = _i; |
13 | l.add((IterableIterator) iteratorFF(func {
|
14 | positions[i]++; |
15 | updateBuffer(); |
16 | int idx = positions[i]-first; |
17 | ret idx >= l(buffer) ? endMarker() : buffer.get(idx); |
18 | })); |
19 | } |
20 | ret l; |
21 | } |
22 | |
23 | int last() { ret first+l(buffer); }
|
24 | |
25 | void updateBuffer {
|
26 | int min = min(positions), max = max(positions); |
27 | while (min > first) { buffer.remove(0); ++first; }
|
28 | while (!done && max >= last()) {
|
29 | if (!it.hasNext()) |
30 | done = true; |
31 | else |
32 | buffer.add(it.next()); |
33 | } |
34 | } |
35 | } |
36 | ret new SplitIterator().iterators(); |
37 | } |
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: | 751 / 769 |
| Version history: | 12 change(s) |
| Referenced in: | [show references] |