| 324 | } |
| 325 | |
| 326 | void put(int key, Ref obj) { |
| 327 | if (obj == null) { |
| 328 | throw new RuntimeException("put a null ref (with key "+key+")"); |
| 329 | } |
| 330 | int i = Arrays.binarySearch(keys, 0, next, key); |
| 331 | if (i >= 0) { |
| 332 | if (objs[i] == null) { |
| 333 | objs[i] = obj; |
| 334 | live++; |
| 335 | } |
| 336 | if (objs[i] != obj) { |
| 337 | throw new RuntimeException("replacing an existing ref (with key "+key+")"); |
| 338 | } |
| 339 | return; |
| 340 | } |
| 341 | if (next >= keys.length) { |
| 342 | grow(); |
| 343 | i = Arrays.binarySearch(keys, 0, next, key); |
| 344 | } |
| 345 | i = ~i; |
| 346 | if (i < next) { |
| 347 | // Insert, shift everything afterwards down. |
| 348 | System.arraycopy(keys, i, keys, i+1, next-i); |
| 349 | System.arraycopy(objs, i, objs, i+1, next-i); |
| 350 | } |
| 351 | keys[i] = key; |
| 352 | objs[i] = obj; |
| 353 | live++; |
| 354 | next++; |
| 355 | } |
| 356 | |
| 357 | private void grow() { |
| 358 | // Compact and (if necessary) grow backing store. |