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:
parent
f79e9281ff
commit
f9c5484cc5
2 changed files with 30 additions and 33 deletions
|
@ -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>
|
||||
|
||||
* enum.c (find_index_i): modified to shut warning up.
|
||||
|
|
58
enum.c
58
enum.c
|
@ -81,19 +81,23 @@ enum_grep(VALUE obj, VALUE pat)
|
|||
}
|
||||
|
||||
static VALUE
|
||||
count_i(VALUE i, VALUE *arg)
|
||||
count_i(VALUE i, VALUE memop)
|
||||
{
|
||||
if (rb_equal(i, arg[0])) {
|
||||
arg[1]++;
|
||||
VALUE *memo = (VALUE*)memop;
|
||||
|
||||
if (rb_equal(i, memo[1])) {
|
||||
memo[0]++;
|
||||
}
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
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))) {
|
||||
(*n)++;
|
||||
memo[0]++;
|
||||
}
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -115,31 +119,24 @@ count_iter_i(VALUE i, long *n, int argc, VALUE *argv)
|
|||
static VALUE
|
||||
enum_count(int argc, VALUE *argv, VALUE obj)
|
||||
{
|
||||
if (argc == 1) {
|
||||
VALUE item, args[2];
|
||||
VALUE memo[2]; /* [count, condition value] */
|
||||
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()) {
|
||||
rb_warn("given block not used");
|
||||
}
|
||||
rb_scan_args(argc, argv, "1", &item);
|
||||
args[0] = item;
|
||||
args[1] = 0;
|
||||
rb_block_call(obj, id_each, 0, 0, count_i, (VALUE)&args);
|
||||
return INT2NUM(args[1]);
|
||||
func = count_i;
|
||||
}
|
||||
else if (argc == 0) {
|
||||
long n;
|
||||
|
||||
RETURN_ENUMERATOR(obj, 0, 0);
|
||||
n = 0;
|
||||
rb_block_call(obj, id_each, 0, 0, count_iter_i, (VALUE)&n);
|
||||
return INT2NUM(n);
|
||||
}
|
||||
else {
|
||||
VALUE v;
|
||||
rb_scan_args(argc, argv, "1", &v);
|
||||
return Qnil; /* not reached */
|
||||
}
|
||||
memo[0] = 0;
|
||||
rb_block_call(obj, id_each, 0, 0, func, (VALUE)&memo);
|
||||
return INT2NUM(memo[0]);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -202,6 +199,7 @@ static VALUE
|
|||
find_index_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
|
||||
{
|
||||
VALUE *memo = (VALUE*)memop;
|
||||
|
||||
if (RTEST(enum_yield(argc, argv))) {
|
||||
memo[0] = UINT2NUM(memo[1]);
|
||||
rb_iter_break();
|
||||
|
@ -234,24 +232,18 @@ enum_find_index(int argc, VALUE *argv, VALUE obj)
|
|||
|
||||
if (argc == 0) {
|
||||
RETURN_ENUMERATOR(obj, 0, 0);
|
||||
memo[0] = Qnil;
|
||||
memo[1] = 0;
|
||||
memo[2] = Qundef;
|
||||
func = find_index_iter_i;
|
||||
}
|
||||
else {
|
||||
VALUE item;
|
||||
|
||||
rb_scan_args(argc, argv, "1", &item);
|
||||
rb_scan_args(argc, argv, "1", &memo[2]);
|
||||
if (rb_block_given_p()) {
|
||||
rb_warn("given block not used");
|
||||
}
|
||||
memo[0] = Qnil;
|
||||
memo[1] = 0;
|
||||
memo[2] = item;
|
||||
func = find_index_i;
|
||||
}
|
||||
|
||||
memo[0] = Qnil;
|
||||
memo[1] = 0;
|
||||
rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
|
||||
return memo[0];
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue