1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* array.c (rb_ary_equal): should handle recursive array.

* hash.c (hash_equal): should handle recursive hash.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13509 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-09-24 13:43:58 +00:00
parent 5f93fa308b
commit c1a432a7d9
3 changed files with 34 additions and 26 deletions

View file

@ -1,3 +1,9 @@
Mon Sep 24 22:36:13 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* array.c (rb_ary_equal): should handle recursive array.
* hash.c (hash_equal): should handle recursive hash.
Mon Sep 24 22:14:13 2007 Yukihiro Matsumoto <matz@ruby-lang.org> Mon Sep 24 22:14:13 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/weakref.rb (WeakRef): remove debug print. [ruby-dev:31799] * lib/weakref.rb (WeakRef): remove debug print. [ruby-dev:31799]

21
array.c
View file

@ -2328,6 +2328,19 @@ rb_ary_rassoc(VALUE ary, VALUE value)
return Qnil; return Qnil;
} }
static VALUE
recursive_equal(VALUE ary1, VALUE ary2, int recur)
{
long i;
if (recur) return Qfalse;
for (i=0; i<RARRAY_LEN(ary1); i++) {
if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
return Qfalse;
}
return Qtrue;
}
/* /*
* call-seq: * call-seq:
* array == other_array -> bool * array == other_array -> bool
@ -2345,8 +2358,6 @@ rb_ary_rassoc(VALUE ary, VALUE value)
static VALUE static VALUE
rb_ary_equal(VALUE ary1, VALUE ary2) rb_ary_equal(VALUE ary1, VALUE ary2)
{ {
long i;
if (ary1 == ary2) return Qtrue; if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) { if (TYPE(ary2) != T_ARRAY) {
if (!rb_respond_to(ary2, rb_intern("to_ary"))) { if (!rb_respond_to(ary2, rb_intern("to_ary"))) {
@ -2355,11 +2366,7 @@ rb_ary_equal(VALUE ary1, VALUE ary2)
return rb_equal(ary2, ary1); return rb_equal(ary2, ary1);
} }
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse; if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
for (i=0; i<RARRAY_LEN(ary1); i++) { return rb_exec_recursive(recursive_equal, ary1, ary2);
if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
return Qfalse;
}
return Qtrue;
} }
/* /*

33
hash.c
View file

@ -1350,8 +1350,9 @@ rb_hash_has_value(VALUE hash, VALUE val)
} }
struct equal_data { struct equal_data {
int result; VALUE result;
st_table *tbl; st_table *tbl;
int eql;
}; };
static int static int
@ -1364,28 +1365,24 @@ eql_i(VALUE key, VALUE val1, struct equal_data *data)
data->result = Qfalse; data->result = Qfalse;
return ST_STOP; return ST_STOP;
} }
if (!rb_eql(val1, val2)) { if (!(data->eql ? rb_eql(val1, val2) : rb_equal(val1, val2))) {
data->result = Qfalse; data->result = Qfalse;
return ST_STOP; return ST_STOP;
} }
return ST_CONTINUE; return ST_CONTINUE;
} }
static int static VALUE
equal_i(VALUE key, VALUE val1, struct equal_data *data) recursive_eql(VALUE hash, VALUE dt, int recur)
{ {
VALUE val2; struct equal_data *data;
if (key == Qundef) return ST_CONTINUE; if (recur) return Qfalse;
if (!st_lookup(data->tbl, key, &val2)) { data = (struct equal_data*)dt;
data->result = Qfalse; data->result = Qtrue;
return ST_STOP; rb_hash_foreach(hash, eql_i, (st_data_t)data);
}
if (!rb_equal(val1, val2)) { return data->result;
data->result = Qfalse;
return ST_STOP;
}
return ST_CONTINUE;
} }
static VALUE static VALUE
@ -1414,10 +1411,8 @@ hash_equal(VALUE hash1, VALUE hash2, int eql)
#endif #endif
data.tbl = RHASH(hash2)->ntbl; data.tbl = RHASH(hash2)->ntbl;
data.result = Qtrue; data.eql = eql;
rb_hash_foreach(hash1, eql ? eql_i : equal_i, (st_data_t)&data); return rb_exec_recursive(recursive_eql, hash1, (VALUE)&data);
return data.result;
} }
/* /*