mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
57b0b6c7b1
commit
ecb93c3fdf
4 changed files with 53 additions and 106 deletions
|
@ -1,3 +1,8 @@
|
|||
Tue Aug 7 01:42:05 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* enum.c (enum_zip): zip no longer converts arguments into
|
||||
arrays, uses enumerators.
|
||||
|
||||
Tue Aug 7 01:27:47 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* cont.c (rb_fiber_yield): change argument ordering. export.
|
||||
|
|
60
array.c
60
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
|
||||
* <i>self</i> with corresponding elements from each argument. This
|
||||
* generates a sequence of <code>self.size</code> <em>n</em>-element
|
||||
* arrays, where <em>n</em> 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<argc; i++) {
|
||||
argv[i] = to_a(argv[i]);
|
||||
if (len > RARRAY_LEN(argv[i]))
|
||||
len = RARRAY_LEN(argv[i]);
|
||||
}
|
||||
if (rb_block_given_p()) {
|
||||
for (i=0; i<len; i++) {
|
||||
VALUE tmp = rb_ary_new2(argc+1);
|
||||
|
||||
rb_ary_push(tmp, rb_ary_elt(ary, i));
|
||||
for (j=0; j<argc; j++) {
|
||||
rb_ary_push(tmp, rb_ary_elt(argv[j], i));
|
||||
}
|
||||
rb_yield(tmp);
|
||||
}
|
||||
return Qnil;
|
||||
}
|
||||
result = rb_ary_new2(len);
|
||||
for (i=0; i<len; i++) {
|
||||
VALUE tmp = rb_ary_new2(argc+1);
|
||||
|
||||
rb_ary_push(tmp, rb_ary_elt(ary, i));
|
||||
for (j=0; j<argc; j++) {
|
||||
rb_ary_push(tmp, rb_ary_elt(argv[j], i));
|
||||
}
|
||||
rb_ary_push(result, tmp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* array.transpose -> 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);
|
||||
|
|
56
enum.c
56
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; i<RARRAY_LEN(args); i++) {
|
||||
rb_ary_push(tmp, rb_ary_entry(RARRAY_PTR(args)[i], idx));
|
||||
VALUE v = rb_funcall(RARRAY_PTR(args)[i], id_next, 0, 0);
|
||||
rb_ary_push(tmp, v);
|
||||
}
|
||||
if (rb_block_given_p()) {
|
||||
if (NIL_P(result)) {
|
||||
rb_yield(tmp);
|
||||
}
|
||||
else {
|
||||
|
@ -1370,26 +1370,31 @@ zip_i(VALUE val, VALUE *memo)
|
|||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
zip_b(NODE *memo)
|
||||
{
|
||||
return rb_block_call(memo->u3.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
|
||||
* <i>enum</i> with corresponding elements from each argument. This
|
||||
* generates a sequence of <code>enum#size</code> <em>n</em>-element
|
||||
* arrays, where <em>n</em> is one more that the count of arguments. If
|
||||
* the size of any argument is less than <code>enum#size</code>,
|
||||
* <code>nil</code> 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 <i>enum</i> and merges corresponding
|
||||
* elements from each <i>args</i>. This generates a sequence of
|
||||
* <em>n</em>-element arrays, where <em>n</em> is one more that the
|
||||
* count of arguments. The length of the sequence is truncated to
|
||||
* the size of the shortest argument (or <i>enum</i>). 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<argc; i++) {
|
||||
argv[i] = rb_convert_type(argv[i], T_ARRAY, "Array", "to_a");
|
||||
argv[i] = rb_funcall(argv[i], rb_intern("to_enum"), 1, ID2SYM(id_each));
|
||||
}
|
||||
RETURN_ENUMERATOR(obj, argc, argv);
|
||||
result = rb_block_given_p() ? Qnil : rb_ary_new();
|
||||
memo[0] = result;
|
||||
memo[1] = rb_ary_new4(argc, argv);
|
||||
memo[2] = 0;
|
||||
rb_block_call(obj, id_each, 0, 0, zip_i, (VALUE)memo);
|
||||
memo = rb_node_newnode(NODE_MEMO, result, rb_ary_new4(argc, argv), obj);
|
||||
rb_rescue2(zip_b, (VALUE)memo, 0, 0, rb_eIndexError, (VALUE)0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -1621,5 +1624,6 @@ Init_Enumerable(void)
|
|||
id_eqq = rb_intern("===");
|
||||
id_each = rb_intern("each");
|
||||
id_cmp = rb_intern("<=>");
|
||||
id_next = rb_intern("next");
|
||||
}
|
||||
|
||||
|
|
38
enumerator.c
38
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue