1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
matz 2002-11-19 08:07:51 +00:00
parent 710e25422b
commit 5315f0d667
6 changed files with 73 additions and 34 deletions

View file

@ -1,3 +1,13 @@
Tue Nov 19 14:35:09 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
* 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 <knu@iDaemons.org>
* instruby.rb: Do not install various working files under bin/.

45
array.c
View file

@ -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++) {
@ -1326,6 +1338,34 @@ rb_ary_zip(argc, argv, ary)
return result;
}
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);

View file

@ -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;

40
enum.c
View file

@ -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; i<RARRAY(args)->len; 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; i<elen; i++) {
RARRAY(tmp)->ptr[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; i<argc; i++) {
argv[i] = rb_convert_type(argv[i], T_ARRAY, "Array", "to_ary");
if (RARRAY(argv[i])->len > len) len = RARRAY(argv[i])->len;
}
result = rb_ary_new2(len);
for (i=0; i<len; i++) {
VALUE tmp = rb_ary_new2(argc+1);
rb_ary_push(tmp, Qnil);
for (j=0; j<argc; j++) {
rb_ary_push(tmp, rb_ary_entry(argv[j], i));
}
rb_ary_push(result, tmp);
}
memo = rb_node_newnode(NODE_MEMO, result, argc, 0);
result = rb_block_given_p() ? Qnil : rb_ary_new();
memo = rb_node_newnode(NODE_MEMO, result, rb_ary_new4(argc, argv), 0);
rb_iterate(rb_each, obj, zip_i, (VALUE)memo);
return result;

View file

@ -51,7 +51,7 @@ module IRB
def IRB.start(ap_path = nil)
$0 = File::basename(ap_path, ".rb") if ap_path
IRB.initialize(ap_path)
IRB.setup(ap_path)
if @CONF[:SCRIPT]
irb = Irb.new(nil, @CONF[:SCRIPT])

2
math.c
View file

@ -12,7 +12,7 @@
#include "ruby.h"
#include <math.h>
#include <sys/errno.h>
#include <errno.h>
VALUE rb_mMath;