Exemple #1
0
static mrb_value
hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, mrb_bool eql)
{
  khash_t(ht) *h1, *h2;
  mrb_bool eq;

  if (mrb_obj_equal(mrb, hash1, hash2)) return mrb_true_value();
  if (!mrb_hash_p(hash2)) {
      if (!mrb_respond_to(mrb, hash2, mrb_intern_lit(mrb, "to_hash"))) {
          return mrb_false_value();
      }
      else {
        if (eql) {
          eq = mrb_eql(mrb, hash2, hash1);
        }
        else {
          eq = mrb_equal(mrb, hash2, hash1);
        }
        return mrb_bool_value(eq);
      }
  }
  h1 = RHASH_TBL(hash1);
  h2 = RHASH_TBL(hash2);
  if (!h1) {
    return mrb_bool_value(!h2);
  }
  if (!h2) return mrb_false_value();
  if (kh_size(h1) != kh_size(h2)) return mrb_false_value();
  else {
    khiter_t k1, k2;
    mrb_value key;

    for (k1 = kh_begin(h1); k1 != kh_end(h1); k1++) {
      if (!kh_exist(h1, k1)) continue;
      key = kh_key(h1,k1);
      k2 = kh_get(ht, mrb, h2, key);
      if (k2 != kh_end(h2)) {
        if (eql)
          eq = mrb_eql(mrb, kh_value(h1,k1), kh_value(h2,k2));
        else
          eq = mrb_equal(mrb, kh_value(h1,k1), kh_value(h2,k2));
        if (eq) {
          continue; /* next key */
        }
      }
      return mrb_false_value();
    }
  }
  return mrb_true_value();
}
Exemple #2
0
static mrb_value
recursive_eql(mrb_state *mrb, mrb_value range, mrb_value obj, int recur)
{
  struct RRange *r = mrb_range_ptr(range);
  struct RRange *o = mrb_range_ptr(obj);

  if (recur) return mrb_true_value(); /* Subtle! */
  if (!mrb_eql(mrb, r->edges->beg, o->edges->beg))
    return mrb_false_value();
  if (!mrb_eql(mrb, r->edges->end, o->edges->end))
    return mrb_false_value();

  if (r->excl != o->excl)
    return mrb_false_value();
  return mrb_true_value();
}
Exemple #3
0
/*
 * code-seq:
 *   struct.eql?(other)   -> true or false
 *
 * Two structures are equal if they are the same object, or if all their
 * fields are equal (using <code>eql?</code>).
 */
static mrb_value
mrb_struct_eql(mrb_state *mrb, mrb_value s)
{
  mrb_value s2;
  mrb_value *ptr, *ptr2;
  mrb_int i, len;
  mrb_bool eql_p;

  mrb_get_args(mrb, "o", &s2);
  if (mrb_obj_equal(mrb, s, s2)) {
    eql_p = 1;
  }
  else if (strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s2)), "Struct") ||
           mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
    eql_p = 0;
  }
  else if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
    mrb_bug(mrb, "inconsistent struct"); /* should never happen */
    eql_p = 0; /* This substuture is just to suppress warnings. never called. */
  }
  else {
    ptr = RSTRUCT_PTR(s);
    ptr2 = RSTRUCT_PTR(s2);
    len = RSTRUCT_LEN(s);
    eql_p = 1;
    for (i=0; i<len; i++) {
      if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
        eql_p = 0;
        break;
      }
    }
  }

  return mrb_bool_value(eql_p);
}
Exemple #4
0
/*
 * code-seq:
 *   struct.eql?(other)   -> true or false
 *
 * Two structures are equal if they are the same object, or if all their
 * fields are equal (using <code>eql?</code>).
 */
static mrb_value
mrb_struct_eql(mrb_state *mrb, mrb_value s)
{
  mrb_value s2;
  mrb_value *ptr, *ptr2;
  mrb_int i, len;

  mrb_get_args(mrb, "o", &s2);
  if (mrb_obj_equal(mrb, s, s2)) {
    return mrb_true_value();
  }
  if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
    return mrb_false_value();
  }
  if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
    mrb_bug(mrb, "inconsistent struct"); /* should never happen */
  }
  ptr = RSTRUCT_PTR(s);
  ptr2 = RSTRUCT_PTR(s2);
  len = RSTRUCT_LEN(s);
  for (i=0; i<len; i++) {
    if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
      return mrb_false_value();
    }
  }

  return mrb_true_value();
}
Exemple #5
0
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);
}
Exemple #6
0
static mrb_value
range_eql(mrb_state *mrb, mrb_value range)
{
  mrb_value obj;
  struct RRange *r, *o;

  mrb_get_args(mrb, "o", &obj);

  if (mrb_obj_equal(mrb, range, obj)) return mrb_true_value();
  if (!mrb_obj_is_kind_of(mrb, obj, RANGE_CLASS)) {
    return mrb_false_value();
  }
  if (mrb_type(obj) != MRB_TT_RANGE) return mrb_false_value();

  r = mrb_range_ptr(range);
  o = mrb_range_ptr(obj);
  if (!mrb_eql(mrb, r->edges->beg, o->edges->beg) ||
      !mrb_eql(mrb, r->edges->end, o->edges->end) ||
      (r->excl != o->excl)) {
    return mrb_false_value();
  }
  return mrb_true_value();
}
Exemple #7
0
static mrb_value
recursive_eql(mrb_state *mrb, mrb_value s, mrb_value s2, int recur)
{
    mrb_value *ptr, *ptr2;
    long i, len;

    if (recur) return mrb_true_value(); /* Subtle! */
    ptr = RSTRUCT_PTR(s);
    ptr2 = RSTRUCT_PTR(s2);
    len = RSTRUCT_LEN(s);
    for (i=0; i<len; i++) {
      if (!mrb_eql(mrb, ptr[i], ptr2[i])) return mrb_false_value();
    }
    return mrb_true_value();
}
Exemple #8
0
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();
}
Exemple #9
0
static mrb_value
hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, int eql)
{
  if (mrb_obj_equal(mrb, hash1, hash2)) return mrb_true_value();
  if (mrb_type(hash2) != MRB_TT_HASH) {
      if (!mrb_respond_to(mrb, hash2, mrb_intern(mrb, "to_hash"))) {
          return mrb_false_value();
      }
      if (eql)
          return mrb_fixnum_value(mrb_eql(mrb, hash2, hash1));
      else
          return mrb_fixnum_value(mrb_equal(mrb, hash2, hash1));
  }
  if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2)) return mrb_false_value();
  if (!RHASH(hash1)->ht || !RHASH(hash2)->ht) return mrb_true_value();

  return mrb_exec_recursive_paired(mrb, recursive_eql, hash1, hash2, (void*)0);
}
Exemple #10
0
static mrb_value
hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, int eql)
{
  khash_t(ht) *h1, *h2;

  if (mrb_obj_equal(mrb, hash1, hash2)) return mrb_true_value();
  if (mrb_type(hash2) != MRB_TT_HASH) {
      if (!mrb_respond_to(mrb, hash2, mrb_intern(mrb, "to_hash"))) {
          return mrb_false_value();
      }
      if (eql)
          return mrb_fixnum_value(mrb_eql(mrb, hash2, hash1));
      else
          return mrb_fixnum_value(mrb_equal(mrb, hash2, hash1));
  }
  h1 = RHASH_TBL(hash1);
  h2 = RHASH_TBL(hash2);
  if (!h2) {
    if (!h2)  return mrb_true_value();
    return mrb_false_value();
  }
  if (!h2) return mrb_false_value();
  if (kh_size(h1) != kh_size(h2)) return mrb_false_value();
  else {
    khiter_t k1, k2;
    mrb_value key;

    for (k1 = kh_begin(h1); k1 != kh_end(h1); k1++) {
      if (!kh_exist(h1, k1)) continue;
      key = kh_key(h1,k1);
      k2 = kh_get(ht, h2, key);
      if (k2 != kh_end(h2)) {
	if (mrb_equal(mrb, kh_value(h1,k1), kh_value(h2,k2))) {
	  continue; /* next key */
	}
      }
      return mrb_false_value();
    }
  }
  return mrb_true_value();
}
Exemple #11
0
static inline khint_t
mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b)
{
  enum mrb_vtype t = mrb_type(a);

  switch (t) {
  case MRB_TT_STRING:
    return mrb_str_equal(mrb, a, b);

  case MRB_TT_SYMBOL:
    if (mrb_type(b) != MRB_TT_SYMBOL) return FALSE;
    return mrb_symbol(a) == mrb_symbol(b);

  case MRB_TT_FIXNUM:
    switch (mrb_type(b)) {
    case MRB_TT_FIXNUM:
      return mrb_fixnum(a) == mrb_fixnum(b);
    case MRB_TT_FLOAT:
      return (mrb_float)mrb_fixnum(a) == mrb_float(b);
    default:
      return FALSE;
    }

  case MRB_TT_FLOAT:
    switch (mrb_type(b)) {
    case MRB_TT_FIXNUM:
      return mrb_float(a) == (mrb_float)mrb_fixnum(b);
    case MRB_TT_FLOAT:
      return mrb_float(a) == mrb_float(b);
    default:
      return FALSE;
    }

  default:
    return mrb_eql(mrb, a, b);
  }
}
Exemple #12
0
static inline khint_t
mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b)
{
  return mrb_eql(mrb, a, b);
}