深入SparseArray

SparseArray简介(来源于文件头注释,android-23)

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

SparseArrays将整数映射到对象。与普通的对象数组不同,索引中可能存在空白。它的目的是比使用HashMap将整数映射到对象的方式更节省内存,这是因为它避免了在键上自动装箱,而且它的数据结构不依赖于每个映射额外的Entry条目对象

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

请注意,此容器将其映射保存在数组数据结构中,使用二分查找查找键。此实现不适合可能包含大量项的数据结构。它通常比传统的HashMap慢,因为查找需要二叉搜索,添加和删除需要在数组中插入和删除条目。对于容纳数百个条目的容器,性能差异不显著,小于50%。

To help with performance, the container includes an optimization when removing keys: instead of compacting its array immediately, it leaves the removed entry marked as deleted. The entry can then be re-used for the same key, or compacted later in a single garbage collection step of all removed entries. This garbage collection will need to be performed at any time the array needs to be grown or the the map size or entry values are retrieved.

为了提高性能,容器在删除键时进行了优化:不立即压缩数组,而是将删除的条目标记为deleted。然后可以对相同的键重用该条目,或者稍后在所有已删除条目单独垃圾收集步骤中压缩该条目。在任何需要增长数组检索映射大小或条目值的时候,都需要执行此垃圾收集。

It is possible to iterate over the items in this container using {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using keyAt(int) with ascending values of the index will return the keys in ascending order, or the values corresponding to the keys in ascending order in the case of valueAt(int).

可以使用{@link #keyAt(int)}和{@link #valueAt(int)}迭代此容器中的项。使用具有索引升序值的keyAt(int)对键进行迭代,将按升序返回键,或者在valueAt(int)的情况下,按升序返回键对应的值

特性

  1. 提供的putget操作仿Map,内部数据结构采用两个数组实现,一个存储键(key,且类型为int,限制了存储的key类型),一个存储值(value)
  2. 初始容量为10
  3. key所在位置索引(index)通过二分查找确定,不适合大量数据存储
  4. 采用原型模式,继承Cloneable,实现克隆逻辑

操作介绍

1. 查询操作get

    public E get(int key) {
        return get(key, null);
    }

    public E get(int key, E valueIfKeyNotFound) {
        // 根据key,在键数组中执行二分查找
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
        
        // 二分查找索引小于零表示未查找到,mValues数组条目内容为DELETED表示数据已移除,这两种都返回默认值
        if (i < 0 || mValues[i] == DELETED) {
            return valueIfKeyNotFound;
        } else {
            return (E) mValues[i];
        }
    }

1.1 二分查找

    static int binarySearch(int[] array, int size, int value) {
        int lo = 0; // 哨兵low
        int hi = size - 1; // 哨兵high

        while (lo <= hi) {
            // 计算二分索引,无符号右移1位,相当于除以2 
            final int mid = (lo + hi) >>> 1;
            final int midVal = array[mid];// while条件下,不管上述移位如何运算,mid下标肯定不会越界

            if (midVal < value) {
                lo = mid + 1; // low从mid+1开始
            } else if (midVal > value) {
                hi = mid - 1; // high从mid-1开始
            } else {
                return mid;  // value found
            }
        }

        // 注意:未查找到,返回负值
        return ~lo;  // value not present
    }

2. 存入操作put

    public void put(int key, E value) {
        // 二分查找key所在索引位置
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            // 在值数组mValues中索引i位置存入value
            mValues[i] = value;
        } else {
            i = ~i; // 按位非

            // 复用被移除对象在mValues的槽位
            if (i < mSize && mValues[i] == DELETED) {
                mKeys[i] = key;
                mValues[i] = value;
                return;
            }

            if (mGarbage && mSize >= mKeys.length) {
                gc();

                // Search again because indices may have changed.
                i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
            }
            
            // 索引位置无法查找到,说明key并不在现有mKeys数组内,需要扩容
            mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
            mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
            mSize++;
        }
    }

3. 移除操作delete

    public void remove(int key) {
        delete(key);
    }

    /**
     * Removes the mapping from the specified key, if there was any.
     */
    public void delete(int key) {
        // 二分查找查询数组的索引
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            if (mValues[i] != DELETED) {
                // 先标记成DELETED,并不立即缩减数组
                mValues[i] = DELETED;
                mGarbage = true;
            }
        }
    }

3.1 指定索引移除操作

    public void removeAt(int index) {
        if (mValues[index] != DELETED) {
            mValues[index] = DELETED;
            mGarbage = true;
        }
    }

3.2 指定范围移除操作

    public void removeAtRange(int index, int size) {
        final int end = Math.min(mSize, index + size);
        for (int i = index; i < end; i++) {
            removeAt(i);
        }
    }

4. append操作

    public void append(int key, E value) {
        // 容量判断
        if (mSize != 0 && key <= mKeys[mSize - 1]) {
            put(key, value);
            return;
        }

        // 移除过数据需要gc
        if (mGarbage && mSize >= mKeys.length) {
            gc();
        }

        // 数组扩容
        mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
        mValues = GrowingArrayUtils.append(mValues, mSize, value);
        mSize++;
    }

5. gc操作及时机

    private void gc() {
        // Log.e("SparseArray", "gc start with " + mSize);

        int n = mSize; // 当前大小
        int o = 0;// 哨兵oldIndex
        int[] keys = mKeys;
        Object[] values = mValues;

        // 遍历value数组,发现GC对象,调整key和value数组
        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;
                }
                
                // 无需gc的场景, o和i保持一致,持续自增;一旦出现DELETED值,在整个遍历的环节,o会落后于i
                o++;
            }
        }

        mGarbage = false;
        mSize = o;

        // Log.e("SparseArray", "gc end with " + mSize);
    }

gc时机

前提有数据被移除,value成为DELETED。gc时机,原则上遵循头文件的描述,具体如下:

  1. 加入数据(put或append)
  2. 根据索引访问key或value
  3. 根据key或value获取索引(key是通过二分查找index,value是通过遍历比对value获取index)
  4. 指定索引位置设置value
  5. 访问当前大小

你可能感兴趣的:(深入SparseArray)