1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util; 18 19 @android.ravenwood.annotation.RavenwoodKeepWholeClass 20 class ContainerHelpers { 21 22 // This is Arrays.binarySearch(), but doesn't do any argument validation. binarySearch(int[] array, int size, int value)23 static int binarySearch(int[] array, int size, int value) { 24 int lo = 0; 25 int hi = size - 1; 26 27 while (lo <= hi) { 28 final int mid = (lo + hi) >>> 1; 29 final int midVal = array[mid]; 30 31 if (midVal < value) { 32 lo = mid + 1; 33 } else if (midVal > value) { 34 hi = mid - 1; 35 } else { 36 return mid; // value found 37 } 38 } 39 return ~lo; // value not present 40 } 41 binarySearch(long[] array, int size, long value)42 static int binarySearch(long[] array, int size, long value) { 43 int lo = 0; 44 int hi = size - 1; 45 46 while (lo <= hi) { 47 final int mid = (lo + hi) >>> 1; 48 final long midVal = array[mid]; 49 50 if (midVal < value) { 51 lo = mid + 1; 52 } else if (midVal > value) { 53 hi = mid - 1; 54 } else { 55 return mid; // value found 56 } 57 } 58 return ~lo; // value not present 59 } 60 } 61