sclass OrderedMultiIterator> extends ItIt {
srecord noeq Stream>(Iterator iterator, A element) is Comparable> {
public int compareTo(Stream stream) {
ret element.compareTo(stream.element);
}
bool advance() {
if (!iterator.hasNext()) false;
element = iterator.next();
true;
}
}
new TreeSet> streams;
void add(Iterable it) {
add(iteratorOrNull(it));
}
void add(Iterator iterator) {
if (iterator == null || !iterator.hasNext()) ret;
A element = iterator.next();
streams.add(new Stream(iterator, element));
}
void addAll(Iterable extends Iterator> iterators) {
fOr (it : iterators) add(it);
}
public bool hasNext() {
ret nempty(streams);
}
public A next() {
var stream = first(streams);
A element = stream.element;
streams.remove(stream);
if (stream.advance())
streams.add(stream);
ret element;
}
}