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