public class

SparseArrayCompat<E>

extends java.lang.Object

implements java.lang.Cloneable

 java.lang.Object

↳androidx.collection.SparseArrayCompat<E>

Overview

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.

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%.

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.

It is possible to iterate over the items in this container using SparseArrayCompat.keyAt(int) and SparseArrayCompat.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).

Summary

Constructors
publicSparseArrayCompat()

Creates a new SparseArray containing no mappings.

publicSparseArrayCompat(int initialCapacity)

Creates a new SparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings.

Methods
public voidappend(int key, java.lang.Object value)

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 voidclear()

Removes all key-value mappings from this SparseArray.

public SparseArrayCompat<java.lang.Object>clone()

public booleancontainsKey(int key)

Returns true if the specified key is mapped.

public booleancontainsValue(java.lang.Object value)

Returns true if the specified value is mapped from any key.

public voiddelete(int key)

public java.lang.Objectget(int key)

Gets the Object mapped from the specified key, or null if no such mapping has been made.

public java.lang.Objectget(int key, java.lang.Object valueIfKeyNotFound)

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

public intindexOfKey(int key)

Returns the index for which SparseArrayCompat.keyAt(int) would return the specified key, or a negative number if the specified key is not mapped.

public intindexOfValue(java.lang.Object value)

Returns an index for which SparseArrayCompat.valueAt(int) would return the specified key, or a negative number if no keys map to the specified value.

public booleanisEmpty()

Return true if size() is 0.

public intkeyAt(int index)

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this SparseArray stores.

public voidput(int key, java.lang.Object 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 voidputAll(SparseArrayCompat<java.lang.Object> other)

Copies all of the mappings from the other to this map.

public java.lang.ObjectputIfAbsent(int key, java.lang.Object value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

public voidremove(int key)

Removes the mapping from the specified key, if there was any.

public booleanremove(int key, java.lang.Object value)

Remove an existing key from the array map only if it is currently mapped to value.

public voidremoveAt(int index)

Removes the mapping at the specified index.

public voidremoveAtRange(int index, int size)

Remove a range of mappings as a batch.

public java.lang.Objectreplace(int key, java.lang.Object value)

Replace the mapping for key only if it is already mapped to a value.

public booleanreplace(int key, java.lang.Object oldValue, java.lang.Object newValue)

Replace the mapping for key only if it is already mapped to a value.

public voidsetValueAt(int index, java.lang.Object value)

Given an index in the range 0...size()-1, sets a new value for the indexth key-value mapping that this SparseArray stores.

public intsize()

Returns the number of key-value mappings that this SparseArray currently stores.

public java.lang.StringtoString()

public java.lang.ObjectvalueAt(int index)

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this SparseArray stores.

from java.lang.Objectequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public SparseArrayCompat()

Creates a new SparseArray containing no mappings.

public SparseArrayCompat(int initialCapacity)

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.

Methods

public SparseArrayCompat<java.lang.Object> clone()

public java.lang.Object get(int key)

Gets the Object mapped from the specified key, or null if no such mapping has been made.

public java.lang.Object get(int key, java.lang.Object valueIfKeyNotFound)

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

public void delete(int key)

Deprecated: Alias for SparseArrayCompat.remove(int).

public void remove(int key)

Removes the mapping from the specified key, if there was any.

public boolean remove(int key, java.lang.Object value)

Remove an existing key from the array map only if it is currently mapped to value.

Parameters:

key: The key of the mapping to remove.
value: The value expected to be mapped to the key.

Returns:

Returns true if the mapping was removed.

public void removeAt(int index)

Removes the mapping at the specified index.

public void removeAtRange(int index, int size)

Remove a range of mappings as a batch.

Parameters:

index: Index to begin at
size: Number of mappings to remove

public java.lang.Object replace(int key, java.lang.Object value)

Replace the mapping for key only if it is already mapped to a value.

Parameters:

key: The key of the mapping to replace.
value: The value to store for the given key.

Returns:

Returns the previous mapped value or null.

public boolean replace(int key, java.lang.Object oldValue, java.lang.Object newValue)

Replace the mapping for key only if it is already mapped to a value.

Parameters:

key: The key of the mapping to replace.
oldValue: The value expected to be mapped to the key.
newValue: The value to store for the given key.

Returns:

Returns true if the value was replaced.

public void put(int key, java.lang.Object 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 putAll(SparseArrayCompat<java.lang.Object> other)

Copies all of the mappings from the other to this map. The effect of this call is equivalent to that of calling SparseArrayCompat.put(int, E) on this map once for each mapping from key to value in other.

public java.lang.Object putIfAbsent(int key, java.lang.Object value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

Parameters:

key: The key under which to store the value.
value: The value to store for the given key.

Returns:

Returns the value that was stored for the given key, or null if there was no such key.

public int size()

Returns the number of key-value mappings that this SparseArray currently stores.

public boolean isEmpty()

Return true if size() is 0.

Returns:

true if size() is 0.

public int keyAt(int index)

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this SparseArray stores.

public java.lang.Object valueAt(int index)

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this SparseArray stores.

public void setValueAt(int index, java.lang.Object value)

Given an index in the range 0...size()-1, sets a new value for the indexth key-value mapping that this SparseArray stores.

public int indexOfKey(int key)

Returns the index for which SparseArrayCompat.keyAt(int) would return the specified key, or a negative number if the specified key is not mapped.

public int indexOfValue(java.lang.Object value)

Returns an index for which SparseArrayCompat.valueAt(int) would return the specified key, or a negative number if no keys map to the specified value.

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.

Note also that unlike most collections' indexOf methods, this method compares values using == rather than equals.

public boolean containsKey(int key)

Returns true if the specified key is mapped.

public boolean containsValue(java.lang.Object value)

Returns true if the specified value is mapped from any key.

public void clear()

Removes all key-value mappings from this SparseArray.

public void append(int key, java.lang.Object value)

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 java.lang.String toString()

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.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.collection;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 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>
 */
public class SparseArrayCompat<E> implements Cloneable {
    private static final Object DELETED = new Object();
    private boolean mGarbage = false;

    private int[] mKeys;
    private Object[] mValues;
    private int mSize;

    /**
     * Creates a new SparseArray containing no mappings.
     */
    public SparseArrayCompat() {
        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.
     */
    public SparseArrayCompat(int initialCapacity) {
        if (initialCapacity == 0) {
            mKeys =  ContainerHelpers.EMPTY_INTS;
            mValues =  ContainerHelpers.EMPTY_OBJECTS;
        } else {
            initialCapacity =  ContainerHelpers.idealIntArraySize(initialCapacity);
            mKeys = new int[initialCapacity];
            mValues = new Object[initialCapacity];
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public SparseArrayCompat<E> clone() {
        SparseArrayCompat<E> clone;
        try {
            clone = (SparseArrayCompat<E>) super.clone();
            clone.mKeys = mKeys.clone();
            clone.mValues = mValues.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(e); // Cannot happen as we implement Cloneable.
        }
        return clone;
    }

    /**
     * Gets the Object mapped from the specified key, or <code>null</code>
     * if no such mapping has been made.
     */
    @Nullable
    @SuppressWarnings("NullAway") // See inline comment.
    public E get(int key) {
        // We pass null as the default to a function which isn't explicitly annotated as nullable.
        // Not marking the function as nullable should allow us to eventually propagate the generic
        // parameter's nullability to the caller. If we were to mark it as nullable now, we would
        // also be forced to mark the return type of that method as nullable which harms the case
        // where you are passing in a non-null default value.
        return get(key, null);
    }

    /**
     * Gets the Object mapped from the specified key, or the specified Object
     * if no such mapping has been made.
     */
    @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];
        }
    }

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

    /**
     * Removes the mapping from the specified key, if there was any.
     */
    public void remove(int key) {
        int i =  ContainerHelpers.binarySearch(mKeys, mSize, key);

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

    /**
     * Remove an existing key from the array map only if it is currently mapped to {@code value}.
     * @param key The key of the mapping to remove.
     * @param value The value expected to be mapped to the key.
     * @return Returns true if the mapping was removed.
     */
    public boolean remove(int key, Object value) {
        int index = indexOfKey(key);
        if (index >= 0) {
            E mapValue = valueAt(index);
            if (value == mapValue || (value != null && value.equals(mapValue))) {
                removeAt(index);
                return true;
            }
        }
        return false;
    }

    /**
     * Removes the mapping at 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
     */
    public void removeAtRange(int index, int size) {
        final int end = Math.min(mSize, index + size);
        for (int i = index; i < end; i++) {
            removeAt(i);
        }
    }

    /**
     * Replace the mapping for {@code key} only if it is already mapped to a value.
     * @param key The key of the mapping to replace.
     * @param value The value to store for the given key.
     * @return Returns the previous mapped value or null.
     */
    @Nullable
    @SuppressWarnings("unchecked")
    public E replace(int key, E value) {
        int index = indexOfKey(key);
        if (index >= 0) {
            E oldValue = (E) mValues[index];
            mValues[index] = value;
            return oldValue;
        }
        return null;
    }

    /**
     * Replace the mapping for {@code key} only if it is already mapped to a value.
     *
     * @param key The key of the mapping to replace.
     * @param oldValue The value expected to be mapped to the key.
     * @param newValue The value to store for the given key.
     * @return Returns true if the value was replaced.
     */
    public boolean replace(int key, E oldValue, E newValue) {
        int index = indexOfKey(key);
        if (index >= 0) {
            Object mapValue = mValues[index];
            if (mapValue == oldValue || (oldValue != null && oldValue.equals(mapValue))) {
                mValues[index] = newValue;
                return true;
            }
        }
        return false;
    }

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

    /**
     * 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) {
            mValues[i] = value;
        } else {
            i = ~i;

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

            if (mSize >= mKeys.length) {
                int n =  ContainerHelpers.idealIntArraySize(mSize + 1);

                int[] nkeys = new int[n];
                Object[] nvalues = new Object[n];

                // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
                System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
                System.arraycopy(mValues, 0, nvalues, 0, mValues.length);

                mKeys = nkeys;
                mValues = nvalues;
            }

            if (mSize - i != 0) {
                // Log.e("SparseArray", "move " + (mSize - i));
                System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
                System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
            }

            mKeys[i] = key;
            mValues[i] = value;
            mSize++;
        }
    }

    /**
     * Copies all of the mappings from the {@code other} to this map. The effect of this call is
     * equivalent to that of calling {@link #put(int, Object)} on this map once for each mapping
     * from key to value in {@code other}.
     */
    public void putAll(@NonNull SparseArrayCompat<? extends E> other) {
        for (int i = 0, size = other.size(); i < size; i++) {
            put(other.keyAt(i), other.valueAt(i));
        }
    }

    /**
     * Add a new value to the array map only if the key does not already have a value or it is
     * mapped to {@code null}.
     * @param key The key under which to store the value.
     * @param value The value to store for the given key.
     * @return Returns the value that was stored for the given key, or null if there
     * was no such key.
     */
    @Nullable
    public E putIfAbsent(int key, E value) {
        E mapValue = get(key);
        if (mapValue == null) {
            put(key, value);
        }
        return mapValue;
    }

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

        return mSize;
    }

    /**
     * Return true if size() is 0.
     * @return true if size() is 0.
     */
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * 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.
     */
    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.
     */
    @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.
     */
    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.
     */
    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}.
     */
    public int indexOfValue(E value) {
        if (mGarbage) {
            gc();
        }

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

        return -1;
    }

    /** Returns true if the specified key is mapped. */
    public boolean containsKey(int key) {
        return indexOfKey(key) >= 0;
    }

    /** Returns true if the specified value is mapped from any key. */
    public boolean containsValue(E value) {
        return indexOfValue(value) >= 0;
    }

    /**
     * Removes all key-value mappings from this SparseArray.
     */
    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();
        }

        int pos = mSize;
        if (pos >= mKeys.length) {
            int n =  ContainerHelpers.idealIntArraySize(pos + 1);

            int[] nkeys = new int[n];
            Object[] nvalues = new Object[n];

            // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
            System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
            System.arraycopy(mValues, 0, nvalues, 0, mValues.length);

            mKeys = nkeys;
            mValues = nvalues;
        }

        mKeys[pos] = key;
        mValues[pos] = value;
        mSize = pos + 1;
    }

    /**
     * {@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();
    }
}