public RubyArray subarray(int begin, int length) { final int arraySize = array_.size(); if (begin > arraySize) { return null; } if (length < 0) { return null; } if (begin < 0) { begin += arraySize; } if (begin + length > arraySize) { length = arraySize - begin; if (length < 0) { length = 0; } } if (length == 0) { return new RubyArray(0); } return new RubyArray(array_.subList(begin, begin + length)); }
private RubyArray insert(int index, RubyArray a) { for (int i = array_.size(); i < index; ++i) { array_.add(RubyConstant.QNIL); } array_.addAll(index, a.array_); return this; }
private RubyArray delete_at(int begin, int length) { final int arraySize = array_.size(); if (begin > arraySize) { return null; } if (length < 0) { return null; } if (begin < 0) { begin += arraySize; } if (begin + length > arraySize) { length = arraySize - begin; if (length < 0) { length = 0; } } while (length > 0) { array_.remove(begin); length--; } return this; }
public RubyArray copy() { RubyArray resultArray = new RubyArray(array_.size()); // for (RubyValue v : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue v = (RubyValue) iter.next(); resultArray.add(v); } return resultArray; }
// @RubyLevelMethod(name="reverse_each") public RubyValue reverse_each(RubyBlock block) { ListIterator /*<RubyValue>*/ ite = array_.listIterator(array_.size()); while (ite.hasPrevious()) { RubyValue v = block.invoke(this, (RubyValue) ite.previous()); if (block.breakedOrReturned()) { return v; } } return this; }
// @RubyLevelMethod(name="delete_if") public RubyValue delete_if(RubyBlock block) { for (int i = 0; i < array_.size(); ) { RubyValue r = block.invoke(this, (RubyValue) array_.get(i)); if (r.isTrue()) { array_.remove(i); } else { ++i; } } return this; }
public RubyArray expand(RubyValue v) { if (v instanceof RubyArray) { // [5,6,*[1, 2]] array_.addAll(((RubyArray) v).array_); } else { // [5,6,*1], [5,6,*nil] array_.add(v); } return this; }
public RubyValue get(int index) { if (index < 0) { index = array_.size() + index; } if (index < 0 || index >= size()) { return RubyConstant.QNIL; } else { return (RubyValue) array_.get(index); } }
public RubyValue set(int start, RubyValue value) { int index = getRealIndex(start); if (index < array_.size()) { array_.set(index, value); } else { for (int i = array_.size(); i < index; ++i) { array_.add(RubyConstant.QNIL); } array_.add(value); } return value; }
// create a new Array containing every element from index to the end public RubyValue collect(int index) { AssertMe.rho_assert(index >= 0); final int size = array_.size() - index; if (size < 0) { return new RubyArray(); } RubyArray a = new RubyArray(size); for (int i = index; i < array_.size(); ++i) { a.add(array_.get(i)); } return a; }
private boolean remove(RubyValue v) { boolean r = false; while (array_.remove(v)) { r = true; } return r; }
private RubyArray plus(RubyArray v) { int size = array_.size() + v.size(); RubyArray resultArray = new RubyArray(size); resultArray.array_.addAll(array_); resultArray.array_.addAll(v.array_); return resultArray; }
// RHO_COMMENT public boolean equals(Object o, boolean bConvToAry) { if (this == o) { return true; } if (o instanceof RubyArray) { RubyArray that = (RubyArray) o; int size = array_.size(); if (size != that.size()) { return false; } for (int i = 0; i < size; ++i) { if (!this.get(i).equals(that.get(i))) { return false; } } return true; } else if (o instanceof RubyValue && bConvToAry) { RubyValue v = (RubyValue) o; if (!v.respondTo(RubyID.toAryID)) { return false; } else { return v.equals(this); } } else { return false; } }
// @RubyLevelMethod(name="uniq!") public RubyValue uniq_danger() { boolean b = false; for (int i = 0; i < array_.size(); ++i) { for (int j = i + 1; j < array_.size(); ) { if (RubyAPI.testEqual((RubyValue) array_.get(i), (RubyValue) array_.get(j))) { array_.remove(j); b = true; } else { ++j; } } } if (!b) return RubyConstant.QNIL; return this; // return b ? this : RubyConstant.QNIL; }
public RubyValue replace(int start, int length, RubyValue value) { int index = getRealIndex(start); if (value == RubyConstant.QNIL) { for (int i = 0; i < length; i++) { array_.remove(index); } return value; } if (length < 0) { throw new RubyException(RubyRuntime.IndexErrorClass, "negative length (" + length + ")"); } else if (0 == length) { if (value instanceof RubyArray) { array_.addAll(index, ((RubyArray) value).array_); } else { array_.add(index, value); } } else { for (int i = 0; i < length - 1; ++i) { array_.remove(index); } if (value instanceof RubyArray) { array_.remove(index); array_.addAll(index, ((RubyArray) value).array_); } else { array_.set(index, value); } } return value; }
public int hashCode() { int hash = 0; // for (RubyValue v : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue v = (RubyValue) iter.next(); hash += v.hashCode(); } return hash; }
// @RubyLevelMethod(name="each") public RubyValue each(RubyBlock block) { // for (RubyValue item : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue item = (RubyValue) iter.next(); RubyValue v = block.invoke(this, item); if (block.breakedOrReturned()) { return v; } } return this; }
// @RubyLevelMethod(name="include?") public RubyValue include(RubyValue v) { // for (RubyValue value : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue value = (RubyValue) iter.next(); if (RubyAPI.testEqual(value, v)) { return RubyConstant.QTRUE; } } return RubyConstant.QFALSE; }
// @RubyLevelMethod(name="delete") public RubyValue delete(RubyValue item, RubyBlock block) { boolean found = false; while (array_.remove(item)) { found = true; } if (block != null && !found) { return block.invoke(item); } else { return found ? item : RubyConstant.QNIL; } }
private int getRealIndex(int i) { int index = i; if (index < 0) { index = array_.size() + index; } if (index < 0) { throw new RubyException(RubyRuntime.IndexErrorClass, "index " + i + " out of array"); } return index; }
// @RubyLevelMethod(name="&") public RubyArray and(RubyValue value) { RubyArray other = value.toAry(); RubyArray a = new RubyArray(); // for (RubyValue v : array_) { for (Iterator iter = array_.iterator(); iter.hasNext(); ) { RubyValue v = (RubyValue) iter.next(); if (other.include(v) == RubyConstant.QTRUE && a.include(v) == RubyConstant.QFALSE) { a.add(v); } } return a; }
public RubyArray times(int times) { if (times < 0) { throw new RubyException(RubyRuntime.ArgumentErrorClass, "negative argument"); } int size = array_.size() * times; RubyArray resultArray = new RubyArray(size); for (int i = 0; i < times; i++) { resultArray.array_.addAll(array_); } return resultArray; }
public RubyValue delete_at(int index) { if (index >= size()) { return RubyConstant.QNIL; } if (index < 0) { index += size(); } if (index < 0) { return RubyConstant.QNIL; } return (RubyValue) array_.remove(index); }
// @RubyLevelMethod(name="unshift") public RubyArray unshift(RubyArray value) { array_.addAll(0, value.array_); return this; }
// @RubyLevelMethod(name="unshift") public RubyArray unshift(RubyValue value) { array_.add(0, value); return this; }
// @RubyLevelMethod(name="concat") public RubyArray concat(RubyValue v) { RubyArray ary = v.toAry(); array_.addAll(ary.array_); return this; }
public Iterator /*<RubyValue>*/ iterator() { return array_.iterator(); }
// @RubyLevelMethod(name="clear") public RubyArray clear() { array_.clear(); return this; }
RubyArray(int size, RubyValue default_value) { this(size); for (int i = 0; i < size; ++i) { array_.add(default_value); } }
public int size() { return array_.size(); }