The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.util; |
| 18 | |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.internal.util.ArrayUtils; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 22 | import com.android.internal.util.GrowingArrayUtils; |
| 23 | |
| 24 | import libcore.util.EmptyArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 27 | * <code>SparseArray</code> maps integers to Objects and, unlike a normal array of Objects, |
| 28 | * its indices can contain gaps. <code>SparseArray</code> is intended to be more memory-efficient |
| 29 | * than a |
| 30 | * <a href="/reference/java/util/HashMap"><code>HashMap</code></a>, because it avoids |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 31 | * auto-boxing keys and its data structure doesn't rely on an extra entry object |
| 32 | * for each mapping. |
| 33 | * |
| 34 | * <p>Note that this container keeps its mappings in an array data structure, |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 35 | * using a binary search to find keys. The implementation is not intended to be appropriate for |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 36 | * data structures |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 37 | * that may contain large numbers of items. It is generally slower than a |
| 38 | * <code>HashMap</code> because lookups require a binary search, |
| 39 | * and adds and removes require inserting |
| 40 | * and deleting entries in the array. For containers holding up to hundreds of items, |
| 41 | * the performance difference is less than 50%. |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 42 | * |
| 43 | * <p>To help with performance, the container includes an optimization when removing |
| 44 | * keys: instead of compacting its array immediately, it leaves the removed entry marked |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 45 | * as deleted. The entry can then be re-used for the same key or compacted later in |
| 46 | * a single garbage collection of all removed entries. This garbage collection |
| 47 | * must be performed whenever the array needs to be grown, or when the map size or |
| 48 | * entry values are retrieved. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 49 | * |
| 50 | * <p>It is possible to iterate over the items in this container using |
| 51 | * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 52 | * <code>keyAt(int)</code> with ascending values of the index returns the |
| 53 | * keys in ascending order. In the case of <code>valueAt(int)</code>, the |
| 54 | * values corresponding to the keys are returned in ascending order. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | */ |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 56 | public class SparseArray<E> implements Cloneable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | private static final Object DELETED = new Object(); |
| 58 | private boolean mGarbage = false; |
| 59 | |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 60 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use keyAt(int) |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 61 | private int[] mKeys; |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 62 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use valueAt(int), setValueAt(int, E) |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 63 | private Object[] mValues; |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 64 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use size() |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 65 | private int mSize; |
| 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Creates a new SparseArray containing no mappings. |
| 69 | */ |
| 70 | public SparseArray() { |
| 71 | this(10); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates a new SparseArray containing no mappings that will not |
| 76 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 77 | * number of mappings. If you supply an initial capacity of 0, the |
| 78 | * sparse array will be initialized with a light-weight representation |
| 79 | * not requiring any additional array allocations. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
| 81 | public SparseArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 82 | if (initialCapacity == 0) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 83 | mKeys = EmptyArray.INT; |
| 84 | mValues = EmptyArray.OBJECT; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 85 | } else { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 86 | mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity); |
| 87 | mKeys = new int[mValues.length]; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 88 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | mSize = 0; |
| 90 | } |
| 91 | |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 92 | @Override |
| 93 | @SuppressWarnings("unchecked") |
| 94 | public SparseArray<E> clone() { |
| 95 | SparseArray<E> clone = null; |
| 96 | try { |
| 97 | clone = (SparseArray<E>) super.clone(); |
| 98 | clone.mKeys = mKeys.clone(); |
| 99 | clone.mValues = mValues.clone(); |
| 100 | } catch (CloneNotSupportedException cnse) { |
| 101 | /* ignore */ |
| 102 | } |
| 103 | return clone; |
| 104 | } |
| 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Gets the Object mapped from the specified key, or <code>null</code> |
| 108 | * if no such mapping has been made. |
| 109 | */ |
| 110 | public E get(int key) { |
| 111 | return get(key, null); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Gets the Object mapped from the specified key, or the specified Object |
| 116 | * if no such mapping has been made. |
| 117 | */ |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 118 | @SuppressWarnings("unchecked") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | public E get(int key, E valueIfKeyNotFound) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 120 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
| 122 | if (i < 0 || mValues[i] == DELETED) { |
| 123 | return valueIfKeyNotFound; |
| 124 | } else { |
| 125 | return (E) mValues[i]; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Removes the mapping from the specified key, if there was any. |
| 131 | */ |
| 132 | public void delete(int key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 133 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
| 135 | if (i >= 0) { |
| 136 | if (mValues[i] != DELETED) { |
| 137 | mValues[i] = DELETED; |
| 138 | mGarbage = true; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 144 | * @hide |
| 145 | * Removes the mapping from the specified key, if there was any, returning the old value. |
| 146 | */ |
| 147 | public E removeReturnOld(int key) { |
| 148 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 149 | |
| 150 | if (i >= 0) { |
| 151 | if (mValues[i] != DELETED) { |
| 152 | final E old = (E) mValues[i]; |
| 153 | mValues[i] = DELETED; |
| 154 | mGarbage = true; |
| 155 | return old; |
| 156 | } |
| 157 | } |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | * Alias for {@link #delete(int)}. |
| 163 | */ |
| 164 | public void remove(int key) { |
| 165 | delete(key); |
| 166 | } |
| 167 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Removes the mapping at the specified index. |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 170 | * |
| 171 | * <p>For indices outside of the range <code>0...size()-1</code>, |
| 172 | * the behavior is undefined.</p> |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 173 | */ |
| 174 | public void removeAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 175 | if (index >= mSize) { |
| 176 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 177 | throw new ArrayIndexOutOfBoundsException(index); |
| 178 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 179 | if (mValues[index] != DELETED) { |
| 180 | mValues[index] = DELETED; |
| 181 | mGarbage = true; |
| 182 | } |
| 183 | } |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 184 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Remove a range of mappings as a batch. |
| 187 | * |
| 188 | * @param index Index to begin at |
| 189 | * @param size Number of mappings to remove |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 190 | * |
| 191 | * <p>For indices outside of the range <code>0...size()-1</code>, |
| 192 | * the behavior is undefined.</p> |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 193 | */ |
| 194 | public void removeAtRange(int index, int size) { |
| 195 | final int end = Math.min(mSize, index + size); |
| 196 | for (int i = index; i < end; i++) { |
| 197 | removeAt(i); |
| 198 | } |
| 199 | } |
| 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | private void gc() { |
| 202 | // Log.e("SparseArray", "gc start with " + mSize); |
| 203 | |
| 204 | int n = mSize; |
| 205 | int o = 0; |
| 206 | int[] keys = mKeys; |
| 207 | Object[] values = mValues; |
| 208 | |
| 209 | for (int i = 0; i < n; i++) { |
| 210 | Object val = values[i]; |
| 211 | |
| 212 | if (val != DELETED) { |
| 213 | if (i != o) { |
| 214 | keys[o] = keys[i]; |
| 215 | values[o] = val; |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 216 | values[i] = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | o++; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | mGarbage = false; |
| 224 | mSize = o; |
| 225 | |
| 226 | // Log.e("SparseArray", "gc end with " + mSize); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Adds a mapping from the specified key to the specified value, |
| 231 | * replacing the previous mapping from the specified key if there |
| 232 | * was one. |
| 233 | */ |
| 234 | public void put(int key, E value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 235 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | |
| 237 | if (i >= 0) { |
| 238 | mValues[i] = value; |
| 239 | } else { |
| 240 | i = ~i; |
| 241 | |
| 242 | if (i < mSize && mValues[i] == DELETED) { |
| 243 | mKeys[i] = key; |
| 244 | mValues[i] = value; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (mGarbage && mSize >= mKeys.length) { |
| 249 | gc(); |
| 250 | |
| 251 | // Search again because indices may have changed. |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 252 | i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 255 | mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); |
| 256 | mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | mSize++; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the number of key-value mappings that this SparseArray |
| 263 | * currently stores. |
| 264 | */ |
| 265 | public int size() { |
| 266 | if (mGarbage) { |
| 267 | gc(); |
| 268 | } |
| 269 | |
| 270 | return mSize; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Given an index in the range <code>0...size()-1</code>, returns |
| 275 | * the key from the <code>index</code>th key-value mapping that this |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 276 | * SparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 277 | * |
| 278 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 279 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 280 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 281 | * key.</p> |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 282 | * |
| 283 | * <p>For indices outside of the range <code>0...size()-1</code>, |
| 284 | * the behavior is undefined.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | */ |
| 286 | public int keyAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 287 | if (index >= mSize) { |
| 288 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 289 | throw new ArrayIndexOutOfBoundsException(index); |
| 290 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | if (mGarbage) { |
| 292 | gc(); |
| 293 | } |
| 294 | |
| 295 | return mKeys[index]; |
| 296 | } |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | /** |
| 299 | * Given an index in the range <code>0...size()-1</code>, returns |
| 300 | * the value from the <code>index</code>th key-value mapping that this |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 301 | * SparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 302 | * |
| 303 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 304 | * to be associated with keys in ascending order, e.g., |
| 305 | * <code>valueAt(0)</code> will return the value associated with the |
| 306 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 307 | * associated with the largest key.</p> |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 308 | * |
| 309 | * <p>For indices outside of the range <code>0...size()-1</code>, |
| 310 | * the behavior is undefined.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | */ |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 312 | @SuppressWarnings("unchecked") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | public E valueAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 314 | if (index >= mSize) { |
| 315 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 316 | throw new ArrayIndexOutOfBoundsException(index); |
| 317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | if (mGarbage) { |
| 319 | gc(); |
| 320 | } |
| 321 | |
| 322 | return (E) mValues[index]; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Given an index in the range <code>0...size()-1</code>, sets a new |
| 327 | * value for the <code>index</code>th key-value mapping that this |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 328 | * SparseArray stores. |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 329 | * |
| 330 | * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | */ |
| 332 | public void setValueAt(int index, E value) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 333 | if (index >= mSize) { |
| 334 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 335 | throw new ArrayIndexOutOfBoundsException(index); |
| 336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | if (mGarbage) { |
| 338 | gc(); |
| 339 | } |
| 340 | |
| 341 | mValues[index] = value; |
| 342 | } |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | /** |
| 345 | * Returns the index for which {@link #keyAt} would return the |
| 346 | * specified key, or a negative number if the specified |
| 347 | * key is not mapped. |
| 348 | */ |
| 349 | public int indexOfKey(int key) { |
| 350 | if (mGarbage) { |
| 351 | gc(); |
| 352 | } |
| 353 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 354 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Returns an index for which {@link #valueAt} would return the |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 359 | * specified value, or a negative number if no keys map to the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | * specified value. |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 361 | * <p>Beware that this is a linear search, unlike lookups by key, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | * and that multiple keys can map to the same value and this will |
| 363 | * find only one of them. |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 364 | * <p>Note also that unlike most collections' {@code indexOf} methods, |
| 365 | * this method compares values using {@code ==} rather than {@code equals}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | */ |
| 367 | public int indexOfValue(E value) { |
| 368 | if (mGarbage) { |
| 369 | gc(); |
| 370 | } |
| 371 | |
Tejas Khorana | f3b09b9 | 2016-07-13 14:00:09 -0700 | [diff] [blame] | 372 | for (int i = 0; i < mSize; i++) { |
| 373 | if (mValues[i] == value) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | return i; |
Tejas Khorana | f3b09b9 | 2016-07-13 14:00:09 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | |
| 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | /** |
Tejas Khorana | f3b09b9 | 2016-07-13 14:00:09 -0700 | [diff] [blame] | 382 | * Returns an index for which {@link #valueAt} would return the |
Laura Davis | e27c351 | 2018-07-26 15:21:45 -0700 | [diff] [blame] | 383 | * specified value, or a negative number if no keys map to the |
Tejas Khorana | f3b09b9 | 2016-07-13 14:00:09 -0700 | [diff] [blame] | 384 | * specified value. |
| 385 | * <p>Beware that this is a linear search, unlike lookups by key, |
| 386 | * and that multiple keys can map to the same value and this will |
| 387 | * find only one of them. |
| 388 | * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}. |
Suprabh Shukla | 0693ff6 | 2017-03-23 16:09:27 -0700 | [diff] [blame] | 389 | * @hide |
Tejas Khorana | f3b09b9 | 2016-07-13 14:00:09 -0700 | [diff] [blame] | 390 | */ |
| 391 | public int indexOfValueByValue(E value) { |
| 392 | if (mGarbage) { |
| 393 | gc(); |
| 394 | } |
| 395 | |
| 396 | for (int i = 0; i < mSize; i++) { |
| 397 | if (value == null) { |
| 398 | if (mValues[i] == null) { |
| 399 | return i; |
| 400 | } |
| 401 | } else { |
| 402 | if (value.equals(mValues[i])) { |
| 403 | return i; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | * Removes all key-value mappings from this SparseArray. |
| 412 | */ |
| 413 | public void clear() { |
| 414 | int n = mSize; |
| 415 | Object[] values = mValues; |
| 416 | |
| 417 | for (int i = 0; i < n; i++) { |
| 418 | values[i] = null; |
| 419 | } |
| 420 | |
| 421 | mSize = 0; |
| 422 | mGarbage = false; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Puts a key/value pair into the array, optimizing for the case where |
| 427 | * the key is greater than all existing keys in the array. |
| 428 | */ |
| 429 | public void append(int key, E value) { |
| 430 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 431 | put(key, value); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (mGarbage && mSize >= mKeys.length) { |
| 436 | gc(); |
| 437 | } |
| 438 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 439 | mKeys = GrowingArrayUtils.append(mKeys, mSize, key); |
| 440 | mValues = GrowingArrayUtils.append(mValues, mSize, value); |
| 441 | mSize++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | } |
Elliott Hughes | 58aff7d | 2013-03-26 16:34:30 -0700 | [diff] [blame] | 443 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 444 | /** |
| 445 | * {@inheritDoc} |
| 446 | * |
| 447 | * <p>This implementation composes a string by iterating over its mappings. If |
| 448 | * this map contains itself as a value, the string "(this Map)" |
| 449 | * will appear in its place. |
| 450 | */ |
| 451 | @Override |
| 452 | public String toString() { |
| 453 | if (size() <= 0) { |
| 454 | return "{}"; |
| 455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 457 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 458 | buffer.append('{'); |
| 459 | for (int i=0; i<mSize; i++) { |
| 460 | if (i > 0) { |
| 461 | buffer.append(", "); |
| 462 | } |
| 463 | int key = keyAt(i); |
| 464 | buffer.append(key); |
| 465 | buffer.append('='); |
| 466 | Object value = valueAt(i); |
| 467 | if (value != this) { |
| 468 | buffer.append(value); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 469 | } else { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 470 | buffer.append("(this Map)"); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 473 | buffer.append('}'); |
| 474 | return buffer.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |