예제 #1
0
파일: array.c 프로젝트: galois17/mruby
/*
 *  call-seq:
 *     ary <=> other_ary   ->  -1, 0, +1 or nil
 *
 *  Comparison---Returns an integer (-1, 0, or +1)
 *  if this array is less than, equal to, or greater than <i>other_ary</i>.
 *  Each object in each array is compared (using <=>). If any value isn't
 *  equal, then that inequality is the return value. If all the
 *  values found are equal, then the return is based on a
 *  comparison of the array lengths.  Thus, two arrays are
 *  ``equal'' according to <code>Array#<=></code> if and only if they have
 *  the same length and the value of each element is equal to the
 *  value of the corresponding element in the other array.
 *
 *     [ "a", "a", "c" ]    <=> [ "a", "b", "c" ]   #=> -1
 *     [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ]            #=> +1
 *
 */
mrb_value
mrb_ary_cmp(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;
  struct RArray *a1, *a2;
  mrb_value r = mrb_nil_value();
  int i, len;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_type(ary2) != MRB_TT_ARRAY) return mrb_nil_value();
  a1 = RARRAY(ary1); a2 = RARRAY(ary2);
  if (a1->len == a2->len && a1->ptr == a2->ptr) return mrb_fixnum_value(0);
  else {
    len = RARRAY_LEN(ary1);
    if (len > RARRAY_LEN(ary2)) {
      len = RARRAY_LEN(ary2);
    }
    for (i=0; i<len; i++) {
      r = mrb_funcall(mrb, ary_elt(ary1, i), "<=>", 1, ary_elt(ary2, i));
      if (mrb_type(r) != MRB_TT_FIXNUM || mrb_fixnum(r) != 0) return r;
    }
  }
  len = a1->len - a2->len;
  return mrb_fixnum_value((len == 0)? 0: (len > 0)? 1: -1);
}
예제 #2
0
파일: array.c 프로젝트: galois17/mruby
static mrb_value
mrb_ary_equal(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_obj_equal(mrb, ary1,ary2)) return mrb_true_value();
  if (mrb_type(ary2) != MRB_TT_ARRAY) {
    if (!mrb_respond_to(mrb, ary2, mrb_intern(mrb, "to_ary"))) {
        return mrb_false_value();
    }
    if (mrb_equal(mrb, ary2, ary1)){
      return mrb_true_value();
    }
    else {
      return mrb_false_value();
    }
  }
  if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
  else {
    int i;

    for (i=0; i<RARRAY_LEN(ary1); i++) {
      if (!mrb_equal(mrb, ary_elt(ary1, i), ary_elt(ary2, i)))
        return mrb_false_value();
    }
    return mrb_true_value();
  }
}
예제 #3
0
파일: array.c 프로젝트: CaptainJet/mruby
static mrb_value
mrb_ary_equal(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;
  mrb_int i;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_obj_equal(mrb, ary1, ary2)) return mrb_true_value();
  if (mrb_special_const_p(ary2)) return mrb_false_value();
  if (!mrb_array_p(ary2)) {
    if (!mrb_respond_to(mrb, ary2, mrb_intern2(mrb, "to_ary", 6))) {
      return mrb_false_value();
    }
    else {
      return mrb_bool_value(mrb_equal(mrb, ary2, ary1));
    }
  }
  if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
  for (i=0; i<RARRAY_LEN(ary1); i++) {
    if (!mrb_equal(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) {
      return mrb_false_value();
    }
  }
  return mrb_true_value();
}
예제 #4
0
파일: array.c 프로젝트: chancelab/mruby
static mrb_value
mrb_ary_eql(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;
  mrb_bool eql_p;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_obj_equal(mrb, ary1, ary2)) {
    eql_p = 1;
  }
  else if (!mrb_array_p(ary2)) {
    eql_p = 0;
  }
  else if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) {
    eql_p = 0;
  }
  else {
    mrb_int i;
    eql_p = 1;
    for (i=0; i<RARRAY_LEN(ary1); i++) {
      if (!mrb_eql(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) {
        eql_p = 0;
        break;
      }
    }
  }

  return mrb_bool_value(eql_p);
}
예제 #5
0
파일: array.c 프로젝트: galois17/mruby
mrb_value
mrb_ary_entry(mrb_value ary, int offset)
{
  if (offset < 0) {
    offset += RARRAY_LEN(ary);
  }
  return ary_elt(ary, offset);
}
예제 #6
0
파일: array.c 프로젝트: CaptainJet/mruby
static mrb_value
mrb_ary_eql(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;
  mrb_int i;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_obj_equal(mrb, ary1, ary2)) return mrb_true_value();
  if (!mrb_array_p(ary2)) return mrb_false_value();
  if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
  for (i=0; i<RARRAY_LEN(ary1); i++) {
    if (!mrb_eql(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) {
      return mrb_false_value();
    }
  }
  return mrb_true_value();
}
예제 #7
0
파일: array.c 프로젝트: chancelab/mruby
static mrb_value
mrb_ary_equal(mrb_state *mrb, mrb_value ary1)
{
  mrb_value ary2;
  mrb_bool equal_p;

  mrb_get_args(mrb, "o", &ary2);
  if (mrb_obj_equal(mrb, ary1, ary2)) {
    equal_p = 1;
  }
  else if (mrb_special_const_p(ary2)) {
    equal_p = 0;
  }
  else if (!mrb_array_p(ary2)) {
    if (!mrb_respond_to(mrb, ary2, mrb_intern2(mrb, "to_ary", 6))) {
        equal_p = 0;
    }
    else {
      equal_p = mrb_equal(mrb, ary2, ary1);
    }
  }
  else if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) {
    equal_p = 0;
  }
  else {
    mrb_int i;

    equal_p = 1;
    for (i=0; i<RARRAY_LEN(ary1); i++) {
      if (!mrb_equal(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) {
        equal_p = 0;
        break;
      }
    }
  }

  return mrb_bool_value(equal_p);
}