Skip to content

Instantly share code, notes, and snippets.

@otrack
Created April 22, 2022 15:34
Show Gist options
  • Save otrack/2ff3f7ce148b0cec5638856299211b13 to your computer and use it in GitHub Desktop.
Save otrack/2ff3f7ce148b0cec5638856299211b13 to your computer and use it in GitHub Desktop.
public static class setsIterator<V> implements Iterator<V> {
Iterator<Set<V>> _inUnion;
Iterator<V> _inSet;
Collection<Set<V>> _elements;
public setsIterator(Collection<Set<V>> elts) {
_elements = elts;
if (_elements.size() > 0) {
_inUnion = _elements.iterator();
_inSet = _inUnion.next().iterator();
}
}
public boolean hasNext() {
if (_inUnion == null) return false;
if (!_inSet.hasNext()) {
do {
if (!_inUnion.hasNext()) return false;
_inSet = _inUnion.next().iterator();
}
while (!_inSet.hasNext());
}
return true;
}
public V next() {
if (!_inSet.hasNext()) {
do {
if (!_inUnion.hasNext()) throw new NoSuchElementException();
_inSet = _inUnion.next().iterator();
}
while (!_inSet.hasNext());
}
return _inSet.next();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment