Merge "Clean up style issues in ArraySet."
This commit is contained in:
committed by
Android (Google) Code Review
commit
27a395c06d
@@ -64,10 +64,10 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
* The first entry in the array is a pointer to the next array in the
|
||||
* list; the second entry is a pointer to the int[] hash code array for it.
|
||||
*/
|
||||
static Object[] mBaseCache;
|
||||
static int mBaseCacheSize;
|
||||
static Object[] mTwiceBaseCache;
|
||||
static int mTwiceBaseCacheSize;
|
||||
static Object[] sBaseCache;
|
||||
static int sBaseCacheSize;
|
||||
static Object[] sTwiceBaseCache;
|
||||
static int sTwiceBaseCacheSize;
|
||||
|
||||
final boolean mIdentityHashCode;
|
||||
int[] mHashes;
|
||||
@@ -152,31 +152,35 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
}
|
||||
|
||||
private void allocArrays(final int size) {
|
||||
if (size == (BASE_SIZE*2)) {
|
||||
if (size == (BASE_SIZE * 2)) {
|
||||
synchronized (ArraySet.class) {
|
||||
if (mTwiceBaseCache != null) {
|
||||
final Object[] array = mTwiceBaseCache;
|
||||
if (sTwiceBaseCache != null) {
|
||||
final Object[] array = sTwiceBaseCache;
|
||||
mArray = array;
|
||||
mTwiceBaseCache = (Object[])array[0];
|
||||
mHashes = (int[])array[1];
|
||||
sTwiceBaseCache = (Object[]) array[0];
|
||||
mHashes = (int[]) array[1];
|
||||
array[0] = array[1] = null;
|
||||
mTwiceBaseCacheSize--;
|
||||
if (DEBUG) Log.d(TAG, "Retrieving 2x cache " + mHashes
|
||||
+ " now have " + mTwiceBaseCacheSize + " entries");
|
||||
sTwiceBaseCacheSize--;
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Retrieving 2x cache " + mHashes + " now have "
|
||||
+ sTwiceBaseCacheSize + " entries");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (size == BASE_SIZE) {
|
||||
synchronized (ArraySet.class) {
|
||||
if (mBaseCache != null) {
|
||||
final Object[] array = mBaseCache;
|
||||
if (sBaseCache != null) {
|
||||
final Object[] array = sBaseCache;
|
||||
mArray = array;
|
||||
mBaseCache = (Object[])array[0];
|
||||
mHashes = (int[])array[1];
|
||||
sBaseCache = (Object[]) array[0];
|
||||
mHashes = (int[]) array[1];
|
||||
array[0] = array[1] = null;
|
||||
mBaseCacheSize--;
|
||||
if (DEBUG) Log.d(TAG, "Retrieving 1x cache " + mHashes
|
||||
+ " now have " + mBaseCacheSize + " entries");
|
||||
sBaseCacheSize--;
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Retrieving 1x cache " + mHashes + " now have " + sBaseCacheSize
|
||||
+ " entries");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -187,32 +191,36 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
}
|
||||
|
||||
private static void freeArrays(final int[] hashes, final Object[] array, final int size) {
|
||||
if (hashes.length == (BASE_SIZE*2)) {
|
||||
if (hashes.length == (BASE_SIZE * 2)) {
|
||||
synchronized (ArraySet.class) {
|
||||
if (mTwiceBaseCacheSize < CACHE_SIZE) {
|
||||
array[0] = mTwiceBaseCache;
|
||||
if (sTwiceBaseCacheSize < CACHE_SIZE) {
|
||||
array[0] = sTwiceBaseCache;
|
||||
array[1] = hashes;
|
||||
for (int i=size-1; i>=2; i--) {
|
||||
for (int i = size - 1; i >= 2; i--) {
|
||||
array[i] = null;
|
||||
}
|
||||
mTwiceBaseCache = array;
|
||||
mTwiceBaseCacheSize++;
|
||||
if (DEBUG) Log.d(TAG, "Storing 2x cache " + array
|
||||
+ " now have " + mTwiceBaseCacheSize + " entries");
|
||||
sTwiceBaseCache = array;
|
||||
sTwiceBaseCacheSize++;
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Storing 2x cache " + array + " now have " + sTwiceBaseCacheSize
|
||||
+ " entries");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (hashes.length == BASE_SIZE) {
|
||||
synchronized (ArraySet.class) {
|
||||
if (mBaseCacheSize < CACHE_SIZE) {
|
||||
array[0] = mBaseCache;
|
||||
if (sBaseCacheSize < CACHE_SIZE) {
|
||||
array[0] = sBaseCache;
|
||||
array[1] = hashes;
|
||||
for (int i=size-1; i>=2; i--) {
|
||||
for (int i = size - 1; i >= 2; i--) {
|
||||
array[i] = null;
|
||||
}
|
||||
mBaseCache = array;
|
||||
mBaseCacheSize++;
|
||||
if (DEBUG) Log.d(TAG, "Storing 1x cache " + array
|
||||
+ " now have " + mBaseCacheSize + " entries");
|
||||
sBaseCache = array;
|
||||
sBaseCacheSize++;
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Storing 1x cache " + array + " now have "
|
||||
+ sBaseCacheSize + " entries");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,7 +329,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
* @return Returns the value stored at the given index.
|
||||
*/
|
||||
public E valueAt(int index) {
|
||||
return (E)mArray[index];
|
||||
return (E) mArray[index];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,8 +366,8 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
|
||||
index = ~index;
|
||||
if (mSize >= mHashes.length) {
|
||||
final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
|
||||
: (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE);
|
||||
final int n = mSize >= (BASE_SIZE * 2) ? (mSize + (mSize >> 1))
|
||||
: (mSize >= BASE_SIZE ? (BASE_SIZE * 2) : BASE_SIZE);
|
||||
|
||||
if (DEBUG) Log.d(TAG, "add: grow from " + mHashes.length + " to " + n);
|
||||
|
||||
@@ -377,8 +385,9 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
}
|
||||
|
||||
if (index < mSize) {
|
||||
if (DEBUG) Log.d(TAG, "add: move " + index + "-" + (mSize-index)
|
||||
+ " to " + (index+1));
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "add: move " + index + "-" + (mSize - index) + " to " + (index + 1));
|
||||
}
|
||||
System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
|
||||
System.arraycopy(mArray, index, mArray, index + 1, mSize - index);
|
||||
}
|
||||
@@ -432,7 +441,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
mSize = N;
|
||||
}
|
||||
} else {
|
||||
for (int i=0; i<N; i++) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
add(array.valueAt(i));
|
||||
}
|
||||
}
|
||||
@@ -469,11 +478,11 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
mArray = EmptyArray.OBJECT;
|
||||
mSize = 0;
|
||||
} else {
|
||||
if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) {
|
||||
if (mHashes.length > (BASE_SIZE * 2) && mSize < mHashes.length / 3) {
|
||||
// Shrunk enough to reduce size of arrays. We don't allow it to
|
||||
// shrink smaller than (BASE_SIZE*2) to avoid flapping between
|
||||
// that and BASE_SIZE.
|
||||
final int n = mSize > (BASE_SIZE*2) ? (mSize + (mSize>>1)) : (BASE_SIZE*2);
|
||||
final int n = mSize > (BASE_SIZE * 2) ? (mSize + (mSize >> 1)) : (BASE_SIZE * 2);
|
||||
|
||||
if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n);
|
||||
|
||||
@@ -488,23 +497,26 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
System.arraycopy(oarray, 0, mArray, 0, index);
|
||||
}
|
||||
if (index < mSize) {
|
||||
if (DEBUG) Log.d(TAG, "remove: copy from " + (index+1) + "-" + mSize
|
||||
+ " to " + index);
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "remove: copy from " + (index + 1) + "-" + mSize
|
||||
+ " to " + index);
|
||||
}
|
||||
System.arraycopy(ohashes, index + 1, mHashes, index, mSize - index);
|
||||
System.arraycopy(oarray, index + 1, mArray, index, mSize - index);
|
||||
}
|
||||
} else {
|
||||
mSize--;
|
||||
if (index < mSize) {
|
||||
if (DEBUG) Log.d(TAG, "remove: move " + (index+1) + "-" + mSize
|
||||
+ " to " + index);
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "remove: move " + (index + 1) + "-" + mSize + " to " + index);
|
||||
}
|
||||
System.arraycopy(mHashes, index + 1, mHashes, index, mSize - index);
|
||||
System.arraycopy(mArray, index + 1, mArray, index, mSize - index);
|
||||
}
|
||||
mArray[mSize] = null;
|
||||
}
|
||||
}
|
||||
return (E)old;
|
||||
return (E) old;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -545,8 +557,8 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
@Override
|
||||
public <T> T[] toArray(T[] array) {
|
||||
if (array.length < mSize) {
|
||||
@SuppressWarnings("unchecked") T[] newArray
|
||||
= (T[]) Array.newInstance(array.getClass().getComponentType(), mSize);
|
||||
@SuppressWarnings("unchecked") T[] newArray =
|
||||
(T[]) Array.newInstance(array.getClass().getComponentType(), mSize);
|
||||
array = newArray;
|
||||
}
|
||||
System.arraycopy(mArray, 0, array, 0, mSize);
|
||||
@@ -577,7 +589,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
}
|
||||
|
||||
try {
|
||||
for (int i=0; i<mSize; i++) {
|
||||
for (int i = 0; i < mSize; i++) {
|
||||
E mine = valueAt(i);
|
||||
if (!set.contains(mine)) {
|
||||
return false;
|
||||
@@ -621,7 +633,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
|
||||
StringBuilder buffer = new StringBuilder(mSize * 14);
|
||||
buffer.append('{');
|
||||
for (int i=0; i<mSize; i++) {
|
||||
for (int i = 0; i < mSize; i++) {
|
||||
if (i > 0) {
|
||||
buffer.append(", ");
|
||||
}
|
||||
@@ -759,7 +771,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
boolean removed = false;
|
||||
for (int i=mSize-1; i>=0; i--) {
|
||||
for (int i = mSize - 1; i >= 0; i--) {
|
||||
if (!collection.contains(mArray[i])) {
|
||||
removeAt(i);
|
||||
removed = true;
|
||||
|
||||
Reference in New Issue
Block a user