ArrayMapy、SparseArray源码学习

ArrayMap和SparseArray是Android中提供用来替代HashMap实现内存优化的集合类,来具体看下其源码实现:
一、ArrayMap简单分析
1)存储原理:
HashMap是使用一个table数组来存储;发生冲突时采用链地址法以链表或者红黑树的形式进行存储;
而ArrayMap使用的是两个数组,mHash数组用来存储hash值(顺序存储);mArray在对应位置存储(比如mHash存储位置为index),偶数位(index<<1)存储key;奇数位(index<<+1)存储value;
发生冲突时,mHash把相同的hash组放在一起存储(因为mHash是按照hash值大小顺序来存储的);

public final class ArrayMap<K, V> implements Map<K, V> {
    int[] mHashes;
    Object[] mArray;
    int mSize;
    public ArrayMap(int capacity) {
        if (capacity == 0) {
            mHashes = EmptyArray.INT;
            mArray = EmptyArray.OBJECT;
        } else {
            allocArrays(capacity);
        }
        mSize = 0;
    }
}

2)添加元素put操作:

public V put(K key, V value) {
    final int hash;
    int index;
    if (key == null) {
        hash = 0;
        index = indexOfNull();
    } else {
        // 根据hash值查找key对应的位置
        hash = key.hashCode();
        index = indexOf(key, hash);
    }
    // 大于0表示该元素已经存在,这里直接覆盖即可
    if (index >= 0) {
        index = (index<<1) + 1;
        final V old = (V)mArray[index];
        mArray[index] = value;
        return old;
    }

    // <0表示不存在,取反得到插入位置
    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[] ohashes = mHashes;
        final Object[] oarray = mArray;
        // 分配数组空间
        allocArrays(n);

        // 将原始数组拷贝到新分配的数组中
        if (mHashes.length > 0) {
            if (DEBUG) Log.d(TAG, "put: copy 0-" + mSize + " to 0");
            System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
            System.arraycopy(oarray, 0, mArray, 0, oarray.length);
        }

        // 释放原来的空间
        freeArrays(ohashes, oarray, mSize);
    }

    // 简单的插入操作
    if (index < mSize) {
        System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
        System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1);
    }

    // 对应位置进行赋值
    mHashes[index] = hash;
    mArray[index<<1] = key;
    mArray[(index<<1)+1] = value;
    mSize++;
    return null;
}

查找插入位置:
HashMap是根据key的hashcode,然后将其高低十六位相与hash&(hash>>>16),进而与table数组的长度length相与(hash&(length-1))来找到对应的table数组插入位置;
而ArrayMap是通过ContainerHelpers的binarySearch即二分查找的方法,根据key的hash值来查找mHash数组(顺序数组),找到对应的index;

// 二分法查找对应位置
int indexOf(Object key, int hash) {
    final int N = mSize;

    // Important fast case: if nothing is in here, nothing to look for.
    if (N == 0) {
        return ~0;
    }

    // 二分查找,查找结果
    int index = ContainerHelpers.binarySearch(mHashes, N, hash);

    // If the hash code wasn't found, then we have no entry for this key.
    // <0表示没有查找到
    if (index < 0) {
        return index;
    }

    // 解决冲突,双向进行查找一次相同的hash值,比较key值是否匹配
    // If the key at the returned index matches, that's what we want.
    if (key.equals(mArray[index<<1])) {
        return index;
    }

    // Search for a matching key after the index.
    int end;
    for (end = index + 1; end < N && mHashes[end] == hash; end++) {
        if (key.equals(mArray[end << 1])) return end;
    }

    // Search for a matching key before the index.
    for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) {
        if (key.equals(mArray[i << 1])) return i;
    }

    // Key not found -- return negative value indicating where a
    // new entry for this key should go.  We use the end of the
    // hash chain to reduce the number of array entries that will
    // need to be copied when inserting.
    return ~end;
}

简单的二分查找:

// 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
}

冲突解决:
HashMap是采用遍历链表来实现;而ArrayMap的mHash表中可能存在多个相同的hash值,即冲突;它通过二分查找,找到相应的一个相等值,然后以这个值为中心,分别向前向后遍历,比较对应的key值是否相等;

继续看前面的put过程:
未找到情况:当没有找到对应的hash值,或者相同的hash,但没有找到相等的key的情况,这个时候,查找结果index表示待插入的位置,未找到返回index的取反(~index);便于插入操作判断是进行插入还是重写;

覆盖操作:
如果index>=0表示当期key值已存在,则需要进行覆盖,将mArray[(index<<1) + 1]即value赋值为新的value值,然后返回old value值;
如果index<0,表示需要执行插入操作,~index即为新元素要插入的位置;

扩充容量:
再插入之前,首先判断当前存储空间是否足够,HashMap的扩容时机是总空间超过容量*状态因子时,而ArrayMap是当mHash的大小超过MAX_SIZE,即进行扩容,扩容的算法为:
n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
: (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE);
BASE_SIZE=4;即容量大于8的时候,扩容1.5倍;容量大于4小于8时,直接分配空间为8;否则分配空间为4;
扩充容量的操作和ArrayList差不多,都是操作数组进行复制。注意这里mHash和mArray都要复制;

插入操作:
插入操作也是简单的数组已知index进行插入问题,简单的arraycopy;

3)get操作较为简单,通过mHash数组获取存储位置,<0表示没有找到,否则返回value值即可;

@Override
public V get(Object key) {
    final int index = indexOfKey(key);
    return index >= 0 ? (V)mArray[(index<<1)+1] : null;
}

4)remove操作:

@Override
public V remove(Object key) {
    // 查找对应的index进行remove
    final int index = indexOfKey(key);
    if (index >= 0) {
        return removeAt(index);
    }

    return null;
}

/**
 * Remove the key/value mapping at the given index.
 * @param index The desired index, must be between 0 and {@link #size()}-1.
 * @return Returns the value that was stored at this index.
 */
public V removeAt(int index) {
    final Object old = mArray[(index << 1) + 1];
    if (mSize <= 1) {
        // Now empty.
        // 删除之后数组中元素就为空了,所以进行空间释放
        freeArrays(mHashes, mArray, mSize);
        mHashes = EmptyArray.INT;
        mArray = EmptyArray.OBJECT;
        mSize = 0;
    } else {
        // 如果空余空间过多,将要收缩空间
        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[] ohashes = mHashes;
            final Object[] oarray = mArray;
            allocArrays(n);

            // 简单的数组元素删除操作
            mSize--;
            if (index > 0) {
                if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0");
                System.arraycopy(ohashes, 0, mHashes, 0, index);
                System.arraycopy(oarray, 0, mArray, 0, index << 1);
            }
            if (index < mSize) {
                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) << 1, mArray, index << 1,
                        (mSize - index) << 1);
            }
        } else {
            mSize--;
            if (index < mSize) {
                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) << 1, mArray, index << 1,
                        (mSize - index) << 1);
            }
            mArray[mSize << 1] = null;
            mArray[(mSize << 1) + 1] = null;
        }
    }
    return (V)old;
}

ArrayMap的remove操作,除了将元素从数组指定位置删除之外;当数组的空余空间过多时,还需要对空间进行紧缩。
紧缩的时机是mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3;
即mHash数组的容量大于8,并且当前存储的元素不足总空间的三分之一,则进行紧缩,来节省内存使用。

二、SparseArray简单分析:
SparseArray是用来替代HashpMap

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

    // 找到对应元素
    if (i >= 0) {
        mValues[i] = value;
    } else {
        i = ~i;

        // 这里是进行复用之前已经删过的元素
        if (i < mSize && mValues[i] == DELETED) {
            mKeys[i] = key;
            mValues[i] = value;
            return;
        }

        // 如果数组空间不够继续存储时,对原数组进行GC
        if (mGarbage && mSize >= mKeys.length) {
            gc();

            // Search again because indices may have changed.
            i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
        }

        // 执行插入操作
        mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
        mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
        mSize++;
    }
}

可以看到和ArrayMap基本相同,也是通过二分查找找到对应的存储位置,然后执行插入,只不过这里多个一个删除元素复用的情况;来看一下删除的逻辑:

public void remove(int key) {
    delete(key);
}
public void delete(int key) {
    int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

    if (i >= 0) {
        if (mValues[i] != DELETED) {
            mValues[i] = DELETED;
            mGarbage = true;
        }
    }
}


/**
 * Removes the mapping at the specified index.
 */
public void removeAt(int index) {
    if (mValues[index] != DELETED) {
        mValues[index] = DELETED;
        mGarbage = true;
    }
}

可以看到删除逻辑中只是将要删除的元素的value值设置成了一个DELETE的空对象,并未进行移位删除操作;它的移位删除操作统一放在了put里面数组空间不够用时进行的gc函数中:

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);
}

简单的数组紧缩操作,从前往后扫描,将非DELETE元素通过交换紧缩到前面空间中来;

因此综上来看,ArrayMap和SparseMap都是通过数组来实现的,一个数组存储hash值(key值),用以通过二分查找查找key对应的存储位置;相对于HashMap的查询效率O(1)而言,ArrayMap和 SparseMap的查询效率O(lgn)并不出色,而且涉及到大量的数组移位操作;但是ArrayMap和 SparseMap最大的好处是节省空间,而且提供了良好的及时进行空间紧缩,特别适合Android这些内存空间比较紧张的开发环境。而SparseMap是对应HashMap特殊情况的优化,在key为Integer时,避免了装箱操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值