All Downloads are FREE. Search and download functionalities are using the official Maven repository.

parallel.partitioners.RangePartitioner Maven / Gradle / Ivy

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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy