1 /* 2 * Copyright 2018 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 androidx.collection; 18 19 class ContainerHelpers { 20 static final int[] EMPTY_INTS = new int[0]; 21 static final long[] EMPTY_LONGS = new long[0]; 22 static final Object[] EMPTY_OBJECTS = new Object[0]; 23 idealIntArraySize(int need)24 public static int idealIntArraySize(int need) { 25 return idealByteArraySize(need * 4) / 4; 26 } 27 idealLongArraySize(int need)28 public static int idealLongArraySize(int need) { 29 return idealByteArraySize(need * 8) / 8; 30 } 31 idealByteArraySize(int need)32 public static int idealByteArraySize(int need) { 33 for (int i = 4; i < 32; i++) 34 if (need <= (1 << i) - 12) 35 return (1 << i) - 12; 36 37 return need; 38 } 39 equal(Object a, Object b)40 public static boolean equal(Object a, Object b) { 41 return a == b || (a != null && a.equals(b)); 42 } 43 44 // This is Arrays.binarySearch(), but doesn't do any argument validation. binarySearch(int[] array, int size, int value)45 static int binarySearch(int[] array, int size, int value) { 46 int lo = 0; 47 int hi = size - 1; 48 49 while (lo <= hi) { 50 int mid = (lo + hi) >>> 1; 51 int 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 binarySearch(long[] array, int size, long value)64 static int binarySearch(long[] array, int size, long value) { 65 int lo = 0; 66 int hi = size - 1; 67 68 while (lo <= hi) { 69 final int mid = (lo + hi) >>> 1; 70 final long midVal = array[mid]; 71 72 if (midVal < value) { 73 lo = mid + 1; 74 } else if (midVal > value) { 75 hi = mid - 1; 76 } else { 77 return mid; // value found 78 } 79 } 80 return ~lo; // value not present 81 } 82 ContainerHelpers()83 private ContainerHelpers() { 84 } 85 } 86