90 } 91 92 @Override 93 public ContinuousArrayData copy() { 94 assert length() == 0; 95 return this; 96 } 97 98 @Override 99 public Object asArrayOfType(final Class<?> componentType) { 100 return Array.newInstance(componentType, 0); 101 } 102 103 @Override 104 public Object[] asObjectArray() { 105 return ScriptRuntime.EMPTY_ARRAY; 106 } 107 108 @Override 109 public ArrayData ensure(final long safeIndex) { 110 if (safeIndex > 0L) { 111 if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { 112 return new SparseArrayData(this, safeIndex + 1); 113 } 114 //known to fit in int 115 return toRealArrayData((int)safeIndex); 116 } 117 return this; 118 } 119 120 @Override 121 public ArrayData convert(final Class<?> type) { 122 return toRealArrayData().convert(type); 123 } 124 125 @Override 126 public ArrayData delete(final int index) { 127 return new DeletedRangeArrayFilter(this, index, index); 128 } 129 130 @Override 131 public ArrayData delete(final long fromIndex, final long toIndex) { 132 return new DeletedRangeArrayFilter(this, fromIndex, toIndex); 133 } 134 135 @Override 136 public void shiftLeft(final int by) { 137 //nop, always empty or we wouldn't be of this class 138 } 139 140 @Override 141 public ArrayData shiftRight(final int by) { 142 return this; //always empty or we wouldn't be of this class 143 } 144 145 @Override 146 public ArrayData shrink(final long newLength) { 147 return this; 148 } 149 150 @Override 151 public ArrayData set(final int index, final Object value, final boolean strict) { 152 return toRealArrayData(index).set(index, value, strict); 153 } 154 155 @Override 156 public ArrayData set(final int index, final int value, final boolean strict) { 157 return toRealArrayData(index).set(index, value, strict); 434 435 /** 436 * Increase length by 1 437 * @return the new length, not the old one (i.e. pre-increment) 438 */ 439 protected final long increaseLength() { 440 return ++this.length; 441 } 442 443 /** 444 * Decrease length by 1. 445 * @return the new length, not the old one (i.e. pre-decrement) 446 */ 447 protected final long decreaseLength() { 448 return --this.length; 449 } 450 451 /** 452 * Shift the array data left 453 * 454 * TODO: explore start at an index and not at zero, to make these operations 455 * even faster. Offset everything from the index. Costs memory but is probably 456 * worth it 457 * 458 * @param by offset to shift 459 */ 460 public abstract void shiftLeft(final int by); 461 462 /** 463 * Shift the array right 464 * 465 * @param by offset to shift 466 467 * @return New arraydata (or same) 468 */ 469 public abstract ArrayData shiftRight(final int by); 470 471 /** 472 * Ensure that the given index exists and won't fail in a subsequent access. 473 * If {@code safeIndex} is equal or greater than the current length the length is 474 * updated to {@code safeIndex + 1}. 475 * 476 * @param safeIndex the index to ensure wont go out of bounds 477 * @return new array data (or same) 478 */ 479 public abstract ArrayData ensure(final long safeIndex); 480 | 90 } 91 92 @Override 93 public ContinuousArrayData copy() { 94 assert length() == 0; 95 return this; 96 } 97 98 @Override 99 public Object asArrayOfType(final Class<?> componentType) { 100 return Array.newInstance(componentType, 0); 101 } 102 103 @Override 104 public Object[] asObjectArray() { 105 return ScriptRuntime.EMPTY_ARRAY; 106 } 107 108 @Override 109 public ArrayData ensure(final long safeIndex) { 110 assert safeIndex >= 0L; 111 if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { 112 return new SparseArrayData(this, safeIndex + 1); 113 } 114 //known to fit in int 115 return toRealArrayData((int)safeIndex); 116 117 } 118 119 @Override 120 public ArrayData convert(final Class<?> type) { 121 return toRealArrayData().convert(type); 122 } 123 124 @Override 125 public ArrayData delete(final int index) { 126 return new DeletedRangeArrayFilter(this, index, index); 127 } 128 129 @Override 130 public ArrayData delete(final long fromIndex, final long toIndex) { 131 return new DeletedRangeArrayFilter(this, fromIndex, toIndex); 132 } 133 134 @Override 135 public ArrayData shiftLeft(final int by) { 136 return this; //nop, always empty or we wouldn't be of this class 137 } 138 139 @Override 140 public ArrayData shiftRight(final int by) { 141 return this; //always empty or we wouldn't be of this class 142 } 143 144 @Override 145 public ArrayData shrink(final long newLength) { 146 return this; 147 } 148 149 @Override 150 public ArrayData set(final int index, final Object value, final boolean strict) { 151 return toRealArrayData(index).set(index, value, strict); 152 } 153 154 @Override 155 public ArrayData set(final int index, final int value, final boolean strict) { 156 return toRealArrayData(index).set(index, value, strict); 433 434 /** 435 * Increase length by 1 436 * @return the new length, not the old one (i.e. pre-increment) 437 */ 438 protected final long increaseLength() { 439 return ++this.length; 440 } 441 442 /** 443 * Decrease length by 1. 444 * @return the new length, not the old one (i.e. pre-decrement) 445 */ 446 protected final long decreaseLength() { 447 return --this.length; 448 } 449 450 /** 451 * Shift the array data left 452 * 453 * TODO: This is used for Array.prototype.shift() which only shifts by 1, 454 * so we might consider dropping the offset parameter. 455 * 456 * @param by offset to shift 457 * @return New arraydata (or same) 458 */ 459 public abstract ArrayData shiftLeft(final int by); 460 461 /** 462 * Shift the array right 463 * 464 * @param by offset to shift 465 466 * @return New arraydata (or same) 467 */ 468 public abstract ArrayData shiftRight(final int by); 469 470 /** 471 * Ensure that the given index exists and won't fail in a subsequent access. 472 * If {@code safeIndex} is equal or greater than the current length the length is 473 * updated to {@code safeIndex + 1}. 474 * 475 * @param safeIndex the index to ensure wont go out of bounds 476 * @return new array data (or same) 477 */ 478 public abstract ArrayData ensure(final long safeIndex); 479 |