/**********************************************************************
enum.c -
$Author$
$Date$
created at: Fri Oct 1 15:15:19 JST 1993
Copyright (C) 1993-2003 Yukihiro Matsumoto
**********************************************************************/
#include "ruby.h"
#include "node.h"
#include "util.h"
VALUE rb_mEnumerable;
static ID id_each, id_eqq, id_cmp;
VALUE
rb_each(VALUE obj)
{
return rb_funcall(obj, id_each, 0, 0);
}
static VALUE
grep_i(VALUE i, VALUE *arg)
{
if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
rb_ary_push(arg[1], i);
}
return Qnil;
}
static VALUE
grep_iter_i(VALUE i, VALUE *arg)
{
if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
rb_ary_push(arg[1], rb_yield(i));
}
return Qnil;
}
/*
* call-seq:
* enum.grep(pattern) => array
* enum.grep(pattern) {| obj | block } => array
*
* Returns an array of every element in <i>enum</i> for which
* <code>Pattern === element</code>. If the optional <em>block</em> is
* supplied, each matching element is passed to it, and the block's
* result is stored in the output array.
*
* (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
* c = IO.constants
* c.grep(/SEEK/) #=> ["SEEK_END", "SEEK_SET", "SEEK_CUR"]
* res = c.grep(/SEEK/) {|v| IO.const_get(v) }
* res #=> [2, 0, 1]
*
*/
static VALUE
enum_grep(VALUE obj, VALUE pat)
{
VALUE ary = rb_ary_new();
VALUE arg[2];
arg[0] = pat;
arg[1] = ary;
rb_iterate(rb_each, obj, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)arg);
return ary;
}
static VALUE
count_i(VALUE i, VALUE *arg)
{
if (rb_equal(i, arg[0])) {
arg[1]++;
}
return Qnil;
}
static VALUE
count_iter_i(VALUE i, long *n)
{
if (RTEST(rb_yield(i))) {
(*n)++;
}
return Qnil;
}
/*
* call-seq:
* enum.count(item) => int
* enum.count {| obj | block } => int
*
* Returns the number of items in <i>enum</i> for which equals to <i>item</i>.
* If a block is given, counts the number of elements yielding a true value.
*
* ary = [1, 2, 4, 2]
* ary.count(2) # => 2
* ary.count{|x|x%2==0} # => 3
*
*/
static VALUE
enum_count(int argc, VALUE *argv, VALUE obj)
{
if (argc == 1) {
VALUE item, args[2];
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_iterate(rb_each, obj, count_i, (VALUE)&args);
return INT2NUM(args[1]);
}
else {
long n = 0;
rb_iterate(rb_each, obj, count_iter_i, (VALUE)&n);
return INT2NUM(n);
}
}
static VALUE
find_i(VALUE i, VALUE *memo)
{
if (RTEST(rb_yield(i))) {
*memo = i;
rb_iter_break();
}
return Qnil;
}
/*
* call-seq:
* enum.detect(ifnone = nil) {| obj | block } => obj or nil
* enum.find(ifnone = nil) {| obj | block } => obj or nil
*
* Passes each entry in <i>enum</i> to <em>block</em>. Returns the
* first for which <em>block</em> is not <code>false</code>. If no
* object matches, calls <i>ifnone</i> and returns its result when it
* is specified, or returns <code>nil</code>
*
* (1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35
*
*/
static VALUE
enum_find(int argc, VALUE *argv, VALUE obj)
{
VALUE memo = Qundef;
VALUE if_none;
rb_scan_args(argc, argv, "01", &if_none);
|