@Test public void testBinarySearch() { for (int pass=0; pass<50; pass++) { final double[] array = new double[64]; for (int i=0; i<array.length; i++) { array[i] = (random.nextInt(100) - 50) / 10; } Arrays.sort(array); for (int i=0; i<300; i++) { final double searchFor = (random.nextInt(150) - 75) / 10; assertEquals("binarySearch", Arrays.binarySearch(array, searchFor), CategoryList.binarySearch(array, searchFor)); } final Category[] categories = new Category[array.length]; for (int i=0; i<categories.length; i++) { categories[i] = new Category(String.valueOf(i), null, random.nextInt(100)).inverse; } Arrays.sort(categories, new CategoryList(new Category[0], null)); assertTrue("isSorted", CategoryList.isSorted(categories)); for (int i=0; i<categories.length; i++) { array[i] = categories[i].inverse.minimum; } for (int i=0; i<categories.length; i++) { final double expected = categories[i].inverse.minimum; final int foundAt = CategoryList.binarySearch(array, expected); final double actual = categories[foundAt].inverse.minimum; assertEquals("binarySearch", toHexString(expected), toHexString(actual)); } } }
static int binarySearch(final double[] array, final double key) { int low = 0; int high = array.length-1; final boolean keyIsNaN = Double.isNaN(key); while (low <= high) { final int mid = (low + high) >> 1; final double midVal = array[mid]; if (midVal < key) { low = mid + 1; continue; } if (midVal > key) { high = mid - 1; continue; } final long midRawBits = Double.doubleToRawLongBits(midVal); final long keyRawBits = Double.doubleToRawLongBits(key); if (midRawBits == keyRawBits) { return mid; } final boolean midIsNaN = Double.isNaN(midVal); final boolean adjustLow; if (keyIsNaN) { adjustLow = (!midIsNaN || midRawBits<keyRawBits); } else { adjustLow = (!midIsNaN && midRawBits<keyRawBits); } if (adjustLow) low = mid + 1; else high = mid - 1; } return -(low + 1); }
CategoryList extends AbstractList<Category> implements MathTransform1D, Comparator<Category>, Serializable { static int binarySearch(final double[] array, final double key) { int low = 0; int high = array.length-1; final boolean keyIsNaN = Double.isNaN(key); while (low <= high) { final int mid = (low + high) >> 1; final double midVal = array[mid]; if (midVal < key) { low = mid + 1; continue; } if (midVal > key) { high = mid - 1; continue; } final long midRawBits = Double.doubleToRawLongBits(midVal); final long keyRawBits = Double.doubleToRawLongBits(key); if (midRawBits == keyRawBits) { return mid; } final boolean midIsNaN = Double.isNaN(midVal); final boolean adjustLow; if (keyIsNaN) { adjustLow = (!midIsNaN || midRawBits<keyRawBits); } else { adjustLow = (!midIsNaN && midRawBits<keyRawBits); } if (adjustLow) low = mid + 1; else high = mid - 1; } return -(low + 1); } }
CategoryList extends AbstractList<Category> implements MathTransform1D, Comparator<Category>, Serializable { static int binarySearch(final double[] array, final double key) { int low = 0; int high = array.length-1; final boolean keyIsNaN = Double.isNaN(key); while (low <= high) { final int mid = (low + high) >> 1; final double midVal = array[mid]; if (midVal < key) { low = mid + 1; continue; } if (midVal > key) { high = mid - 1; continue; } final long midRawBits = Double.doubleToRawLongBits(midVal); final long keyRawBits = Double.doubleToRawLongBits(key); if (midRawBits == keyRawBits) { return mid; } final boolean midIsNaN = Double.isNaN(midVal); final boolean adjustLow; if (keyIsNaN) { adjustLow = (!midIsNaN || midRawBits<keyRawBits); } else { adjustLow = (!midIsNaN && midRawBits<keyRawBits); } if (adjustLow) low = mid + 1; else high = mid - 1; } return -(low + 1); } CategoryList(final Category[] categories, final Unit<?> units);   CategoryList(Category[] categories, Unit<?> units, boolean searchNearest, CategoryList inverse); }
CategoryList extends AbstractList<Category> implements MathTransform1D, Comparator<Category>, Serializable { static int binarySearch(final double[] array, final double key) { int low = 0; int high = array.length-1; final boolean keyIsNaN = Double.isNaN(key); while (low <= high) { final int mid = (low + high) >> 1; final double midVal = array[mid]; if (midVal < key) { low = mid + 1; continue; } if (midVal > key) { high = mid - 1; continue; } final long midRawBits = Double.doubleToRawLongBits(midVal); final long keyRawBits = Double.doubleToRawLongBits(key); if (midRawBits == keyRawBits) { return mid; } final boolean midIsNaN = Double.isNaN(midVal); final boolean adjustLow; if (keyIsNaN) { adjustLow = (!midIsNaN || midRawBits<keyRawBits); } else { adjustLow = (!midIsNaN && midRawBits<keyRawBits); } if (adjustLow) low = mid + 1; else high = mid - 1; } return -(low + 1); } CategoryList(final Category[] categories, final Unit<?> units);   CategoryList(Category[] categories, Unit<?> units, boolean searchNearest, CategoryList inverse); final int compare(final Category o1, final Category o2); CategoryList geophysics(final boolean geo); final InternationalString getName(); Unit<?> getUnits(); final NumberRange<?> getRange(); final ColorModel getColorModel(final int visibleBand, final int numBands); final ColorModel getColorModel(final int visibleBand, final int numBands, final int type); final Category getCategory(final double sample); final String format(final double value, final Locale locale); final int size(); final Category get(final int i); @Override final Category[] toArray(); @Override final String toString(); @Override boolean equals(final Object object); final int getSourceDimensions(); final int getTargetDimensions(); boolean isIdentity(); final MathTransform1D inverse(); final DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst); final Matrix derivative(final DirectPosition point); final double derivative(final double value); final double transform(double value); final void transform(double[] srcPts, int srcOff,
                                double[] dstPts, int dstOff, int numPts); final void transform(float[] srcPts, int srcOff,
                                float[] dstPts, int dstOff, int numPts); final void transform(float[] srcPts, int srcOff,
                                double[] dstPts, int dstOff, int numPts); final void transform(double[] srcPts, int srcOff,
                                float[] dstPts, int dstOff, int numPts); final void transform(final WritableRectIter iterator); String toWKT(); }
CategoryList extends AbstractList<Category> implements MathTransform1D, Comparator<Category>, Serializable { static int binarySearch(final double[] array, final double key) { int low = 0; int high = array.length-1; final boolean keyIsNaN = Double.isNaN(key); while (low <= high) { final int mid = (low + high) >> 1; final double midVal = array[mid]; if (midVal < key) { low = mid + 1; continue; } if (midVal > key) { high = mid - 1; continue; } final long midRawBits = Double.doubleToRawLongBits(midVal); final long keyRawBits = Double.doubleToRawLongBits(key); if (midRawBits == keyRawBits) { return mid; } final boolean midIsNaN = Double.isNaN(midVal); final boolean adjustLow; if (keyIsNaN) { adjustLow = (!midIsNaN || midRawBits<keyRawBits); } else { adjustLow = (!midIsNaN && midRawBits<keyRawBits); } if (adjustLow) low = mid + 1; else high = mid - 1; } return -(low + 1); } CategoryList(final Category[] categories, final Unit<?> units);   CategoryList(Category[] categories, Unit<?> units, boolean searchNearest, CategoryList inverse); final int compare(final Category o1, final Category o2); CategoryList geophysics(final boolean geo); final InternationalString getName(); Unit<?> getUnits(); final NumberRange<?> getRange(); final ColorModel getColorModel(final int visibleBand, final int numBands); final ColorModel getColorModel(final int visibleBand, final int numBands, final int type); final Category getCategory(final double sample); final String format(final double value, final Locale locale); final int size(); final Category get(final int i); @Override final Category[] toArray(); @Override final String toString(); @Override boolean equals(final Object object); final int getSourceDimensions(); final int getTargetDimensions(); boolean isIdentity(); final MathTransform1D inverse(); final DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst); final Matrix derivative(final DirectPosition point); final double derivative(final double value); final double transform(double value); final void transform(double[] srcPts, int srcOff,
                                double[] dstPts, int dstOff, int numPts); final void transform(float[] srcPts, int srcOff,
                                float[] dstPts, int dstOff, int numPts); final void transform(float[] srcPts, int srcOff,
                                double[] dstPts, int dstOff, int numPts); final void transform(double[] srcPts, int srcOff,
                                float[] dstPts, int dstOff, int numPts); final void transform(final WritableRectIter iterator); String toWKT();  }