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 | 2802965 | 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 | |
| 118 | /** |
| 119 | * Adds a mapping from the specified key to the specified value, |
| 120 | * replacing the previous mapping from the specified key if there |
| 121 | * was one. |
| 122 | */ |
| 123 | public void put(int key, boolean value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 124 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | if (i >= 0) { |
| 127 | mValues[i] = value; |
| 128 | } else { |
| 129 | i = ~i; |
| 130 | |
| 131 | if (mSize >= mKeys.length) { |
| 132 | int n = ArrayUtils.idealIntArraySize(mSize + 1); |
| 133 | |
| 134 | int[] nkeys = new int[n]; |
| 135 | boolean[] nvalues = new boolean[n]; |
| 136 | |
| 137 | // Log.e("SparseBooleanArray", "grow " + mKeys.length + " to " + n); |
| 138 | System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
| 139 | System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
| 140 | |
| 141 | mKeys = nkeys; |
| 142 | mValues = nvalues; |
| 143 | } |
| 144 | |
| 145 | if (mSize - i != 0) { |
| 146 | // Log.e("SparseBooleanArray", "move " + (mSize - i)); |
| 147 | System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
| 148 | System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
| 149 | } |
| 150 | |
| 151 | mKeys[i] = key; |
| 152 | mValues[i] = value; |
| 153 | mSize++; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns the number of key-value mappings that this SparseBooleanArray |
| 159 | * currently stores. |
| 160 | */ |
| 161 | public int size() { |
| 162 | return mSize; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Given an index in the range <code>0...size()-1</code>, returns |
| 167 | * 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] | 168 | * SparseBooleanArray stores. |
| 169 | * |
| 170 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 171 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 172 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 173 | * key.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | */ |
| 175 | public int keyAt(int index) { |
| 176 | return mKeys[index]; |
| 177 | } |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Given an index in the range <code>0...size()-1</code>, returns |
| 181 | * 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] | 182 | * SparseBooleanArray stores. |
| 183 | * |
| 184 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 185 | * to be associated with keys in ascending order, e.g., |
| 186 | * <code>valueAt(0)</code> will return the value associated with the |
| 187 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 188 | * associated with the largest key.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | */ |
| 190 | public boolean valueAt(int index) { |
| 191 | return mValues[index]; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the index for which {@link #keyAt} would return the |
| 196 | * specified key, or a negative number if the specified |
| 197 | * key is not mapped. |
| 198 | */ |
| 199 | public int indexOfKey(int key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 200 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns an index for which {@link #valueAt} would return the |
| 205 | * specified key, or a negative number if no keys map to the |
| 206 | * specified value. |
| 207 | * Beware that this is a linear search, unlike lookups by key, |
| 208 | * and that multiple keys can map to the same value and this will |
| 209 | * find only one of them. |
| 210 | */ |
| 211 | public int indexOfValue(boolean value) { |
| 212 | for (int i = 0; i < mSize; i++) |
| 213 | if (mValues[i] == value) |
| 214 | return i; |
| 215 | |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Removes all key-value mappings from this SparseBooleanArray. |
| 221 | */ |
| 222 | public void clear() { |
| 223 | mSize = 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Puts a key/value pair into the array, optimizing for the case where |
| 228 | * the key is greater than all existing keys in the array. |
| 229 | */ |
| 230 | public void append(int key, boolean value) { |
| 231 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 232 | put(key, value); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | int pos = mSize; |
| 237 | if (pos >= mKeys.length) { |
| 238 | int n = ArrayUtils.idealIntArraySize(pos + 1); |
| 239 | |
| 240 | int[] nkeys = new int[n]; |
| 241 | boolean[] nvalues = new boolean[n]; |
| 242 | |
| 243 | // Log.e("SparseBooleanArray", "grow " + mKeys.length + " to " + n); |
| 244 | System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
| 245 | System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
| 246 | |
| 247 | mKeys = nkeys; |
| 248 | mValues = nvalues; |
| 249 | } |
| 250 | |
| 251 | mKeys[pos] = key; |
| 252 | mValues[pos] = value; |
| 253 | mSize = pos + 1; |
| 254 | } |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * {@inheritDoc} |
| 258 | * |
| 259 | * <p>This implementation composes a string by iterating over its mappings. |
| 260 | */ |
| 261 | @Override |
| 262 | public String toString() { |
| 263 | if (size() <= 0) { |
| 264 | return "{}"; |
| 265 | } |
| 266 | |
| 267 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 268 | buffer.append('{'); |
| 269 | for (int i=0; i<mSize; i++) { |
| 270 | if (i > 0) { |
| 271 | buffer.append(", "); |
| 272 | } |
| 273 | int key = keyAt(i); |
| 274 | buffer.append(key); |
| 275 | buffer.append('='); |
| 276 | boolean value = valueAt(i); |
| 277 | buffer.append(value); |
| 278 | } |
| 279 | buffer.append('}'); |
| 280 | return buffer.toString(); |
| 281 | } |
| 282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | private int[] mKeys; |
| 284 | private boolean[] mValues; |
| 285 | private int mSize; |
| 286 | } |