* array.c (rb_ary_max, rb_ary_min): implement a block by itself instead

of delegating Enumerable#max/min.

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

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

@ -1,3 +1,8 @@
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
of delegating Enumerable#max/min.
Thu Mar 17 21:09:34 2016 Yusuke Endoh <mame@ruby-lang.org>
* array.c (rb_ary_max, rb_ary_min): Array#max and Array#min added.

44
array.c
Просмотреть файл

@ -4210,14 +4210,24 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary)
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 ary)
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;