Libraryless. Click here for Pure Java version (2583L/16K).
1 | // null elements are not supported |
2 | static <A extends Comparable<A>, B extends Iterable<A>> ItIt<A> intersectMultipleSortedCollectionsI(L<B> collections) { |
3 | if (empty(collections)) ret emptyItIt(); |
4 | |
5 | ret iteratorFromFunction_if0(new IF0<A> { |
6 | Iterator<A>[] iterators = makeIteratorArray(collections); |
7 | int n = iterators.length; |
8 | A[] objects = (A[]) new Comparable[n]; |
9 | bool done; |
10 | |
11 | { |
12 | // Grab first round of objects |
13 | for i to n: |
14 | if (step(i)) break; |
15 | } |
16 | |
17 | // returns true if done |
18 | bool step(int i) { |
19 | if (!iterators[i].hasNext()) |
20 | { set done; true; } |
21 | else { |
22 | objects[i] = iterators[i].next(); |
23 | ifdef intersectMultipleSortedCollectionsI_debug |
24 | print("Stepped " + (i+1) + " => " + objects[i]); |
25 | endifdef |
26 | } |
27 | false; |
28 | } |
29 | |
30 | public A get() { |
31 | while ping (true) { |
32 | if (done) null; |
33 | |
34 | // get min & max |
35 | |
36 | A min = objects[0], max = objects[0]; |
37 | for (int i = 1; i < n; i++) { |
38 | A o = objects[i]; |
39 | if (min.compareTo(o) > 0) min = o; |
40 | if (max.compareTo(o) < 0) max = o; |
41 | } |
42 | |
43 | // min = max? then return element |
44 | |
45 | ifdef intersectMultipleSortedCollectionsI_debug |
46 | print("min=" + min + ", max=" + max); |
47 | endifdef |
48 | |
49 | if (min == max) { |
50 | for i to n: if (step(i)) break; |
51 | ret min; |
52 | } |
53 | |
54 | // advance all to max |
55 | |
56 | for i to n: |
57 | while (objects[i].compareTo(max) < 0) |
58 | if (step(i)) null; |
59 | } |
60 | } |
61 | }); |
62 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029046 |
Snippet name: | intersectMultipleSortedCollectionsI [iterator] |
Eternal ID of this version: | #1029046/10 |
Text MD5: | 6c5a0680e4bd6b2982747c93ed7d588a |
Transpilation MD5: | 91daceac10cbd3df351d8251e8991de2 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-07-17 17:10:57 |
Source code size: | 1675 bytes / 62 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 157 / 260 |
Version history: | 9 change(s) |
Referenced in: | [show references] |