1021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov/*
2021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Copyright (C) 2011 The Android Open Source Project
3021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov *
4021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License");
5021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * you may not use this file except in compliance with the License.
6021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * You may obtain a copy of the License at
7021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov *
8021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov *      http://www.apache.org/licenses/LICENSE-2.0
9021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov *
10021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software
11021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS,
12021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * See the License for the specific language governing permissions and
14021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * limitations under the License.
15021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */
16021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
17021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovpackage android.util;
18021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
19021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovimport com.android.internal.util.ArrayUtils;
20776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinskiimport com.android.internal.util.GrowingArrayUtils;
21776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski
22776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinskiimport libcore.util.EmptyArray;
23021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
24021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov/**
25021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * SparseLongArrays map integers to longs.  Unlike a normal array of longs,
26b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * there can be gaps in the indices.  It is intended to be more memory efficient
27b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * than using a HashMap to map Integers to Longs, both because it avoids
28b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * auto-boxing keys and values and its data structure doesn't rely on an extra entry object
29b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * for each mapping.
30b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn *
31b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * <p>Note that this container keeps its mappings in an array data structure,
32b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * using a binary search to find keys.  The implementation is not intended to be appropriate for
33b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * data structures
34b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * that may contain large numbers of items.  It is generally slower than a traditional
35b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * HashMap, since lookups require a binary search and adds and removes require inserting
36b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * and deleting entries in the array.  For containers holding up to hundreds of items,
37b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * the performance difference is not significant, less than 50%.</p>
385771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda *
395771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <p>It is possible to iterate over the items in this container using
405771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using
415771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <code>keyAt(int)</code> with ascending values of the index will return the
425771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * keys in ascending order, or the values corresponding to the keys in ascending
43ebb47950f21d3c41955a591000dfb1f195e746feNewton Allen * order in the case of <code>valueAt(int)</code>.</p>
44021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */
45021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovpublic class SparseLongArray implements Cloneable {
46021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    private int[] mKeys;
47021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    private long[] mValues;
48021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    private int mSize;
49021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
50021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
51021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Creates a new SparseLongArray containing no mappings.
52021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
53021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public SparseLongArray() {
54021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        this(10);
55021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
56021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
57021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
58021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Creates a new SparseLongArray containing no mappings that will not
59021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * require any additional memory allocation to store the specified
60f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn     * number of mappings.  If you supply an initial capacity of 0, the
61f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn     * sparse array will be initialized with a light-weight representation
62f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn     * not requiring any additional array allocations.
63021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
64021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public SparseLongArray(int initialCapacity) {
65f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn        if (initialCapacity == 0) {
66776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mKeys = EmptyArray.INT;
67776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mValues = EmptyArray.LONG;
68f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn        } else {
69776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mValues = ArrayUtils.newUnpaddedLongArray(initialCapacity);
70776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mKeys = new int[mValues.length];
71f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn        }
72021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        mSize = 0;
73021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
74021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
75021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    @Override
76021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public SparseLongArray clone() {
77021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        SparseLongArray clone = null;
78021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        try {
79021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            clone = (SparseLongArray) super.clone();
80021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            clone.mKeys = mKeys.clone();
81021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            clone.mValues = mValues.clone();
82021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        } catch (CloneNotSupportedException cnse) {
83021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            /* ignore */
84021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        }
85021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return clone;
86021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
87021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
88021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
89021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Gets the long mapped from the specified key, or <code>0</code>
90021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * if no such mapping has been made.
91021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
92021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public long get(int key) {
93021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return get(key, 0);
94021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
95021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
96021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
97021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Gets the long mapped from the specified key, or the specified value
98021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * if no such mapping has been made.
99021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
100021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public long get(int key, long valueIfKeyNotFound) {
1013e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
102021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
103021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        if (i < 0) {
104021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            return valueIfKeyNotFound;
105021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        } else {
106021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            return mValues[i];
107021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        }
108021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
109021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
110021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
111021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Removes the mapping from the specified key, if there was any.
112021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
113021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public void delete(int key) {
1143e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
115021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
116021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        if (i >= 0) {
117021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            removeAt(i);
118021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        }
119021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
120021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
121021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
122e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * @hide
123e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * Remove a range of mappings as a batch.
124e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     *
125e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * @param index Index to begin at
126e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * @param size Number of mappings to remove
127e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     *
128e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * <p>For indices outside of the range <code>0...size()-1</code>,
129e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     * the behavior is undefined.</p>
130e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla     */
131e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla    public void removeAtRange(int index, int size) {
132e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla        size = Math.min(size, mSize - index);
133e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla        System.arraycopy(mKeys, index + size, mKeys, index, mSize - (index + size));
134e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla        System.arraycopy(mValues, index + size, mValues, index, mSize - (index + size));
135e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla        mSize -= size;
136e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla    }
137e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla
138e6e723d588bf7ebd7ec28d97c31ee44b5b4e0b54Suprabh Shukla    /**
139021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Removes the mapping at the given index.
140021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
141021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public void removeAt(int index) {
142021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1));
143021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1));
144021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        mSize--;
145021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
146021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
147021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
148021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Adds a mapping from the specified key to the specified value,
149021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * replacing the previous mapping from the specified key if there
150021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * was one.
151021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
152021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public void put(int key, long value) {
1533e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
154021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
155021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        if (i >= 0) {
156021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            mValues[i] = value;
157021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        } else {
158021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            i = ~i;
159021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
160776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
161776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski            mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
162021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            mSize++;
163021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        }
164021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
165021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
166021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
167021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Returns the number of key-value mappings that this SparseIntArray
168021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * currently stores.
169021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
170021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public int size() {
171021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return mSize;
172021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
173021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
174021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
175021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Given an index in the range <code>0...size()-1</code>, returns
176021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * the key from the <code>index</code>th key-value mapping that this
177021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * SparseLongArray stores.
1785771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     *
1795771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * <p>The keys corresponding to indices in ascending order are guaranteed to
1805771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * be in ascending order, e.g., <code>keyAt(0)</code> will return the
1815771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * smallest key and <code>keyAt(size()-1)</code> will return the largest
1825771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * key.</p>
183021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
184021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public int keyAt(int index) {
185021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return mKeys[index];
186021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
187021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
188021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
189021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Given an index in the range <code>0...size()-1</code>, returns
190021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * the value from the <code>index</code>th key-value mapping that this
191021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * SparseLongArray stores.
1925771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     *
1935771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * <p>The values corresponding to indices in ascending order are guaranteed
1945771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * to be associated with keys in ascending order, e.g.,
1955771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * <code>valueAt(0)</code> will return the value associated with the
1965771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * smallest key and <code>valueAt(size()-1)</code> will return the value
1975771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda     * associated with the largest key.</p>
198021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
199021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public long valueAt(int index) {
200021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return mValues[index];
201021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
202021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
203021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
204021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Returns the index for which {@link #keyAt} would return the
205021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * specified key, or a negative number if the specified
206021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * key is not mapped.
207021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
208021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public int indexOfKey(int key) {
2093e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        return ContainerHelpers.binarySearch(mKeys, mSize, key);
210021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
211021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
212021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
213021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Returns an index for which {@link #valueAt} would return the
214021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * specified key, or a negative number if no keys map to the
215021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * specified value.
216021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Beware that this is a linear search, unlike lookups by key,
217021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * and that multiple keys can map to the same value and this will
218021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * find only one of them.
219021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
220021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public int indexOfValue(long value) {
221021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        for (int i = 0; i < mSize; i++)
222021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            if (mValues[i] == value)
223021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov                return i;
224021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
225021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        return -1;
226021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
227021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
228021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
229021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Removes all key-value mappings from this SparseIntArray.
230021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
231021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public void clear() {
232021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        mSize = 0;
233021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
234021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
235021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    /**
236021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * Puts a key/value pair into the array, optimizing for the case where
237021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     * the key is greater than all existing keys in the array.
238021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov     */
239021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    public void append(int key, long value) {
240021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        if (mSize != 0 && key <= mKeys[mSize - 1]) {
241021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            put(key, value);
242021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov            return;
243021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov        }
244021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov
245776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski        mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
246776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski        mValues = GrowingArrayUtils.append(mValues, mSize, value);
247776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski        mSize++;
248021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov    }
2493e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn
2503e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn    /**
2513e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn     * {@inheritDoc}
2523e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn     *
2533e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn     * <p>This implementation composes a string by iterating over its mappings.
2543e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn     */
2553e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn    @Override
2563e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn    public String toString() {
2573e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        if (size() <= 0) {
2583e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            return "{}";
2593e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        }
2603e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn
2613e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        StringBuilder buffer = new StringBuilder(mSize * 28);
2623e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        buffer.append('{');
2633e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        for (int i=0; i<mSize; i++) {
2643e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            if (i > 0) {
2653e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn                buffer.append(", ");
2663e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            }
2673e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            int key = keyAt(i);
2683e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            buffer.append(key);
2693e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            buffer.append('=');
2703e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            long value = valueAt(i);
2713e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn            buffer.append(value);
2723e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        }
2733e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        buffer.append('}');
2743e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn        return buffer.toString();
2753e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn    }
276021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov}
277