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

* enum.c (count_i, count_iter_i, enum_count, enum_find_index):

Reduce code.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15974 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-04-11 08:26:45 +00:00
parent f79e9281ff
commit f9c5484cc5
2 changed files with 30 additions and 33 deletions

View file

@ -1,3 +1,8 @@
Fri Apr 11 17:25:09 2008 Akinori MUSHA <knu@iDaemons.org>
* enum.c (count_i, count_iter_i, enum_count, enum_find_index):
Reduce code.
Fri Apr 11 17:06:01 2008 Yukihiro Matsumoto <matz@ruby-lang.org> Fri Apr 11 17:06:01 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (find_index_i): modified to shut warning up. * enum.c (find_index_i): modified to shut warning up.

58
enum.c
View file

@ -81,19 +81,23 @@ enum_grep(VALUE obj, VALUE pat)
} }
static VALUE static VALUE
count_i(VALUE i, VALUE *arg) count_i(VALUE i, VALUE memop)
{ {
if (rb_equal(i, arg[0])) { VALUE *memo = (VALUE*)memop;
arg[1]++;
if (rb_equal(i, memo[1])) {
memo[0]++;
} }
return Qnil; return Qnil;
} }
static VALUE static VALUE
count_iter_i(VALUE i, long *n, int argc, VALUE *argv) count_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
{ {
VALUE *memo = (VALUE*)memop;
if (RTEST(enum_yield(argc, argv))) { if (RTEST(enum_yield(argc, argv))) {
(*n)++; memo[0]++;
} }
return Qnil; return Qnil;
} }
@ -115,31 +119,24 @@ count_iter_i(VALUE i, long *n, int argc, VALUE *argv)
static VALUE static VALUE
enum_count(int argc, VALUE *argv, VALUE obj) enum_count(int argc, VALUE *argv, VALUE obj)
{ {
if (argc == 1) { VALUE memo[2]; /* [count, condition value] */
VALUE item, args[2]; rb_block_call_func *func;
if (argc == 0) {
RETURN_ENUMERATOR(obj, 0, 0);
func = count_iter_i;
}
else {
rb_scan_args(argc, argv, "1", &memo[1]);
if (rb_block_given_p()) { if (rb_block_given_p()) {
rb_warn("given block not used"); rb_warn("given block not used");
} }
rb_scan_args(argc, argv, "1", &item); func = count_i;
args[0] = item;
args[1] = 0;
rb_block_call(obj, id_each, 0, 0, count_i, (VALUE)&args);
return INT2NUM(args[1]);
} }
else if (argc == 0) {
long n;
RETURN_ENUMERATOR(obj, 0, 0); memo[0] = 0;
n = 0; rb_block_call(obj, id_each, 0, 0, func, (VALUE)&memo);
rb_block_call(obj, id_each, 0, 0, count_iter_i, (VALUE)&n); return INT2NUM(memo[0]);
return INT2NUM(n);
}
else {
VALUE v;
rb_scan_args(argc, argv, "1", &v);
return Qnil; /* not reached */
}
} }
static VALUE static VALUE
@ -202,6 +199,7 @@ static VALUE
find_index_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv) find_index_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
{ {
VALUE *memo = (VALUE*)memop; VALUE *memo = (VALUE*)memop;
if (RTEST(enum_yield(argc, argv))) { if (RTEST(enum_yield(argc, argv))) {
memo[0] = UINT2NUM(memo[1]); memo[0] = UINT2NUM(memo[1]);
rb_iter_break(); rb_iter_break();
@ -234,24 +232,18 @@ enum_find_index(int argc, VALUE *argv, VALUE obj)
if (argc == 0) { if (argc == 0) {
RETURN_ENUMERATOR(obj, 0, 0); RETURN_ENUMERATOR(obj, 0, 0);
memo[0] = Qnil;
memo[1] = 0;
memo[2] = Qundef;
func = find_index_iter_i; func = find_index_iter_i;
} }
else { else {
VALUE item; rb_scan_args(argc, argv, "1", &memo[2]);
rb_scan_args(argc, argv, "1", &item);
if (rb_block_given_p()) { if (rb_block_given_p()) {
rb_warn("given block not used"); rb_warn("given block not used");
} }
memo[0] = Qnil;
memo[1] = 0;
memo[2] = item;
func = find_index_i; func = find_index_i;
} }
memo[0] = Qnil;
memo[1] = 0;
rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo); rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
return memo[0]; return memo[0];
} }