Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 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 | class ContainerHelpers { |
| 20 | static final boolean[] EMPTY_BOOLEANS = new boolean[0]; |
| 21 | static final int[] EMPTY_INTS = new int[0]; |
| 22 | static final long[] EMPTY_LONGS = new long[0]; |
| 23 | static final Object[] EMPTY_OBJECTS = new Object[0]; |
| 24 | |
| 25 | // This is Arrays.binarySearch(), but doesn't do any argument validation. |
| 26 | static int binarySearch(int[] array, int size, int value) { |
| 27 | int lo = 0; |
| 28 | int hi = size - 1; |
| 29 | |
| 30 | while (lo <= hi) { |
| 31 | final int mid = (lo + hi) >>> 1; |
| 32 | final int midVal = array[mid]; |
| 33 | |
| 34 | if (midVal < value) { |
| 35 | lo = mid + 1; |
| 36 | } else if (midVal > value) { |
| 37 | hi = mid - 1; |
| 38 | } else { |
| 39 | return mid; // value found |
| 40 | } |
| 41 | } |
| 42 | return ~lo; // value not present |
| 43 | } |
| 44 | |
| 45 | static int binarySearch(long[] array, int size, long value) { |
| 46 | int lo = 0; |
| 47 | int hi = size - 1; |
| 48 | |
| 49 | while (lo <= hi) { |
| 50 | final int mid = (lo + hi) >>> 1; |
| 51 | final long midVal = array[mid]; |
| 52 | |
| 53 | if (midVal < value) { |
| 54 | lo = mid + 1; |
| 55 | } else if (midVal > value) { |
| 56 | hi = mid - 1; |
| 57 | } else { |
| 58 | return mid; // value found |
| 59 | } |
| 60 | } |
| 61 | return ~lo; // value not present |
| 62 | } |
| 63 | } |