diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-19 11:11:25 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-19 11:11:25 +0000 |
commit | 80627a281e22589c8f35d31f99469daa31a2a75f (patch) | |
tree | ab7fbbd0205b87b1aac6b75cf0b54efec7832c07 /array.c | |
parent | 932ac5d35ddc129b209161a6ae4d3356979554af (diff) | |
download | ruby-80627a281e22589c8f35d31f99469daa31a2a75f.tar.gz |
* array.c (rb_ary_equal, rb_ary_eql, rb_ary_hash, rb_ary_cmp):
Make Array#eql?, #hash, #== and #<=> use rb_exec_recursive() and
handle recursive data properly.
* hash.c (hash_equal, rb_hash_hash): Make Hash#eql?, #hash and #==
use rb_exec_recursive() and handle recursive data properly.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16081 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 111 |
1 files changed, 81 insertions, 30 deletions
@@ -2595,6 +2595,22 @@ rb_ary_rassoc(ary, value) return Qnil; } +static VALUE recursive_equal _((VALUE, VALUE, int)); +static VALUE +recursive_equal(ary1, ary2, recur) + VALUE ary1, ary2; + int recur; +{ + long i; + + if (recur) return Qfalse; + for (i=0; i<RARRAY(ary1)->len; i++) { + if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i))) + return Qfalse; + } + return Qtrue; +} + /* * call-seq: * array == other_array -> bool @@ -2613,8 +2629,6 @@ static VALUE rb_ary_equal(ary1, ary2) VALUE ary1, ary2; { - long i; - if (ary1 == ary2) return Qtrue; if (TYPE(ary2) != T_ARRAY) { if (!rb_respond_to(ary2, rb_intern("to_ary"))) { @@ -2623,8 +2637,20 @@ rb_ary_equal(ary1, ary2) return rb_equal(ary2, ary1); } if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse; + return rb_exec_recursive(recursive_equal, ary1, ary2); +} + +static VALUE recursive_eql _((VALUE, VALUE, int)); +static VALUE +recursive_eql(ary1, ary2, recur) + VALUE ary1, ary2; + int recur; +{ + long i; + + if (recur) return Qfalse; for (i=0; i<RARRAY(ary1)->len; i++) { - if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i))) + if (!rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i))) return Qfalse; } return Qtrue; @@ -2642,33 +2668,26 @@ static VALUE rb_ary_eql(ary1, ary2) VALUE ary1, ary2; { - long i; - if (ary1 == ary2) return Qtrue; if (TYPE(ary2) != T_ARRAY) return Qfalse; if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse; - for (i=0; i<RARRAY(ary1)->len; i++) { - if (!rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i))) - return Qfalse; - } - return Qtrue; + return rb_exec_recursive(recursive_eql, ary1, ary2); } -/* - * call-seq: - * array.hash -> fixnum - * - * Compute a hash-code for this array. Two arrays with the same content - * will have the same hash code (and will compare using <code>eql?</code>). - */ - +static VALUE recursive_hash _((VALUE, VALUE, int)); static VALUE -rb_ary_hash(ary) +recursive_hash(ary, dummy, recur) VALUE ary; + VALUE dummy; + int recur; { long i, h; VALUE n; + if (recur) { + return LONG2FIX(0); + } + h = RARRAY(ary)->len; for (i=0; i<RARRAY(ary)->len; i++) { h = (h << 1) | (h<0 ? 1 : 0); @@ -2680,6 +2699,21 @@ rb_ary_hash(ary) /* * call-seq: + * array.hash -> fixnum + * + * Compute a hash-code for this array. Two arrays with the same content + * will have the same hash code (and will compare using <code>eql?</code>). + */ + +static VALUE +rb_ary_hash(ary) + VALUE ary; +{ + return rb_exec_recursive(recursive_hash, ary, 0); +} + +/* + * call-seq: * array.include?(obj) -> true or false * * Returns <code>true</code> if the given object is present in @@ -2707,6 +2741,29 @@ rb_ary_includes(ary, item) } +static VALUE recursive_cmp _((VALUE, VALUE, int)); +static VALUE +recursive_cmp(ary1, ary2, recur) + VALUE ary1; + VALUE ary2; + int recur; +{ + long i, len; + + if (recur) return Qnil; + len = RARRAY(ary1)->len; + if (len > RARRAY(ary2)->len) { + len = RARRAY(ary2)->len; + } + for (i=0; i<len; i++) { + VALUE v = rb_funcall(rb_ary_elt(ary1, i), id_cmp, 1, rb_ary_elt(ary2, i)); + if (v != INT2FIX(0)) { + return v; + } + } + return Qundef; +} + /* * call-seq: * array <=> other_array -> -1, 0, +1 @@ -2731,19 +2788,13 @@ VALUE rb_ary_cmp(ary1, ary2) VALUE ary1, ary2; { - long i, len; + long len; + VALUE v; ary2 = to_ary(ary2); - len = RARRAY(ary1)->len; - if (len > RARRAY(ary2)->len) { - len = RARRAY(ary2)->len; - } - for (i=0; i<len; i++) { - VALUE v = rb_funcall(rb_ary_elt(ary1, i), id_cmp, 1, rb_ary_elt(ary2, i)); - if (v != INT2FIX(0)) { - return v; - } - } + if (ary1 == ary2) return INT2FIX(0); + v = rb_exec_recursive(recursive_cmp, ary1, ary2); + if (v != Qundef) return v; len = RARRAY(ary1)->len - RARRAY(ary2)->len; if (len == 0) return INT2FIX(0); if (len > 0) return INT2FIX(1); |