| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.ObjectInputStream; |
| 12 |
import java.io.ObjectOutputStream; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class UintMap implements Serializable |
| 25 |
{ |
| 26 |
static final long serialVersionUID = 4242698212885848444L; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public UintMap() { |
| 32 |
this(4); |
| 33 |
} |
| 34 |
|
| 35 |
public UintMap(int initialCapacity) { |
| 36 |
if (initialCapacity < 0) Kit.codeBug(); |
| 37 |
|
| 38 |
int minimalCapacity = initialCapacity * 4 / 3; |
| 39 |
int i; |
| 40 |
for (i = 2; (1 << i) < minimalCapacity; ++i) { } |
| 41 |
power = i; |
| 42 |
if (check && power < 2) Kit.codeBug(); |
| 43 |
} |
| 44 |
|
| 45 |
public boolean isEmpty() { |
| 46 |
return keyCount == 0; |
| 47 |
} |
| 48 |
|
| 49 |
public int size() { |
| 50 |
return keyCount; |
| 51 |
} |
| 52 |
|
| 53 |
public boolean has(int key) { |
| 54 |
if (key < 0) Kit.codeBug(); |
| 55 |
return 0 <= findIndex(key); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public Object getObject(int key) { |
| 63 |
if (key < 0) Kit.codeBug(); |
| 64 |
if (values != null) { |
| 65 |
int index = findIndex(key); |
| 66 |
if (0 <= index) { |
| 67 |
return values[index]; |
| 68 |
} |
| 69 |
} |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public int getInt(int key, int defaultValue) { |
| 78 |
if (key < 0) Kit.codeBug(); |
| 79 |
int index = findIndex(key); |
| 80 |
if (0 <= index) { |
| 81 |
if (ivaluesShift != 0) { |
| 82 |
return keys[ivaluesShift + index]; |
| 83 |
} |
| 84 |
return 0; |
| 85 |
} |
| 86 |
return defaultValue; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public int getExistingInt(int key) { |
| 96 |
if (key < 0) Kit.codeBug(); |
| 97 |
int index = findIndex(key); |
| 98 |
if (0 <= index) { |
| 99 |
if (ivaluesShift != 0) { |
| 100 |
return keys[ivaluesShift + index]; |
| 101 |
} |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
Kit.codeBug(); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void put(int key, Object value) { |
| 114 |
if (key < 0) Kit.codeBug(); |
| 115 |
int index = ensureIndex(key, false); |
| 116 |
if (values == null) { |
| 117 |
values = new Object[1 << power]; |
| 118 |
} |
| 119 |
values[index] = value; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public void put(int key, int value) { |
| 127 |
if (key < 0) Kit.codeBug(); |
| 128 |
int index = ensureIndex(key, true); |
| 129 |
if (ivaluesShift == 0) { |
| 130 |
int N = 1 << power; |
| 131 |
|
| 132 |
if (keys.length != N * 2) { |
| 133 |
int[] tmp = new int[N * 2]; |
| 134 |
System.arraycopy(keys, 0, tmp, 0, N); |
| 135 |
keys = tmp; |
| 136 |
} |
| 137 |
ivaluesShift = N; |
| 138 |
} |
| 139 |
keys[ivaluesShift + index] = value; |
| 140 |
} |
| 141 |
|
| 142 |
public void remove(int key) { |
| 143 |
if (key < 0) Kit.codeBug(); |
| 144 |
int index = findIndex(key); |
| 145 |
if (0 <= index) { |
| 146 |
keys[index] = DELETED; |
| 147 |
--keyCount; |
| 148 |
|
| 149 |
|
| 150 |
if (values != null) { values[index] = null; } |
| 151 |
if (ivaluesShift != 0) { keys[ivaluesShift + index] = 0; } |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public void clear() { |
| 156 |
int N = 1 << power; |
| 157 |
if (keys != null) { |
| 158 |
for (int i = 0; i != N; ++i) { |
| 159 |
keys[i] = EMPTY; |
| 160 |
} |
| 161 |
if (values != null) { |
| 162 |
for (int i = 0; i != N; ++i) { |
| 163 |
values[i] = null; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
ivaluesShift = 0; |
| 168 |
keyCount = 0; |
| 169 |
occupiedCount = 0; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public int[] getKeys() { |
| 174 |
int[] keys = this.keys; |
| 175 |
int n = keyCount; |
| 176 |
int[] result = new int[n]; |
| 177 |
for (int i = 0; n != 0; ++i) { |
| 178 |
int entry = keys[i]; |
| 179 |
if (entry != EMPTY && entry != DELETED) { |
| 180 |
result[--n] = entry; |
| 181 |
} |
| 182 |
} |
| 183 |
return result; |
| 184 |
} |
| 185 |
|
| 186 |
private static int tableLookupStep(int fraction, int mask, int power) { |
| 187 |
int shift = 32 - 2 * power; |
| 188 |
if (shift >= 0) { |
| 189 |
return ((fraction >>> shift) & mask) | 1; |
| 190 |
} |
| 191 |
else { |
| 192 |
return (fraction & (mask >>> -shift)) | 1; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
private int findIndex(int key) { |
| 197 |
int[] keys = this.keys; |
| 198 |
if (keys != null) { |
| 199 |
int fraction = key * A; |
| 200 |
int index = fraction >>> (32 - power); |
| 201 |
int entry = keys[index]; |
| 202 |
if (entry == key) { return index; } |
| 203 |
if (entry != EMPTY) { |
| 204 |
|
| 205 |
int mask = (1 << power) - 1; |
| 206 |
int step = tableLookupStep(fraction, mask, power); |
| 207 |
int n = 0; |
| 208 |
do { |
| 209 |
if (check) { |
| 210 |
if (n >= occupiedCount) Kit.codeBug(); |
| 211 |
++n; |
| 212 |
} |
| 213 |
index = (index + step) & mask; |
| 214 |
entry = keys[index]; |
| 215 |
if (entry == key) { return index; } |
| 216 |
} while (entry != EMPTY); |
| 217 |
} |
| 218 |
} |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private int insertNewKey(int key) { |
| 225 |
if (check && occupiedCount != keyCount) Kit.codeBug(); |
| 226 |
if (check && keyCount == 1 << power) Kit.codeBug(); |
| 227 |
int[] keys = this.keys; |
| 228 |
int fraction = key * A; |
| 229 |
int index = fraction >>> (32 - power); |
| 230 |
if (keys[index] != EMPTY) { |
| 231 |
int mask = (1 << power) - 1; |
| 232 |
int step = tableLookupStep(fraction, mask, power); |
| 233 |
int firstIndex = index; |
| 234 |
do { |
| 235 |
if (check && keys[index] == DELETED) Kit.codeBug(); |
| 236 |
index = (index + step) & mask; |
| 237 |
if (check && firstIndex == index) Kit.codeBug(); |
| 238 |
} while (keys[index] != EMPTY); |
| 239 |
} |
| 240 |
keys[index] = key; |
| 241 |
++occupiedCount; |
| 242 |
++keyCount; |
| 243 |
return index; |
| 244 |
} |
| 245 |
|
| 246 |
private void rehashTable(boolean ensureIntSpace) { |
| 247 |
if (keys != null) { |
| 248 |
|
| 249 |
if (keyCount * 2 >= occupiedCount) { |
| 250 |
|
| 251 |
++power; |
| 252 |
} |
| 253 |
} |
| 254 |
int N = 1 << power; |
| 255 |
int[] old = keys; |
| 256 |
int oldShift = ivaluesShift; |
| 257 |
if (oldShift == 0 && !ensureIntSpace) { |
| 258 |
keys = new int[N]; |
| 259 |
} |
| 260 |
else { |
| 261 |
ivaluesShift = N; keys = new int[N * 2]; |
| 262 |
} |
| 263 |
for (int i = 0; i != N; ++i) { keys[i] = EMPTY; } |
| 264 |
|
| 265 |
Object[] oldValues = values; |
| 266 |
if (oldValues != null) { values = new Object[N]; } |
| 267 |
|
| 268 |
int oldCount = keyCount; |
| 269 |
occupiedCount = 0; |
| 270 |
if (oldCount != 0) { |
| 271 |
keyCount = 0; |
| 272 |
for (int i = 0, remaining = oldCount; remaining != 0; ++i) { |
| 273 |
int key = old[i]; |
| 274 |
if (key != EMPTY && key != DELETED) { |
| 275 |
int index = insertNewKey(key); |
| 276 |
if (oldValues != null) { |
| 277 |
values[index] = oldValues[i]; |
| 278 |
} |
| 279 |
if (oldShift != 0) { |
| 280 |
keys[ivaluesShift + index] = old[oldShift + i]; |
| 281 |
} |
| 282 |
--remaining; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
private int ensureIndex(int key, boolean intType) { |
| 290 |
int index = -1; |
| 291 |
int firstDeleted = -1; |
| 292 |
int[] keys = this.keys; |
| 293 |
if (keys != null) { |
| 294 |
int fraction = key * A; |
| 295 |
index = fraction >>> (32 - power); |
| 296 |
int entry = keys[index]; |
| 297 |
if (entry == key) { return index; } |
| 298 |
if (entry != EMPTY) { |
| 299 |
if (entry == DELETED) { firstDeleted = index; } |
| 300 |
|
| 301 |
int mask = (1 << power) - 1; |
| 302 |
int step = tableLookupStep(fraction, mask, power); |
| 303 |
int n = 0; |
| 304 |
do { |
| 305 |
if (check) { |
| 306 |
if (n >= occupiedCount) Kit.codeBug(); |
| 307 |
++n; |
| 308 |
} |
| 309 |
index = (index + step) & mask; |
| 310 |
entry = keys[index]; |
| 311 |
if (entry == key) { return index; } |
| 312 |
if (entry == DELETED && firstDeleted < 0) { |
| 313 |
firstDeleted = index; |
| 314 |
} |
| 315 |
} while (entry != EMPTY); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (check && keys != null && keys[index] != EMPTY) |
| 320 |
Kit.codeBug(); |
| 321 |
if (firstDeleted >= 0) { |
| 322 |
index = firstDeleted; |
| 323 |
} |
| 324 |
else { |
| 325 |
|
| 326 |
if (keys == null || occupiedCount * 4 >= (1 << power) * 3) { |
| 327 |
|
| 328 |
rehashTable(intType); |
| 329 |
return insertNewKey(key); |
| 330 |
} |
| 331 |
++occupiedCount; |
| 332 |
} |
| 333 |
keys[index] = key; |
| 334 |
++keyCount; |
| 335 |
return index; |
| 336 |
} |
| 337 |
|
| 338 |
private void writeObject(ObjectOutputStream out) |
| 339 |
throws IOException |
| 340 |
{ |
| 341 |
out.defaultWriteObject(); |
| 342 |
|
| 343 |
int count = keyCount; |
| 344 |
if (count != 0) { |
| 345 |
boolean hasIntValues = (ivaluesShift != 0); |
| 346 |
boolean hasObjectValues = (values != null); |
| 347 |
out.writeBoolean(hasIntValues); |
| 348 |
out.writeBoolean(hasObjectValues); |
| 349 |
|
| 350 |
for (int i = 0; count != 0; ++i) { |
| 351 |
int key = keys[i]; |
| 352 |
if (key != EMPTY && key != DELETED) { |
| 353 |
--count; |
| 354 |
out.writeInt(key); |
| 355 |
if (hasIntValues) { |
| 356 |
out.writeInt(keys[ivaluesShift + i]); |
| 357 |
} |
| 358 |
if (hasObjectValues) { |
| 359 |
out.writeObject(values[i]); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
private void readObject(ObjectInputStream in) |
| 367 |
throws IOException, ClassNotFoundException |
| 368 |
{ |
| 369 |
in.defaultReadObject(); |
| 370 |
|
| 371 |
int writtenKeyCount = keyCount; |
| 372 |
if (writtenKeyCount != 0) { |
| 373 |
keyCount = 0; |
| 374 |
boolean hasIntValues = in.readBoolean(); |
| 375 |
boolean hasObjectValues = in.readBoolean(); |
| 376 |
|
| 377 |
int N = 1 << power; |
| 378 |
if (hasIntValues) { |
| 379 |
keys = new int[2 * N]; |
| 380 |
ivaluesShift = N; |
| 381 |
}else { |
| 382 |
keys = new int[N]; |
| 383 |
} |
| 384 |
for (int i = 0; i != N; ++i) { |
| 385 |
keys[i] = EMPTY; |
| 386 |
} |
| 387 |
if (hasObjectValues) { |
| 388 |
values = new Object[N]; |
| 389 |
} |
| 390 |
for (int i = 0; i != writtenKeyCount; ++i) { |
| 391 |
int key = in.readInt(); |
| 392 |
int index = insertNewKey(key); |
| 393 |
if (hasIntValues) { |
| 394 |
int ivalue = in.readInt(); |
| 395 |
keys[ivaluesShift + index] = ivalue; |
| 396 |
} |
| 397 |
if (hasObjectValues) { |
| 398 |
values[index] = in.readObject(); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
private static final int A = 0x9e3779b9; |
| 407 |
|
| 408 |
private static final int EMPTY = -1; |
| 409 |
private static final int DELETED = -2; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
private transient int[] keys; |
| 417 |
private transient Object[] values; |
| 418 |
|
| 419 |
private int power; |
| 420 |
private int keyCount; |
| 421 |
private transient int occupiedCount; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
private transient int ivaluesShift; |
| 426 |
|
| 427 |
|
| 428 |
private static final boolean check = false; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
} |