diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-12-29 19:49:23 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-12-29 19:49:23 +0000 |
commit | 5ba025477764bb87251894ebec5c62780f8ebb65 (patch) | |
tree | 6e9921e3e150ee8f8f23e132ac9aad680c8e8e05 /enum.c | |
parent | 873b2f9fe3672f9006f0b73c96a103d73bac7092 (diff) | |
download | ruby-5ba025477764bb87251894ebec5c62780f8ebb65.tar.gz |
* 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
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 57 |
1 files changed, 42 insertions, 15 deletions
@@ -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; } |