From ecb93c3fdf347a3d14c13f8f9292bebf77ebfa07 Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 6 Aug 2007 16:53:36 +0000 Subject: [PATCH] * enum.c (enum_zip): zip no longer converts arguments into arrays, uses enumerators. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12892 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ array.c | 60 ---------------------------------------------------- enum.c | 56 +++++++++++++++++++++++++----------------------- enumerator.c | 38 ++++++++++++++++----------------- 4 files changed, 53 insertions(+), 106 deletions(-) diff --git a/ChangeLog b/ChangeLog index 53dbdcef6d..591b36a054 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Aug 7 01:42:05 2007 Yukihiro Matsumoto + + * enum.c (enum_zip): zip no longer converts arguments into + arrays, uses enumerators. + Tue Aug 7 01:27:47 2007 Yukihiro Matsumoto * cont.c (rb_fiber_yield): change argument ordering. export. diff --git a/array.c b/array.c index 3bb07e679c..d3b3b909d6 100644 --- a/array.c +++ b/array.c @@ -1956,65 +1956,6 @@ rb_ary_delete_if(VALUE ary) return ary; } -/* - * call-seq: - * array.zip(arg, ...) -> an_array - * array.zip(arg, ...) {| arr | block } -> nil - * - * Converts any arguments to arrays, then merges elements of - * self with corresponding elements from each argument. This - * generates a sequence of self.size n-element - * arrays, where n is one more that the count of arguments. - * The size of returned array is truncated to the size of the - * shortest argument enumerable. If a block given, it is invoked - * for each output array, otherwise an array of arrays is returned. - * - * a = [ 4, 5, 6 ] - * b = [ 7, 8, 9 ] - * - * [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] - * [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]] - * a.zip([1,2],[8]) #=> [[4,1,8]] - */ - -static VALUE -rb_ary_zip(int argc, VALUE *argv, VALUE ary) -{ - int i, j; - long len; - VALUE result; - - len = RARRAY_LEN(ary); - for (i=0; i RARRAY_LEN(argv[i])) - len = RARRAY_LEN(argv[i]); - } - if (rb_block_given_p()) { - for (i=0; i an_array @@ -3042,7 +2983,6 @@ Init_Array(void) rb_define_method(rb_cArray, "delete_if", rb_ary_delete_if, 0); rb_define_method(rb_cArray, "reject", rb_ary_reject, 0); rb_define_method(rb_cArray, "reject!", rb_ary_reject_bang, 0); - rb_define_method(rb_cArray, "zip", rb_ary_zip, -1); rb_define_method(rb_cArray, "transpose", rb_ary_transpose, 0); rb_define_method(rb_cArray, "replace", rb_ary_replace, 1); rb_define_method(rb_cArray, "clear", rb_ary_clear, 0); diff --git a/enum.c b/enum.c index 3ca42ca4bd..fa58762463 100644 --- a/enum.c +++ b/enum.c @@ -15,7 +15,7 @@ #include "ruby/util.h" VALUE rb_mEnumerable; -static ID id_each, id_eqq, id_cmp; +static ID id_each, id_eqq, id_cmp, id_next; static VALUE grep_i(VALUE i, VALUE *arg) @@ -1348,20 +1348,20 @@ enum_each_with_index(int argc, VALUE *argv, VALUE obj) } static VALUE -zip_i(VALUE val, VALUE *memo) +zip_i(VALUE val, NODE *memo) { - VALUE result = memo[0]; - VALUE args = memo[1]; - int idx = memo[2]++; - VALUE tmp; + volatile VALUE result = memo->u1.value; + volatile VALUE args = memo->u2.value; + volatile VALUE tmp; int i; tmp = rb_ary_new2(RARRAY_LEN(args) + 1); rb_ary_store(tmp, 0, val); for (i=0; iu3.value, id_each, 0, 0, zip_i, (VALUE)memo); +} + /* * call-seq: - * enum.zip(arg, ...) => array + * enum.zip(arg, ...) => enumerator * enum.zip(arg, ...) {|arr| block } => nil * - * Converts any arguments to arrays, then merges elements of - * enum with corresponding elements from each argument. This - * generates a sequence of enum#size n-element - * arrays, where n is one more that the count of arguments. If - * the size of any argument is less than enum#size, - * nil values are supplied. If a block given, it is - * invoked for each output array, otherwise an array of arrays is - * returned. + * Takes one element from enum and merges corresponding + * elements from each args. This generates a sequence of + * n-element arrays, where n is one more that the + * count of arguments. The length of the sequence is truncated to + * the size of the shortest argument (or enum). If a block + * given, it is invoked for each output array, otherwise an array of + * arrays is returned. * * a = [ 4, 5, 6 ] * b = [ 7, 8, 9 ] * - * (1..3).zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] - * "cat\ndog".zip([1]) #=> [["cat\n", 1], ["dog", nil]] - * (1..3).zip #=> [[1], [2], [3]] + * [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + * [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]] + * a.zip([1,2],[8]) #=> [[4,1,8]] * */ @@ -1398,17 +1403,15 @@ enum_zip(int argc, VALUE *argv, VALUE obj) { int i; VALUE result; - VALUE memo[3]; + NODE *memo; for (i=0; i"); + id_next = rb_intern("next"); } diff --git a/enumerator.c b/enumerator.c index f7f8c33dbc..3260a809f4 100644 --- a/enumerator.c +++ b/enumerator.c @@ -39,6 +39,7 @@ struct enumerator { VALUE (*iter)(VALUE, struct enumerator *); VALUE fib; VALUE next; + VALUE dst; }; static void @@ -50,6 +51,7 @@ enumerator_mark(void *p) rb_gc_mark(ptr->args); rb_gc_mark(ptr->fib); rb_gc_mark(ptr->next); + rb_gc_mark(ptr->dst); } static struct enumerator * @@ -232,7 +234,7 @@ enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv) } if (argc) ptr->args = rb_ary_new4(argc, argv); ptr->fib = 0; - ptr->next = Qundef; + ptr->next = ptr->dst = Qundef; return enum_obj; } @@ -360,29 +362,27 @@ enumerator_to_splat(VALUE obj) } static VALUE -next_ii(VALUE i, VALUE *args) +next_ii(VALUE i, VALUE obj) { - struct enumerator *e = enumerator_ptr(args[0]); + struct enumerator *e = enumerator_ptr(obj); VALUE tmp = e->next; e->next = i; if (tmp != Qundef) { e->next = i; - rb_fiber_yield(args[1], 1, &tmp); + e->dst = rb_fiber_yield(e->dst, 1, &tmp); } return Qnil; } static VALUE -next_i(VALUE dummy, VALUE *args) +next_i(VALUE curr, VALUE obj) { - VALUE tmp[2]; /* store in local variable */ + struct enumerator *e = enumerator_ptr(obj); - - tmp[0] = args[0]; /* enumerator */ - tmp[1] = args[1]; /* current fibder */ - rb_block_call(args[0], rb_intern("each"), 0, 0, next_ii, (VALUE)tmp); - return enumerator_ptr(tmp[0])->next; + e->dst = curr; + rb_block_call(obj, rb_intern("each"), 0, 0, next_ii, obj); + return e->next; } /* @@ -403,21 +403,19 @@ static VALUE enumerator_next(VALUE obj) { struct enumerator *e = enumerator_ptr(obj); - VALUE args[2]; - VALUE v; - - args[0] = obj; - args[1] = rb_fiber_current(); + VALUE curr, v; + curr = rb_fiber_current(); if (!e->fib) { - e->fib = rb_block_call(rb_cFiber, rb_intern("new"), 0, 0, next_i, (VALUE)args); + e->dst = curr; + e->fib = rb_block_call(rb_cFiber, rb_intern("new"), 0, 0, next_i, obj); } else if (!rb_fiber_alive_p(e->fib)) { e->fib = 0; - e->next = Qundef; + e->next = e->dst = Qundef; rb_raise(rb_eIndexError, "Enumerator#each reached at end"); } - v = rb_fiber_yield(e->fib, 0, 0); + v = rb_fiber_yield(e->fib, 1, &curr); return v; } @@ -447,7 +445,7 @@ enumerator_rewind(VALUE obj) struct enumerator *e = enumerator_ptr(obj); e->fib = 0; - e->next = Qundef; + e->next = e->dst = Qundef; return obj; }