| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
|
| 22 |
import com.android.internal.util.ArrayUtils; |
| 23 |
|
| 24 |
import libcore.util.EmptyArray; |
| 25 |
|
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Collection; |
| 28 |
import java.util.ConcurrentModificationException; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.Set; |
| 31 |
import java.util.function.BiConsumer; |
| 32 |
import java.util.function.BiFunction; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public final class ArrayMap<K, V> implements Map<K, V> { |
| 59 |
private static final boolean DEBUG = false; |
| 60 |
private static final String TAG = "ArrayMap"; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static final boolean CONCURRENT_MODIFICATION_EXCEPTIONS = true; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private static final int BASE_SIZE = 4; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 84 |
private static final int CACHE_SIZE = 10; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 90 |
static final int[] EMPTY_IMMUTABLE_INTS = new int[0]; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 96 |
public static final ArrayMap EMPTY = new ArrayMap<>(-1); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 105 |
static Object[] mBaseCache; |
| 106 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 107 |
static int mBaseCacheSize; |
| 108 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 109 |
static Object[] mTwiceBaseCache; |
| 110 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 111 |
static int mTwiceBaseCacheSize; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private static final Object sBaseCacheLock = new Object(); |
| 117 |
private static final Object sTwiceBaseCacheLock = new Object(); |
| 118 |
|
| 119 |
private final boolean mIdentityHashCode; |
| 120 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 121 |
int[] mHashes; |
| 122 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 123 |
Object[] mArray; |
| 124 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 125 |
int mSize; |
| 126 |
private MapCollections<K, V> mCollections; |
| 127 |
|
| 128 |
private static int binarySearchHashes(int[] hashes, int N, int hash) { |
| 129 |
try { |
| 130 |
return ContainerHelpers.binarySearch(hashes, N, hash); |
| 131 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 132 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS) { |
| 133 |
throw new ConcurrentModificationException(); |
| 134 |
} else { |
| 135 |
throw e; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 141 |
int indexOf(Object key, int hash) { |
| 142 |
final int N = mSize; |
| 143 |
|
| 144 |
|
| 145 |
if (N == 0) { |
| 146 |
return ~0; |
| 147 |
} |
| 148 |
|
| 149 |
int index = binarySearchHashes(mHashes, N, hash); |
| 150 |
|
| 151 |
|
| 152 |
if (index < 0) { |
| 153 |
return index; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
if (key.equals(mArray[index<<1])) { |
| 158 |
return index; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
int end; |
| 163 |
for (end = index + 1; end < N && mHashes[end] == hash; end++) { |
| 164 |
if (key.equals(mArray[end << 1])) return end; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) { |
| 169 |
if (key.equals(mArray[i << 1])) return i; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
return ~end; |
| 177 |
} |
| 178 |
|
| 179 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 180 |
int indexOfNull() { |
| 181 |
final int N = mSize; |
| 182 |
|
| 183 |
|
| 184 |
if (N == 0) { |
| 185 |
return ~0; |
| 186 |
} |
| 187 |
|
| 188 |
int index = binarySearchHashes(mHashes, N, 0); |
| 189 |
|
| 190 |
|
| 191 |
if (index < 0) { |
| 192 |
return index; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
if (null == mArray[index<<1]) { |
| 197 |
return index; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
int end; |
| 202 |
for (end = index + 1; end < N && mHashes[end] == 0; end++) { |
| 203 |
if (null == mArray[end << 1]) return end; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
for (int i = index - 1; i >= 0 && mHashes[i] == 0; i--) { |
| 208 |
if (null == mArray[i << 1]) return i; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
return ~end; |
| 216 |
} |
| 217 |
|
| 218 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 219 |
private void allocArrays(final int size) { |
| 220 |
if (mHashes == EMPTY_IMMUTABLE_INTS) { |
| 221 |
throw new UnsupportedOperationException("ArrayMap is immutable"); |
| 222 |
} |
| 223 |
if (size == (BASE_SIZE*2)) { |
| 224 |
synchronized (sTwiceBaseCacheLock) { |
| 225 |
if (mTwiceBaseCache != null) { |
| 226 |
final Object[] array = mTwiceBaseCache; |
| 227 |
mArray = array; |
| 228 |
try { |
| 229 |
mTwiceBaseCache = (Object[]) array[0]; |
| 230 |
mHashes = (int[]) array[1]; |
| 231 |
if (mHashes != null) { |
| 232 |
array[0] = array[1] = null; |
| 233 |
mTwiceBaseCacheSize--; |
| 234 |
if (DEBUG) { |
| 235 |
Log.d(TAG, "Retrieving 2x cache " + Arrays.toString(mHashes) |
| 236 |
+ " now have " + mTwiceBaseCacheSize + " entries"); |
| 237 |
} |
| 238 |
return; |
| 239 |
} |
| 240 |
} catch (ClassCastException e) { |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
Slog.wtf(TAG, "Found corrupt ArrayMap cache: [0]=" + array[0] |
| 245 |
+ " [1]=" + array[1]); |
| 246 |
mTwiceBaseCache = null; |
| 247 |
mTwiceBaseCacheSize = 0; |
| 248 |
} |
| 249 |
} |
| 250 |
} else if (size == BASE_SIZE) { |
| 251 |
synchronized (sBaseCacheLock) { |
| 252 |
if (mBaseCache != null) { |
| 253 |
final Object[] array = mBaseCache; |
| 254 |
mArray = array; |
| 255 |
try { |
| 256 |
mBaseCache = (Object[]) array[0]; |
| 257 |
mHashes = (int[]) array[1]; |
| 258 |
if (mHashes != null) { |
| 259 |
array[0] = array[1] = null; |
| 260 |
mBaseCacheSize--; |
| 261 |
if (DEBUG) { |
| 262 |
Log.d(TAG, "Retrieving 1x cache " + Arrays.toString(mHashes) |
| 263 |
+ " now have " + mBaseCacheSize + " entries"); |
| 264 |
} |
| 265 |
return; |
| 266 |
} |
| 267 |
} catch (ClassCastException e) { |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
Slog.wtf(TAG, "Found corrupt ArrayMap cache: [0]=" + array[0] |
| 272 |
+ " [1]=" + array[1]); |
| 273 |
mBaseCache = null; |
| 274 |
mBaseCacheSize = 0; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
mHashes = new int[size]; |
| 280 |
mArray = new Object[size<<1]; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 288 |
private static void freeArrays(final int[] hashes, final Object[] array, final int size) { |
| 289 |
if (hashes.length == (BASE_SIZE*2)) { |
| 290 |
synchronized (sTwiceBaseCacheLock) { |
| 291 |
if (mTwiceBaseCacheSize < CACHE_SIZE) { |
| 292 |
array[0] = mTwiceBaseCache; |
| 293 |
array[1] = hashes; |
| 294 |
for (int i=(size<<1)-1; i>=2; i--) { |
| 295 |
array[i] = null; |
| 296 |
} |
| 297 |
mTwiceBaseCache = array; |
| 298 |
mTwiceBaseCacheSize++; |
| 299 |
if (DEBUG) { |
| 300 |
Log.d(TAG, "Storing 2x cache " + Arrays.toString(array) |
| 301 |
+ " now have " + mTwiceBaseCacheSize + " entries"); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} else if (hashes.length == BASE_SIZE) { |
| 306 |
synchronized (sBaseCacheLock) { |
| 307 |
if (mBaseCacheSize < CACHE_SIZE) { |
| 308 |
array[0] = mBaseCache; |
| 309 |
array[1] = hashes; |
| 310 |
for (int i=(size<<1)-1; i>=2; i--) { |
| 311 |
array[i] = null; |
| 312 |
} |
| 313 |
mBaseCache = array; |
| 314 |
mBaseCacheSize++; |
| 315 |
if (DEBUG) { |
| 316 |
Log.d(TAG, "Storing 1x cache " + Arrays.toString(array) |
| 317 |
+ " now have " + mBaseCacheSize + " entries"); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public ArrayMap() { |
| 329 |
this(0, false); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public ArrayMap(int capacity) { |
| 336 |
this(capacity, false); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
public ArrayMap(int capacity, boolean identityHashCode) { |
| 341 |
mIdentityHashCode = identityHashCode; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (capacity < 0) { |
| 347 |
mHashes = EMPTY_IMMUTABLE_INTS; |
| 348 |
mArray = EmptyArray.OBJECT; |
| 349 |
} else if (capacity == 0) { |
| 350 |
mHashes = EmptyArray.INT; |
| 351 |
mArray = EmptyArray.OBJECT; |
| 352 |
} else { |
| 353 |
allocArrays(capacity); |
| 354 |
} |
| 355 |
mSize = 0; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public ArrayMap(ArrayMap<K, V> map) { |
| 362 |
this(); |
| 363 |
if (map != null) { |
| 364 |
putAll(map); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
@Override |
| 372 |
public void clear() { |
| 373 |
if (mSize > 0) { |
| 374 |
final int[] ohashes = mHashes; |
| 375 |
final Object[] oarray = mArray; |
| 376 |
final int osize = mSize; |
| 377 |
mHashes = EmptyArray.INT; |
| 378 |
mArray = EmptyArray.OBJECT; |
| 379 |
mSize = 0; |
| 380 |
freeArrays(ohashes, oarray, osize); |
| 381 |
} |
| 382 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS && mSize > 0) { |
| 383 |
throw new ConcurrentModificationException(); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public void erase() { |
| 392 |
if (mSize > 0) { |
| 393 |
final int N = mSize<<1; |
| 394 |
final Object[] array = mArray; |
| 395 |
for (int i=0; i<N; i++) { |
| 396 |
array[i] = null; |
| 397 |
} |
| 398 |
mSize = 0; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public void ensureCapacity(int minimumCapacity) { |
| 407 |
final int osize = mSize; |
| 408 |
if (mHashes.length < minimumCapacity) { |
| 409 |
final int[] ohashes = mHashes; |
| 410 |
final Object[] oarray = mArray; |
| 411 |
allocArrays(minimumCapacity); |
| 412 |
if (mSize > 0) { |
| 413 |
System.arraycopy(ohashes, 0, mHashes, 0, osize); |
| 414 |
System.arraycopy(oarray, 0, mArray, 0, osize<<1); |
| 415 |
} |
| 416 |
freeArrays(ohashes, oarray, osize); |
| 417 |
} |
| 418 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS && mSize != osize) { |
| 419 |
throw new ConcurrentModificationException(); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
@Override |
| 430 |
public boolean containsKey(Object key) { |
| 431 |
return indexOfKey(key) >= 0; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public int indexOfKey(Object key) { |
| 441 |
return key == null ? indexOfNull() |
| 442 |
: indexOf(key, mIdentityHashCode ? System.identityHashCode(key) : key.hashCode()); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public int indexOfValue(Object value) { |
| 454 |
final int N = mSize*2; |
| 455 |
final Object[] array = mArray; |
| 456 |
if (value == null) { |
| 457 |
for (int i=1; i<N; i+=2) { |
| 458 |
if (array[i] == null) { |
| 459 |
return i>>1; |
| 460 |
} |
| 461 |
} |
| 462 |
} else { |
| 463 |
for (int i=1; i<N; i+=2) { |
| 464 |
if (value.equals(array[i])) { |
| 465 |
return i>>1; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
return -1; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
@Override |
| 480 |
public boolean containsValue(Object value) { |
| 481 |
return indexOfValue(value) >= 0; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
@Override |
| 491 |
public V get(Object key) { |
| 492 |
final int index = indexOfKey(key); |
| 493 |
return index >= 0 ? (V)mArray[(index<<1)+1] : null; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public K keyAt(int index) { |
| 508 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 509 |
|
| 510 |
|
| 511 |
throw new ArrayIndexOutOfBoundsException(index); |
| 512 |
} |
| 513 |
return (K)mArray[index << 1]; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public V valueAt(int index) { |
| 528 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 529 |
|
| 530 |
|
| 531 |
throw new ArrayIndexOutOfBoundsException(index); |
| 532 |
} |
| 533 |
return (V)mArray[(index << 1) + 1]; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
public V setValueAt(int index, V value) { |
| 549 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 550 |
|
| 551 |
|
| 552 |
throw new ArrayIndexOutOfBoundsException(index); |
| 553 |
} |
| 554 |
index = (index << 1) + 1; |
| 555 |
V old = (V)mArray[index]; |
| 556 |
mArray[index] = value; |
| 557 |
return old; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
@Override |
| 564 |
public boolean isEmpty() { |
| 565 |
return mSize <= 0; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
@Override |
| 577 |
public V put(K key, V value) { |
| 578 |
final int osize = mSize; |
| 579 |
final int hash; |
| 580 |
int index; |
| 581 |
if (key == null) { |
| 582 |
hash = 0; |
| 583 |
index = indexOfNull(); |
| 584 |
} else { |
| 585 |
hash = mIdentityHashCode ? System.identityHashCode(key) : key.hashCode(); |
| 586 |
index = indexOf(key, hash); |
| 587 |
} |
| 588 |
if (index >= 0) { |
| 589 |
index = (index<<1) + 1; |
| 590 |
final V old = (V)mArray[index]; |
| 591 |
mArray[index] = value; |
| 592 |
return old; |
| 593 |
} |
| 594 |
|
| 595 |
index = ~index; |
| 596 |
if (osize >= mHashes.length) { |
| 597 |
final int n = osize >= (BASE_SIZE*2) ? (osize+(osize>>1)) |
| 598 |
: (osize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE); |
| 599 |
|
| 600 |
if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n); |
| 601 |
|
| 602 |
final int[] ohashes = mHashes; |
| 603 |
final Object[] oarray = mArray; |
| 604 |
allocArrays(n); |
| 605 |
|
| 606 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS && osize != mSize) { |
| 607 |
throw new ConcurrentModificationException(); |
| 608 |
} |
| 609 |
|
| 610 |
if (mHashes.length > 0) { |
| 611 |
if (DEBUG) Log.d(TAG, "put: copy 0-" + osize + " to 0"); |
| 612 |
System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length); |
| 613 |
System.arraycopy(oarray, 0, mArray, 0, oarray.length); |
| 614 |
} |
| 615 |
|
| 616 |
freeArrays(ohashes, oarray, osize); |
| 617 |
} |
| 618 |
|
| 619 |
if (index < osize) { |
| 620 |
if (DEBUG) Log.d(TAG, "put: move " + index + "-" + (osize-index) |
| 621 |
+ " to " + (index+1)); |
| 622 |
System.arraycopy(mHashes, index, mHashes, index + 1, osize - index); |
| 623 |
System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1); |
| 624 |
} |
| 625 |
|
| 626 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS) { |
| 627 |
if (osize != mSize || index >= mHashes.length) { |
| 628 |
throw new ConcurrentModificationException(); |
| 629 |
} |
| 630 |
} |
| 631 |
mHashes[index] = hash; |
| 632 |
mArray[index<<1] = key; |
| 633 |
mArray[(index<<1)+1] = value; |
| 634 |
mSize++; |
| 635 |
return null; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
@UnsupportedAppUsage(maxTargetSdk = 28) |
| 644 |
public void append(K key, V value) { |
| 645 |
int index = mSize; |
| 646 |
final int hash = key == null ? 0 |
| 647 |
: (mIdentityHashCode ? System.identityHashCode(key) : key.hashCode()); |
| 648 |
if (index >= mHashes.length) { |
| 649 |
throw new IllegalStateException("Array is full"); |
| 650 |
} |
| 651 |
if (index > 0 && mHashes[index-1] > hash) { |
| 652 |
RuntimeException e = new RuntimeException("here"); |
| 653 |
e.fillInStackTrace(); |
| 654 |
Log.w(TAG, "New hash " + hash |
| 655 |
+ " is before end of array hash " + mHashes[index-1] |
| 656 |
+ " at index " + index + (DEBUG ? " key " + key : ""), e); |
| 657 |
put(key, value); |
| 658 |
return; |
| 659 |
} |
| 660 |
mSize = index+1; |
| 661 |
mHashes[index] = hash; |
| 662 |
index <<= 1; |
| 663 |
mArray[index] = key; |
| 664 |
mArray[index+1] = value; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
public void validate() { |
| 676 |
final int N = mSize; |
| 677 |
if (N <= 1) { |
| 678 |
|
| 679 |
return; |
| 680 |
} |
| 681 |
int basehash = mHashes[0]; |
| 682 |
int basei = 0; |
| 683 |
for (int i=1; i<N; i++) { |
| 684 |
int hash = mHashes[i]; |
| 685 |
if (hash != basehash) { |
| 686 |
basehash = hash; |
| 687 |
basei = i; |
| 688 |
continue; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
final Object cur = mArray[i<<1]; |
| 693 |
for (int j=i-1; j>=basei; j--) { |
| 694 |
final Object prev = mArray[j<<1]; |
| 695 |
if (cur == prev) { |
| 696 |
throw new IllegalArgumentException("Duplicate key in ArrayMap: " + cur); |
| 697 |
} |
| 698 |
if (cur != null && prev != null && cur.equals(prev)) { |
| 699 |
throw new IllegalArgumentException("Duplicate key in ArrayMap: " + cur); |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
public void putAll(ArrayMap<? extends K, ? extends V> array) { |
| 710 |
final int N = array.mSize; |
| 711 |
ensureCapacity(mSize + N); |
| 712 |
if (mSize == 0) { |
| 713 |
if (N > 0) { |
| 714 |
System.arraycopy(array.mHashes, 0, mHashes, 0, N); |
| 715 |
System.arraycopy(array.mArray, 0, mArray, 0, N<<1); |
| 716 |
mSize = N; |
| 717 |
} |
| 718 |
} else { |
| 719 |
for (int i=0; i<N; i++) { |
| 720 |
put(array.keyAt(i), array.valueAt(i)); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
@Override |
| 732 |
public V remove(Object key) { |
| 733 |
final int index = indexOfKey(key); |
| 734 |
if (index >= 0) { |
| 735 |
return removeAt(index); |
| 736 |
} |
| 737 |
|
| 738 |
return null; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
public V removeAt(int index) { |
| 753 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 754 |
|
| 755 |
|
| 756 |
throw new ArrayIndexOutOfBoundsException(index); |
| 757 |
} |
| 758 |
|
| 759 |
final Object old = mArray[(index << 1) + 1]; |
| 760 |
final int osize = mSize; |
| 761 |
final int nsize; |
| 762 |
if (osize <= 1) { |
| 763 |
|
| 764 |
if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0"); |
| 765 |
final int[] ohashes = mHashes; |
| 766 |
final Object[] oarray = mArray; |
| 767 |
mHashes = EmptyArray.INT; |
| 768 |
mArray = EmptyArray.OBJECT; |
| 769 |
freeArrays(ohashes, oarray, osize); |
| 770 |
nsize = 0; |
| 771 |
} else { |
| 772 |
nsize = osize - 1; |
| 773 |
if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) { |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
final int n = osize > (BASE_SIZE*2) ? (osize + (osize>>1)) : (BASE_SIZE*2); |
| 778 |
|
| 779 |
if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n); |
| 780 |
|
| 781 |
final int[] ohashes = mHashes; |
| 782 |
final Object[] oarray = mArray; |
| 783 |
allocArrays(n); |
| 784 |
|
| 785 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS && osize != mSize) { |
| 786 |
throw new ConcurrentModificationException(); |
| 787 |
} |
| 788 |
|
| 789 |
if (index > 0) { |
| 790 |
if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0"); |
| 791 |
System.arraycopy(ohashes, 0, mHashes, 0, index); |
| 792 |
System.arraycopy(oarray, 0, mArray, 0, index << 1); |
| 793 |
} |
| 794 |
if (index < nsize) { |
| 795 |
if (DEBUG) Log.d(TAG, "remove: copy from " + (index+1) + "-" + nsize |
| 796 |
+ " to " + index); |
| 797 |
System.arraycopy(ohashes, index + 1, mHashes, index, nsize - index); |
| 798 |
System.arraycopy(oarray, (index + 1) << 1, mArray, index << 1, |
| 799 |
(nsize - index) << 1); |
| 800 |
} |
| 801 |
} else { |
| 802 |
if (index < nsize) { |
| 803 |
if (DEBUG) Log.d(TAG, "remove: move " + (index+1) + "-" + nsize |
| 804 |
+ " to " + index); |
| 805 |
System.arraycopy(mHashes, index + 1, mHashes, index, nsize - index); |
| 806 |
System.arraycopy(mArray, (index + 1) << 1, mArray, index << 1, |
| 807 |
(nsize - index) << 1); |
| 808 |
} |
| 809 |
mArray[nsize << 1] = null; |
| 810 |
mArray[(nsize << 1) + 1] = null; |
| 811 |
} |
| 812 |
} |
| 813 |
if (CONCURRENT_MODIFICATION_EXCEPTIONS && osize != mSize) { |
| 814 |
throw new ConcurrentModificationException(); |
| 815 |
} |
| 816 |
mSize = nsize; |
| 817 |
return (V)old; |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
@Override |
| 824 |
public int size() { |
| 825 |
return mSize; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
@Override |
| 837 |
public boolean equals(@Nullable Object object) { |
| 838 |
if (this == object) { |
| 839 |
return true; |
| 840 |
} |
| 841 |
if (object instanceof Map) { |
| 842 |
Map<?, ?> map = (Map<?, ?>) object; |
| 843 |
if (size() != map.size()) { |
| 844 |
return false; |
| 845 |
} |
| 846 |
|
| 847 |
try { |
| 848 |
for (int i=0; i<mSize; i++) { |
| 849 |
K key = keyAt(i); |
| 850 |
V mine = valueAt(i); |
| 851 |
Object theirs = map.get(key); |
| 852 |
if (mine == null) { |
| 853 |
if (theirs != null || !map.containsKey(key)) { |
| 854 |
return false; |
| 855 |
} |
| 856 |
} else if (!mine.equals(theirs)) { |
| 857 |
return false; |
| 858 |
} |
| 859 |
} |
| 860 |
} catch (NullPointerException ignored) { |
| 861 |
return false; |
| 862 |
} catch (ClassCastException ignored) { |
| 863 |
return false; |
| 864 |
} |
| 865 |
return true; |
| 866 |
} |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
@Override |
| 874 |
public int hashCode() { |
| 875 |
final int[] hashes = mHashes; |
| 876 |
final Object[] array = mArray; |
| 877 |
int result = 0; |
| 878 |
for (int i = 0, v = 1, s = mSize; i < s; i++, v+=2) { |
| 879 |
Object value = array[v]; |
| 880 |
result += hashes[i] ^ (value == null ? 0 : value.hashCode()); |
| 881 |
} |
| 882 |
return result; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
@Override |
| 893 |
public String toString() { |
| 894 |
if (isEmpty()) { |
| 895 |
return "{}"; |
| 896 |
} |
| 897 |
|
| 898 |
StringBuilder buffer = new StringBuilder(mSize * 28); |
| 899 |
buffer.append('{'); |
| 900 |
for (int i=0; i<mSize; i++) { |
| 901 |
if (i > 0) { |
| 902 |
buffer.append(", "); |
| 903 |
} |
| 904 |
Object key = keyAt(i); |
| 905 |
if (key != this) { |
| 906 |
buffer.append(key); |
| 907 |
} else { |
| 908 |
buffer.append("(this Map)"); |
| 909 |
} |
| 910 |
buffer.append('='); |
| 911 |
Object value = valueAt(i); |
| 912 |
if (value != this) { |
| 913 |
buffer.append(ArrayUtils.deepToString(value)); |
| 914 |
} else { |
| 915 |
buffer.append("(this Map)"); |
| 916 |
} |
| 917 |
} |
| 918 |
buffer.append('}'); |
| 919 |
return buffer.toString(); |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
private MapCollections<K, V> getCollection() { |
| 928 |
if (mCollections == null) { |
| 929 |
mCollections = new MapCollections<K, V>() { |
| 930 |
@Override |
| 931 |
protected int colGetSize() { |
| 932 |
return mSize; |
| 933 |
} |
| 934 |
|
| 935 |
@Override |
| 936 |
protected Object colGetEntry(int index, int offset) { |
| 937 |
return mArray[(index<<1) + offset]; |
| 938 |
} |
| 939 |
|
| 940 |
@Override |
| 941 |
protected int colIndexOfKey(Object key) { |
| 942 |
return indexOfKey(key); |
| 943 |
} |
| 944 |
|
| 945 |
@Override |
| 946 |
protected int colIndexOfValue(Object value) { |
| 947 |
return indexOfValue(value); |
| 948 |
} |
| 949 |
|
| 950 |
@Override |
| 951 |
protected Map<K, V> colGetMap() { |
| 952 |
return ArrayMap.this; |
| 953 |
} |
| 954 |
|
| 955 |
@Override |
| 956 |
protected void colPut(K key, V value) { |
| 957 |
put(key, value); |
| 958 |
} |
| 959 |
|
| 960 |
@Override |
| 961 |
protected V colSetValue(int index, V value) { |
| 962 |
return setValueAt(index, value); |
| 963 |
} |
| 964 |
|
| 965 |
@Override |
| 966 |
protected void colRemoveAt(int index) { |
| 967 |
removeAt(index); |
| 968 |
} |
| 969 |
|
| 970 |
@Override |
| 971 |
protected void colClear() { |
| 972 |
clear(); |
| 973 |
} |
| 974 |
}; |
| 975 |
} |
| 976 |
return mCollections; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
public boolean containsAll(Collection<?> collection) { |
| 986 |
return MapCollections.containsAllHelper(this, collection); |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
@Override |
| 997 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
| 998 |
if (action == null) { |
| 999 |
throw new NullPointerException("action must not be null"); |
| 1000 |
} |
| 1001 |
|
| 1002 |
final int size = mSize; |
| 1003 |
for (int i = 0; i < size; ++i) { |
| 1004 |
if (size != mSize) { |
| 1005 |
throw new ConcurrentModificationException(); |
| 1006 |
} |
| 1007 |
action.accept(keyAt(i), valueAt(i)); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
@Override |
| 1016 |
public void putAll(Map<? extends K, ? extends V> map) { |
| 1017 |
ensureCapacity(mSize + map.size()); |
| 1018 |
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { |
| 1019 |
put(entry.getKey(), entry.getValue()); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
public boolean removeAll(Collection<?> collection) { |
| 1029 |
return MapCollections.removeAllHelper(this, collection); |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
@Override |
| 1042 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
| 1043 |
if (function == null) { |
| 1044 |
throw new NullPointerException("function must not be null"); |
| 1045 |
} |
| 1046 |
|
| 1047 |
final int size = mSize; |
| 1048 |
try { |
| 1049 |
for (int i = 0; i < size; ++i) { |
| 1050 |
final int valIndex = (i << 1) + 1; |
| 1051 |
|
| 1052 |
mArray[valIndex] = function.apply((K) mArray[i << 1], (V) mArray[valIndex]); |
| 1053 |
} |
| 1054 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 1055 |
throw new ConcurrentModificationException(); |
| 1056 |
} |
| 1057 |
if (size != mSize) { |
| 1058 |
throw new ConcurrentModificationException(); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
public boolean retainAll(Collection<?> collection) { |
| 1069 |
return MapCollections.retainAllHelper(this, collection); |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
@Override |
| 1087 |
public Set<Map.Entry<K, V>> entrySet() { |
| 1088 |
return getCollection().getEntrySet(); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
@Override |
| 1100 |
public Set<K> keySet() { |
| 1101 |
return getCollection().getKeySet(); |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
@Override |
| 1113 |
public Collection<V> values() { |
| 1114 |
return getCollection().getValues(); |
| 1115 |
} |
| 1116 |
} |