Little sunflower mother's class begins: "SparseArrays source code analysis"

Let's take a look at the official introduction of SparseArrays

/**
 * 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.
 *
 * <p>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%.</p>
 *
 * <p>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.</p>
 *
 * <p>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>.</p>
 */
/**
 * SparseArrays Map integers to Objects. Different from the usual Objects array, there is an interval between index es.
 * The purpose is to improve memory efficiency. Compared with using HashMap to map integers to Objects, it is because SparseArrays
 * You can avoid automatically boxing Keys and its data structure independent of each mapped extra entry.
 * Note that its container uses array structure to map, and uses binary form to index keys.
 * This is not suitable for a large number of items. It is slower than the traditional HashMap, because the binary search is needed for finding, adding and deleting
 * Insert and delete operations. The performance difference of containers used to store hundreds of item s is not significant, less than 50%.
 * To improve performance, the container contains a strategy that when keys are deleted, instead of compressing the data immediately, entries are left behind
 * And marked as delete. This entry will be reused by the same keys, or the garbage collection signal will be used when all elements are deleted
 * Compression. The garbage collector will execute when the array needs to grow or the map size and entry value are modified.
 * You can use keyAt(int) and valueAt(int) for iterations.
 * Using the keyAt index will iterate over the keys in the form of growth, and the returned keys will be sorted in ascending order.
 * valueAt The value of will return the corresponding value in the form of increasing keys.
 */
public class SparseArray<E> implements Cloneable {
    // When value is deleted, value = DELETED 
    private static final Object DELETED = new Object();
    // true, indicating that garbage collection is needed, and garbage collection will be triggered when inserting and deleting
    private boolean mGarbage = false;

    // Used to store key
    private int[] mKeys;
    // Used to store value
    private Object[] mValues;
    // Number of storage element data
    private int mSize;

    /**
     * Creates a new SparseArray containing no mappings.
     * Create a new SparseArray without mapping
     */
    public SparseArray() {
        // Default capacity is 10
        this(10);
    }

    /**
     * Creates a new SparseArray containing no mappings that will not
     * require any additional memory allocation to store the specified
     * number of mappings.  If you supply an initial capacity of 0, the
     * sparse array will be initialized with a light-weight representation
     * not requiring any additional array allocations.
     * Creating a new SparseArray does not contain any mapping relationships, and does not open up memory to store a specified number of mappings.
     * If you want to set the capacity to 0 during initialization, SparseArray will be initialized with a lightweight representation.
     *  EmptyArray.INT = new int[0];
     *  EmptyArray.OBJECT = new object[0];
     */
    public SparseArray(int initialCapacity) {
        if (initialCapacity == 0) {
            mKeys = EmptyArray.INT;
            mValues = EmptyArray.OBJECT;
        } else {
            /**
             * ArrayUtils contains some methods that you can call to find out
             * the most efficient increments by which to grow arrays.
             */
            mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity);
            mKeys = new int[mValues.length];
        }
        //Initialize saved capacity to 0
        mSize = 0;
    }

    @Override
    @SuppressWarnings("unchecked")
    public SparseArray<E> clone() {
        SparseArray<E> clone = null;
        try {
            clone = (SparseArray<E>) super.clone();
            clone.mKeys = mKeys.clone();
            clone.mValues = mValues.clone();
        } catch (CloneNotSupportedException cnse) {
            /* ignore */
        }
        return clone;
    }

    /**
     * Gets the Object mapped from the specified key, or <code>null</code>
     * if no such mapping has been made.
     */
    public E get(int key) {
        return get(key, null);
    }

    /**
     * Gets the Object mapped from the specified key, or the specified Object
     * if no such mapping has been made.
     * Or you must specify the value value of the key mapping. If you cannot find it, return valueIfKeyNotFound
     */
    @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];
        }
    }

    /**
     * Removes the mapping from the specified key, if there was any.
     */
    public void delete(int key) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
        // If you can find the corresponding value through key
        if (i >= 0) {
            // If mValues[i] is not deleted
            if (mValues[i] != DELETED) {
                // Indicates that the element is in the state to be deleted, and represents mGarbage true, indicating that garbage collection is required
                mValues[i] = DELETED;
                mGarbage = true;
            }
        }
    }

    /**
     * @hide
     * Removes the mapping from the specified key, if there was any, returning the old value.
     * Delete the mapping element by specifying the key. If the element can be found through the key, the original element will be returned
     */
    public E removeReturnOld(int key) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            if (mValues[i] != DELETED) {
                final E old = (E) mValues[i];
                mValues[i] = DELETED;
                mGarbage = true;
                return old;
            }
        }
        return null;
    }

    /**
     * Alias for {@link #delete(int)}.
     */
    public void remove(int key) {
        delete(key);
    }

    /**
     * Removes the mapping at the specified index.
     *
     * <p>For indices outside of the range <code>0...size()-1</code>,
     * the behavior is undefined.</p>
     * Delete the value element of the specified index
     */
    public void removeAt(int index) {
        if (mValues[index] != DELETED) {
            mValues[index] = DELETED;
            mGarbage = true;
        }
    }

    /**
     * Remove a range of mappings as a batch.
     *
     * @param index Index to begin at
     * @param size Number of mappings to remove
     *
     * <p>For indices outside of the range <code>0...size()-1</code>,
     * the behavior is undefined.</p>
     * Delete the mapping relationship in batch. index is the start subscript and size is the quantity to be deleted
     */
    public void removeAtRange(int index, int size) {
        final int end = Math.min(mSize, index + size);
        for (int i = index; i < end; i++) {
            removeAt(i);
        }
    }

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

        int n = mSize;
        // Number of existing, non DELETED elements
        int o = 0;
        int[] keys = mKeys;
        Object[] values = mValues;

        for (int i = 0; i < n; i++) {
            // Traverse value to see if it is deleted
            Object val = values[i];

            if (val != DELETED) {
                if (i != o) {
                    // If the angle mark is different, it means the position needs to be moved
                    keys[o] = keys[i];
                    values[o] = val;
                    values[i] = null;
                }

                o++;
            }
        }

        mGarbage = false;
        mSize = o;

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

    /**
     * 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);

        if (i >= 0) {
            // When you can find the index of the corresponding value through the key, replace it directly
            mValues[i] = value;
        } else {
            i = ~i;
            // I < 0 means no key found
            // When I < Msize, i.e. key needs to be inserted forward, i.e. it needs to be inserted into the existing queue, and the state to be inserted is DELETED
            // Directly replace
            if (i < mSize && mValues[i] == DELETED) {
                mKeys[i] = key;
                mValues[i] = value;
                return;
            }
            // When garbage collection is required and the capacity is greater than or equal to the length of the key array, it means that the capacity is full, then gc
            if (mGarbage && mSize >= mKeys.length) {
                gc();

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

            //Inserts an element in the specified index, after which the element moves backward,
            //And self growth operation, when the capacity is insufficient to expand. double when array elements are greater than 8. Otherwise expand to 8
            mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
            mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
            mSize++;
        }
    }

    /**
     * Returns the number of key-value mappings that this SparseArray
     * currently stores.
     */
    public int size() {
        if (mGarbage) {
            gc();
        }

        return mSize;
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, returns
     * the key from the <code>index</code>th key-value mapping that this
     * SparseArray stores.
     *
     * <p>The keys corresponding to indices in ascending order are guaranteed to
     * be in ascending order, e.g., <code>keyAt(0)</code> will return the
     * smallest key and <code>keyAt(size()-1)</code> will return the largest
     * key.</p>
     *
     * <p>For indices outside of the range <code>0...size()-1</code>,
     * the behavior is undefined.</p>
     */
    public int keyAt(int index) {
        if (mGarbage) {
            gc();
        }

        return mKeys[index];
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, returns
     * the value from the <code>index</code>th key-value mapping that this
     * SparseArray stores.
     *
     * <p>The values corresponding to indices in ascending order are guaranteed
     * to be associated with keys in ascending order, e.g.,
     * <code>valueAt(0)</code> will return the value associated with the
     * smallest key and <code>valueAt(size()-1)</code> will return the value
     * associated with the largest key.</p>
     *
     * <p>For indices outside of the range <code>0...size()-1</code>,
     * the behavior is undefined.</p>
     */
    @SuppressWarnings("unchecked")
    public E valueAt(int index) {
        if (mGarbage) {
            gc();
        }

        return (E) mValues[index];
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, sets a new
     * value for the <code>index</code>th key-value mapping that this
     * SparseArray stores.
     *
     * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined.</p>
     */
    public void setValueAt(int index, E value) {
        if (mGarbage) {
            gc();
        }

        mValues[index] = value;
    }

    /**
     * Returns the index for which {@link #keyAt} would return the
     * specified key, or a negative number if the specified
     * key is not mapped.
     * Returns the index corresponding to the specified key, or a negative number if it cannot be found
     */
    public int indexOfKey(int key) {
        if (mGarbage) {
            gc();
        }

        return ContainerHelpers.binarySearch(mKeys, mSize, key);
    }

    /**
     * Returns an index for which {@link #valueAt} would return the
     * specified key, or a negative number if no keys map to the
     * specified value.
     * <p>Beware that this is a linear search, unlike lookups by key,
     * and that multiple keys can map to the same value and this will
     * find only one of them.
     * <p>Note also that unlike most collections' {@code indexOf} methods,
     * this method compares values using {@code ==} rather than {@code equals}.
     * Return value to find index, or - 1 if not found
     */
    public int indexOfValue(E value) {
        if (mGarbage) {
            gc();
        }

        for (int i = 0; i < mSize; i++) {
            if (mValues[i] == value) {
                return i;
            }
        }

        return -1;
    }

    /**
     * Returns an index for which {@link #valueAt} would return the
     * specified key, or a negative number if no keys map to the
     * specified value.
     * <p>Beware that this is a linear search, unlike lookups by key,
     * and that multiple keys can map to the same value and this will
     * find only one of them.
     * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}.
     * @hide
     * Find the corresponding index through value. If value is null, how to return the first null index. If not, and if not
     * null Position, return to - 1
     */
    public int indexOfValueByValue(E value) {
        if (mGarbage) {
            gc();
        }

        for (int i = 0; i < mSize; i++) {
            if (value == null) {
                if (mValues[i] == null) {
                    return i;
                }
            } else {
                if (value.equals(mValues[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * Removes all key-value mappings from this SparseArray.
     * Clear all valid values, set to null
     */
    public void clear() {
        int n = mSize;
        Object[] values = mValues;

        for (int i = 0; i < n; i++) {
            values[i] = null;
        }

        mSize = 0;
        mGarbage = false;
    }

    /**
     * Puts a key/value pair into the array, optimizing for the case where
     * the key is greater than all existing keys in the array.
     */
    public void append(int key, E value) {
        if (mSize != 0 && key <= mKeys[mSize - 1]) {
            put(key, value);
            return;
        }

        if (mGarbage && mSize >= mKeys.length) {
            gc();
        }
        // Added at the end of the array. If there is not enough space, double needs to be expanded adaptively
        mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
        mValues = GrowingArrayUtils.append(mValues, mSize, value);
        mSize++;
    }

    /**
     * {@inheritDoc}
     *
     * <p>This implementation composes a string by iterating over its mappings. If
     * this map contains itself as a value, the string "(this Map)"
     * will appear in its place.
     */
    @Override
    public String toString() {
        if (size() <= 0) {
            return "{}";
        }

        StringBuilder buffer = new StringBuilder(mSize * 28);
        buffer.append('{');
        for (int i=0; i<mSize; i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            int key = keyAt(i);
            buffer.append(key);
            buffer.append('=');
            Object value = valueAt(i);
            if (value != this) {
                buffer.append(value);
            } else {
                buffer.append("(this Map)");
            }
        }
        buffer.append('}');
        return buffer.toString();
    }
}

Here are some search algorithms and extension methods designed in this class:

/**
 * Half search.
 */
static int binarySearch(int[] array, int size, int value) {
    int lo = 0;
    //hi is the size stored in the current array, not the length of the array
    int hi = size - 1;

    while (lo <= hi) {
        //Find the middle index by moving the sign 1 bit to the right
        final int mid = (lo + hi) >>> 1;
        //Find the middle value
        final int midVal = array[mid];

        if (midVal < value) {
            //If the value of midval is less than value, the upper half will be searched in half.
            lo = mid + 1;
        } else if (midVal > value) {
            //If the value of midval is greater than value, the lower half will be searched in half.
            hi = mid - 1;
        } else {
            return mid;  // value found
        }
    }

    return ~lo;  // value not present
}

So there's a question. Half search requires ordered array contents. How can SparseArrays determine array order? , we need to see how put works.
When put is called for the first time:

int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
  1. When size=0, key=1, rerun - 1
    index=~(-1), mKeys[0] = 1
  2. When size=1, key=2, rerun - 2
    index=~(-2), mKeys[0] = 1,mKeys[1] = 2
  3. When size=2, key=3, rerun - 3
    index=~(-3), mKeys[0] = 1,mKeys[1] = 2,mKeys[2] = 3
  4. When size=3, key=0, rerun - 1
    index=~(-1)=0,mSize = 3,
    mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
    Insert through GrowingArrayUtils.insert, and the result is:
    mKeys[0] = 0,mKeys[1] = 1,mKeys[2] =2,mKeys[2] =3

so, mKeys are ordered.
You can search this version through ContainerHelpers.binarySearch, find the index corresponding to the key, and quickly find the value through the index.

Make a performance comparison later.
To be continued

Keywords: less

Added by ElkySS on Tue, 31 Mar 2020 00:29:36 +0300