Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2006 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 com.android.internal.util;
     18 
     19 import java.lang.reflect.Array;
     20 
     21 // XXX these should be changed to reflect the actual memory allocator we use.
     22 // it looks like right now objects want to be powers of 2 minus 8
     23 // and the array size eats another 4 bytes
     24 
     25 /**
     26  * ArrayUtils contains some methods that you can call to find out
     27  * the most efficient increments by which to grow arrays.
     28  */
     29 public class ArrayUtils
     30 {
     31     private static Object[] EMPTY = new Object[0];
     32     private static final int CACHE_SIZE = 73;
     33     private static Object[] sCache = new Object[CACHE_SIZE];
     34 
     35     private ArrayUtils() { /* cannot be instantiated */ }
     36 
     37     public static int idealByteArraySize(int need) {
     38         for (int i = 4; i < 32; i++)
     39             if (need <= (1 << i) - 12)
     40                 return (1 << i) - 12;
     41 
     42         return need;
     43     }
     44 
     45     public static int idealBooleanArraySize(int need) {
     46         return idealByteArraySize(need);
     47     }
     48 
     49     public static int idealShortArraySize(int need) {
     50         return idealByteArraySize(need * 2) / 2;
     51     }
     52 
     53     public static int idealCharArraySize(int need) {
     54         return idealByteArraySize(need * 2) / 2;
     55     }
     56 
     57     public static int idealIntArraySize(int need) {
     58         return idealByteArraySize(need * 4) / 4;
     59     }
     60 
     61     public static int idealFloatArraySize(int need) {
     62         return idealByteArraySize(need * 4) / 4;
     63     }
     64 
     65     public static int idealObjectArraySize(int need) {
     66         return idealByteArraySize(need * 4) / 4;
     67     }
     68 
     69     public static int idealLongArraySize(int need) {
     70         return idealByteArraySize(need * 8) / 8;
     71     }
     72 
     73     /**
     74      * Checks if the beginnings of two byte arrays are equal.
     75      *
     76      * @param array1 the first byte array
     77      * @param array2 the second byte array
     78      * @param length the number of bytes to check
     79      * @return true if they're equal, false otherwise
     80      */
     81     public static boolean equals(byte[] array1, byte[] array2, int length) {
     82         if (array1 == array2) {
     83             return true;
     84         }
     85         if (array1 == null || array2 == null || array1.length < length || array2.length < length) {
     86             return false;
     87         }
     88         for (int i = 0; i < length; i++) {
     89             if (array1[i] != array2[i]) {
     90                 return false;
     91             }
     92         }
     93         return true;
     94     }
     95 
     96     /**
     97      * Returns an empty array of the specified type.  The intent is that
     98      * it will return the same empty array every time to avoid reallocation,
     99      * although this is not guaranteed.
    100      */
    101     public static <T> T[] emptyArray(Class<T> kind) {
    102         if (kind == Object.class) {
    103             return (T[]) EMPTY;
    104         }
    105 
    106         int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    107         Object cache = sCache[bucket];
    108 
    109         if (cache == null || cache.getClass().getComponentType() != kind) {
    110             cache = Array.newInstance(kind, 0);
    111             sCache[bucket] = cache;
    112 
    113             // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    114         }
    115 
    116         return (T[]) cache;
    117     }
    118 
    119     /**
    120      * Checks that value is present as at least one of the elements of the array.
    121      * @param array the array to check in
    122      * @param value the value to check for
    123      * @return true if the value is present in the array
    124      */
    125     public static <T> boolean contains(T[] array, T value) {
    126         for (T element : array) {
    127             if (element == null) {
    128                 if (value == null) return true;
    129             } else {
    130                 if (value != null && element.equals(value)) return true;
    131             }
    132         }
    133         return false;
    134     }
    135 
    136     public static boolean contains(int[] array, int value) {
    137         for (int element : array) {
    138             if (element == value) {
    139                 return true;
    140             }
    141         }
    142         return false;
    143     }
    144 
    145     public static long total(long[] array) {
    146         long total = 0;
    147         for (long value : array) {
    148             total += value;
    149         }
    150         return total;
    151     }
    152 
    153     /**
    154      * Appends an element to a copy of the array and returns the copy.
    155      * @param array The original array, or null to represent an empty array.
    156      * @param element The element to add.
    157      * @return A new array that contains all of the elements of the original array
    158      * with the specified element added at the end.
    159      */
    160     @SuppressWarnings("unchecked")
    161     public static <T> T[] appendElement(Class<T> kind, T[] array, T element) {
    162         final T[] result;
    163         final int end;
    164         if (array != null) {
    165             end = array.length;
    166             result = (T[])Array.newInstance(kind, end + 1);
    167             System.arraycopy(array, 0, result, 0, end);
    168         } else {
    169             end = 0;
    170             result = (T[])Array.newInstance(kind, 1);
    171         }
    172         result[end] = element;
    173         return result;
    174     }
    175 
    176     /**
    177      * Removes an element from a copy of the array and returns the copy.
    178      * If the element is not present, then the original array is returned unmodified.
    179      * @param array The original array, or null to represent an empty array.
    180      * @param element The element to remove.
    181      * @return A new array that contains all of the elements of the original array
    182      * except the first copy of the specified element removed.  If the specified element
    183      * was not present, then returns the original array.  Returns null if the result
    184      * would be an empty array.
    185      */
    186     @SuppressWarnings("unchecked")
    187     public static <T> T[] removeElement(Class<T> kind, T[] array, T element) {
    188         if (array != null) {
    189             final int length = array.length;
    190             for (int i = 0; i < length; i++) {
    191                 if (array[i] == element) {
    192                     if (length == 1) {
    193                         return null;
    194                     }
    195                     T[] result = (T[])Array.newInstance(kind, length - 1);
    196                     System.arraycopy(array, 0, result, 0, i);
    197                     System.arraycopy(array, i + 1, result, i, length - i - 1);
    198                     return result;
    199                 }
    200             }
    201         }
    202         return array;
    203     }
    204 
    205     public static int[] appendInt(int[] cur, int val) {
    206         if (cur == null) {
    207             return new int[] { val };
    208         }
    209         final int N = cur.length;
    210         for (int i = 0; i < N; i++) {
    211             if (cur[i] == val) {
    212                 return cur;
    213             }
    214         }
    215         int[] ret = new int[N + 1];
    216         System.arraycopy(cur, 0, ret, 0, N);
    217         ret[N] = val;
    218         return ret;
    219     }
    220 
    221     public static int[] removeInt(int[] cur, int val) {
    222         if (cur == null) {
    223             return null;
    224         }
    225         final int N = cur.length;
    226         for (int i = 0; i < N; i++) {
    227             if (cur[i] == val) {
    228                 int[] ret = new int[N - 1];
    229                 if (i > 0) {
    230                     System.arraycopy(cur, 0, ret, 0, i);
    231                 }
    232                 if (i < (N - 1)) {
    233                     System.arraycopy(cur, i + 1, ret, i, N - i - 1);
    234                 }
    235                 return ret;
    236             }
    237         }
    238         return cur;
    239     }
    240 }
    241