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