diff options
Diffstat (limited to 'core/java/android/util/SparseLongArray.java')
| -rw-r--r-- | core/java/android/util/SparseLongArray.java | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/core/java/android/util/SparseLongArray.java b/core/java/android/util/SparseLongArray.java index 2f7a6fe63bb5..c608996c99bf 100644 --- a/core/java/android/util/SparseLongArray.java +++ b/core/java/android/util/SparseLongArray.java @@ -24,6 +24,7 @@ import com.android.internal.util.ArrayUtils; * than using a HashMap to map Integers to Longs. */ public class SparseLongArray implements Cloneable { + static final long[] EMPTY_LONGS = new long[0]; private int[] mKeys; private long[] mValues; @@ -39,13 +40,19 @@ public class SparseLongArray implements Cloneable { /** * Creates a new SparseLongArray containing no mappings that will not * require any additional memory allocation to store the specified - * number of mappings. + * number of mappings. If you supply an initial capacity of 0, the + * sparse array will be initialized with a light-weight representation + * not requiring any additional array allocations. */ public SparseLongArray(int initialCapacity) { - initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity); - - mKeys = new int[initialCapacity]; - mValues = new long[initialCapacity]; + if (initialCapacity == 0) { + mKeys = SparseArray.EMPTY_INTS; + mValues = EMPTY_LONGS; + } else { + initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity); + mKeys = new int[initialCapacity]; + mValues = new long[initialCapacity]; + } mSize = 0; } @@ -75,7 +82,7 @@ public class SparseLongArray implements Cloneable { * if no such mapping has been made. */ public long get(int key, long valueIfKeyNotFound) { - int i = binarySearch(mKeys, 0, mSize, key); + int i = SparseArray.binarySearch(mKeys, mSize, key); if (i < 0) { return valueIfKeyNotFound; @@ -88,7 +95,7 @@ public class SparseLongArray implements Cloneable { * Removes the mapping from the specified key, if there was any. */ public void delete(int key) { - int i = binarySearch(mKeys, 0, mSize, key); + int i = SparseArray.binarySearch(mKeys, mSize, key); if (i >= 0) { removeAt(i); @@ -110,7 +117,7 @@ public class SparseLongArray implements Cloneable { * was one. */ public void put(int key, long value) { - int i = binarySearch(mKeys, 0, mSize, key); + int i = SparseArray.binarySearch(mKeys, mSize, key); if (i >= 0) { mValues[i] = value; @@ -164,7 +171,7 @@ public class SparseLongArray implements Cloneable { * key is not mapped. */ public int indexOfKey(int key) { - return binarySearch(mKeys, 0, mSize, key); + return SparseArray.binarySearch(mKeys, mSize, key); } /** @@ -222,24 +229,4 @@ public class SparseLongArray implements Cloneable { mKeys = nkeys; mValues = nvalues; } - - private static int binarySearch(int[] a, int start, int len, long key) { - int high = start + len, low = start - 1, guess; - - while (high - low > 1) { - guess = (high + low) / 2; - - if (a[guess] < key) - low = guess; - else - high = guess; - } - - if (high == start + len) - return ~(start + len); - else if (a[high] == key) - return high; - else - return ~high; - } } |
