Example usage for java.util Arrays binarySearch

List of usage examples for java.util Arrays binarySearch

Introduction

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

Prototype

public static int binarySearch(Object[] a, Object key) 

Source Link

Document

Searches the specified array for the specified object using the binary search algorithm.

Usage

From source file:Main.java

/**
 * Returns the child nodes of the passed node where the type of the child node is in the passed array of type codes
 * @param node The node to return the child nodes of
 * @param types The short codes for the node types
 * @return a [possibly empty] list of nodes
 *///from  ww  w.j a  va  2  s. com
public static List<Node> getChildNodes(final Node node, short... types) {
    if (node == null)
        return Collections.emptyList();
    if (types == null || types.length == 0)
        return Collections.emptyList();
    final List<Node> nodes = new ArrayList<Node>();
    for (Node n : nodeListToList(node.getChildNodes())) {
        if (Arrays.binarySearch(types, n.getNodeType()) >= 0) {
            nodes.add(n);
        }
    }
    return nodes;
}

From source file:gedi.util.math.function.StepFunction.java

public double applyAsDouble(double x) {
    int index = Arrays.binarySearch(this.x, x);
    double fx = 0;

    if (index < -1) {
        // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]".
        fx = y[-index - 2];/*from   w w  w . j av a2 s .  c  o m*/
    } else if (index >= 0) {
        // "x" is exactly "abscissa[index]".
        fx = y[index];
    } else {
        // Otherwise, "x" is smaller than the first value in "abscissa"
        // (hence the returned value should be "ordinate[0]").
        fx = y[0];
    }

    return fx;
}

From source file:gedi.util.math.function.PiecewiseLinearFunction.java

public double applyAsDouble(double x) {
    int index = Arrays.binarySearch(this.x, x);
    double fx = 0;

    if (index < -1) {
        // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]".
        if (-index - 1 == this.x.length)
            fx = y[-index - 2];//w w w .  j  av a  2s.  c  om
        else {
            double frac = (x - this.x[-index - 2]) / (this.x[-index - 1] - this.x[-index - 2]);
            fx = (1 - frac) * y[-index - 2] + frac * y[-index - 1];
        }
    } else if (index >= 0) {
        // "x" is exactly "abscissa[index]".
        fx = y[index];
    } else {
        // Otherwise, "x" is smaller than the first value in "abscissa"
        // (hence the returned value should be "ordinate[0]").
        fx = y[0];
    }

    return fx;
}

From source file:com.actelion.research.spiritcore.util.StatUtils.java

/**
 * Calculate Kruskal-Walis (http://en.wikipedia.org/wiki/Kruskal%E2%80%93Wallis_one-way_analysis_of_variance)
 * @param values/*from www .j  a  v  a2  s  .c  o m*/
 * @return
 */
public static double getKruskalWallis(List<double[]> values) {

    //Assign ranks
    assert values.size() > 1;

    List<Double> allDoubles = new ArrayList<>();
    for (double[] a : values) {
        assert a.length > 0;
        for (double d : a) {
            allDoubles.add(d);
        }
    }
    int N = allDoubles.size();
    Collections.sort(allDoubles);
    double[] allDoublesArray = new double[allDoubles.size()];
    for (int i = 0; i < allDoubles.size(); i++)
        allDoublesArray[i] = allDoubles.get(i);
    List<double[]> ranks = new ArrayList<double[]>();
    for (double[] a : values) {
        double[] rankArray = new double[a.length];
        ranks.add(rankArray);
        for (int i = 0; i < a.length; i++) {
            int r = Arrays.binarySearch(allDoublesArray, a[i]);
            assert r >= 0;
            int r1 = r, r2 = r;
            while (r1 > 0 && allDoublesArray[r1 - 1] == a[i])
                r1--;
            while (r2 < allDoublesArray.length - 1 && allDoublesArray[r2 + 1] == a[i])
                r2++;
            rankArray[i] = (r1 + r2) / 2.0 + 1;
        }
    }

    //Calculate rank average per group
    List<Double> rankSum = new ArrayList<Double>();
    for (double[] a : ranks) {
        double sum = 0;
        for (double d : a)
            sum += d;
        rankSum.add(sum);
    }

    double sum = 0;
    for (int i = 0; i < ranks.size(); i++) {
        sum += rankSum.get(i) * rankSum.get(i) / ranks.get(i).length;
    }
    double H = 12.0 / (N * (N + 1)) * sum - 3 * (N + 1);

    ChiSquaredDistribution chi = new ChiSquaredDistribution(values.size() - 1);
    double K = 1 - chi.cumulativeProbability(H);

    return K;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionTargetRaceId.java

@Override
public boolean testImpl(Env env) {
    if (!(env.target instanceof L2Npc))
        return false;

    return Arrays.binarySearch(_raceIds, ((L2Npc) env.target).getTemplate().getRace().ordinal()) >= 0;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionPlayerClassIdRestriction.java

@Override
public boolean testImpl(Env env) {
    if (!(env.player instanceof L2Player))
        return false;

    return Arrays.binarySearch(_classIds, ((L2Player) env.player).getClassId().getId()) >= 0;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionTargetClassIdRestriction.java

@Override
public boolean testImpl(Env env) {
    if (!(env.target instanceof L2Player))
        return false;

    return Arrays.binarySearch(_classIds, ((L2Player) env.target).getClassId().getId()) >= 0;
}

From source file:com.trenako.validation.ISOCountryValidator.java

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (StringUtils.isBlank(value)) {
        return true;
    }/*from   w w  w  . j a v  a2 s. co m*/
    return Arrays.binarySearch(Locale.getISOCountries(), value.toUpperCase()) >= 0;
}

From source file:com.trenako.validation.ISOLanguageValidator.java

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (StringUtils.isBlank(value)) {
        return true;
    }/* ww  w.  j a va 2  s.co m*/
    return Arrays.binarySearch(Locale.getISOLanguages(), value.toLowerCase()) >= 0;
}

From source file:org.drugepi.hdps.ZBiasCalculator.java

public static void scoreVariables(List<HdpsVariable> variableList) {
    // copy variables list
    List<HdpsVariable> expSortVariableList = new ArrayList<HdpsVariable>();
    List<HdpsVariable> outcomeSortVariableList = new ArrayList<HdpsVariable>();

    for (HdpsVariable var : variableList) {
        var.zBiasScore = 0;

        if ((var.expAssocRankingVariable != HdpsVariable.INVALID)
                && (var.outcomeAssocRankingVariable != HdpsVariable.INVALID)) {
            expSortVariableList.add(var);
            outcomeSortVariableList.add(var);
        }/*from   www.j a v  a 2  s .c om*/
    }

    // sort variables by exposure association (strongest first) 
    Collections.sort(expSortVariableList, new HdpsVariableReverseExposureAssociationComparator());

    // sort variables by outcome association (weakest first) 
    Collections.sort(outcomeSortVariableList, new HdpsVariableReverseOutcomeAssociationComparator());
    Collections.reverse(outcomeSortVariableList);

    // create an array of outcome strengths
    double[] outcomeStrengths = new double[outcomeSortVariableList.size()];
    for (int i = 0; i < outcomeStrengths.length; i++)
        outcomeStrengths[i] = outcomeSortVariableList.get(i).outcomeAssocRankingVariable;

    // array that will store breaks between deciles
    // find the median of outcome strength 
    Percentile pctile = new Percentile();

    // Find quintiles 1 through 5 of outcome weakness
    // AMONG the weakest half of the variables.
    // List is sorted strongest first, so the weakest variables 
    // will be at the end
    // quintile 1 = weakest 
    // don't use startsOfQuintile[0]

    double median = pctile.evaluate(outcomeStrengths, 50.0);
    int searchCeiling = Arrays.binarySearch(outcomeStrengths, median);
    if (searchCeiling < 0)
        searchCeiling = -(searchCeiling + 1);

    int startsOfQuintile[] = new int[7];

    for (int quintile = 1; quintile <= 5; quintile++) {
        // find the probability that *begins* this quintile
        double p = (quintile - 1) * 20;
        if (p > 0) {
            double quintileStartP = pctile.evaluate(outcomeStrengths, 0, searchCeiling, (quintile - 1) * 20);

            startsOfQuintile[quintile] = Arrays.binarySearch(outcomeStrengths, quintileStartP);
            if (startsOfQuintile[quintile] < 0)
                startsOfQuintile[quintile] = -(startsOfQuintile[quintile] + 1);
        } else
            startsOfQuintile[quintile] = 0;
    }
    startsOfQuintile[6] = searchCeiling;

    // score the variables, BUT make quintile 5 the weakest
    for (int quintile = 1; quintile <= 5; quintile++) {
        for (int i = startsOfQuintile[quintile]; i < startsOfQuintile[quintile + 1]; i++) {
            HdpsVariable v = outcomeSortVariableList.get(i);
            v.zBiasScore = 6 - quintile;
        }
    }

    //      for (HdpsVariable v: outcomeSortVariableList) {
    //         System.out.printf("%s    %1.4f    %d\n", v.varName, v.outcomeAssocRankingVariable, v.zBiasScore);
    //      }
}