diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-10-25 00:21:54 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-10-25 00:21:54 +0000 |
commit | 805e130d20dd3f776f809836cbd251417ed6d382 (patch) | |
tree | 8aacc75aca279a95ef5dda62f14e54013980e872 | |
parent | f1e4b10a84d43ec15424f86a692957f3f94cb860 (diff) |
* enumerator.c (enumerator_by_slice): new method added.
* enumerator.c (enumerator_by_cons): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11219 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | enumerator.c | 64 |
2 files changed, 70 insertions, 0 deletions
@@ -7,6 +7,12 @@ Wed Oct 25 00:58:19 2006 Nobuyoshi Nakada <nobu@ruby-lang.org> * win32/mkexports.rb, win32/resource.rb: use unique variable names. +Tue Oct 24 19:18:53 2006 Yukihiro Matsumoto <matz@ruby-lang.org> + + * enumerator.c (enumerator_by_slice): new method added. + + * enumerator.c (enumerator_by_cons): ditto. + Tue Oct 24 18:56:13 2006 Yukihiro Matsumoto <matz@ruby-lang.org> * enumerator.c (enum_each_slice, enum_each_cons): returns diff --git a/enumerator.c b/enumerator.c index 51ebb2c2b1..988f7bc615 100644 --- a/enumerator.c +++ b/enumerator.c @@ -313,6 +313,7 @@ enumerator_with_index(VALUE obj) int argc = 0; VALUE *argv = 0; +/* RETURN_ENUMERATOR(obj, 0, 0); ?? */ if (e->args) { argc = RARRAY_LEN(e->args); argv = RARRAY_PTR(e->args); @@ -322,6 +323,67 @@ enumerator_with_index(VALUE obj) } /* + * call-seq: + * e.by_slice {...} + * + * Iterates the given block for each slice of <n> elements. + * + */ +static VALUE +enumerator_by_slice(VALUE obj, VALUE n) +{ + struct enumerator *e = enumerator_ptr(obj); + int argc = 0; + VALUE *argv = 0; + long size = NUM2LONG(n); + VALUE args[2], ary; + + if (size <= 0) rb_raise(rb_eArgError, "invalid slice size"); + RETURN_ENUMERATOR(obj, 1, &n); + args[0] = rb_ary_new2(size); + args[1] = (VALUE)size; + if (e->args) { + argc = RARRAY_LEN(e->args); + argv = RARRAY_PTR(e->args); + } + + return rb_block_call(e->method, rb_intern("call"), argc, argv, + each_slice_i, (VALUE)&args); + return Qnil; +} + +/* + * call-seq: + * e.by_cons {...} + * + * Iterates the given block for each array of consecutive <n> + * elements. + * + */ +static VALUE +enumerator_by_cons(VALUE obj, VALUE n) +{ + struct enumerator *e = enumerator_ptr(obj); + int argc = 0; + VALUE *argv = 0; + long size = NUM2LONG(n); + VALUE args[2], ary; + + if (size <= 0) rb_raise(rb_eArgError, "invalid slice size"); + RETURN_ENUMERATOR(obj, 1, &n); + args[0] = rb_ary_new2(size); + args[1] = (VALUE)size; + if (e->args) { + argc = RARRAY_LEN(e->args); + argv = RARRAY_PTR(e->args); + } + + return rb_block_call(e->method, rb_intern("call"), argc, argv, + each_cons_i, (VALUE)&args); + return Qnil; +} + +/* * call-seq: * e.to_splat => array * @@ -350,6 +412,8 @@ Init_Enumerator(void) rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1); rb_define_method(rb_cEnumerator, "each", enumerator_each, 0); rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0); + rb_define_method(rb_cEnumerator, "by_slice", enumerator_by_slice, 1); + rb_define_method(rb_cEnumerator, "by_cons", enumerator_by_cons, 1); rb_define_method(rb_cEnumerator, "to_splat", enumerator_to_splat, 0); sym_each = ID2SYM(rb_intern("each")); |