| 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.NonNull; |
| 20 |
import android.os.Parcel; |
| 21 |
|
| 22 |
import com.android.internal.util.ArrayUtils; |
| 23 |
import com.android.internal.util.GrowingArrayUtils; |
| 24 |
import com.android.internal.util.Preconditions; |
| 25 |
import com.android.internal.util.function.LongObjPredicate; |
| 26 |
|
| 27 |
import libcore.util.EmptyArray; |
| 28 |
|
| 29 |
import java.util.Arrays; |
| 30 |
import java.util.Objects; |
| 31 |
|
| 32 |
|
| 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 |
|
| 59 |
|
| 60 |
public class LongSparseArray<E> implements Cloneable { |
| 61 |
private static final Object DELETED = new Object(); |
| 62 |
private boolean mGarbage = false; |
| 63 |
|
| 64 |
private long[] mKeys; |
| 65 |
private Object[] mValues; |
| 66 |
private int mSize; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public LongSparseArray() { |
| 72 |
this(10); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public LongSparseArray(int initialCapacity) { |
| 83 |
if (initialCapacity == 0) { |
| 84 |
mKeys = EmptyArray.LONG; |
| 85 |
mValues = EmptyArray.OBJECT; |
| 86 |
} else { |
| 87 |
mKeys = ArrayUtils.newUnpaddedLongArray(initialCapacity); |
| 88 |
mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity); |
| 89 |
} |
| 90 |
mSize = 0; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
@SuppressWarnings("unchecked") |
| 95 |
public LongSparseArray<E> clone() { |
| 96 |
LongSparseArray<E> clone = null; |
| 97 |
try { |
| 98 |
clone = (LongSparseArray<E>) super.clone(); |
| 99 |
clone.mKeys = mKeys.clone(); |
| 100 |
clone.mValues = mValues.clone(); |
| 101 |
} catch (CloneNotSupportedException cnse) { |
| 102 |
|
| 103 |
} |
| 104 |
return clone; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public E get(long key) { |
| 112 |
return get(key, null); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@SuppressWarnings("unchecked") |
| 120 |
public E get(long key, E valueIfKeyNotFound) { |
| 121 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 122 |
|
| 123 |
if (i < 0 || mValues[i] == DELETED) { |
| 124 |
return valueIfKeyNotFound; |
| 125 |
} else { |
| 126 |
return (E) mValues[i]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public void delete(long key) { |
| 134 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 135 |
|
| 136 |
if (i >= 0) { |
| 137 |
if (mValues[i] != DELETED) { |
| 138 |
mValues[i] = DELETED; |
| 139 |
mGarbage = true; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void remove(long key) { |
| 148 |
delete(key); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
@SuppressWarnings("unchecked") |
| 153 |
public void removeIf(@NonNull LongObjPredicate<? super E> filter) { |
| 154 |
Objects.requireNonNull(filter); |
| 155 |
for (int i = 0; i < mSize; ++i) { |
| 156 |
if (mValues[i] != DELETED && filter.test(mKeys[i], (E) mValues[i])) { |
| 157 |
mValues[i] = DELETED; |
| 158 |
mGarbage = true; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void removeAt(int index) { |
| 172 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 173 |
|
| 174 |
|
| 175 |
throw new ArrayIndexOutOfBoundsException(index); |
| 176 |
} |
| 177 |
if (mValues[index] != DELETED) { |
| 178 |
mValues[index] = DELETED; |
| 179 |
mGarbage = true; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private void gc() { |
| 184 |
|
| 185 |
|
| 186 |
int n = mSize; |
| 187 |
int o = 0; |
| 188 |
long[] keys = mKeys; |
| 189 |
Object[] values = mValues; |
| 190 |
|
| 191 |
for (int i = 0; i < n; i++) { |
| 192 |
Object val = values[i]; |
| 193 |
|
| 194 |
if (val != DELETED) { |
| 195 |
if (i != o) { |
| 196 |
keys[o] = keys[i]; |
| 197 |
values[o] = val; |
| 198 |
values[i] = null; |
| 199 |
} |
| 200 |
|
| 201 |
o++; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
mGarbage = false; |
| 206 |
mSize = o; |
| 207 |
|
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public void put(long key, E value) { |
| 217 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 218 |
|
| 219 |
if (i >= 0) { |
| 220 |
mValues[i] = value; |
| 221 |
} else { |
| 222 |
i = ~i; |
| 223 |
|
| 224 |
if (i < mSize && mValues[i] == DELETED) { |
| 225 |
mKeys[i] = key; |
| 226 |
mValues[i] = value; |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
if (mGarbage && mSize >= mKeys.length) { |
| 231 |
gc(); |
| 232 |
|
| 233 |
|
| 234 |
i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 235 |
} |
| 236 |
|
| 237 |
mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); |
| 238 |
mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); |
| 239 |
mSize++; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public int size() { |
| 248 |
if (mGarbage) { |
| 249 |
gc(); |
| 250 |
} |
| 251 |
|
| 252 |
return mSize; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public long keyAt(int index) { |
| 271 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 272 |
|
| 273 |
|
| 274 |
throw new ArrayIndexOutOfBoundsException(index); |
| 275 |
} |
| 276 |
if (mGarbage) { |
| 277 |
gc(); |
| 278 |
} |
| 279 |
|
| 280 |
return mKeys[index]; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
@SuppressWarnings("unchecked") |
| 300 |
public E valueAt(int index) { |
| 301 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 302 |
|
| 303 |
|
| 304 |
throw new ArrayIndexOutOfBoundsException(index); |
| 305 |
} |
| 306 |
if (mGarbage) { |
| 307 |
gc(); |
| 308 |
} |
| 309 |
|
| 310 |
return (E) mValues[index]; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void setValueAt(int index, E value) { |
| 324 |
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
| 325 |
|
| 326 |
|
| 327 |
throw new ArrayIndexOutOfBoundsException(index); |
| 328 |
} |
| 329 |
if (mGarbage) { |
| 330 |
gc(); |
| 331 |
} |
| 332 |
|
| 333 |
mValues[index] = value; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public int indexOfKey(long key) { |
| 342 |
if (mGarbage) { |
| 343 |
gc(); |
| 344 |
} |
| 345 |
|
| 346 |
return ContainerHelpers.binarySearch(mKeys, mSize, key); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public int indexOfValue(E value) { |
| 358 |
if (mGarbage) { |
| 359 |
gc(); |
| 360 |
} |
| 361 |
|
| 362 |
for (int i = 0; i < mSize; i++) { |
| 363 |
if (mValues[i] == value) { |
| 364 |
return i; |
| 365 |
} |
| 366 |
} |
| 367 |
return -1; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public int indexOfValueByValue(E value) { |
| 381 |
if (mGarbage) { |
| 382 |
gc(); |
| 383 |
} |
| 384 |
|
| 385 |
for (int i = 0; i < mSize; i++) { |
| 386 |
if (value == null) { |
| 387 |
if (mValues[i] == null) { |
| 388 |
return i; |
| 389 |
} |
| 390 |
} else { |
| 391 |
if (value.equals(mValues[i])) { |
| 392 |
return i; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
return -1; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public void clear() { |
| 403 |
int n = mSize; |
| 404 |
Object[] values = mValues; |
| 405 |
|
| 406 |
for (int i = 0; i < n; i++) { |
| 407 |
values[i] = null; |
| 408 |
} |
| 409 |
|
| 410 |
mSize = 0; |
| 411 |
mGarbage = false; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public void append(long key, E value) { |
| 419 |
if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 420 |
put(key, value); |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
if (mGarbage && mSize >= mKeys.length) { |
| 425 |
gc(); |
| 426 |
} |
| 427 |
|
| 428 |
mKeys = GrowingArrayUtils.append(mKeys, mSize, key); |
| 429 |
mValues = GrowingArrayUtils.append(mValues, mSize, value); |
| 430 |
mSize++; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
@Override |
| 441 |
public String toString() { |
| 442 |
if (size() <= 0) { |
| 443 |
return "{}"; |
| 444 |
} |
| 445 |
|
| 446 |
StringBuilder buffer = new StringBuilder(mSize * 28); |
| 447 |
buffer.append('{'); |
| 448 |
for (int i=0; i<mSize; i++) { |
| 449 |
if (i > 0) { |
| 450 |
buffer.append(", "); |
| 451 |
} |
| 452 |
long key = keyAt(i); |
| 453 |
buffer.append(key); |
| 454 |
buffer.append('='); |
| 455 |
Object value = valueAt(i); |
| 456 |
if (value != this) { |
| 457 |
buffer.append(value); |
| 458 |
} else { |
| 459 |
buffer.append("(this Map)"); |
| 460 |
} |
| 461 |
} |
| 462 |
buffer.append('}'); |
| 463 |
return buffer.toString(); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public static class StringParcelling implements |
| 470 |
com.android.internal.util.Parcelling<LongSparseArray<String>> { |
| 471 |
@Override |
| 472 |
public void parcel(LongSparseArray<String> array, Parcel dest, int parcelFlags) { |
| 473 |
if (array == null) { |
| 474 |
dest.writeInt(-1); |
| 475 |
return; |
| 476 |
} |
| 477 |
|
| 478 |
int size = array.mSize; |
| 479 |
|
| 480 |
dest.writeInt(size); |
| 481 |
dest.writeLongArray(array.mKeys); |
| 482 |
|
| 483 |
dest.writeStringArray(Arrays.copyOfRange(array.mValues, 0, size, String[].class)); |
| 484 |
} |
| 485 |
|
| 486 |
@Override |
| 487 |
public LongSparseArray<String> unparcel(Parcel source) { |
| 488 |
int size = source.readInt(); |
| 489 |
if (size == -1) { |
| 490 |
return null; |
| 491 |
} |
| 492 |
|
| 493 |
LongSparseArray<String> array = new LongSparseArray<>(0); |
| 494 |
array.mSize = size; |
| 495 |
array.mKeys = source.createLongArray(); |
| 496 |
array.mValues = source.createStringArray(); |
| 497 |
|
| 498 |
|
| 499 |
Preconditions.checkArgument(array.mKeys.length >= size); |
| 500 |
Preconditions.checkArgument(array.mValues.length >= size); |
| 501 |
|
| 502 |
if (size > 0) { |
| 503 |
long last = array.mKeys[0]; |
| 504 |
for (int i = 1; i < size; i++) { |
| 505 |
Preconditions.checkArgument(last < array.mKeys[i]); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
return array; |
| 510 |
} |
| 511 |
} |
| 512 |
} |