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, |
| 23 | * there can be gaps in the indices. It is intended to be more efficient |
| 24 | * than using a HashMap to map Integers to Integers. |
| 25 | */ |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 26 | public class SparseIntArray implements Cloneable { |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 27 | private int[] mKeys; |
| 28 | private int[] mValues; |
| 29 | private int mSize; |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Creates a new SparseIntArray containing no mappings. |
| 33 | */ |
| 34 | public SparseIntArray() { |
| 35 | this(10); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Creates a new SparseIntArray containing no mappings that will not |
| 40 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 41 | * number of mappings. If you supply an initial capacity of 0, the |
| 42 | * sparse array will be initialized with a light-weight representation |
| 43 | * not requiring any additional array allocations. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
| 45 | public SparseIntArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 46 | if (initialCapacity == 0) { |
| 47 | mKeys = SparseArray.EMPTY_INTS; |
| 48 | mValues = SparseArray.EMPTY_INTS; |
| 49 | } else { |
| 50 | initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity); |
| 51 | mKeys = new int[initialCapacity]; |
| 52 | mValues = new int[initialCapacity]; |
| 53 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mSize = 0; |
| 55 | } |
| 56 | |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public SparseIntArray clone() { |
| 59 | SparseIntArray clone = null; |
| 60 | try { |
| 61 | clone = (SparseIntArray) super.clone(); |
| 62 | clone.mKeys = mKeys.clone(); |
| 63 | clone.mValues = mValues.clone(); |
| 64 | } catch (CloneNotSupportedException cnse) { |
| 65 | /* ignore */ |
| 66 | } |
| 67 | return clone; |
| 68 | } |
| 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Gets the int mapped from the specified key, or <code>0</code> |
| 72 | * if no such mapping has been made. |
| 73 | */ |
| 74 | public int get(int key) { |
| 75 | return get(key, 0); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the int mapped from the specified key, or the specified value |
| 80 | * if no such mapping has been made. |
| 81 | */ |
| 82 | public int get(int key, int valueIfKeyNotFound) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 83 | int i = SparseArray.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | if (i < 0) { |
| 86 | return valueIfKeyNotFound; |
| 87 | } else { |
| 88 | return mValues[i]; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Removes the mapping from the specified key, if there was any. |
| 94 | */ |
| 95 | public void delete(int key) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 96 | int i = SparseArray.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
| 98 | if (i >= 0) { |
| 99 | removeAt(i); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Removes the mapping at the given index. |
| 105 | */ |
| 106 | public void removeAt(int index) { |
| 107 | System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); |
| 108 | System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1)); |
| 109 | mSize--; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adds a mapping from the specified key to the specified value, |
| 114 | * replacing the previous mapping from the specified key if there |
| 115 | * was one. |
| 116 | */ |
| 117 | public void put(int key, int value) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 118 | int i = SparseArray.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
| 120 | if (i >= 0) { |
| 121 | mValues[i] = value; |
| 122 | } else { |
| 123 | i = ~i; |
| 124 | |
| 125 | if (mSize >= mKeys.length) { |
| 126 | int n = ArrayUtils.idealIntArraySize(mSize + 1); |
| 127 | |
| 128 | int[] nkeys = new int[n]; |
| 129 | int[] nvalues = new int[n]; |
| 130 | |
| 131 | // Log.e("SparseIntArray", "grow " + mKeys.length + " to " + n); |
| 132 | System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
| 133 | System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
| 134 | |
| 135 | mKeys = nkeys; |
| 136 | mValues = nvalues; |
| 137 | } |
| 138 | |
| 139 | if (mSize - i != 0) { |
| 140 | // Log.e("SparseIntArray", "move " + (mSize - i)); |
| 141 | System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
| 142 | System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
| 143 | } |
| 144 | |
| 145 | mKeys[i] = key; |
| 146 | mValues[i] = value; |
| 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 |
| 162 | * SparseIntArray stores. |
| 163 | */ |
| 164 | public int keyAt(int index) { |
| 165 | return mKeys[index]; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Given an index in the range <code>0...size()-1</code>, returns |
| 170 | * the value from the <code>index</code>th key-value mapping that this |
| 171 | * SparseIntArray stores. |
| 172 | */ |
| 173 | public int valueAt(int index) { |
| 174 | return mValues[index]; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the index for which {@link #keyAt} would return the |
| 179 | * specified key, or a negative number if the specified |
| 180 | * key is not mapped. |
| 181 | */ |
| 182 | public int indexOfKey(int key) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame^] | 183 | return SparseArray.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns an index for which {@link #valueAt} would return the |
| 188 | * specified key, or a negative number if no keys map to the |
| 189 | * specified value. |
| 190 | * Beware that this is a linear search, unlike lookups by key, |
| 191 | * and that multiple keys can map to the same value and this will |
| 192 | * find only one of them. |
| 193 | */ |
| 194 | public int indexOfValue(int value) { |
| 195 | for (int i = 0; i < mSize; i++) |
| 196 | if (mValues[i] == value) |
| 197 | return i; |
| 198 | |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Removes all key-value mappings from this SparseIntArray. |
| 204 | */ |
| 205 | public void clear() { |
| 206 | mSize = 0; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Puts a key/value pair into the array, optimizing for the case where |
| 211 | * the key is greater than all existing keys in the array. |
| 212 | */ |
| 213 | public void append(int key, int value) { |
| 214 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 215 | put(key, value); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | int pos = mSize; |
| 220 | if (pos >= mKeys.length) { |
| 221 | int n = ArrayUtils.idealIntArraySize(pos + 1); |
| 222 | |
| 223 | int[] nkeys = new int[n]; |
| 224 | int[] nvalues = new int[n]; |
| 225 | |
| 226 | // Log.e("SparseIntArray", "grow " + mKeys.length + " to " + n); |
| 227 | System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
| 228 | System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
| 229 | |
| 230 | mKeys = nkeys; |
| 231 | mValues = nvalues; |
| 232 | } |
| 233 | |
| 234 | mKeys[pos] = key; |
| 235 | mValues[pos] = value; |
| 236 | mSize = pos + 1; |
| 237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | } |