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

ruby-changes:42077

From: mame <ko1@a...>
Date: Thu, 17 Mar 2016 21:25:45 +0900 (JST)
Subject: [ruby-changes:42077] mame:r54151 (trunk): * array.c (rb_ary_max, rb_ary_min): implement a block by itself instead

mame	2016-03-17 21:25:40 +0900 (Thu, 17 Mar 2016)

  New Revision: 54151

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

  Log:
    * array.c (rb_ary_max, rb_ary_min): implement a block by itself instead
      of delegating Enumerable#max/min.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 54150)
+++ array.c	(revision 54151)
@@ -4210,14 +4210,24 @@ rb_ary_max(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4210
 
     rb_scan_args(argc, argv, "01", &num);
 
-    if (!NIL_P(num) || rb_block_given_p())
+    if (!NIL_P(num))
        return rb_call_super(argc, argv); /* XXX: should redefine? */
 
-    for (i = 0; i < RARRAY_LEN(ary); i++) {
-       v = RARRAY_AREF(ary, i);
-       if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) > 0) {
-           result = v;
-       }
+    if (rb_block_given_p()) {
+	for (i = 0; i < RARRAY_LEN(ary); i++) {
+	   v = RARRAY_AREF(ary, i);
+	   if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) > 0) {
+	       result = v;
+	   }
+	}
+    }
+    else {
+	for (i = 0; i < RARRAY_LEN(ary); i++) {
+	   v = RARRAY_AREF(ary, i);
+	   if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) > 0) {
+	       result = v;
+	   }
+	}
     }
     if (result == Qundef) return Qnil;
     return result;
@@ -4255,14 +4265,24 @@ rb_ary_min(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4265
 
     rb_scan_args(argc, argv, "01", &num);
 
-    if (!NIL_P(num) || rb_block_given_p())
+    if (!NIL_P(num))
        return rb_call_super(argc, argv); /* XXX: should redefine? */
 
-    for (i = 0; i < RARRAY_LEN(ary); i++) {
-       v = RARRAY_AREF(ary, i);
-       if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) < 0) {
-           result = v;
-       }
+    if (rb_block_given_p()) {
+	for (i = 0; i < RARRAY_LEN(ary); i++) {
+	   v = RARRAY_AREF(ary, i);
+	   if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) < 0) {
+	       result = v;
+	   }
+	}
+    }
+    else {
+	for (i = 0; i < RARRAY_LEN(ary); i++) {
+	   v = RARRAY_AREF(ary, i);
+	   if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) < 0) {
+	       result = v;
+	   }
+	}
     }
     if (result == Qundef) return Qnil;
     return result;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54150)
+++ ChangeLog	(revision 54151)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Mar 17 21:24:52 2016  Yusuke Endoh  <mame@r...>
+
+	* array.c (rb_ary_max, rb_ary_min): implement a block by itself instead
+	  of delegating Enumerable#max/min.
+
 Thu Mar 17 21:09:34 2016  Yusuke Endoh  <mame@r...>
 
 	* array.c (rb_ary_max, rb_ary_min): Array#max and Array#min added.

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

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