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; }
// 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; } }
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 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; }
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; }
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; }
// @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; }
// @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; }
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="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 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); } }
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; }
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 int size() { return array_.size(); }