ruby-changes:31389
From: glass <ko1@a...>
Date: Tue, 29 Oct 2013 23:08:55 +0900 (JST)
Subject: [ruby-changes:31389] glass:r43468 (trunk): * array.c (rb_ary_zip): some refactoring.
glass 2013-10-29 23:08:48 +0900 (Tue, 29 Oct 2013) New Revision: 43468 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43468 Log: * array.c (rb_ary_zip): some refactoring. Modified files: trunk/ChangeLog trunk/array.c Index: array.c =================================================================== --- array.c (revision 43467) +++ array.c (revision 43468) @@ -3217,44 +3217,51 @@ take_items(VALUE obj, long n) https://github.com/ruby/ruby/blob/trunk/array.c#L3217 static VALUE rb_ary_zip(int argc, VALUE *argv, VALUE ary) { - int i, j, block_given, arity = 0; - long len; + int i, j; + long len = RARRAY_LEN(ary); VALUE result = Qnil; - len = RARRAY_LEN(ary); for (i=0; i<argc; i++) { argv[i] = take_items(argv[i], len); } - block_given = rb_block_given_p(); - if (block_given) - arity = rb_block_arity(); - else - result = rb_ary_new2(len); + if (rb_block_given_p()) { + int arity = rb_block_arity(); - if (block_given && arity > 1 && argc+1 < 0x100) { - VALUE *tmp = ALLOCA_N(VALUE, argc+1); + if (arity > 1 && argc+1 < 0x100) { + VALUE *tmp = ALLOCA_N(VALUE, argc+1); - for (i=0; i<RARRAY_LEN(ary); i++) { - tmp[0] = RARRAY_AREF(ary, i); - for (j=0; j<argc; j++) { - tmp[j+1] = rb_ary_elt(argv[j], i); + for (i=0; i<RARRAY_LEN(ary); i++) { + tmp[0] = RARRAY_AREF(ary, i); + for (j=0; j<argc; j++) { + tmp[j+1] = rb_ary_elt(argv[j], i); + } + rb_yield_values2(argc+1, tmp); + } + } + else { + for (i=0; i<RARRAY_LEN(ary); i++) { + VALUE tmp = rb_ary_new2(argc+1); + + rb_ary_push(tmp, RARRAY_AREF(ary, i)); + for (j=0; j<argc; j++) { + rb_ary_push(tmp, rb_ary_elt(argv[j], i)); + } + rb_yield(tmp); } - rb_yield_values2(argc+1, tmp); } } else { - for (i=0; i<RARRAY_LEN(ary); i++) { - VALUE tmp = rb_ary_new2(argc+1); + result = rb_ary_new_capa(len); + + for (i=0; i<len; i++) { + VALUE tmp = rb_ary_new_capa(argc+1); rb_ary_push(tmp, RARRAY_AREF(ary, i)); for (j=0; j<argc; j++) { rb_ary_push(tmp, rb_ary_elt(argv[j], i)); } - if (block_given) - rb_yield(tmp); - else - rb_ary_push(result, tmp); + rb_ary_push(result, tmp); } } Index: ChangeLog =================================================================== --- ChangeLog (revision 43467) +++ ChangeLog (revision 43468) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Oct 29 23:01:18 2013 Masaki Matsushita <glass.saga@g...> + + * array.c (rb_ary_zip): some refactoring. + Tue Oct 29 22:11:37 2013 Masaki Matsushita <glass.saga@g...> * array.c (rb_ary_uniq_bang): use st_foreach() instead of for loop. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/