diff options
author | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-15 04:20:46 +0000 |
---|---|---|
committer | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-15 04:20:46 +0000 |
commit | 05ac51d2258b7681df7484a4e0ca787de84c7af9 (patch) | |
tree | 67826d260d299bc16de16604fc7e48b85e353638 | |
parent | 0675246ba6456ee4bad43d0fa13b401ce92d6309 (diff) |
* complex.c (nucomp_eql_p): new.
* complex.c (nucomp_hash): should use hash values of the elements.
* rational.c (nurat_hash): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | complex.c | 22 | ||||
-rw-r--r-- | hash.c | 4 | ||||
-rw-r--r-- | rational.c | 6 | ||||
-rw-r--r-- | test/ruby/test_complex.rb | 4 |
5 files changed, 35 insertions, 9 deletions
@@ -1,3 +1,11 @@ +Mon Sep 15 13:17:21 2008 Tadayoshi Funaba <tadf@dotrb.org> + + * 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@fsij.org> * transcode_data.h (rb_transcoder): resetsize_func and resetstate_func @@ -23,7 +23,7 @@ VALUE rb_cComplex; 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(conjugate) fun1(denominator) fun1(exact_p) fun1(floor) +fun1(hash) fun1(inspect) fun1(negate) fun1(numerator) @@ -847,7 +848,21 @@ static VALUE 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 @@ -1354,7 +1369,7 @@ Init_Complex(void) 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 @@ Init_Complex(void) 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); @@ -39,8 +39,6 @@ static ID id_hash, id_yield, id_default; 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 @@ rb_any_cmp(VALUE a, VALUE b) return a != b; } - args[0] = a; - args[1] = b; return !rb_eql(a, b); } diff --git a/rational.c b/rational.c index c4411d4425..6cc75c8d7f 100644 --- a/rational.c +++ b/rational.c @@ -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 @@ binop(xor, '^') fun1(abs) fun1(floor) +fun1(hash) fun1(inspect) fun1(integer_p) fun1(negate) @@ -1486,6 +1487,7 @@ Init_Rational(void) 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?"); diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 6f9cabb7b9..493e2e2c39 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -51,6 +51,7 @@ class Complex_Test < Test::Unit::TestCase 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 @@ class Complex_Test < Test::Unit::TestCase 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 |