
org.davidmoten.kool.internal.operators.stream.PowerSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kool Show documentation
Show all versions of kool Show documentation
Streaming library supporting reuse and many operators
package org.davidmoten.kool.internal.operators.stream;
import java.util.NoSuchElementException;
import java.util.Set;
import org.davidmoten.kool.Stream;
import org.davidmoten.kool.StreamIterator;
import com.github.davidmoten.guavamini.Sets;
public class PowerSet implements Stream> {
private final int n;
public PowerSet(int n) {
this.n = n;
}
@Override
public StreamIterator> iterator() {
return new StreamIterator>() {
boolean[] b = new boolean[n];
@Override
public boolean hasNext() {
return b != null;
}
@Override
public Set next() {
if (b == null) {
throw new NoSuchElementException();
}
Set set = Sets.newHashSet();
for (int i = 0; i < b.length; i++) {
if (b[i]) {
set.add(i + 1);
}
}
int i = 0;
while (true) {
if (i == b.length) {
break;
}
boolean x = b[i];
b[i] = !b[i];
if (!x) {
break;
}
i++;
}
if (i == b.length) {
b = null;
}
return set;
}
@Override
public void dispose() {
// do nothing
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy