* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54152 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2016-03-17 12:37:20 +00:00
Родитель 90ab5beb42
Коммит 4ca0483a28
4 изменённых файлов: 29 добавлений и 9 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Thu Mar 17 21:35:52 2016 Yusuke Endoh <mame@ruby-lang.org>
* 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@ruby-lang.org>
* array.c (rb_ary_max, rb_ary_min): implement a block by itself instead

Просмотреть файл

@ -4211,7 +4211,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary)
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 ary)
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++) {

24
enum.c
Просмотреть файл

@ -1374,8 +1374,8 @@ nmin_i(VALUE i, VALUE *_data, int argc, VALUE *argv)
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, int rev)
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 obj)
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 obj)
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 obj)
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 obj)
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);

Просмотреть файл

@ -806,6 +806,7 @@ int rb_data_is_encoding(VALUE obj);
/* 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;