summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-29 14:08:48 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-29 14:08:48 +0000
commit887ff70f23e20081b424d1986988e005f8cce312 (patch)
tree2f6bff0a0fc4df50352448fc1d8b3a5357c80261
parent806b93ed9b647956ac99f64d100674aa5cc2d6df (diff)
* array.c (rb_ary_zip): some refactoring.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--array.c49
2 files changed, 32 insertions, 21 deletions
diff --git a/ChangeLog b/ChangeLog
index 1d74783..25354e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
* array.c (rb_ary_uniq_bang): use st_foreach() instead of for loop.
diff --git a/array.c b/array.c
index 19878f0..5e122a2 100644
--- a/array.c
+++ b/array.c
@@ -3217,44 +3217,51 @@ take_items(VALUE obj, long n)
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);
}
}