parallel.partitioners.RangePartitioner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jstat Show documentation
Show all versions of jstat Show documentation
Java Library for Statistical Analysis.
The newest version!
package parallel.partitioners;
import java.util.ArrayList;
import java.util.List;
public class RangePartitioner {
public static List> partition(int start, int end, int nParts){
List> partitions = new ArrayList<>();
if(nParts ==0 ){
partitions.add(new ArrayList<>());
for (int i = start; i < end; i++) {
partitions.get(0).add(i);
}
return partitions;
}
int workSize = (end-start)/nParts;
int partStart = 0;
int partEnd = workSize;
for (int i = 0; i < nParts; i++) {
partitions.add(new ArrayList<>());
for (int p = partStart; p < partEnd; ++p) {
partitions.get(i).add(p);
}
partStart = partEnd;
partEnd += workSize;
}
// collect what is left
while(partStart < end){
partitions.get(nParts - 1).add(partStart++);
}
return partitions;
}
}