![JAR search and dependency download from the Maven repository](/logo.png)
org.paukov.combinatorics3.SimpleSubSetIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of combinatoricslib3 Show documentation
Show all versions of combinatoricslib3 Show documentation
Simple java library to generate permutations, combinations and other combinatorial sequences
/**
* Combinatorics Library 3
* Copyright 2009-2016 Dmytro Paukov [email protected]
*/
package org.paukov.combinatorics3;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Iterator over the all subsets
*
* @author Dmytro Paukov
* @version 3.1.0
* @see SubSetGenerator
*
* @param
* Type of the elements of the subsets
*/
class SimpleSubSetIterator implements Iterator> {
private final SimpleSubSetGenerator generator;
private final int length;
private List currentSubSet = null;
private long currentIndex = 0;
/**
* internal bit vector, representing the subset
*/
private int[] bitVector = null;
SimpleSubSetIterator(final SimpleSubSetGenerator generator) {
this.generator = generator;
this.length = generator.originalVector.size();
this.currentSubSet = new ArrayList<>();
this.bitVector = new int[length + 2];
this.currentIndex = 0;
}
/**
* Returns true if iteration is done, otherwise false
*
* @see Iterator#hasNext()
*/
@Override
public boolean hasNext() {
return bitVector[length + 1] != 1;
}
/**
* Returns the next subset if it is available
*
* @see Iterator#next()
*/
@Override
public List next() {
currentIndex++;
currentSubSet.clear();
for (int index = 1; index <= length; index++) {
if (bitVector[index] == 1) {
T value = this.generator.originalVector.get(index - 1);
currentSubSet.add(value);
}
}
int i = 1;
while (bitVector[i] == 1) {
bitVector[i] = 0;
i++;
}
bitVector[i] = 1;
return new ArrayList<>(currentSubSet);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "SimpleSubSetIterator=[#" + currentIndex + ", " + currentSubSet + "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy