* array.c (rb_ary_zip): some refactoring.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
glass 2013-10-29 14:08:48 +00:00
Родитель 806b93ed9b
Коммит 887ff70f23
2 изменённых файлов: 32 добавлений и 21 удалений

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

@ -1,3 +1,7 @@
Tue Oct 29 23:01:18 2013 Masaki Matsushita <glass.saga@gmail.com>
* array.c (rb_ary_zip): some refactoring.
Tue Oct 29 22:11:37 2013 Masaki Matsushita <glass.saga@gmail.com> Tue Oct 29 22:11:37 2013 Masaki Matsushita <glass.saga@gmail.com>
* array.c (rb_ary_uniq_bang): use st_foreach() instead of for loop. * array.c (rb_ary_uniq_bang): use st_foreach() instead of for loop.

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

@ -3217,44 +3217,51 @@ take_items(VALUE obj, long n)
static VALUE static VALUE
rb_ary_zip(int argc, VALUE *argv, VALUE ary) rb_ary_zip(int argc, VALUE *argv, VALUE ary)
{ {
int i, j, block_given, arity = 0; int i, j;
long len; long len = RARRAY_LEN(ary);
VALUE result = Qnil; VALUE result = Qnil;
len = RARRAY_LEN(ary);
for (i=0; i<argc; i++) { for (i=0; i<argc; i++) {
argv[i] = take_items(argv[i], len); argv[i] = take_items(argv[i], len);
} }
block_given = rb_block_given_p(); if (rb_block_given_p()) {
if (block_given) int arity = rb_block_arity();
arity = rb_block_arity();
else
result = rb_ary_new2(len);
if (block_given && arity > 1 && argc+1 < 0x100) { if (arity > 1 && argc+1 < 0x100) {
VALUE *tmp = ALLOCA_N(VALUE, argc+1); VALUE *tmp = ALLOCA_N(VALUE, argc+1);
for (i=0; i<RARRAY_LEN(ary); i++) { for (i=0; i<RARRAY_LEN(ary); i++) {
tmp[0] = RARRAY_AREF(ary, i); tmp[0] = RARRAY_AREF(ary, i);
for (j=0; j<argc; j++) { for (j=0; j<argc; j++) {
tmp[j+1] = rb_ary_elt(argv[j], i); 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 { else {
for (i=0; i<RARRAY_LEN(ary); i++) { result = rb_ary_new_capa(len);
VALUE tmp = rb_ary_new2(argc+1);
for (i=0; i<len; i++) {
VALUE tmp = rb_ary_new_capa(argc+1);
rb_ary_push(tmp, RARRAY_AREF(ary, i)); rb_ary_push(tmp, RARRAY_AREF(ary, i));
for (j=0; j<argc; j++) { for (j=0; j<argc; j++) {
rb_ary_push(tmp, rb_ary_elt(argv[j], i)); rb_ary_push(tmp, rb_ary_elt(argv[j], i));
} }
if (block_given) rb_ary_push(result, tmp);
rb_yield(tmp);
else
rb_ary_push(result, tmp);
} }
} }