[前][次][番号順一覧][スレッド一覧]

ruby-changes:66159

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Wed, 12 May 2021 10:31:10 +0900 (JST)
Subject: [ruby-changes:66159] cc0dc67bbb (master): cdhash_cmp: can also take complex

https://git.ruby-lang.org/ruby.git/commit/?id=cc0dc67bbb

From cc0dc67bbbe1951ff90004bc987f78545625d772 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Fri, 7 May 2021 12:49:32 +0900
Subject: cdhash_cmp: can also take complex

There are complex literals `123i`, which can also be a case condition.
---
 compile.c                  | 15 +++++++++++----
 complex.c                  | 12 +++++++++---
 internal/complex.h         |  1 +
 test/ruby/test_rational.rb |  4 ++++
 4 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/compile.c b/compile.c
index 341361d..d9894d2 100644
--- a/compile.c
+++ b/compile.c
@@ -2005,10 +2005,15 @@ cdhash_cmp(VALUE val, VALUE lit) https://github.com/ruby/ruby/blob/trunk/compile.c#L2005
     else if (tlit == T_FLOAT) {
         return rb_float_cmp(lit, val);
     }
-    else if (tlit ==  T_RATIONAL) {
-        const struct RRational *dat1 = RRATIONAL(val);
-        const struct RRational *dat2 = RRATIONAL(lit);
-        return (dat1->num == dat2->num) && (dat1->den == dat2->den);
+    else if (tlit == T_RATIONAL) {
+        const struct RRational *rat1 = RRATIONAL(val);
+        const struct RRational *rat2 = RRATIONAL(lit);
+        return (rat1->num == rat2->num) && (rat1->den == rat2->den);
+    }
+    else if (tlit == T_COMPLEX) {
+        const struct RComplex *comp1 = RCOMPLEX(val);
+        const struct RComplex *comp2 = RCOMPLEX(lit);
+        return (comp1->real == comp2->real) && (comp1->imag == comp2->imag);
     }
     else {
         UNREACHABLE_RETURN(-1);
@@ -2030,6 +2035,8 @@ cdhash_hash(VALUE a) https://github.com/ruby/ruby/blob/trunk/compile.c#L2035
         return rb_dbl_long_hash(RFLOAT_VALUE(a));
       case T_RATIONAL:
         return rb_rational_hash(a);
+      case T_COMPLEX:
+        return rb_complex_hash(a);
       default:
         UNREACHABLE_RETURN(0);
     }
diff --git a/complex.c b/complex.c
index fe8def2..66d9754 100644
--- a/complex.c
+++ b/complex.c
@@ -1326,8 +1326,8 @@ nucomp_numerator(VALUE self) https://github.com/ruby/ruby/blob/trunk/complex.c#L1326
 }
 
 /* :nodoc: */
-static VALUE
-nucomp_hash(VALUE self)
+st_index_t
+rb_complex_hash(VALUE self)
 {
     st_index_t v, h[2];
     VALUE n;
@@ -1338,7 +1338,13 @@ nucomp_hash(VALUE self) https://github.com/ruby/ruby/blob/trunk/complex.c#L1338
     n = rb_hash(dat->imag);
     h[1] = NUM2LONG(n);
     v = rb_memhash(h, sizeof(h));
-    return ST2FIX(v);
+    return v;
+}
+
+static VALUE
+nucomp_hash(VALUE self)
+{
+    return ST2FIX(rb_complex_hash(self));
 }
 
 /* :nodoc: */
diff --git a/internal/complex.h b/internal/complex.h
index 6f63a9e..9eae804 100644
--- a/internal/complex.h
+++ b/internal/complex.h
@@ -25,5 +25,6 @@ struct RComplex { https://github.com/ruby/ruby/blob/trunk/internal/complex.h#L25
 
 /* complex.c */
 VALUE rb_dbl_complex_new_polar_pi(double abs, double ang);
+st_index_t rb_complex_hash(VALUE comp);
 
 #endif /* INTERNAL_COMPLEX_H */
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index 5262b6e..27d0527 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -839,6 +839,10 @@ class Rational_Test < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L839
       n = case 3/2r when 1.5r then true else false end
       assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
     RUBY
+    assert_separately([], <<-RUBY)
+      n = case 1i when 1i then true else false end
+      assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
+    RUBY
   end
 
   def test_Rational_with_invalid_exception
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]