/** Convert a find index return to the insert location in the array */ static int apply(int idx) { if ( idx >= 0 ) return idx ; return decodeIndex(idx) ; }
public static <T extends Comparable<? super T>> int binarySearch(T[] array, int low, int high, T value) { check(array.length, low, high) ; high -- ; while( low <= high ) { int mid = ( low + high ) >>> 1 ; T k = array[mid] ; int x = k.compareTo(value) ; // comparator.compare(k, value) ; if ( x < 0 ) low = mid + 1 ; else if ( x > 0 ) high = mid - 1 ; else return mid ; } return encodeIndex(low) ; }
public static int binarySearch(IntBuffer buff, int value) { return binarySearch(buff, 0, buff.limit(), value) ; }
int find1(byte[] data) { for ( int i = 0 ; i < numSlot ; i++ ) { int x = compare(i, data) ; if ( x == 0 ) return i ; if ( x > 0 ) return encodeIndex(i) ; } return encodeIndex(numSlot) ; }
private static void search(IntBuffer b, int k) { int idx1 = Alg.linearSearch(b, k) ; int idx2 = Alg.binarySearch(b, k) ; assertEquals(idx1, idx2) ; }
static int linearSearch(IntBuffer buff, int key) { return linearSearch(buff, 0, buff.limit(), key) ; }
int find1(byte[] data) { for ( int i = 0 ; i < numSlot ; i++ ) { int x = compare(i, data) ; if ( x == 0 ) return i ; if ( x > 0 ) return encodeIndex(i) ; } return encodeIndex(numSlot) ; }
private static void search(IntBuffer b, int k, int low, int high) { int idx1 = Alg.linearSearch(b, low, high, k) ; int idx2 = Alg.binarySearch(b, low, high, k) ; assertEquals(idx1, idx2) ; } }
static int linearSearch(IntBuffer buff, int key) { return linearSearch(buff, 0, buff.limit(), key) ; }
static int linearSearch(IntBuffer buff, int low, int high, int key) { int len = buff.limit(); // In int units. check(len, low, high) ; for ( int i = low ; i < high ; i++ ) { int k2 = buff.get(i) ; if ( k2 == key ) return i ; if ( k2 > key ) return encodeIndex(i) ; } return encodeIndex(high) ; }
/** Convert a find index return to the insert location in the array */ static int apply(int idx) { if ( idx >= 0 ) return idx ; return decodeIndex(idx) ; }
int find1(byte[] data) { for ( int i = 0 ; i < numSlot ; i++ ) { int x = compare(i, data) ; if ( x == 0 ) return i ; if ( x > 0 ) return encodeIndex(i) ; } return encodeIndex(numSlot) ; }
@Test public void binary7() { int[] data = {2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8} ; IntBuffer b = make(data) ; int idx = 0 ; idx = Alg.binarySearch(b, 4) ; assertEquals(4, data[idx]) ; idx = Alg.linearSearch(b, 8) ; assertEquals(8, data[idx]) ; search(b, 3) ; search(b, 5) ; search(b, 9) ; }
static int linearSearch(IntBuffer buff, int key) { return linearSearch(buff, 0, buff.limit(), key) ; }
public static int binarySearch(IntBuffer buff, int value) { return binarySearch(buff, 0, buff.limit(), value) ; }
public static <T extends Comparable<? super T>> int binarySearch(T[] array, int low, int high, T value) { check(array.length, low, high) ; high -- ; while( low <= high ) { int mid = ( low + high ) >>> 1 ; T k = array[mid] ; int x = k.compareTo(value) ; // comparator.compare(k, value) ; if ( x < 0 ) low = mid + 1 ; else if ( x > 0 ) high = mid - 1 ; else return mid ; } return encodeIndex(low) ; }
private static int convert(int idx) { if ( idx >= 0 ) return idx ; return decodeIndex(idx) ; }
/** Search for key in range fromIndex (inclusive) to toIndex (exclusive) */ public int find(Record rec, int fromIndex, int toIndex) { int low = fromIndex ; int high = toIndex - 1 ; byte[] key = rec.getKey() ; // http://en.wikipedia.org/wiki/Binary_search while (low <= high) { int mid = (low + high) >>> 1 ; // int divide by 2 int x = compare(mid, key) ; // System.out.printf("Compare: %d(%s) %s ==> %d\n", mid, // Record.str(get(mid)), Record.str(data), x) ; if ( x < 0 ) low = mid + 1 ; else if ( x > 0 ) high = mid - 1 ; else return mid ; } // On exit, when not finding, low is the least value // above, including off the end of the array. return encodeIndex(low) ; }
@Test public void linear4() { int[] data = {9} ; IntBuffer b = make(data) ; int idx ; idx = Alg.linearSearch(b, 1) ; assertEquals(-1, idx) ; idx = Alg.linearSearch(b, 9) ; assertEquals(0, idx) ; idx = Alg.linearSearch(b, 100) ; assertEquals(-2, idx) ; }
public static int binarySearch(IntBuffer buff, int value) { return binarySearch(buff, 0, buff.limit(), value) ; }