SparseArray 稀疏数组
一、几个重要的成员变量
private static final Object DELETED = new Object();/**用于标记已被删除value**/
private boolean mGarbage = false; /**是否存在垃圾**/
private int[] mKeys; /***键keys整型数组**/
private Object[] mValues; /***值values Object对象数组**/
private int mSize; /***keys-values大小组**/
二、构建函数 默认创建 大小为initialCapacity =10数组
public SparseArray(int initialCapacity) {
if (initialCapacity == 0) {
mKeys = EmptyArray.INT;
mValues = EmptyArray.OBJECT;
} else {
mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity);
mKeys = new int[mValues.length];
}
mSize = 0;
}
public static Object[] newUnpaddedObjectArray(int minLen) {
return (Object[])VMRuntime.getRuntime().newUnpaddedArray(Object.class, minLen);
}
三、增
/** 增加一对key-value
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(int key, E value) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); //二分查找方式找到key对应位置i
if (i >= 0) {
mValues[i] = value;//1、之前位置i有应的value则直接重新赋值
} else {
i = ~i;
if (i < mSize && mValues[i] == DELETED) {//2、位置i小于mSize,有应的value为已删除,则重新赋值
mKeys[i] = key;
mValues[i] = value;
return;
}
if (mGarbage && mSize >= mKeys.length) {//3、当前的mSize大于=mKeys的,重新GC
gc();
// Search again because indices may have changed.
i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
}
mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);/**扩展mKeys的大小**/
mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);/**扩展mValues的大小**/
mSize++;
}
}
二分查找:在键数组中mKeys中查找键key,mKeys数组大小为mSize
ContainerHelpers.binarySearch(mKeys, mSize, key); //二分查找方式找到key对应位置i
// This is Arrays.binarySearch(), but doesn't do any argument validation.
static int binarySearch(int[] array, int size, int value) {
int lo = 0;
int hi = size - 1;
while (lo <= hi) {
final int mid = (lo + hi) >>> 1;
final int midVal = array[mid];
if (midVal < value) {
lo = mid + 1;
} else if (midVal > value) {
hi = mid - 1;
} else {
return mid; // value found
}
}
return ~lo; // value not present
}
四、查
@SuppressWarnings("unchecked")
public E get(int key, E valueIfKeyNotFound) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}
五、回收内存释放空间
/***回收内存***/
private void gc() {
// Log.e("SparseArray", "gc start with " + mSize);
int n = mSize;
int o = 0;
int[] keys = mKeys;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
Object val = values[i];
if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
values[i] = null;
}
o++;
}
}
mGarbage = false;
mSize = o;
// Log.e("SparseArray", "gc end with " + mSize);
}