Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 java.util.Collection; |
| 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | /** |
| 24 | * ArrayMap is a generic key->value mapping data structure that is |
| 25 | * designed to be more memory efficient than a traditional {@link java.util.HashMap}. |
| 26 | * It keeps its mappings in an array data structure -- an integer array of hash |
| 27 | * codes for each item, and an Object array of the key/value pairs. This allows it to |
| 28 | * avoid having to create an extra object for every entry put in to the map, and it |
| 29 | * also tries to control the growth of the size of these arrays more aggressively |
| 30 | * (since growing them only requires copying the entries in the array, not rebuilding |
| 31 | * a hash map). |
| 32 | * |
| 33 | * <p>Note that this implementation is not intended to be appropriate for data structures |
| 34 | * that may contain large numbers of items. It is generally slower than a traditional |
| 35 | * HashMap, since lookups require a binary search and adds and removes require inserting |
| 36 | * and deleting entries in the array. For containers holding up to hundreds of items, |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 37 | * the performance difference is not significant, less than 50%.</p> |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 38 | * |
| 39 | * <p><b>Note:</b> unlike {@link java.util.HashMap}, this container does not support |
| 40 | * null keys.</p> |
| 41 | * |
| 42 | * <p>Because this container is intended to better balance memory use, unlike most other |
| 43 | * standard Java containers it will shrink its array as items are removed from it. Currently |
| 44 | * you have no control over this shrinking -- if you set a capacity and then remove an |
| 45 | * item, it may reduce the capacity to better match the current size. In the future an |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 46 | * explicit call to set the capacity should turn off this aggressive shrinking behavior.</p> |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 47 | */ |
| 48 | public final class ArrayMap<K, V> implements Map<K, V> { |
| 49 | private static final boolean DEBUG = false; |
| 50 | private static final String TAG = "ArrayMap"; |
| 51 | |
| 52 | /** |
| 53 | * The minimum amount by which the capacity of a ArrayMap will increase. |
| 54 | * This is tuned to be relatively space-efficient. |
| 55 | */ |
| 56 | private static final int BASE_SIZE = 4; |
| 57 | |
| 58 | /** |
| 59 | * Maximum number of entries to have in array caches. |
| 60 | */ |
| 61 | private static final int CACHE_SIZE = 10; |
| 62 | |
| 63 | /** |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 64 | * @hide Special immutable empty ArrayMap. |
| 65 | */ |
| 66 | public static final ArrayMap EMPTY = new ArrayMap(true); |
| 67 | |
| 68 | /** |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 69 | * Caches of small array objects to avoid spamming garbage. The cache |
| 70 | * Object[] variable is a pointer to a linked list of array objects. |
| 71 | * The first entry in the array is a pointer to the next array in the |
| 72 | * list; the second entry is a pointer to the int[] hash code array for it. |
| 73 | */ |
| 74 | static Object[] mBaseCache; |
| 75 | static int mBaseCacheSize; |
| 76 | static Object[] mTwiceBaseCache; |
| 77 | static int mTwiceBaseCacheSize; |
| 78 | |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Special hash array value that indicates the container is immutable. |
| 81 | */ |
| 82 | static final int[] EMPTY_IMMUTABLE_INTS = new int[0]; |
| 83 | |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 84 | int[] mHashes; |
| 85 | Object[] mArray; |
| 86 | int mSize; |
| 87 | MapCollections<K, V> mCollections; |
| 88 | |
| 89 | private int indexOf(Object key, int hash) { |
| 90 | final int N = mSize; |
| 91 | |
| 92 | // Important fast case: if nothing is in here, nothing to look for. |
| 93 | if (N == 0) { |
| 94 | return ~0; |
| 95 | } |
| 96 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 97 | int index = ContainerHelpers.binarySearch(mHashes, N, hash); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 98 | |
| 99 | // If the hash code wasn't found, then we have no entry for this key. |
| 100 | if (index < 0) { |
| 101 | return index; |
| 102 | } |
| 103 | |
| 104 | // If the key at the returned index matches, that's what we want. |
| 105 | if (mArray[index<<1].equals(key)) { |
| 106 | return index; |
| 107 | } |
| 108 | |
| 109 | // Search for a matching key after the index. |
| 110 | int end; |
| 111 | for (end = index + 1; end < N && mHashes[end] == hash; end++) { |
| 112 | if (mArray[end << 1].equals(key)) return end; |
| 113 | } |
| 114 | |
| 115 | // Search for a matching key before the index. |
| 116 | for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) { |
| 117 | if (mArray[i << 1].equals(key)) return i; |
| 118 | } |
| 119 | |
| 120 | // Key not found -- return negative value indicating where a |
| 121 | // new entry for this key should go. We use the end of the |
| 122 | // hash chain to reduce the number of array entries that will |
| 123 | // need to be copied when inserting. |
| 124 | return ~end; |
| 125 | } |
| 126 | |
| 127 | private void allocArrays(final int size) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 128 | if (mHashes == EMPTY_IMMUTABLE_INTS) { |
| 129 | throw new UnsupportedOperationException("ArrayMap is immutable"); |
| 130 | } |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 131 | if (size == (BASE_SIZE*2)) { |
| 132 | synchronized (ArrayMap.class) { |
| 133 | if (mTwiceBaseCache != null) { |
| 134 | final Object[] array = mTwiceBaseCache; |
| 135 | mArray = array; |
| 136 | mTwiceBaseCache = (Object[])array[0]; |
| 137 | mHashes = (int[])array[1]; |
| 138 | array[0] = array[1] = null; |
| 139 | mTwiceBaseCacheSize--; |
| 140 | if (DEBUG) Log.d(TAG, "Retrieving 2x cache " + mHashes |
| 141 | + " now have " + mTwiceBaseCacheSize + " entries"); |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | } else if (size == BASE_SIZE) { |
| 146 | synchronized (ArrayMap.class) { |
| 147 | if (mBaseCache != null) { |
| 148 | final Object[] array = mBaseCache; |
| 149 | mArray = array; |
| 150 | mBaseCache = (Object[])array[0]; |
| 151 | mHashes = (int[])array[1]; |
| 152 | array[0] = array[1] = null; |
| 153 | mBaseCacheSize--; |
| 154 | if (DEBUG) Log.d(TAG, "Retrieving 1x cache " + mHashes |
| 155 | + " now have " + mBaseCacheSize + " entries"); |
| 156 | return; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | mHashes = new int[size]; |
| 162 | mArray = new Object[size<<1]; |
| 163 | } |
| 164 | |
| 165 | private static void freeArrays(final int[] hashes, final Object[] array, final int size) { |
| 166 | if (hashes.length == (BASE_SIZE*2)) { |
| 167 | synchronized (ArrayMap.class) { |
| 168 | if (mTwiceBaseCacheSize < CACHE_SIZE) { |
| 169 | array[0] = mTwiceBaseCache; |
| 170 | array[1] = hashes; |
| 171 | for (int i=(size<<1)-1; i>=2; i--) { |
| 172 | array[i] = null; |
| 173 | } |
| 174 | mTwiceBaseCache = array; |
| 175 | mTwiceBaseCacheSize++; |
| 176 | if (DEBUG) Log.d(TAG, "Storing 2x cache " + array |
| 177 | + " now have " + mTwiceBaseCacheSize + " entries"); |
| 178 | } |
| 179 | } |
| 180 | } else if (hashes.length == BASE_SIZE) { |
| 181 | synchronized (ArrayMap.class) { |
| 182 | if (mBaseCacheSize < CACHE_SIZE) { |
| 183 | array[0] = mBaseCache; |
| 184 | array[1] = hashes; |
| 185 | for (int i=(size<<1)-1; i>=2; i--) { |
| 186 | array[i] = null; |
| 187 | } |
| 188 | mBaseCache = array; |
| 189 | mBaseCacheSize++; |
| 190 | if (DEBUG) Log.d(TAG, "Storing 1x cache " + array |
| 191 | + " now have " + mBaseCacheSize + " entries"); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Create a new empty ArrayMap. The default capacity of an array map is 0, and |
| 199 | * will grow once items are added to it. |
| 200 | */ |
| 201 | public ArrayMap() { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 202 | mHashes = ContainerHelpers.EMPTY_INTS; |
| 203 | mArray = ContainerHelpers.EMPTY_OBJECTS; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 204 | mSize = 0; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Create a new ArrayMap with a given initial capacity. |
| 209 | */ |
| 210 | public ArrayMap(int capacity) { |
| 211 | if (capacity == 0) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 212 | mHashes = ContainerHelpers.EMPTY_INTS; |
| 213 | mArray = ContainerHelpers.EMPTY_OBJECTS; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 214 | } else { |
| 215 | allocArrays(capacity); |
| 216 | } |
| 217 | mSize = 0; |
| 218 | } |
Dianne Hackborn | 21ab6f4 | 2013-06-10 15:59:15 -0700 | [diff] [blame] | 219 | |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 220 | private ArrayMap(boolean immutable) { |
| 221 | mHashes = EMPTY_IMMUTABLE_INTS; |
| 222 | mArray = ContainerHelpers.EMPTY_OBJECTS; |
| 223 | mSize = 0; |
| 224 | } |
| 225 | |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Create a new ArrayMap with the mappings from the given ArrayMap. |
| 228 | */ |
| 229 | public ArrayMap(ArrayMap map) { |
| 230 | this(); |
| 231 | if (map != null) { |
| 232 | putAll(map); |
| 233 | } |
| 234 | } |
| 235 | |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 236 | /** |
| 237 | * Make the array map empty. All storage is released. |
| 238 | */ |
| 239 | @Override |
| 240 | public void clear() { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 241 | if (mSize > 0) { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 242 | freeArrays(mHashes, mArray, mSize); |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 243 | mHashes = ContainerHelpers.EMPTY_INTS; |
| 244 | mArray = ContainerHelpers.EMPTY_OBJECTS; |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 245 | mSize = 0; |
| 246 | } |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Ensure the array map can hold at least <var>minimumCapacity</var> |
| 251 | * items. |
| 252 | */ |
| 253 | public void ensureCapacity(int minimumCapacity) { |
| 254 | if (mHashes.length < minimumCapacity) { |
Dianne Hackborn | 21ab6f4 | 2013-06-10 15:59:15 -0700 | [diff] [blame] | 255 | final int[] ohashes = mHashes; |
| 256 | final Object[] oarray = mArray; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 257 | allocArrays(minimumCapacity); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 258 | if (mSize > 0) { |
| 259 | System.arraycopy(ohashes, 0, mHashes, 0, mSize); |
| 260 | System.arraycopy(oarray, 0, mArray, 0, mSize<<1); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 261 | } |
| 262 | freeArrays(ohashes, oarray, mSize); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Check whether a key exists in the array. |
| 268 | * |
| 269 | * @param key The key to search for. |
| 270 | * @return Returns true if the key exists, else false. |
| 271 | */ |
| 272 | @Override |
| 273 | public boolean containsKey(Object key) { |
| 274 | return indexOf(key, key.hashCode()) >= 0; |
| 275 | } |
| 276 | |
| 277 | private int indexOfValue(Object value) { |
| 278 | final int N = mSize*2; |
| 279 | final Object[] array = mArray; |
| 280 | if (value == null) { |
| 281 | for (int i=1; i<N; i+=2) { |
| 282 | if (array[i] == null) { |
| 283 | return i>>1; |
| 284 | } |
| 285 | } |
| 286 | } else { |
| 287 | for (int i=1; i<N; i+=2) { |
| 288 | if (value.equals(array[i])) { |
| 289 | return i>>1; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Check whether a value exists in the array. This requires a linear search |
| 298 | * through the entire array. |
| 299 | * |
| 300 | * @param value The value to search for. |
| 301 | * @return Returns true if the value exists, else false. |
| 302 | */ |
| 303 | @Override |
| 304 | public boolean containsValue(Object value) { |
| 305 | return indexOfValue(value) >= 0; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Retrieve a value from the array. |
| 310 | * @param key The key of the value to retrieve. |
| 311 | * @return Returns the value associated with the given key, |
| 312 | * or null if there is no such key. |
| 313 | */ |
| 314 | @Override |
| 315 | public V get(Object key) { |
| 316 | final int index = indexOf(key, key.hashCode()); |
| 317 | return index >= 0 ? (V)mArray[(index<<1)+1] : null; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Return the key at the given index in the array. |
| 322 | * @param index The desired index, must be between 0 and {@link #size()}-1. |
| 323 | * @return Returns the key stored at the given index. |
| 324 | */ |
| 325 | public K keyAt(int index) { |
| 326 | return (K)mArray[index << 1]; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Return the value at the given index in the array. |
| 331 | * @param index The desired index, must be between 0 and {@link #size()}-1. |
| 332 | * @return Returns the value stored at the given index. |
| 333 | */ |
| 334 | public V valueAt(int index) { |
| 335 | return (V)mArray[(index << 1) + 1]; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Set the value at a given index in the array. |
| 340 | * @param index The desired index, must be between 0 and {@link #size()}-1. |
| 341 | * @param value The new value to store at this index. |
| 342 | * @return Returns the previous value at the given index. |
| 343 | */ |
| 344 | public V setValueAt(int index, V value) { |
| 345 | index = (index << 1) + 1; |
| 346 | V old = (V)mArray[index]; |
| 347 | mArray[index] = value; |
| 348 | return old; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Return true if the array map contains no items. |
| 353 | */ |
| 354 | @Override |
| 355 | public boolean isEmpty() { |
| 356 | return mSize <= 0; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Add a new value to the array map. |
| 361 | * @param key The key under which to store the value. <b>Must not be null.</b> If |
| 362 | * this key already exists in the array, its value will be replaced. |
| 363 | * @param value The value to store for the given key. |
| 364 | * @return Returns the old value that was stored for the given key, or null if there |
| 365 | * was no such key. |
| 366 | */ |
| 367 | @Override |
| 368 | public V put(K key, V value) { |
| 369 | final int hash = key.hashCode(); |
| 370 | int index = indexOf(key, hash); |
| 371 | if (index >= 0) { |
| 372 | index = (index<<1) + 1; |
| 373 | final V old = (V)mArray[index]; |
| 374 | mArray[index] = value; |
| 375 | return old; |
| 376 | } |
| 377 | |
| 378 | index = ~index; |
| 379 | if (mSize >= mHashes.length) { |
| 380 | final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1)) |
| 381 | : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE); |
| 382 | |
| 383 | if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n); |
| 384 | |
| 385 | final int[] ohashes = mHashes; |
| 386 | final Object[] oarray = mArray; |
| 387 | allocArrays(n); |
| 388 | |
| 389 | if (mHashes.length > 0) { |
| 390 | if (DEBUG) Log.d(TAG, "put: copy 0-" + mSize + " to 0"); |
| 391 | System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length); |
| 392 | System.arraycopy(oarray, 0, mArray, 0, oarray.length); |
| 393 | } |
| 394 | |
| 395 | freeArrays(ohashes, oarray, mSize); |
| 396 | } |
| 397 | |
| 398 | if (index < mSize) { |
| 399 | if (DEBUG) Log.d(TAG, "put: move " + index + "-" + (mSize-index) |
| 400 | + " to " + (index+1)); |
| 401 | System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index); |
| 402 | System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1); |
| 403 | } |
| 404 | |
| 405 | mHashes[index] = hash; |
| 406 | mArray[index<<1] = key; |
| 407 | mArray[(index<<1)+1] = value; |
| 408 | mSize++; |
| 409 | return null; |
| 410 | } |
| 411 | |
| 412 | /** |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 413 | * Special fast path for appending items to the end of the array without validation. |
| 414 | * The array must already be large enough to contain the item. |
| 415 | * @hide |
| 416 | */ |
| 417 | public void append(K key, V value) { |
| 418 | int index = mSize; |
| 419 | final int hash = key.hashCode(); |
| 420 | if (index >= mHashes.length) { |
| 421 | throw new IllegalStateException("Array is full"); |
| 422 | } |
| 423 | if (index > 0 && mHashes[index-1] > hash) { |
| 424 | throw new IllegalArgumentException("New hash " + hash |
| 425 | + " is before end of array hash " + mHashes[index-1]); |
| 426 | } |
| 427 | mSize = index+1; |
| 428 | mHashes[index] = hash; |
| 429 | index <<= 1; |
| 430 | mArray[index] = key; |
| 431 | mArray[index+1] = value; |
| 432 | } |
| 433 | |
| 434 | /** |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 435 | * Perform a {@link #put(Object, Object)} of all key/value pairs in <var>array</var> |
| 436 | * @param array The array whose contents are to be retrieved. |
| 437 | */ |
| 438 | public void putAll(ArrayMap<? extends K, ? extends V> array) { |
| 439 | final int N = array.mSize; |
| 440 | ensureCapacity(mSize + N); |
Chet Haase | f4130cf | 2013-06-06 16:34:33 -0700 | [diff] [blame] | 441 | if (mSize == 0) { |
| 442 | if (N > 0) { |
| 443 | System.arraycopy(array.mHashes, 0, mHashes, 0, N); |
| 444 | System.arraycopy(array.mArray, 0, mArray, 0, N<<1); |
| 445 | mSize = N; |
| 446 | } |
| 447 | } else { |
| 448 | for (int i=0; i<N; i++) { |
| 449 | put(array.keyAt(i), array.valueAt(i)); |
| 450 | } |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Remove an existing key from the array map. |
| 456 | * @param key The key of the mapping to remove. |
| 457 | * @return Returns the value that was stored under the key, or null if there |
| 458 | * was no such key. |
| 459 | */ |
| 460 | @Override |
| 461 | public V remove(Object key) { |
| 462 | int index = indexOf(key, key.hashCode()); |
| 463 | if (index >= 0) { |
| 464 | return removeAt(index); |
| 465 | } |
| 466 | |
| 467 | return null; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Remove the key/value mapping at the given index. |
| 472 | * @param index The desired index, must be between 0 and {@link #size()}-1. |
| 473 | * @return Returns the value that was stored at this index. |
| 474 | */ |
| 475 | public V removeAt(int index) { |
| 476 | final V old = (V)mArray[(index << 1) + 1]; |
| 477 | if (mSize <= 1) { |
| 478 | // Now empty. |
| 479 | if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0"); |
| 480 | freeArrays(mHashes, mArray, mSize); |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 481 | mHashes = ContainerHelpers.EMPTY_INTS; |
| 482 | mArray = ContainerHelpers.EMPTY_OBJECTS; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 483 | mSize = 0; |
| 484 | } else { |
| 485 | if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) { |
| 486 | // Shrunk enough to reduce size of arrays. We don't allow it to |
| 487 | // shrink smaller than (BASE_SIZE*2) to avoid flapping between |
| 488 | // that and BASE_SIZE. |
| 489 | final int n = mSize > (BASE_SIZE*2) ? (mSize + (mSize>>1)) : (BASE_SIZE*2); |
| 490 | |
| 491 | if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n); |
| 492 | |
| 493 | final int[] ohashes = mHashes; |
| 494 | final Object[] oarray = mArray; |
| 495 | allocArrays(n); |
| 496 | |
| 497 | mSize--; |
| 498 | if (index > 0) { |
| 499 | if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0"); |
| 500 | System.arraycopy(ohashes, 0, mHashes, 0, index); |
| 501 | System.arraycopy(oarray, 0, mArray, 0, index << 1); |
| 502 | } |
| 503 | if (index < mSize) { |
| 504 | if (DEBUG) Log.d(TAG, "remove: copy from " + (index+1) + "-" + mSize |
| 505 | + " to " + index); |
| 506 | System.arraycopy(ohashes, index + 1, mHashes, index, mSize - index); |
| 507 | System.arraycopy(oarray, (index + 1) << 1, mArray, index << 1, |
| 508 | (mSize - index) << 1); |
| 509 | } |
| 510 | } else { |
| 511 | mSize--; |
| 512 | if (index < mSize) { |
| 513 | if (DEBUG) Log.d(TAG, "remove: move " + (index+1) + "-" + mSize |
| 514 | + " to " + index); |
| 515 | System.arraycopy(mHashes, index + 1, mHashes, index, mSize - index); |
| 516 | System.arraycopy(mArray, (index + 1) << 1, mArray, index << 1, |
| 517 | (mSize - index) << 1); |
| 518 | } |
| 519 | mArray[mSize << 1] = null; |
| 520 | mArray[(mSize << 1) + 1] = null; |
| 521 | } |
| 522 | } |
| 523 | return old; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Return the number of items in this array map. |
| 528 | */ |
| 529 | @Override |
| 530 | public int size() { |
| 531 | return mSize; |
| 532 | } |
| 533 | |
Dianne Hackborn | 21ab6f4 | 2013-06-10 15:59:15 -0700 | [diff] [blame] | 534 | /** |
| 535 | * {@inheritDoc} |
| 536 | * |
| 537 | * <p>This implementation returns false if the object is not a map, or |
| 538 | * if the maps have different sizes. Otherwise, for each key in this map, |
| 539 | * values of both maps are compared. If the values for any key are not |
| 540 | * equal, the method returns false, otherwise it returns true. |
| 541 | */ |
| 542 | @Override |
| 543 | public boolean equals(Object object) { |
| 544 | if (this == object) { |
| 545 | return true; |
| 546 | } |
| 547 | if (object instanceof Map) { |
| 548 | Map<?, ?> map = (Map<?, ?>) object; |
| 549 | if (size() != map.size()) { |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | try { |
| 554 | for (int i=0; i<mSize; i++) { |
| 555 | K key = keyAt(i); |
| 556 | V mine = valueAt(i); |
| 557 | Object theirs = map.get(key); |
| 558 | if (mine == null) { |
| 559 | if (theirs != null || !map.containsKey(key)) { |
| 560 | return false; |
| 561 | } |
| 562 | } else if (!mine.equals(theirs)) { |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | } catch (NullPointerException ignored) { |
| 567 | return false; |
| 568 | } catch (ClassCastException ignored) { |
| 569 | return false; |
| 570 | } |
| 571 | return true; |
| 572 | } |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * {@inheritDoc} |
| 578 | */ |
| 579 | @Override |
| 580 | public int hashCode() { |
| 581 | final int[] hashes = mHashes; |
| 582 | final Object[] array = mArray; |
| 583 | int result = 0; |
| 584 | for (int i = 0, v = 1, s = mSize; i < s; i++, v+=2) { |
| 585 | Object value = array[v]; |
| 586 | result += hashes[i] ^ (value == null ? 0 : value.hashCode()); |
| 587 | } |
| 588 | return result; |
| 589 | } |
| 590 | |
Dianne Hackborn | a17c0f5 | 2013-06-20 18:59:11 -0700 | [diff] [blame] | 591 | /** |
| 592 | * {@inheritDoc} |
| 593 | * |
| 594 | * <p>This implementation composes a string by iterating over its mappings. If |
| 595 | * this map contains itself as a key or a value, the string "(this Map)" |
| 596 | * will appear in its place. |
| 597 | */ |
| 598 | @Override |
| 599 | public String toString() { |
| 600 | if (isEmpty()) { |
| 601 | return "{}"; |
| 602 | } |
| 603 | |
| 604 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 605 | buffer.append('{'); |
| 606 | for (int i=0; i<mSize; i++) { |
| 607 | if (i > 0) { |
| 608 | buffer.append(", "); |
| 609 | } |
| 610 | Object key = keyAt(i); |
| 611 | if (key != this) { |
| 612 | buffer.append(key); |
| 613 | } else { |
| 614 | buffer.append("(this Map)"); |
| 615 | } |
| 616 | buffer.append('='); |
| 617 | Object value = valueAt(i); |
| 618 | if (value != this) { |
| 619 | buffer.append(value); |
| 620 | } else { |
| 621 | buffer.append("(this Map)"); |
| 622 | } |
| 623 | } |
| 624 | buffer.append('}'); |
| 625 | return buffer.toString(); |
| 626 | } |
| 627 | |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 628 | // ------------------------------------------------------------------------ |
| 629 | // Interop with traditional Java containers. Not as efficient as using |
| 630 | // specialized collection APIs. |
| 631 | // ------------------------------------------------------------------------ |
| 632 | |
| 633 | private MapCollections<K, V> getCollection() { |
| 634 | if (mCollections == null) { |
| 635 | mCollections = new MapCollections<K, V>() { |
| 636 | @Override |
| 637 | protected int colGetSize() { |
| 638 | return mSize; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | protected Object colGetEntry(int index, int offset) { |
| 643 | return mArray[(index<<1) + offset]; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | protected int colIndexOfKey(Object key) { |
| 648 | return indexOf(key, key.hashCode()); |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | protected int colIndexOfValue(Object value) { |
| 653 | return indexOfValue(value); |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | protected Map<K, V> colGetMap() { |
| 658 | return ArrayMap.this; |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | protected void colPut(K key, V value) { |
| 663 | put(key, value); |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | protected V colSetValue(int index, V value) { |
| 668 | return setValueAt(index, value); |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | protected void colRemoveAt(int index) { |
| 673 | removeAt(index); |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | protected void colClear() { |
| 678 | clear(); |
| 679 | } |
| 680 | }; |
| 681 | } |
| 682 | return mCollections; |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Determine if the array map contains all of the keys in the given collection. |
| 687 | * @param collection The collection whose contents are to be checked against. |
| 688 | * @return Returns true if this array map contains a key for every entry |
| 689 | * in <var>collection</var>, else returns false. |
| 690 | */ |
| 691 | public boolean containsAll(Collection<?> collection) { |
| 692 | return MapCollections.containsAllHelper(this, collection); |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Perform a {@link #put(Object, Object)} of all key/value pairs in <var>map</var> |
| 697 | * @param map The map whose contents are to be retrieved. |
| 698 | */ |
| 699 | @Override |
| 700 | public void putAll(Map<? extends K, ? extends V> map) { |
| 701 | ensureCapacity(mSize + map.size()); |
| 702 | for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { |
| 703 | put(entry.getKey(), entry.getValue()); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Remove all keys in the array map that exist in the given collection. |
| 709 | * @param collection The collection whose contents are to be used to remove keys. |
| 710 | * @return Returns true if any keys were removed from the array map, else false. |
| 711 | */ |
| 712 | public boolean removeAll(Collection<?> collection) { |
| 713 | return MapCollections.removeAllHelper(this, collection); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Remove all keys in the array map that do <b>not</b> exist in the given collection. |
| 718 | * @param collection The collection whose contents are to be used to determine which |
| 719 | * keys to keep. |
| 720 | * @return Returns true if any keys were removed from the array map, else false. |
| 721 | */ |
| 722 | public boolean retainAll(Collection<?> collection) { |
| 723 | return MapCollections.retainAllHelper(this, collection); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Return a {@link java.util.Set} for iterating over and interacting with all mappings |
| 728 | * in the array map. |
| 729 | * |
| 730 | * <p><b>Note:</b> this is a very inefficient way to access the array contents, it |
| 731 | * requires generating a number of temporary objects.</p> |
| 732 | * |
| 733 | * <p><b>Note:</b></p> the semantics of this |
| 734 | * Set are subtly different than that of a {@link java.util.HashMap}: most important, |
| 735 | * the {@link java.util.Map.Entry Map.Entry} object returned by its iterator is a single |
| 736 | * object that exists for the entire iterator, so you can <b>not</b> hold on to it |
| 737 | * after calling {@link java.util.Iterator#next() Iterator.next}.</p> |
| 738 | */ |
| 739 | @Override |
| 740 | public Set<Map.Entry<K, V>> entrySet() { |
| 741 | return getCollection().getEntrySet(); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Return a {@link java.util.Set} for iterating over and interacting with all keys |
| 746 | * in the array map. |
| 747 | * |
Chet Haase | f4130cf | 2013-06-06 16:34:33 -0700 | [diff] [blame] | 748 | * <p><b>Note:</b> this is a fairly inefficient way to access the array contents, it |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 749 | * requires generating a number of temporary objects.</p> |
| 750 | */ |
| 751 | @Override |
| 752 | public Set<K> keySet() { |
| 753 | return getCollection().getKeySet(); |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Return a {@link java.util.Collection} for iterating over and interacting with all values |
| 758 | * in the array map. |
| 759 | * |
Chet Haase | f4130cf | 2013-06-06 16:34:33 -0700 | [diff] [blame] | 760 | * <p><b>Note:</b> this is a fairly inefficient way to access the array contents, it |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 761 | * requires generating a number of temporary objects.</p> |
| 762 | */ |
| 763 | @Override |
| 764 | public Collection<V> values() { |
| 765 | return getCollection().getValues(); |
| 766 | } |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 767 | } |