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