// null elements are not supported
static , B extends Iterable> ItIt intersectMultipleSortedCollectionsI(L collections) {
if (empty(collections)) ret emptyItIt();
ret iteratorFromFunction_if0(new IF0 {
Iterator[] iterators = makeIteratorArray(collections);
int n = iterators.length;
A[] objects = (A[]) new Comparable[n];
bool done;
{
// Grab first round of objects
for i to n:
if (step(i)) break;
}
// returns true if done
bool step(int i) {
if (!iterators[i].hasNext())
{ set done; true; }
else {
objects[i] = iterators[i].next();
ifdef intersectMultipleSortedCollectionsI_debug
print("Stepped " + (i+1) + " => " + objects[i]);
endifdef
}
false;
}
public A get() {
while ping (true) {
if (done) null;
// get min & max
A min = objects[0], max = objects[0];
for (int i = 1; i < n; i++) {
A o = objects[i];
if (min.compareTo(o) > 0) min = o;
if (max.compareTo(o) < 0) max = o;
}
// min = max? then return element
ifdef intersectMultipleSortedCollectionsI_debug
print("min=" + min + ", max=" + max);
endifdef
if (min == max) {
for i to n: if (step(i)) break;
ret min;
}
// advance all to max
for i to n:
while (objects[i].compareTo(max) < 0)
if (step(i)) null;
}
}
});
}