ruby-changes:42078
From: mame <ko1@a...>
Date: Thu, 17 Mar 2016 21:37:26 +0900 (JST)
Subject: [ruby-changes:42078] mame:r54152 (trunk): * array.c (rb_ary_max, rb_ary_min): implement Array#max and min with
mame 2016-03-17 21:37:20 +0900 (Thu, 17 Mar 2016) New Revision: 54152 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54152 Log: * array.c (rb_ary_max, rb_ary_min): implement Array#max and min with arguments. replace super call with rb_nmin_run. * enum.c (nmin_run): exported (as rb_nmin_run). * internal.h: added a prototype for rb_nmin_run. Modified files: trunk/ChangeLog trunk/array.c trunk/enum.c trunk/internal.h Index: array.c =================================================================== --- array.c (revision 54151) +++ array.c (revision 54152) @@ -4211,7 +4211,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4211 rb_scan_args(argc, argv, "01", &num); if (!NIL_P(num)) - return rb_call_super(argc, argv); /* XXX: should redefine? */ + return rb_nmin_run(ary, num, 0, 1, 1); if (rb_block_given_p()) { for (i = 0; i < RARRAY_LEN(ary); i++) { @@ -4266,7 +4266,7 @@ rb_ary_min(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4266 rb_scan_args(argc, argv, "01", &num); if (!NIL_P(num)) - return rb_call_super(argc, argv); /* XXX: should redefine? */ + return rb_nmin_run(ary, num, 0, 0, 1); if (rb_block_given_p()) { for (i = 0; i < RARRAY_LEN(ary); i++) { Index: ChangeLog =================================================================== --- ChangeLog (revision 54151) +++ ChangeLog (revision 54152) @@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Mar 17 21:35:52 2016 Yusuke Endoh <mame@r...> + + * array.c (rb_ary_max, rb_ary_min): implement Array#max and min with + arguments. replace super call with rb_nmin_run. + + * enum.c (nmin_run): exported (as rb_nmin_run). + + * internal.h: added a prototype for rb_nmin_run. + 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 Index: enum.c =================================================================== --- enum.c (revision 54151) +++ enum.c (revision 54152) @@ -1374,8 +1374,8 @@ nmin_i(VALUE i, VALUE *_data, int argc, https://github.com/ruby/ruby/blob/trunk/enum.c#L1374 return Qnil; } -static VALUE -nmin_run(VALUE obj, VALUE num, int by, int rev) +VALUE +rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary) { VALUE result; struct nmin_data data; @@ -1398,7 +1398,17 @@ nmin_run(VALUE obj, VALUE num, int by, i https://github.com/ruby/ruby/blob/trunk/enum.c#L1398 data.by = by; data.method = rev ? (by ? "max_by" : "max") : (by ? "min_by" : "min"); - rb_block_call(obj, id_each, 0, 0, nmin_i, (VALUE)&data); + if (ary) { + long i; + for (i = 0; i < RARRAY_LEN(obj); i++) { + VALUE args[1]; + args[0] = RARRAY_AREF(obj, i); + nmin_i(obj, (VALUE*)&data, 1, args); + } + } + else { + rb_block_call(obj, id_each, 0, 0, nmin_i, (VALUE)&data); + } nmin_filter(&data); result = data.buf; if (by) { @@ -1564,7 +1574,7 @@ enum_min(int argc, VALUE *argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1574 rb_scan_args(argc, argv, "01", &num); if (!NIL_P(num)) - return nmin_run(obj, num, 0, 0); + return rb_nmin_run(obj, num, 0, 0, 0); m->min = Qundef; m->cmp_opt.opt_methods = 0; @@ -1657,7 +1667,7 @@ enum_max(int argc, VALUE *argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1667 rb_scan_args(argc, argv, "01", &num); if (!NIL_P(num)) - return nmin_run(obj, num, 0, 1); + return rb_nmin_run(obj, num, 0, 1, 0); m->max = Qundef; m->cmp_opt.opt_methods = 0; @@ -1878,7 +1888,7 @@ enum_min_by(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L1888 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); if (!NIL_P(num)) - return nmin_run(obj, num, 1, 0); + return rb_nmin_run(obj, num, 1, 0, 0); memo = MEMO_NEW(Qundef, Qnil, 0); rb_block_call(obj, id_each, 0, 0, min_by_i, (VALUE)memo); @@ -1983,7 +1993,7 @@ enum_max_by(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L1993 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); if (!NIL_P(num)) - return nmin_run(obj, num, 1, 1); + return rb_nmin_run(obj, num, 1, 1, 0); memo = MEMO_NEW(Qundef, Qnil, 0); rb_block_call(obj, id_each, 0, 0, max_by_i, (VALUE)memo); Index: internal.h =================================================================== --- internal.h (revision 54151) +++ internal.h (revision 54152) @@ -806,6 +806,7 @@ int rb_data_is_encoding(VALUE obj); https://github.com/ruby/ruby/blob/trunk/internal.h#L806 /* enum.c */ VALUE rb_f_send(int argc, VALUE *argv, VALUE recv); +VALUE rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary); /* error.c */ extern VALUE rb_eEAGAIN; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/