Пример #1
0
  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));
  }
Пример #2
0
 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;
 }
Пример #3
0
  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;
  }
Пример #4
0
  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;
  }
Пример #5
0
 // @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;
 }
Пример #6
0
 // @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;
 }
Пример #7
0
  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;
  }
Пример #8
0
  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);
    }
  }
Пример #9
0
  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;
  }
Пример #10
0
  // 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;
  }
Пример #11
0
 private boolean remove(RubyValue v) {
   boolean r = false;
   while (array_.remove(v)) {
     r = true;
   }
   return r;
 }
Пример #12
0
 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;
 }
Пример #13
0
  // 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;
    }
  }
Пример #14
0
  // @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;
  }
Пример #15
0
 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;
 }
Пример #16
0
 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;
 }
Пример #17
0
 // @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;
 }
Пример #18
0
  // @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;
  }
Пример #19
0
  // @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;
    }
  }
Пример #20
0
  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;
  }
Пример #21
0
  // @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;
  }
Пример #22
0
  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;
  }
Пример #23
0
  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);
  }
Пример #24
0
 // @RubyLevelMethod(name="unshift")
 public RubyArray unshift(RubyArray value) {
   array_.addAll(0, value.array_);
   return this;
 }
Пример #25
0
 // @RubyLevelMethod(name="unshift")
 public RubyArray unshift(RubyValue value) {
   array_.add(0, value);
   return this;
 }
Пример #26
0
 // @RubyLevelMethod(name="concat")
 public RubyArray concat(RubyValue v) {
   RubyArray ary = v.toAry();
   array_.addAll(ary.array_);
   return this;
 }
Пример #27
0
 public Iterator /*<RubyValue>*/ iterator() {
   return array_.iterator();
 }
Пример #28
0
 // @RubyLevelMethod(name="clear")
 public RubyArray clear() {
   array_.clear();
   return this;
 }
Пример #29
0
 RubyArray(int size, RubyValue default_value) {
   this(size);
   for (int i = 0; i < size; ++i) {
     array_.add(default_value);
   }
 }
Пример #30
0
 public int size() {
   return array_.size();
 }