ruby-changes:7834
From: tadf <ko1@a...>
Date: Mon, 15 Sep 2008 13:21:06 +0900 (JST)
Subject: [ruby-changes:7834] Ruby:r19355 (trunk): * complex.c (nucomp_eql_p): new.
tadf 2008-09-15 13:20:46 +0900 (Mon, 15 Sep 2008) New Revision: 19355 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19355 Log: * complex.c (nucomp_eql_p): new. * complex.c (nucomp_hash): should use hash values of the elements. * rational.c (nurat_hash): ditto. Modified files: trunk/ChangeLog trunk/complex.c trunk/hash.c trunk/rational.c trunk/test/ruby/test_complex.rb Index: complex.c =================================================================== --- complex.c (revision 19354) +++ complex.c (revision 19355) @@ -23,7 +23,7 @@ static ID id_Unify, id_abs, id_abs2, id_arg, id_cmp, id_conjugate, id_convert, id_denominator, id_divmod, id_equal_p, id_exact_p, id_expt, - id_floor, id_format, id_idiv, id_inspect, id_negate, id_new, id_new_bang, + id_floor, id_hash, id_idiv, id_inspect, id_negate, id_new, id_new_bang, id_numerator, id_polar, id_quo, id_scalar_p, id_to_f, id_to_i, id_to_r, id_to_s, id_truncate; @@ -163,6 +163,7 @@ fun1(denominator) fun1(exact_p) fun1(floor) +fun1(hash) fun1(inspect) fun1(negate) fun1(numerator) @@ -847,9 +848,23 @@ nucomp_hash(VALUE self) { get_dat1(self); - return f_xor(dat->real, dat->image); + return f_xor(f_hash(dat->real), f_hash(dat->image)); } +static VALUE +nucomp_eql_p(VALUE self, VALUE other) +{ + if (k_complex_p(other)) { + get_dat2(self, other); + + return f_boolcast((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) && + (CLASS_OF(adat->image) == CLASS_OF(bdat->image)) && + f_equal_p(self, other)); + + } + return Qfalse; +} + #ifndef HAVE_SIGNBIT #ifdef signbit #define HAVE_SIGNBIT 1 @@ -1354,7 +1369,7 @@ id_exact_p = rb_intern("exact?"); id_expt = rb_intern("**"); id_floor = rb_intern("floor"); - id_format = rb_intern("format"); + id_hash = rb_intern("hash"); id_idiv = rb_intern("div"); id_inspect = rb_intern("inspect"); id_negate = rb_intern("-@"); @@ -1451,6 +1466,7 @@ rb_define_method(rb_cComplex, "denominator", nucomp_denominator, 0); rb_define_method(rb_cComplex, "hash", nucomp_hash, 0); + rb_define_method(rb_cComplex, "eql?", nucomp_eql_p, 1); rb_define_method(rb_cComplex, "to_s", nucomp_to_s, 0); rb_define_method(rb_cComplex, "inspect", nucomp_inspect, 0); Index: ChangeLog =================================================================== --- ChangeLog (revision 19354) +++ ChangeLog (revision 19355) @@ -1,3 +1,11 @@ +Mon Sep 15 13:17:21 2008 Tadayoshi Funaba <tadf@d...> + + * complex.c (nucomp_eql_p): new. + + * complex.c (nucomp_hash): should use hash values of the elements. + + * rational.c (nurat_hash): ditto. + Mon Sep 15 11:11:04 2008 Tanaka Akira <akr@f...> * transcode_data.h (rb_transcoder): resetsize_func and resetstate_func Index: hash.c =================================================================== --- hash.c (revision 19354) +++ hash.c (revision 19355) @@ -39,8 +39,6 @@ static int rb_any_cmp(VALUE a, VALUE b) { - VALUE args[2]; - if (a == b) return 0; if (FIXNUM_P(a) && FIXNUM_P(b)) { return a != b; @@ -54,8 +52,6 @@ return a != b; } - args[0] = a; - args[1] = b; return !rb_eql(a, b); } Index: test/ruby/test_complex.rb =================================================================== --- test/ruby/test_complex.rb (revision 19354) +++ test/ruby/test_complex.rb (revision 19355) @@ -51,6 +51,7 @@ def test_hash assert_instance_of(Fixnum, Complex(1,2).hash) + assert_instance_of(Fixnum, Complex(1.0,2.0).hash) h = {} h[Complex(0)] = 0 @@ -63,6 +64,9 @@ h[Complex(0,0)] = 9 assert_equal(4, h.size) + + h[Complex(0.0,0.0)] = 9.0 + assert_equal(5, h.size) end def test_freeze Index: rational.c =================================================================== --- rational.c (revision 19354) +++ rational.c (revision 19355) @@ -27,8 +27,8 @@ VALUE rb_cRational; static ID id_Unify, id_abs, id_cmp, id_convert, id_equal_p, id_expt, - id_floor, id_format, id_idiv, id_inspect, id_integer_p, id_negate, - id_new, id_new_bang, id_to_f, id_to_i, id_to_s, id_truncate; + id_floor, id_format, id_hash, id_idiv, id_inspect, id_integer_p, + id_negate, id_new, id_new_bang, id_to_f, id_to_i, id_to_s, id_truncate; #define f_boolcast(x) ((x) ? Qtrue : Qfalse) @@ -139,6 +139,7 @@ fun1(abs) fun1(floor) +fun1(hash) fun1(inspect) fun1(integer_p) fun1(negate) @@ -1486,6 +1487,7 @@ id_expt = rb_intern("**"); id_floor = rb_intern("floor"); id_format = rb_intern("format"); + id_hash = rb_intern("hash"); id_idiv = rb_intern("div"); id_inspect = rb_intern("inspect"); id_integer_p = rb_intern("integer?"); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/