summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-19 11:11:25 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-19 11:11:25 +0000
commit80627a281e22589c8f35d31f99469daa31a2a75f (patch)
treeab7fbbd0205b87b1aac6b75cf0b54efec7832c07
parent932ac5d35ddc129b209161a6ae4d3356979554af (diff)
* 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
-rw-r--r--ChangeLog9
-rw-r--r--NEWS13
-rw-r--r--array.c111
-rw-r--r--hash.c93
4 files changed, 189 insertions, 37 deletions
diff --git a/ChangeLog b/ChangeLog
index 8c1cbb3d6c..a01d340ce1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Sat Apr 19 20:05:39 2008 Akinori MUSHA <[email protected]>
+
+ * 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.
+
Sat Apr 19 19:26:09 2008 Akinori MUSHA <[email protected]>
* intern.h, eval.c (rb_exec_recursive): New internal function to
diff --git a/NEWS b/NEWS
index bafff6e959..722dd8fe16 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,13 @@ with all sufficient information, see the ChangeLog file.
Takes an optional argument that determines the level of recursion
to flatten.
+ * Array#eql?
+ * Array#hash
+ * Array#==
+ * Array#<=>
+
+ Handle recursive data properly.
+
* Array#index
* Array#rindex
@@ -122,6 +129,12 @@ with all sufficient information, see the ChangeLog file.
New alias to #inject.
+ * Hash#eql?
+ * Hash#hash
+ * Hash#==
+
+ Handle recursive data properly.
+
* Hash#delete_if
* Hash#each
* Hash#each_key
diff --git a/array.c b/array.c
index 1ab1716390..3789e4f6d9 100644
--- a/array.c
+++ b/array.c
@@ -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);
diff --git a/hash.c b/hash.c
index f7ad09032b..cb8e63a3e7 100644
--- a/hash.c
+++ b/hash.c
@@ -1501,11 +1501,13 @@ rb_hash_has_value(hash, val)
struct equal_data {
int result;
st_table *tbl;
+ int eql;
};
static int
-equal_i(key, val1, data)
- VALUE key, val1;
+eql_i(key, val1, data)
+ VALUE key;
+ VALUE val1;
struct equal_data *data;
{
VALUE val2;
@@ -1515,13 +1517,30 @@ equal_i(key, val1, data)
data->result = Qfalse;
return ST_STOP;
}
- if (!rb_equal(val1, val2)) {
+ if (!(data->eql ? rb_eql(val1, val2) : rb_equal(val1, val2))) {
data->result = Qfalse;
return ST_STOP;
}
return ST_CONTINUE;
}
+static VALUE recursive_eql _((VALUE, VALUE, int));
+static VALUE
+recursive_eql(hash, dt, recur)
+ VALUE hash;
+ VALUE dt;
+ int recur;
+{
+ struct equal_data *data;
+
+ 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
hash_equal(hash1, hash2, eql)
VALUE hash1, hash2;
@@ -1545,10 +1564,8 @@ hash_equal(hash1, hash2, eql)
}
data.tbl = RHASH(hash2)->tbl;
- data.result = Qtrue;
- rb_hash_foreach(hash1, equal_i, (st_data_t)&data);
-
- return data.result;
+ data.eql = eql;
+ return rb_exec_recursive(recursive_eql, hash1, (VALUE)&data);
}
/*
@@ -1578,6 +1595,66 @@ rb_hash_equal(hash1, hash2)
}
static int
+hash_i(key, val, hval)
+ VALUE key;
+ VALUE val;
+ int *hval;
+{
+ if (key == Qundef) return ST_CONTINUE;
+ *hval ^= rb_hash(key);
+ *hval *= 137;
+ *hval ^= rb_hash(val);
+ return ST_CONTINUE;
+}
+
+static VALUE
+recursive_hash(hash, dummy, recur)
+ VALUE hash;
+ VALUE dummy;
+ int recur;
+{
+ int hval;
+
+ if (recur) {
+ return LONG2FIX(0);
+ }
+ hval = RHASH(hash)->tbl->num_entries;
+ rb_hash_foreach(hash, hash_i, (st_data_t)&hval);
+ return INT2FIX(hval);
+}
+
+/*
+ * 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_hash_hash(hash)
+ VALUE hash;
+{
+ return rb_exec_recursive(recursive_hash, hash, 0);
+}
+
+
+/*
+ * call-seq:
+ * hash.eql?(other) -> true or false
+ *
+ * Returns <code>true</code> if <i>hash</i> and <i>other</i> are
+ * both hashes with the same content.
+ */
+
+static VALUE
+rb_hash_eql(hash1, hash2)
+ VALUE hash1, hash2;
+{
+ return hash_equal(hash1, hash2, Qtrue);
+}
+
+static int
rb_hash_invert_i(key, value, hash)
VALUE key, value;
VALUE hash;
@@ -2545,6 +2622,8 @@ Init_Hash()
rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
rb_define_method(rb_cHash,"[]", rb_hash_aref, 1);
+ rb_define_method(rb_cHash,"hash", rb_hash_hash, 0);
+ rb_define_method(rb_cHash,"eql?", rb_hash_eql, 1);
rb_define_method(rb_cHash,"fetch", rb_hash_fetch, -1);
rb_define_method(rb_cHash,"[]=", rb_hash_aset, 2);
rb_define_method(rb_cHash,"store", rb_hash_aset, 2);