зеркало из https://github.com/github/ruby.git
* 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:
Родитель
5f93fa308b
Коммит
c1a432a7d9
|
@ -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>
|
||||
|
||||
* lib/weakref.rb (WeakRef): remove debug print. [ruby-dev:31799]
|
||||
|
|
21
array.c
21
array.c
|
@ -2328,6 +2328,19 @@ rb_ary_rassoc(VALUE ary, VALUE value)
|
|||
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:
|
||||
* array == other_array -> bool
|
||||
|
@ -2345,8 +2358,6 @@ rb_ary_rassoc(VALUE ary, VALUE value)
|
|||
static VALUE
|
||||
rb_ary_equal(VALUE ary1, VALUE ary2)
|
||||
{
|
||||
long i;
|
||||
|
||||
if (ary1 == ary2) return Qtrue;
|
||||
if (TYPE(ary2) != T_ARRAY) {
|
||||
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);
|
||||
}
|
||||
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) 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;
|
||||
return rb_exec_recursive(recursive_equal, ary1, ary2);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
33
hash.c
33
hash.c
|
@ -1350,8 +1350,9 @@ rb_hash_has_value(VALUE hash, VALUE val)
|
|||
}
|
||||
|
||||
struct equal_data {
|
||||
int result;
|
||||
VALUE result;
|
||||
st_table *tbl;
|
||||
int eql;
|
||||
};
|
||||
|
||||
static int
|
||||
|
@ -1364,28 +1365,24 @@ eql_i(VALUE key, VALUE val1, struct equal_data *data)
|
|||
data->result = Qfalse;
|
||||
return ST_STOP;
|
||||
}
|
||||
if (!rb_eql(val1, val2)) {
|
||||
if (!(data->eql ? rb_eql(val1, val2) : rb_equal(val1, val2))) {
|
||||
data->result = Qfalse;
|
||||
return ST_STOP;
|
||||
}
|
||||
return ST_CONTINUE;
|
||||
}
|
||||
|
||||
static int
|
||||
equal_i(VALUE key, VALUE val1, struct equal_data *data)
|
||||
static VALUE
|
||||
recursive_eql(VALUE hash, VALUE dt, int recur)
|
||||
{
|
||||
VALUE val2;
|
||||
struct equal_data *data;
|
||||
|
||||
if (key == Qundef) return ST_CONTINUE;
|
||||
if (!st_lookup(data->tbl, key, &val2)) {
|
||||
data->result = Qfalse;
|
||||
return ST_STOP;
|
||||
}
|
||||
if (!rb_equal(val1, val2)) {
|
||||
data->result = Qfalse;
|
||||
return ST_STOP;
|
||||
}
|
||||
return ST_CONTINUE;
|
||||
if (recur) return Qfalse;
|
||||
data = (struct equal_data*)dt;
|
||||
data->result = Qtrue;
|
||||
rb_hash_foreach(hash, eql_i, (st_data_t)data);
|
||||
|
||||
return data->result;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -1414,10 +1411,8 @@ hash_equal(VALUE hash1, VALUE hash2, int eql)
|
|||
#endif
|
||||
|
||||
data.tbl = RHASH(hash2)->ntbl;
|
||||
data.result = Qtrue;
|
||||
rb_hash_foreach(hash1, eql ? eql_i : equal_i, (st_data_t)&data);
|
||||
|
||||
return data.result;
|
||||
data.eql = eql;
|
||||
return rb_exec_recursive(recursive_eql, hash1, (VALUE)&data);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Загрузка…
Ссылка в новой задаче