Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

public static byte[] copyArray(byte[] original, int start, int length) {
    byte[] copy = new byte[length];
    System.arraycopy(original, start, copy, 0, Math.min(original.length - start, length));
    return copy;/*from w  w  w.  j  a v a  2  s .  c o m*/
}

From source file:Main.java

/**
 * This treatment does not check that the specified ranges are increasing,
 * to allow for toString of not (yet) sorted ranges.
 * //from  ww w .ja  va2s. c om
 * @param sb Buffer where to add representation of the specified ranges.
 * @param maxNbrOfRanges Max number of ranges to consider. Must be >= 0.
 * @param sequencesRanges Sequences ranges, as {min1,max1,min2,max2,etc.}.
 */
public static void appendStringSequencesRanges(StringBuilder sb, int maxNbrOfRanges,
        final long... sequencesRanges) {
    if (maxNbrOfRanges < 0) {
        throw new IllegalArgumentException("max number of ranges [" + maxNbrOfRanges + "] must be >= 0");
    }
    // Does the ranges check.
    final int nbrOfRanges = computeNbrOfRanges(sequencesRanges);
    final int nbrOfRangesToConsider = Math.min(nbrOfRanges, maxNbrOfRanges);
    sb.append("{");
    if (nbrOfRanges != 0) {
        boolean firstDone = false;
        for (int i = 0; i < nbrOfRangesToConsider; i++) {
            if (firstDone) {
                sb.append(",");
            }
            final long min = sequencesRanges[2 * i];
            final long max = sequencesRanges[2 * i + 1];
            sb.append(min);
            if (max != min) {
                sb.append("..");
                sb.append(max);
            }
            firstDone = true;
        }
    }
    if (nbrOfRangesToConsider != nbrOfRanges) {
        if (nbrOfRangesToConsider != 0) {
            sb.append(",");
        }
        sb.append("...");
    }
    sb.append("}");
}

From source file:Main.java

public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {

    Bitmap bitmap;/*from www. ja  v  a  2s .co m*/
    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }

    if (radius < 1) {
        return (null);
    }

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return (bitmap);
}

From source file:edu.cmu.lti.oaqa.annographix.solr.SolrEvalUtils.java

/**
 * Saves query results to a TREC result file.
 * // w w w  .ja va 2s  . c o  m
 * @param topicId     a question ID.
 * @param results     found entries to memorize.
 * @param trecFile    an object used to write to the output file.
 * @param runId       a run ID.
 * @param maxNum      a maximum number of results to save (can be less than 
 *                    the number of retrieved entries).
 * @throws IOException
 */
public static void saveTrecResults(String topicId, SolrRes[] results, BufferedWriter trecFile, String runId,
        int maxNum) throws IOException {
    boolean bNothing = true;
    for (int i = 0; i < Math.min(results.length, maxNum); ++i) {
        bNothing = false;
        saveTrecOneEntry(trecFile, topicId, results[i].mDocId, (i + 1), results[i].mScore, runId);
    }
    /*
     *  If nothing is returned, let's a fake entry, otherwise trec_eval
     *  will completely ignore output for this query (it won't give us zero
     *  as it should have been!)
     */
    if (bNothing) {
        saveTrecOneEntry(trecFile, topicId, FAKE_DOC_ID, 1, 0, runId);
    }
}

From source file:Main.java

/**
 * @deprecated/* w w w .  j a  va  2  s . c  o  m*/
 */
// TODO optimize this method.
public static Bitmap blur(Bitmap source, int radius) {
    if (source == null) {
        return null;
    }
    Bitmap bitmap = source.copy(source.getConfig(), true);
    if (radius < 1 || bitmap == null) {
        return null;
    }
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum = divsum * divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return bitmap;
}

From source file:net.dontdrinkandroot.utils.collections.CollectionUtils.java

public static <T extends Number> AggregationResult aggregate(final Collection<T> collection) {

    if (collection.isEmpty()) {
        throw new IllegalArgumentException("Collection must not be empty");
    }/*  www  . j  ava 2 s.c  om*/

    final AggregationResult result = new AggregationResult();
    result.setSize(collection.size());
    double sum = 0;
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;

    /* Determine min, max, sum */
    for (final T entry : collection) {
        if (entry != null) {
            final double value = entry.doubleValue();
            max = Math.max(max, value);
            min = Math.min(min, value);
            sum += value;
        }
    }
    result.setMin(min);
    result.setMax(max);
    result.setSum(sum);

    result.setAvg(sum / result.getSize());

    result.setMean(CollectionUtils.getMean(collection));

    return result;
}

From source file:Range.java

/**
 * @param r//www .  ja  v  a2s . c om
 * @param dest
 * @return <code>true</code> if the intersection exists
 */
public boolean intersection(Range r, Range dest) {
    if (intersects(r)) {
        dest.set(Math.max(min, r.min), Math.min(max, r.max));
        return true;
    }

    return false;
}

From source file:Main.java

/**
 * Make a color scaled using a defined factor.
 *
 * @param color//w  ww  . jav  a 2s  .co m
 *          the color to scale.
 * @param factor
 *          the factor to use.
 * @return the scaled color.
 */
public static Color getScaledColor(Color color, double factor) {
    if (color == null) {
        return null;
    }
    if (factor <= 1) {
        return new Color(Math.max((int) (color.getRed() * factor), 0),
                Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0),
                color.getAlpha());
    }
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();

    int i = (int) (1.0 / (factor - 1));
    if (r == 0 && g == 0 && b == 0) {
        return new Color(i, i, i);
    }
    if (r > 0 && r < i) {
        r = i;
    }
    if (g > 0 && g < i) {
        g = i;
    }
    if (b > 0 && b < i) {
        b = i;
    }

    return new Color(Math.min((int) (r / (2 - factor)), 255), Math.min((int) (g / (2 - factor)), 255),
            Math.min((int) (b / (2 - factor)), 255));

}

From source file:com.diversityarrays.kdxplore.trialtool.SimplePlotCellRenderer.java

static private String getAttachmentCountCharacter(int n) {
    UnicodeChars.Number[] numbers = UnicodeChars.Number.values();
    return numbers[Math.min(n, numbers.length - 1)].parenthesis;
}

From source file:Main.java

/**
 * Returns a three-element array of mutable {@code List}s:
 * <ol>//from   ww w  .java  2  s . com
 *    </li>
 *      the intersection of {@code a} and {@code b}
 *    <li>
 *    </li>
 *      the {@code a} - {@code b} difference
 *    <li>
 *    </li>
 *      the {@code b} - {@code a} difference
 *    <li>
 * </ol>
 *
 * @param       comparator
 *          a comparator to sort results, or {@code null} to remain
 *          unsorted
 */
public static <T> List<T>[] getIntersectAndDiffs(Collection<? extends T> a, Collection<? extends T> b,
        Comparator<T> comparator) {

    int aSize = a.size();
    List<T> aDiff = new ArrayList<T>(aSize);
    aDiff.addAll(a);

    int bSize = b.size();
    List<T> bDiff = new ArrayList<T>(bSize);
    bDiff.addAll(b);

    if (comparator != null) {
        Collections.sort(aDiff, comparator);
        Collections.sort(bDiff, comparator);
    }

    List<T> abInter = new ArrayList<T>(Math.min(aSize, bSize));

    for (int i = aDiff.size() - 1; i >= 0; i--) {
        T element = aDiff.get(i);

        int index = comparator == null ? bDiff.indexOf(element)
                : Collections.binarySearch(bDiff, element, comparator);

        if (index != -1) {
            bDiff.remove(index);
            aDiff.remove(i);
            abInter.add(element);
        }
    }

    @SuppressWarnings({ "unchecked" })
    List<T>[] array = (List<T>[]) new List[] { abInter, aDiff, bDiff };

    return array;
}