深入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 <code>keyAt(int)</code> 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 <code>valueAt(int)</code>.

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

特性

  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. 访问当前大小
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容