private void alloc(int length) { IRubyObject[] newValues = length == 0 ? IRubyObject.NULL_ARRAY : new IRubyObject[length]; Helpers.fillNil(newValues, getRuntime()); values = newValues; begin = 0; }
private void spliceRealloc(int length, int valuesLength) { int tryLength = valuesLength + (valuesLength >> 1); int len = length > tryLength ? length : tryLength; IRubyObject[] vals = new IRubyObject[len]; System.arraycopy(values, begin, vals, 0, realLength); // only fill if there actually will remain trailing storage if (len > length) Helpers.fillNil(vals, length, len, getRuntime()); begin = 0; values = vals; }
/** rb_ary_modify_check * */ protected final void modifyCheck() { if ((flags & TMPLOCK_OR_FROZEN_ARR_F) != 0) { if ((flags & FROZEN_F) != 0) throw getRuntime().newFrozenError(this.getMetaClass()); if ((flags & TMPLOCK_ARR_F) != 0) throw getRuntime().newTypeError("can't modify array during iteration"); } }
public int lastIndexOf(Object element) { if (element != null) { IRubyObject convertedElement = JavaUtil.convertJavaToUsableRubyObject(getRuntime(), element); for (int i = realLength - 1; i >= 0; i--) { if (convertedElement.equals(eltInternal(i))) { return i; } } } return -1; }
public T eltSetOk(int offset, T value) { try { return eltInternalSet(offset, value); } catch (ArrayIndexOutOfBoundsException ex) { throw concurrentModification(getRuntime(), ex); } }
private void storeRealloc(long index, int valuesLength) { long newLength = valuesLength >> 1; if (newLength < ARRAY_DEFAULT_SIZE) newLength = ARRAY_DEFAULT_SIZE; newLength += index; if (index >= Integer.MAX_VALUE || newLength >= Integer.MAX_VALUE) { throw getRuntime().newIndexError("index " + index + " too big"); } realloc((int) newLength, valuesLength); }
/** rb_ary_elt * */ private final IRubyObject elt(long offset) { if (offset < 0 || offset >= realLength) { return getRuntime().getNil(); } return eltOk(offset); }
/** rb_ary_length * */ @JRubyMethod(name = "length", alias = "size") public RubyFixnum length() { return getRuntime().newFixnum(realLength); }
@Override public IRubyObject getRemainingAsIO() { if (scriptLines == null) return null; return new RubyIO(scriptLines.getRuntime(), getRemainingAsChannel()); } }
public T eltOk(long offset) { try { return (T) eltInternal((int)offset); } catch (ArrayIndexOutOfBoundsException ex) { throw concurrentModification(getRuntime(), ex); } }
@Deprecated // not used public static IRubyObject arrayEntryOrNil(RubyArray array, int index) { if (index < array.getLength()) { return array.eltInternal(index); } else { return array.getRuntime().getNil(); } }
@Deprecated // not used public static IRubyObject arrayEntryOrNilTwo(RubyArray array) { if (2 < array.getLength()) { return array.eltInternal(2); } else { return array.getRuntime().getNil(); } }
/** rb_ary_first * */ @JRubyMethod(name = "first") public IRubyObject first() { if (realLength == 0) return getRuntime().getNil(); return eltOk(0); }
@Override public boolean equals(Object other) { if (this == other) return true; if (other instanceof RubyArray) { return op_equal(getRuntime().getCurrentContext(), (RubyArray) other).isTrue(); } return false; }
/** rb_ary_nitems * */ @JRubyMethod(name = "nitems") public IRubyObject nitems() { int n = 0; for (int i = 0; i < realLength; i++) { if (!eltOk(i).isNil()) n++; } return getRuntime().newFixnum(n); }
@Deprecated // not used public static IRubyObject arrayPostOrNilOne(RubyArray array, int pre, int post) { if (pre + post < array.getLength()) { return array.eltInternal(array.getLength() - post + 1); } else if (pre + 1 < array.getLength()) { return array.eltInternal(pre + 1); } else { return array.getRuntime().getNil(); } }
@Deprecated // not used public static IRubyObject arrayPostOrNilZero(RubyArray array, int pre, int post) { if (pre + post < array.getLength()) { return array.eltInternal(array.getLength() - post + 0); } else if (pre + 0 < array.getLength()) { return array.eltInternal(pre + 0); } else { return array.getRuntime().getNil(); } }
@Deprecated // not used public static IRubyObject arrayPostOrNil(RubyArray array, int pre, int post, int index) { if (pre + post < array.getLength()) { return array.eltInternal(array.getLength() - post + index); } else if (pre + index < array.getLength()) { return array.eltInternal(pre + index); } else { return array.getRuntime().getNil(); } }
/** rb_ary_slice_bang * */ @JRubyMethod(name = "slice!") public IRubyObject slice_bang(IRubyObject arg0, IRubyObject arg1) { modifyCheck(); return slice_internal(getRuntime(), RubyNumeric.num2int(arg0), RubyNumeric.num2int(arg1)); }
@Override public ByteList gets() { int length = completeSource.length(); if (offset >= length) return null; // At end of source/eof int end = completeSource.indexOf('\n', offset) + 1; if (end == 0) end = length; ByteList line = completeSource.makeShared(offset, end - offset); offset = end; if (scriptLines != null) scriptLines.append(RubyString.newString(scriptLines.getRuntime(), line)); return line; }