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 | |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 26 | import java.util.Arrays; |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | /** |
| 29 | * SparseIntArrays map integers to integers. Unlike a normal array of integers, |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 30 | * there can be gaps in the indices. It is intended to be more memory efficient |
| 31 | * than using a HashMap to map Integers to Integers, both because it avoids |
| 32 | * auto-boxing keys and values and its data structure doesn't rely on an extra entry object |
| 33 | * for each mapping. |
| 34 | * |
| 35 | * <p>Note that this container keeps its mappings in an array data structure, |
| 36 | * using a binary search to find keys. The implementation is not intended to be appropriate for |
| 37 | * data structures |
| 38 | * that may contain large numbers of items. It is generally slower than a traditional |
| 39 | * HashMap, since lookups require a binary search 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 not significant, less than 50%.</p> |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 42 | * |
| 43 | * <p>It is possible to iterate over the items in this container using |
| 44 | * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using |
| 45 | * <code>keyAt(int)</code> with ascending values of the index will return the |
| 46 | * keys in ascending order, or the values corresponding to the keys in ascending |
Newton Allen | ebb4795 | 2013-11-21 13:27:10 -0800 | [diff] [blame] | 47 | * order in the case of <code>valueAt(int)</code>.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | */ |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 49 | public class SparseIntArray implements Cloneable { |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 50 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use keyAt(int) |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 51 | private int[] mKeys; |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 52 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use valueAt(int), setValueAt(int, int) |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 53 | private int[] mValues; |
Jake Wharton | a8a0435 | 2018-09-29 01:52:24 -0400 | [diff] [blame] | 54 | @UnsupportedAppUsage(maxTargetSdk = 28) // Use size() |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 55 | private int mSize; |
| 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Creates a new SparseIntArray containing no mappings. |
| 59 | */ |
| 60 | public SparseIntArray() { |
| 61 | this(10); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new SparseIntArray containing no mappings that will not |
| 66 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 67 | * number of mappings. If you supply an initial capacity of 0, the |
| 68 | * sparse array will be initialized with a light-weight representation |
| 69 | * not requiring any additional array allocations. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | */ |
| 71 | public SparseIntArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 72 | if (initialCapacity == 0) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 73 | mKeys = EmptyArray.INT; |
| 74 | mValues = EmptyArray.INT; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 75 | } else { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 76 | mKeys = ArrayUtils.newUnpaddedIntArray(initialCapacity); |
| 77 | mValues = new int[mKeys.length]; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 78 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | mSize = 0; |
| 80 | } |
| 81 | |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public SparseIntArray clone() { |
| 84 | SparseIntArray clone = null; |
| 85 | try { |
| 86 | clone = (SparseIntArray) super.clone(); |
| 87 | clone.mKeys = mKeys.clone(); |
| 88 | clone.mValues = mValues.clone(); |
| 89 | } catch (CloneNotSupportedException cnse) { |
| 90 | /* ignore */ |
| 91 | } |
| 92 | return clone; |
| 93 | } |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Gets the int mapped from the specified key, or <code>0</code> |
| 97 | * if no such mapping has been made. |
| 98 | */ |
| 99 | public int get(int key) { |
| 100 | return get(key, 0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Gets the int mapped from the specified key, or the specified value |
| 105 | * if no such mapping has been made. |
| 106 | */ |
| 107 | public int get(int key, int valueIfKeyNotFound) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 108 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | if (i < 0) { |
| 111 | return valueIfKeyNotFound; |
| 112 | } else { |
| 113 | return mValues[i]; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Removes the mapping from the specified key, if there was any. |
| 119 | */ |
| 120 | public void delete(int key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 121 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
| 123 | if (i >= 0) { |
| 124 | removeAt(i); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Removes the mapping at the given index. |
| 130 | */ |
| 131 | public void removeAt(int index) { |
| 132 | System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); |
| 133 | System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1)); |
| 134 | mSize--; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Adds a mapping from the specified key to the specified value, |
| 139 | * replacing the previous mapping from the specified key if there |
| 140 | * was one. |
| 141 | */ |
| 142 | public void put(int key, int value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 143 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
| 145 | if (i >= 0) { |
| 146 | mValues[i] = value; |
| 147 | } else { |
| 148 | i = ~i; |
| 149 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 150 | mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); |
| 151 | mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | mSize++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the number of key-value mappings that this SparseIntArray |
| 158 | * currently stores. |
| 159 | */ |
| 160 | public int size() { |
| 161 | return mSize; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Given an index in the range <code>0...size()-1</code>, returns |
| 166 | * the key from the <code>index</code>th key-value mapping that this |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 167 | * SparseIntArray stores. |
| 168 | * |
| 169 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 170 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 171 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 172 | * key.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | */ |
| 174 | public int keyAt(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 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | return mKeys[index]; |
| 180 | } |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | /** |
| 183 | * Given an index in the range <code>0...size()-1</code>, returns |
| 184 | * the value from the <code>index</code>th key-value mapping that this |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 185 | * SparseIntArray stores. |
| 186 | * |
| 187 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 188 | * to be associated with keys in ascending order, e.g., |
| 189 | * <code>valueAt(0)</code> will return the value associated with the |
| 190 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 191 | * associated with the largest key.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | */ |
| 193 | public int valueAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 194 | if (index >= mSize) { |
| 195 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 196 | throw new ArrayIndexOutOfBoundsException(index); |
| 197 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | return mValues[index]; |
| 199 | } |
| 200 | |
| 201 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 202 | * Directly set the value at a particular index. |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 203 | */ |
| 204 | public void setValueAt(int index, int value) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 205 | if (index >= mSize) { |
| 206 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 207 | throw new ArrayIndexOutOfBoundsException(index); |
| 208 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 209 | mValues[index] = value; |
| 210 | } |
| 211 | |
| 212 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | * Returns the index for which {@link #keyAt} would return the |
| 214 | * specified key, or a negative number if the specified |
| 215 | * key is not mapped. |
| 216 | */ |
| 217 | public int indexOfKey(int key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 218 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns an index for which {@link #valueAt} would return the |
| 223 | * specified key, or a negative number if no keys map to the |
| 224 | * specified value. |
| 225 | * Beware that this is a linear search, unlike lookups by key, |
| 226 | * and that multiple keys can map to the same value and this will |
| 227 | * find only one of them. |
| 228 | */ |
| 229 | public int indexOfValue(int value) { |
| 230 | for (int i = 0; i < mSize; i++) |
| 231 | if (mValues[i] == value) |
| 232 | return i; |
| 233 | |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Removes all key-value mappings from this SparseIntArray. |
| 239 | */ |
| 240 | public void clear() { |
| 241 | mSize = 0; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Puts a key/value pair into the array, optimizing for the case where |
| 246 | * the key is greater than all existing keys in the array. |
| 247 | */ |
| 248 | public void append(int key, int value) { |
| 249 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 250 | put(key, value); |
| 251 | return; |
| 252 | } |
| 253 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 254 | mKeys = GrowingArrayUtils.append(mKeys, mSize, key); |
| 255 | mValues = GrowingArrayUtils.append(mValues, mSize, value); |
| 256 | mSize++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | } |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
Filip Gruszczynski | 20aa0ae | 2015-10-30 10:08:27 -0700 | [diff] [blame] | 260 | * Provides a copy of keys. |
| 261 | * |
| 262 | * @hide |
| 263 | * */ |
| 264 | public int[] copyKeys() { |
| 265 | if (size() == 0) { |
| 266 | return null; |
| 267 | } |
| 268 | return Arrays.copyOf(mKeys, size()); |
| 269 | } |
| 270 | |
| 271 | /** |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 272 | * {@inheritDoc} |
| 273 | * |
| 274 | * <p>This implementation composes a string by iterating over its mappings. |
| 275 | */ |
| 276 | @Override |
| 277 | public String toString() { |
| 278 | if (size() <= 0) { |
| 279 | return "{}"; |
| 280 | } |
| 281 | |
| 282 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 283 | buffer.append('{'); |
| 284 | for (int i=0; i<mSize; i++) { |
| 285 | if (i > 0) { |
| 286 | buffer.append(", "); |
| 287 | } |
| 288 | int key = keyAt(i); |
| 289 | buffer.append(key); |
| 290 | buffer.append('='); |
| 291 | int value = valueAt(i); |
| 292 | buffer.append(value); |
| 293 | } |
| 294 | buffer.append('}'); |
| 295 | return buffer.toString(); |
| 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |