org.paukov.combinatorics.IntegerGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of combinatoricslib Show documentation
Show all versions of combinatoricslib Show documentation
Very simple java library to generate permutations, combinations and other
combinatorial sequences.
The newest version!
/**
* Combinatorics Library
* Copyright 2012 Dmytro Paukov [email protected]
*/
package org.paukov.combinatorics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* This is a special abstract class for all integer generators.
*
* There is a general pattern how to use the integer generators.
*
*
* // create an integer vector
* IntegerVector vector = new IntegerVector(new int[] { elements });
*
* // create a concrete integer generator
* IntegerGenerator generator = IntegerFactory.create < Concrete > Generator(vector);
*
* // Iterate the generated objects
* for (IntegerVector v : generator) {
* System.out.println(v);
* }
*
*
* @author Dmytro Paukov
* @version 2.0
* @see IntegerVector
* @see IntegerFactory
* @see IGenerator
*/
public abstract class IntegerGenerator implements IGenerator {
/**
* Returns all generated vectors as a list
*
* @return List of all generated objects/vectors
*/
@Override
public List generateAllObjects() {
return generateFilteredObjects(null);
}
/**
* Returns the generated vectors filtered by a filter
*
* @param filter The filter to be applied to the generated result
* @return The list of the filtered vectors
* @see IFilter
*/
@Override
public List generateFilteredObjects(
IFilter filter) {
List list = new ArrayList();
long index = 0;
for (IntegerVector vector : this) {
if (filter == null || filter.accepted(index, vector)) {
list.add(vector);
}
index++;
}
return list;
}
/**
* Returns vectors as a list for specified range of indexes (from the
* startIndex
to stopIndex
)
*
* @return List of the generated objects/vectors
*/
@Override
public List generateObjectsRange(long startIndex,
long stopIndex) {
assert (startIndex <= stopIndex);
List list = new ArrayList();
Iterator iterator = this.iterator();
int index = 1;
while (iterator.hasNext()) {
if (index >= startIndex && index <= stopIndex) {
list.add(iterator.next());
} else if (index > stopIndex) {
return list;
} else {
iterator.next();
}
index++;
}
return list;
}
}