Example usage for java.util Arrays parallelSort

List of usage examples for java.util Arrays parallelSort

Introduction

In this page you can find the example usage for java.util Arrays parallelSort.

Prototype

@SuppressWarnings("unchecked")
public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) 

Source Link

Document

Sorts the specified array of objects according to the order induced by the specified comparator.

Usage

From source file:sadl.integration.MonteCarloIntegration.java

public void preprocess(ContinuousDistribution d, double stepSize, double xMin, double xMax) {
    if (d instanceof SingleValueDistribution) {
        singleValueDis = true;/*  w w  w .  ja va  2 s .  c  o  m*/
        preprocessed = true;
        return;
    }
    if (Double.isInfinite(xMin)) {
        xMin = Double.MIN_VALUE;
    }
    if (Double.isInfinite(xMax)) {
        xMax = Double.MAX_VALUE;
    }
    final Pair<Double, Double> minMax = findExtreme(d, xMin, xMax, stepSize);
    final double yMin = minMax.getLeft().doubleValue();
    final double yMax = minMax.getRight().doubleValue();
    final double xDiff = xMax - xMin;
    final double yDiff = yMax - yMin;

    int pointsFound = 0;
    int pointsRejected = 0;
    integral = new MonteCarloPoint[pointsToStore];
    while (pointsFound < pointsToStore) {
        final double xSampled = xMin + (xDiff * xRandom.nextDouble());
        final double ySampled = yMin + (yDiff * yRandom.nextDouble());
        final double pdfValue = d.pdf(xSampled);
        if (pdfValue > 0 && ySampled <= pdfValue) {
            // store the point because the sampled y value is smaller than the pdf value at the x value
            integral[pointsFound] = new MonteCarloPoint(xSampled, pdfValue);
            pointsFound++;
        } else {
            pointsRejected++;
        }
    }
    logger.debug("Rejected {} points", pointsRejected);
    logger.debug("Accepted {} points", pointsFound);
    if (Settings.isParallel()) {
        Arrays.parallelSort(integral, new MonteCarloPointComparator());
    } else {
        Arrays.sort(integral, new MonteCarloPointComparator());
    }

    // Collections.sort(integral2);
    // integral2.sort((m1, m2) -> Double.compare(m1.getX(), m2.getX()));
    preprocessed = true;
}