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

ruby-changes:44662

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 15:07:07 +0900 (JST)
Subject: [ruby-changes:44662] mrkn:r56735 (trunk): rational.c: optimize Rational#-@

mrkn	2016-11-12 15:07:01 +0900 (Sat, 12 Nov 2016)

  New Revision: 56735

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56735

  Log:
    rational.c: optimize Rational#-@
    
    * rational.c (nurat_negate): introduced to optimize Rational#-@.
      Author: Tadashi Saito <tad.a.digger@g...>

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56734)
+++ rational.c	(revision 56735)
@@ -637,6 +637,19 @@ nurat_denominator(VALUE self) https://github.com/ruby/ruby/blob/trunk/rational.c#L637
     return dat->den;
 }
 
+/*
+ * call-seq:
+ *    -rat  ->  rational
+ *
+ * Negates +rat+.
+ */
+static VALUE
+nurat_negate(VALUE self)
+{
+    get_dat1(self);
+    return f_rational_new2(CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
+}
+
 #ifndef NDEBUG
 #define f_imul f_imul_orig
 #endif
@@ -2591,6 +2604,7 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2604
     rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
     rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
 
+    rb_define_method(rb_cRational, "-@", nurat_negate, 0);
     rb_define_method(rb_cRational, "+", rb_rational_plus, 1);
     rb_define_method(rb_cRational, "-", nurat_sub, 1);
     rb_define_method(rb_cRational, "*", nurat_mul, 1);

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

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