summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-11-19 08:07:51 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-11-19 08:07:51 +0000
commit5315f0d66741aa8caca75d30953b35733474bf87 (patch)
tree00da6cc4c3bf018f949dfcbc5c88f8662fee1480 /array.c
parent710e25422b099e8ce2c13d868effaefa10366378 (diff)
* 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
Diffstat (limited to 'array.c')
-rw-r--r--array.c45
1 files changed, 43 insertions, 2 deletions
diff --git a/array.c b/array.c
index bed2390..9f6362d 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; i<argc; i++) {
argv[i] = to_ary(argv[i]);
- if (RARRAY(argv[i])->len > len) len = RARRAY(argv[i])->len;
+ }
+ if (rb_block_given_p()) {
+ for (i=0; i<len; i++) {
+ VALUE tmp = rb_ary_new2(argc+1);
+
+ rb_ary_push(tmp, rb_ary_entry(ary, i));
+ for (j=0; j<argc; j++) {
+ rb_ary_push(tmp, rb_ary_entry(argv[j], i));
+ }
+ rb_yield(tmp);
+ }
+ return Qnil;
}
result = rb_ary_new2(len);
for (i=0; i<len; i++) {
@@ -1327,6 +1339,34 @@ rb_ary_zip(argc, argv, ary)
}
static VALUE
+rb_ary_transpose(ary)
+{
+ long elen = -1, alen, i, j;
+ VALUE tmp, result;
+
+ alen = RARRAY(ary)->len;
+ if (alen == 0) return rb_ary_dup(ary);
+ for (i=0; i<alen; i++) {
+ tmp = to_ary(RARRAY(ary)->ptr[i]);
+ if (elen < 0) { /* first element */
+ elen = RARRAY(tmp)->len;
+ result = rb_ary_new2(elen);
+ for (j=0; j<elen; j++) {
+ rb_ary_store(result, j, rb_ary_new2(alen));
+ }
+ }
+ else if (elen != RARRAY(tmp)->len) {
+ rb_raise(rb_eIndexError, "element size differ (%d should be %d)",
+ RARRAY(tmp)->len, elen);
+ }
+ for (j=0; j<elen; j++) {
+ rb_ary_store(RARRAY(result)->ptr[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);