net.jqwik.engine.support.combinatorics.SetIterator Maven / Gradle / Ivy
package net.jqwik.engine.support.combinatorics;
import java.util.*;
public class SetIterator implements Iterator> {
private final Iterator> combinedListIterator;
private final Set> generatedSets = new HashSet<>();
private final int setSize;
private Set next;
public SetIterator(Iterable elementIterable, int setSize) {
this.setSize = setSize;
List> iterables = new ArrayList<>();
for (int i = 0; i < setSize; i++) {
iterables.add(elementIterable);
}
combinedListIterator = new CombinedIterator<>(iterables);
next = findNext();
}
private Set findNext() {
while (combinedListIterator.hasNext()) {
HashSet candidate = new HashSet<>(combinedListIterator.next());
if (candidate.size() != setSize || generatedSets.contains(candidate)) {
continue;
}
generatedSets.add(candidate);
return candidate;
}
return null;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Set next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Set current = next;
next = findNext();
return current;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy