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

* enum.c (enum_inject): minor improvement. [ruby-dev:30792]

* enum.c (one_i): no needs to iterate once the result became false.

* enum.c (enum_one): fix for an example.

* enum.c (one_iter_i, none_iter_i): DRY.;


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-05-18 18:06:54 +00:00
parent 9eec640e9d
commit 3d18c14e2c
2 changed files with 48 additions and 55 deletions

View file

@ -1,3 +1,13 @@
Sat May 19 03:08:05 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* enum.c (enum_inject): minor improvement. [ruby-dev:30792]
* enum.c (one_i): no needs to iterate once the result became false.
* enum.c (enum_one): fix for an example.
* enum.c (one_iter_i, none_iter_i): DRY.;
Sat May 19 01:07:42 2007 Yukihiro Matsumoto <matz@ruby-lang.org> Sat May 19 01:07:42 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (enum_inject): it is now can work without block. you * enum.c (enum_inject): it is now can work without block. you

93
enum.c
View file

@ -335,30 +335,28 @@ enum_to_a(VALUE obj)
} }
static VALUE static VALUE
inject_i(VALUE i, VALUE memo) inject_i(VALUE i, VALUE p)
{ {
if (RARRAY_PTR(memo)[0] == Qundef) { VALUE *memo = (VALUE *)p;
RARRAY_PTR(memo)[0] = i; if (memo[0] == Qundef) {
memo[0] = i;
} }
else { else {
RARRAY_PTR(memo)[1] = i; memo[0] = rb_yield_values(2, memo[0], i);
RARRAY_PTR(memo)[0] = rb_yield(memo);
} }
return Qnil; return Qnil;
} }
static VALUE static VALUE
inject_op_i(VALUE i, NODE *node) inject_op_i(VALUE i, VALUE p)
{ {
VALUE memo = node->nd_rval; VALUE *memo = (VALUE *)p;
if (RARRAY_PTR(memo)[0] == Qundef) { if (memo[0] == Qundef) {
RARRAY_PTR(memo)[0] = i; memo[0] = i;
} }
else { else {
VALUE v = RARRAY_PTR(memo)[0]; memo[0] = rb_funcall(memo[0], (ID)memo[1], 1, i);
RARRAY_PTR(memo)[1] = i;
RARRAY_PTR(memo)[0] = rb_funcall(v, node->nd_vid, 1, i);
} }
return Qnil; return Qnil;
} }
@ -428,37 +426,32 @@ inject_op_i(VALUE i, NODE *node)
static VALUE static VALUE
enum_inject(int argc, VALUE *argv, VALUE obj) enum_inject(int argc, VALUE *argv, VALUE obj)
{ {
VALUE memo, a1, a2, tmp; VALUE memo[2];
NODE *node = 0; VALUE (*iter)(VALUE, VALUE) = inject_i;
switch (rb_scan_args(argc, argv, "02", &a1, &a2)) { switch (rb_scan_args(argc, argv, "02", &memo[0], &memo[1])) {
case 0: case 0:
memo = rb_ary_new3(2, Qundef, Qnil); memo[0] = Qundef;
rb_block_call(obj, id_each, 0, 0, inject_i, memo);
break; break;
case 1: case 1:
if (rb_block_given_p()) { if (rb_block_given_p()) {
memo = rb_ary_new3(2, a1, Qnil); break;
rb_block_call(obj, id_each, 0, 0, inject_i, memo);
}
else {
memo = rb_ary_new3(2, Qundef, Qnil);
node = rb_node_newnode(NODE_MEMO, rb_to_id(a1), memo, 0);
rb_block_call(obj, id_each, 0, 0, inject_op_i, (VALUE)node);
} }
memo[1] = (VALUE)rb_to_id(memo[0]);
memo[0] = Qundef;
iter = inject_op_i;
break; break;
case 2: case 2:
if (rb_block_given_p()) { if (rb_block_given_p()) {
rb_warning("given block not used"); rb_warning("given block not used");
} }
memo = rb_ary_new3(2, a2, Qnil); memo[1] = (VALUE)rb_to_id(memo[1]);
node = rb_node_newnode(NODE_MEMO, rb_to_id(a1), memo, 0); iter = inject_op_i;
rb_block_call(obj, id_each, 0, 0, inject_op_i, (VALUE)node);
break; break;
} }
tmp = RARRAY_PTR(memo)[0]; rb_block_call(obj, id_each, 0, 0, iter, (VALUE)memo);
if (tmp == Qundef) return Qnil; if (memo[0] == Qundef) return Qnil;
return tmp; return memo[0];
} }
static VALUE static VALUE
@ -829,20 +822,6 @@ enum_any(VALUE obj)
return result; return result;
} }
static VALUE
one_iter_i(VALUE i, VALUE *memo)
{
if (RTEST(rb_yield(i))) {
if (*memo == Qundef) {
*memo = Qtrue;
}
else if (*memo == Qtrue) {
*memo = Qfalse;
}
}
return Qnil;
}
static VALUE static VALUE
one_i(VALUE i, VALUE *memo) one_i(VALUE i, VALUE *memo)
{ {
@ -852,11 +831,18 @@ one_i(VALUE i, VALUE *memo)
} }
else if (*memo == Qtrue) { else if (*memo == Qtrue) {
*memo = Qfalse; *memo = Qfalse;
rb_iter_break();
} }
} }
return Qnil; return Qnil;
} }
static VALUE
one_iter_i(VALUE i, VALUE *memo)
{
return one_i(rb_yield(i), memo);
}
/* /*
* call-seq: * call-seq:
* enum.one? [{|obj| block }] => true or false * enum.one? [{|obj| block }] => true or false
@ -869,7 +855,8 @@ one_i(VALUE i, VALUE *memo)
* *
* %w{ant bear cat}.one? {|word| word.length == 4} #=> true * %w{ant bear cat}.one? {|word| word.length == 4} #=> true
* %w{ant bear cat}.one? {|word| word.length >= 4} #=> false * %w{ant bear cat}.one? {|word| word.length >= 4} #=> false
* [ nil, true, 99 ].one? #=> true * [ nil, true, 99 ].one? #=> false
* [ nil, true, false ].one? #=> true
* *
*/ */
@ -883,16 +870,6 @@ enum_one(VALUE obj)
return result; return result;
} }
static VALUE
none_iter_i(VALUE i, VALUE *memo)
{
if (RTEST(rb_yield(i))) {
*memo = Qfalse;
rb_iter_break();
}
return Qnil;
}
static VALUE static VALUE
none_i(VALUE i, VALUE *memo) none_i(VALUE i, VALUE *memo)
{ {
@ -903,6 +880,12 @@ none_i(VALUE i, VALUE *memo)
return Qnil; return Qnil;
} }
static VALUE
none_iter_i(VALUE i, VALUE *memo)
{
return none_i(rb_yield(i), memo);
}
/* /*
* call-seq: * call-seq:
* enum.none? [{|obj| block }] => true or false * enum.none? [{|obj| block }] => true or false