| 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 ObjToIntMap implements Serializable |
| 25 |
{ |
| 26 |
static final long serialVersionUID = -1542220580748809402L; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static class Iterator { |
| 34 |
|
| 35 |
Iterator(ObjToIntMap master) { |
| 36 |
this.master = master; |
| 37 |
} |
| 38 |
|
| 39 |
final void init(Object[] keys, int[] values, int keyCount) { |
| 40 |
this.keys = keys; |
| 41 |
this.values = values; |
| 42 |
this.cursor = -1; |
| 43 |
this.remaining = keyCount; |
| 44 |
} |
| 45 |
|
| 46 |
public void start() { |
| 47 |
master.initIterator(this); |
| 48 |
next(); |
| 49 |
} |
| 50 |
|
| 51 |
public boolean done() { |
| 52 |
return remaining < 0; |
| 53 |
} |
| 54 |
|
| 55 |
public void next() { |
| 56 |
if (remaining == -1) Kit.codeBug(); |
| 57 |
if (remaining == 0) { |
| 58 |
remaining = -1; |
| 59 |
cursor = -1; |
| 60 |
}else { |
| 61 |
for (++cursor; ; ++cursor) { |
| 62 |
Object key = keys[cursor]; |
| 63 |
if (key != null && key != DELETED) { |
| 64 |
--remaining; |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public Object getKey() { |
| 72 |
Object key = keys[cursor]; |
| 73 |
if (key == UniqueTag.NULL_VALUE) { key = null; } |
| 74 |
return key; |
| 75 |
} |
| 76 |
|
| 77 |
public int getValue() { |
| 78 |
return values[cursor]; |
| 79 |
} |
| 80 |
|
| 81 |
public void setValue(int value) { |
| 82 |
values[cursor] = value; |
| 83 |
} |
| 84 |
|
| 85 |
ObjToIntMap master; |
| 86 |
private int cursor; |
| 87 |
private int remaining; |
| 88 |
private Object[] keys; |
| 89 |
private int[] values; |
| 90 |
} |
| 91 |
|
| 92 |
public ObjToIntMap() { |
| 93 |
this(4); |
| 94 |
} |
| 95 |
|
| 96 |
public ObjToIntMap(int keyCountHint) { |
| 97 |
if (keyCountHint < 0) Kit.codeBug(); |
| 98 |
|
| 99 |
int minimalCapacity = keyCountHint * 4 / 3; |
| 100 |
int i; |
| 101 |
for (i = 2; (1 << i) < minimalCapacity; ++i) { } |
| 102 |
power = i; |
| 103 |
if (check && power < 2) Kit.codeBug(); |
| 104 |
} |
| 105 |
|
| 106 |
public boolean isEmpty() { |
| 107 |
return keyCount == 0; |
| 108 |
} |
| 109 |
|
| 110 |
public int size() { |
| 111 |
return keyCount; |
| 112 |
} |
| 113 |
|
| 114 |
public boolean has(Object key) { |
| 115 |
if (key == null) { key = UniqueTag.NULL_VALUE; } |
| 116 |
return 0 <= findIndex(key); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public int get(Object key, int defaultValue) { |
| 124 |
if (key == null) { key = UniqueTag.NULL_VALUE; } |
| 125 |
int index = findIndex(key); |
| 126 |
if (0 <= index) { |
| 127 |
return values[index]; |
| 128 |
} |
| 129 |
return defaultValue; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public int getExisting(Object key) { |
| 138 |
if (key == null) { key = UniqueTag.NULL_VALUE; } |
| 139 |
int index = findIndex(key); |
| 140 |
if (0 <= index) { |
| 141 |
return values[index]; |
| 142 |
} |
| 143 |
|
| 144 |
Kit.codeBug(); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
public void put(Object key, int value) { |
| 149 |
if (key == null) { key = UniqueTag.NULL_VALUE; } |
| 150 |
int index = ensureIndex(key); |
| 151 |
values[index] = value; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public Object intern(Object keyArg) { |
| 160 |
boolean nullKey = false; |
| 161 |
if (keyArg == null) { |
| 162 |
nullKey = true; |
| 163 |
keyArg = UniqueTag.NULL_VALUE; |
| 164 |
} |
| 165 |
int index = ensureIndex(keyArg); |
| 166 |
values[index] = 0; |
| 167 |
return (nullKey) ? null : keys[index]; |
| 168 |
} |
| 169 |
|
| 170 |
public void remove(Object key) { |
| 171 |
if (key == null) { key = UniqueTag.NULL_VALUE; } |
| 172 |
int index = findIndex(key); |
| 173 |
if (0 <= index) { |
| 174 |
keys[index] = DELETED; |
| 175 |
--keyCount; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public void clear() { |
| 180 |
int i = keys.length; |
| 181 |
while (i != 0) { |
| 182 |
keys[--i] = null; |
| 183 |
} |
| 184 |
keyCount = 0; |
| 185 |
occupiedCount = 0; |
| 186 |
} |
| 187 |
|
| 188 |
public Iterator newIterator() { |
| 189 |
return new Iterator(this); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
final void initIterator(Iterator i) { |
| 196 |
i.init(keys, values, keyCount); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public Object[] getKeys() { |
| 201 |
Object[] array = new Object[keyCount]; |
| 202 |
getKeys(array, 0); |
| 203 |
return array; |
| 204 |
} |
| 205 |
|
| 206 |
public void getKeys(Object[] array, int offset) { |
| 207 |
int count = keyCount; |
| 208 |
for (int i = 0; count != 0; ++i) { |
| 209 |
Object key = keys[i]; |
| 210 |
if (key != null && key != DELETED) { |
| 211 |
if (key == UniqueTag.NULL_VALUE) { key = null; } |
| 212 |
array[offset] = key; |
| 213 |
++offset; |
| 214 |
--count; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private static int tableLookupStep(int fraction, int mask, int power) { |
| 220 |
int shift = 32 - 2 * power; |
| 221 |
if (shift >= 0) { |
| 222 |
return ((fraction >>> shift) & mask) | 1; |
| 223 |
} |
| 224 |
else { |
| 225 |
return (fraction & (mask >>> -shift)) | 1; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private int findIndex(Object key) { |
| 230 |
if (keys != null) { |
| 231 |
int hash = key.hashCode(); |
| 232 |
int fraction = hash * A; |
| 233 |
int index = fraction >>> (32 - power); |
| 234 |
Object test = keys[index]; |
| 235 |
if (test != null) { |
| 236 |
int N = 1 << power; |
| 237 |
if (test == key |
| 238 |
|| (values[N + index] == hash && test.equals(key))) |
| 239 |
{ |
| 240 |
return index; |
| 241 |
} |
| 242 |
|
| 243 |
int mask = N - 1; |
| 244 |
int step = tableLookupStep(fraction, mask, power); |
| 245 |
int n = 0; |
| 246 |
for (;;) { |
| 247 |
if (check) { |
| 248 |
if (n >= occupiedCount) Kit.codeBug(); |
| 249 |
++n; |
| 250 |
} |
| 251 |
index = (index + step) & mask; |
| 252 |
test = keys[index]; |
| 253 |
if (test == null) { |
| 254 |
break; |
| 255 |
} |
| 256 |
if (test == key |
| 257 |
|| (values[N + index] == hash && test.equals(key))) |
| 258 |
{ |
| 259 |
return index; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
return -1; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private int insertNewKey(Object key, int hash) { |
| 270 |
if (check && occupiedCount != keyCount) Kit.codeBug(); |
| 271 |
if (check && keyCount == 1 << power) Kit.codeBug(); |
| 272 |
int fraction = hash * A; |
| 273 |
int index = fraction >>> (32 - power); |
| 274 |
int N = 1 << power; |
| 275 |
if (keys[index] != null) { |
| 276 |
int mask = N - 1; |
| 277 |
int step = tableLookupStep(fraction, mask, power); |
| 278 |
int firstIndex = index; |
| 279 |
do { |
| 280 |
if (check && keys[index] == DELETED) Kit.codeBug(); |
| 281 |
index = (index + step) & mask; |
| 282 |
if (check && firstIndex == index) Kit.codeBug(); |
| 283 |
} while (keys[index] != null); |
| 284 |
} |
| 285 |
keys[index] = key; |
| 286 |
values[N + index] = hash; |
| 287 |
++occupiedCount; |
| 288 |
++keyCount; |
| 289 |
|
| 290 |
return index; |
| 291 |
} |
| 292 |
|
| 293 |
private void rehashTable() { |
| 294 |
if (keys == null) { |
| 295 |
if (check && keyCount != 0) Kit.codeBug(); |
| 296 |
if (check && occupiedCount != 0) Kit.codeBug(); |
| 297 |
int N = 1 << power; |
| 298 |
keys = new Object[N]; |
| 299 |
values = new int[2 * N]; |
| 300 |
} |
| 301 |
else { |
| 302 |
|
| 303 |
if (keyCount * 2 >= occupiedCount) { |
| 304 |
|
| 305 |
++power; |
| 306 |
} |
| 307 |
int N = 1 << power; |
| 308 |
Object[] oldKeys = keys; |
| 309 |
int[] oldValues = values; |
| 310 |
int oldN = oldKeys.length; |
| 311 |
keys = new Object[N]; |
| 312 |
values = new int[2 * N]; |
| 313 |
|
| 314 |
int remaining = keyCount; |
| 315 |
occupiedCount = keyCount = 0; |
| 316 |
for (int i = 0; remaining != 0; ++i) { |
| 317 |
Object key = oldKeys[i]; |
| 318 |
if (key != null && key != DELETED) { |
| 319 |
int keyHash = oldValues[oldN + i]; |
| 320 |
int index = insertNewKey(key, keyHash); |
| 321 |
values[index] = oldValues[i]; |
| 322 |
--remaining; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
private int ensureIndex(Object key) { |
| 330 |
int hash = key.hashCode(); |
| 331 |
int index = -1; |
| 332 |
int firstDeleted = -1; |
| 333 |
if (keys != null) { |
| 334 |
int fraction = hash * A; |
| 335 |
index = fraction >>> (32 - power); |
| 336 |
Object test = keys[index]; |
| 337 |
if (test != null) { |
| 338 |
int N = 1 << power; |
| 339 |
if (test == key |
| 340 |
|| (values[N + index] == hash && test.equals(key))) |
| 341 |
{ |
| 342 |
return index; |
| 343 |
} |
| 344 |
if (test == DELETED) { |
| 345 |
firstDeleted = index; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
int mask = N - 1; |
| 350 |
int step = tableLookupStep(fraction, mask, power); |
| 351 |
int n = 0; |
| 352 |
for (;;) { |
| 353 |
if (check) { |
| 354 |
if (n >= occupiedCount) Kit.codeBug(); |
| 355 |
++n; |
| 356 |
} |
| 357 |
index = (index + step) & mask; |
| 358 |
test = keys[index]; |
| 359 |
if (test == null) { |
| 360 |
break; |
| 361 |
} |
| 362 |
if (test == key |
| 363 |
|| (values[N + index] == hash && test.equals(key))) |
| 364 |
{ |
| 365 |
return index; |
| 366 |
} |
| 367 |
if (test == DELETED && firstDeleted < 0) { |
| 368 |
firstDeleted = index; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (check && keys != null && keys[index] != null) |
| 375 |
Kit.codeBug(); |
| 376 |
if (firstDeleted >= 0) { |
| 377 |
index = firstDeleted; |
| 378 |
} |
| 379 |
else { |
| 380 |
|
| 381 |
if (keys == null || occupiedCount * 4 >= (1 << power) * 3) { |
| 382 |
|
| 383 |
rehashTable(); |
| 384 |
return insertNewKey(key, hash); |
| 385 |
} |
| 386 |
++occupiedCount; |
| 387 |
} |
| 388 |
keys[index] = key; |
| 389 |
values[(1 << power) + index] = hash; |
| 390 |
++keyCount; |
| 391 |
return index; |
| 392 |
} |
| 393 |
|
| 394 |
private void writeObject(ObjectOutputStream out) |
| 395 |
throws IOException |
| 396 |
{ |
| 397 |
out.defaultWriteObject(); |
| 398 |
|
| 399 |
int count = keyCount; |
| 400 |
for (int i = 0; count != 0; ++i) { |
| 401 |
Object key = keys[i]; |
| 402 |
if (key != null && key != DELETED) { |
| 403 |
--count; |
| 404 |
out.writeObject(key); |
| 405 |
out.writeInt(values[i]); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
private void readObject(ObjectInputStream in) |
| 411 |
throws IOException, ClassNotFoundException |
| 412 |
{ |
| 413 |
in.defaultReadObject(); |
| 414 |
|
| 415 |
int writtenKeyCount = keyCount; |
| 416 |
if (writtenKeyCount != 0) { |
| 417 |
keyCount = 0; |
| 418 |
int N = 1 << power; |
| 419 |
keys = new Object[N]; |
| 420 |
values = new int[2 * N]; |
| 421 |
for (int i = 0; i != writtenKeyCount; ++i) { |
| 422 |
Object key = in.readObject(); |
| 423 |
int hash = key.hashCode(); |
| 424 |
int index = insertNewKey(key, hash); |
| 425 |
values[index] = in.readInt(); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
private static final int A = 0x9e3779b9; |
| 433 |
|
| 434 |
private static final Object DELETED = new Object(); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
private transient Object[] keys; |
| 442 |
private transient int[] values; |
| 443 |
|
| 444 |
private int power; |
| 445 |
private int keyCount; |
| 446 |
private transient int occupiedCount; |
| 447 |
|
| 448 |
|
| 449 |
private static final boolean check = false; |
| 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 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
} |