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

ruby-changes:42966

From: mrkn <ko1@a...>
Date: Wed, 18 May 2016 09:16:14 +0900 (JST)
Subject: [ruby-changes:42966] mrkn:r55040 (trunk): Extract int_range_sum from enum_sum

mrkn	2016-05-18 09:16:06 +0900 (Wed, 18 May 2016)

  New Revision: 55040

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

  Log:
    Extract int_range_sum from enum_sum
    
    * enum.c (enum_sum, int_range_sum): Extract int_range_sum from
      enum_sum.

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
Index: enum.c
===================================================================
--- enum.c	(revision 55039)
+++ enum.c	(revision 55040)
@@ -3666,6 +3666,26 @@ enum_sum_iter_i(RB_BLOCK_CALL_FUNC_ARGLI https://github.com/ruby/ruby/blob/trunk/enum.c#L3666
     return Qnil;
 }
 
+static VALUE
+int_range_sum(VALUE beg, VALUE end, int excl, VALUE init)
+{
+    if (excl) {
+        if (FIXNUM_P(end))
+            end = LONG2FIX(FIX2LONG(end) - 1);
+        else
+            end = rb_big_minus(end, LONG2FIX(1));
+    }
+
+    if (rb_int_ge(end, beg)) {
+        VALUE a;
+        a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1));
+        a = rb_int_mul(a, rb_int_plus(end, beg));
+        a = rb_int_idiv(a, LONG2FIX(2));
+        return rb_int_plus(init, a);
+    }
+
+    return init;
+}
 
 /*
  * call-seq:
@@ -3719,22 +3739,7 @@ enum_sum(int argc, VALUE* argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L3739
         if (!memo.block_given && !memo.float_value &&
                 (FIXNUM_P(beg) || RB_TYPE_P(beg, T_BIGNUM)) &&
                 (FIXNUM_P(end) || RB_TYPE_P(end, T_BIGNUM))) {
-            if (excl) {
-                if (FIXNUM_P(end))
-                    end = LONG2FIX(FIX2LONG(end) - 1);
-                else
-                    end = rb_big_minus(end, LONG2FIX(1));
-            }
-            if (rb_int_ge(end, beg)) {
-                VALUE a;
-                a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1));
-                a = rb_int_mul(a, rb_int_plus(end, beg));
-                a = rb_int_idiv(a, LONG2FIX(2));
-                return rb_int_plus(memo.v, a);
-            }
-            else {
-                return memo.v;
-            }
+            return int_range_sum(beg, end, excl, memo.v);
         }
     }
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55039)
+++ ChangeLog	(revision 55040)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed May 18 09:14:00 2016  Kenta Murata  <mrkn@m...>
+
+	* enum.c (enum_sum, int_range_sum): Extract int_range_sum from
+	  enum_sum.
+
 Wed May 18 03:16:06 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* re.c (match_values_at): fix regression at r55036.
@@ -15,7 +20,7 @@ Wed May 18 01:57:43 2016  NARUSE, Yui  < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L20
 
 	* re.c (namev_to_backref_number): separeted.
 
-Tue May 18 00:05:00 2016  Kenta Murata  <mrkn@m...>
+Wed May 18 00:05:00 2016  Kenta Murata  <mrkn@m...>
 
 	* enum.c (enum_sum): Optimize for a range from int to int.
 

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

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