From 5ba025477764bb87251894ebec5c62780f8ebb65 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 29 Dec 2006 19:49:23 +0000 Subject: * enum.c (enum_each_with_index): reuse array for yield parameters. * enum.c (enum_min, enum_max): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ enum.c | 57 ++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index caf9b07c6c..7eb02d83c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sat Dec 30 04:38:23 2006 Yukihiro Matsumoto + + * enum.c (enum_each_with_index): reuse array for yield parameters. + + * enum.c (enum_min, enum_max): ditto. + Sat Dec 30 04:25:29 2006 Yukihiro Matsumoto * enum.c (enum_inject): reuse array for yield parameters. diff --git a/enum.c b/enum.c index e5be2e4753..c0a38aa125 100644 --- a/enum.c +++ b/enum.c @@ -887,7 +887,10 @@ min_ii(VALUE i, VALUE *memo) *memo = i; } else { - cmp = rb_yield_values(2, i, *memo); + VALUE ary = memo[1]; + RARRAY_PTR(ary)[0] = i; + RARRAY_PTR(ary)[1] = *memo; + cmp = rb_yield(ary); if (rb_cmpint(cmp, i, *memo) < 0) { *memo = i; } @@ -913,11 +916,18 @@ min_ii(VALUE i, VALUE *memo) static VALUE enum_min(VALUE obj) { - VALUE result = Qundef; + VALUE result[2]; - rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? min_ii : min_i, (VALUE)&result); - if (result == Qundef) return Qnil; - return result; + result[0] = Qundef; + if (rb_block_given_p()) { + result[1] = rb_ary_new3(2, Qnil, Qnil); + rb_block_call(obj, id_each, 0, 0, min_ii, (VALUE)result); + } + else { + rb_block_call(obj, id_each, 0, 0, min_i, (VALUE)result); + } + if (result[0] == Qundef) return Qnil; + return result[0]; } static VALUE @@ -946,7 +956,10 @@ max_ii(VALUE i, VALUE *memo) *memo = i; } else { - cmp = rb_yield_values(2, i, *memo); + VALUE ary = memo[1]; + RARRAY_PTR(ary)[0] = i; + RARRAY_PTR(ary)[1] = *memo; + cmp = rb_yield(ary); if (rb_cmpint(cmp, i, *memo) > 0) { *memo = i; } @@ -971,11 +984,18 @@ max_ii(VALUE i, VALUE *memo) static VALUE enum_max(VALUE obj) { - VALUE result = Qundef; + VALUE result[2]; - rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? max_ii : max_i, (VALUE)&result); - if (result == Qundef) return Qnil; - return result; + result[0] = Qundef; + if (rb_block_given_p()) { + result[1] = rb_ary_new3(2, Qnil, Qnil); + rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)result); + } + else { + rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)result); + } + if (result[0] == Qundef) return Qnil; + return result[0]; } static VALUE @@ -1095,10 +1115,16 @@ enum_member(VALUE obj, VALUE val) } static VALUE -each_with_index_i(VALUE val, VALUE *memo) +each_with_index_i(VALUE val, VALUE memo) { - rb_yield_values(2, val, INT2FIX(*memo)); - ++*memo; + long n; + + RARRAY_PTR(memo)[0] = val; + rb_yield(memo); + val = RARRAY_PTR(memo)[1]; + n = NUM2LONG(val); + n++; + RARRAY_PTR(memo)[1] = INT2NUM(n); return Qnil; } @@ -1120,11 +1146,12 @@ each_with_index_i(VALUE val, VALUE *memo) static VALUE enum_each_with_index(VALUE obj) { - VALUE memo = 0; + VALUE memo; RETURN_ENUMERATOR(obj, 0, 0); - rb_block_call(obj, id_each, 0, 0, each_with_index_i, (VALUE)&memo); + memo = rb_ary_new3(2, Qnil, INT2FIX(0)); + rb_block_call(obj, id_each, 0, 0, each_with_index_i, memo); return obj; } -- cgit v1.2.3