From 5315f0d66741aa8caca75d30953b35733474bf87 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 19 Nov 2002 08:07:51 +0000 Subject: * array.c (rb_ary_zip): iterates over items in the receiver. zipped with nil if argument arrays are shorter. if arrays are longer, left items are ignored. now works with blocks. * enum.c (zip_i): changed for new behavior. * array.c (rb_ary_transpose): added. [new] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ array.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- bignum.c | 8 +++----- enum.c | 40 +++++++++++++++------------------------- lib/irb.rb | 2 +- math.c | 2 +- 6 files changed, 73 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index 455f558eb0..0832bc5f09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Tue Nov 19 14:35:09 2002 Yukihiro Matsumoto + + * array.c (rb_ary_zip): iterates over items in the receiver. + zipped with nil if argument arrays are shorter. if arrays are + longer, left items are ignored. now works with blocks. + + * enum.c (zip_i): changed for new behavior. + + * array.c (rb_ary_transpose): added. [new] + Tue Nov 19 05:12:21 2002 Akinori MUSHA * instruby.rb: Do not install various working files under bin/. diff --git a/array.c b/array.c index bed2390282..9f6362d3d9 100644 --- a/array.c +++ b/array.c @@ -1305,13 +1305,25 @@ rb_ary_zip(argc, argv, ary) VALUE *argv; VALUE ary; { - int i, j, len; + int i, j; + long len; VALUE result; len = RARRAY(ary)->len; for (i=0; ilen > len) len = RARRAY(argv[i])->len; + } + if (rb_block_given_p()) { + for (i=0; ilen; + if (alen == 0) return rb_ary_dup(ary); + for (i=0; iptr[i]); + if (elen < 0) { /* first element */ + elen = RARRAY(tmp)->len; + result = rb_ary_new2(elen); + for (j=0; jlen) { + rb_raise(rb_eIndexError, "element size differ (%d should be %d)", + RARRAY(tmp)->len, elen); + } + for (j=0; jptr[j], i, RARRAY(tmp)->ptr[j]); + } + } + return result; +} + static VALUE rb_ary_replace(copy, orig) VALUE copy, orig; @@ -1893,6 +1933,7 @@ Init_Array() 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); rb_define_method(rb_cArray, "fill", rb_ary_fill, -1); diff --git a/bignum.c b/bignum.c index 0d23189955..f3ab04ab8e 100644 --- a/bignum.c +++ b/bignum.c @@ -182,8 +182,6 @@ rb_int2inum(n) #ifdef HAVE_LONG_LONG -#define DIGSPERLONGLONG ((unsigned int)(SIZEOF_LONG_LONG/SIZEOF_BDIGITS)) - void rb_quad_pack(buf, val) char *buf; @@ -233,14 +231,14 @@ rb_quad_unpack(buf, sign) } i = 0; - big = bignew(DIGSPERLONGLONG, 1); + big = bignew(DIGSPERLL, 1); digits = BDIGITS(big); - while (i < DIGSPERLONGLONG) { + while (i < DIGSPERLL) { digits[i++] = BIGLO(q); q = BIGDN(q); } - i = DIGSPERLONGLONG; + i = DIGSPERLL; while (i-- && !digits[i]) ; RBIGNUM(big)->len = i+1; diff --git a/enum.c b/enum.c index c5191fdfba..1200ce35e6 100644 --- a/enum.c +++ b/enum.c @@ -491,23 +491,23 @@ zip_i(val, memo) VALUE val; NODE *memo; { - VALUE ary = memo->u1.value; - int i = memo->u3.cnt++; - int elen = memo->u2.argc+1; + VALUE result = memo->u1.value; + VALUE args = memo->u2.value; + int idx = memo->u3.cnt++; VALUE tmp; + int i; - if (i < RARRAY(ary)->len) { - tmp = RARRAY(ary)->ptr[i]; - RARRAY(tmp)->ptr[0] = val; + tmp = rb_ary_new2(RARRAY(args)->len + 1); + rb_ary_store(tmp, 0, val); + RARRAY(tmp)->ptr[0] = val; + for (i=0; ilen; i++) { + rb_ary_push(tmp, rb_ary_entry(RARRAY(args)->ptr[i], idx)); + } + if (rb_block_given_p()) { + rb_yield(tmp); } else { - tmp = rb_ary_new2(elen); - RARRAY(tmp)->ptr[0] = val; - for (i=1; iptr[i] = Qnil; - } - RARRAY(tmp)->len = elen; - rb_ary_push(ary, tmp); + rb_ary_push(result, tmp); } return Qnil; } @@ -525,19 +525,9 @@ enum_zip(argc, argv, obj) len = 0; for (i=0; ilen > len) len = RARRAY(argv[i])->len; - } - result = rb_ary_new2(len); - for (i=0; i -#include +#include VALUE rb_mMath; -- cgit v1.2.3