diff options
| author | Adam Lesinski <adamlesinski@google.com> | 2014-03-07 11:30:59 -0500 |
|---|---|---|
| committer | Adam Lesinski <adamlesinski@google.com> | 2014-03-27 11:42:10 -0700 |
| commit | 776abc24cdd18610232a50b997cce3cffa74609b (patch) | |
| tree | 9ec6c84c00b7e8e653b9bc1c91e612cdf2afb32d /core/java/android/util/LongSparseArray.java | |
| parent | 350159c65a52092db04f1b8efce6943f61e50e73 (diff) | |
Uses VMRuntime.newUnpaddedArray for ideal array sizes
Bug:13028925
Change-Id: I0a9301248b10a339afbdc5e4ffe3310ac4fa1fb7
Diffstat (limited to 'core/java/android/util/LongSparseArray.java')
| -rw-r--r-- | core/java/android/util/LongSparseArray.java | 57 |
1 files changed, 12 insertions, 45 deletions
diff --git a/core/java/android/util/LongSparseArray.java b/core/java/android/util/LongSparseArray.java index dab853a7fe27..6b45ff484e67 100644 --- a/core/java/android/util/LongSparseArray.java +++ b/core/java/android/util/LongSparseArray.java @@ -17,6 +17,9 @@ package android.util; import com.android.internal.util.ArrayUtils; +import com.android.internal.util.GrowingArrayUtils; + +import libcore.util.EmptyArray; /** * SparseArray mapping longs to Objects. Unlike a normal array of Objects, @@ -70,12 +73,11 @@ public class LongSparseArray<E> implements Cloneable { */ public LongSparseArray(int initialCapacity) { if (initialCapacity == 0) { - mKeys = ContainerHelpers.EMPTY_LONGS; - mValues = ContainerHelpers.EMPTY_OBJECTS; + mKeys = EmptyArray.LONG; + mValues = EmptyArray.OBJECT; } else { - initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity); - mKeys = new long[initialCapacity]; - mValues = new Object[initialCapacity]; + mKeys = ArrayUtils.newUnpaddedLongArray(initialCapacity); + mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity); } mSize = 0; } @@ -202,28 +204,8 @@ public class LongSparseArray<E> implements Cloneable { i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); } - if (mSize >= mKeys.length) { - int n = ArrayUtils.idealLongArraySize(mSize + 1); - - long[] nkeys = new long[n]; - Object[] nvalues = new Object[n]; - - // Log.e("SparseArray", "grow " + mKeys.length + " to " + n); - System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); - System.arraycopy(mValues, 0, nvalues, 0, mValues.length); - - mKeys = nkeys; - mValues = nvalues; - } - - if (mSize - i != 0) { - // Log.e("SparseArray", "move " + (mSize - i)); - System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); - System.arraycopy(mValues, i, mValues, i + 1, mSize - i); - } - - mKeys[i] = key; - mValues[i] = value; + mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); + mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); mSize++; } } @@ -353,24 +335,9 @@ public class LongSparseArray<E> implements Cloneable { gc(); } - int pos = mSize; - if (pos >= mKeys.length) { - int n = ArrayUtils.idealLongArraySize(pos + 1); - - long[] nkeys = new long[n]; - Object[] nvalues = new Object[n]; - - // Log.e("SparseArray", "grow " + mKeys.length + " to " + n); - System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); - System.arraycopy(mValues, 0, nvalues, 0, mValues.length); - - mKeys = nkeys; - mValues = nvalues; - } - - mKeys[pos] = key; - mValues[pos] = value; - mSize = pos + 1; + mKeys = GrowingArrayUtils.append(mKeys, mSize, key); + mValues = GrowingArrayUtils.append(mValues, mSize, value); + mSize++; } /** |
