1998-01-16 07:13:05 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
enum.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 1 15:15:19 JST 1993
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Copyright (C) 1993-1999 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_mEnumerable;
|
1998-01-16 07:13:05 -05:00
|
|
|
static ID id_each, id_eqq, id_cmp;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_each(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
return rb_funcall(obj, id_each, 0, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
grep_i(i, arg)
|
|
|
|
VALUE i, *arg;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(arg[1], i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
grep_iter_i(i, pat)
|
|
|
|
VALUE i, pat;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_funcall(pat, id_eqq, 1, i))) {
|
|
|
|
rb_yield(i);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_grep(obj, pat)
|
|
|
|
VALUE obj, pat;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_iterator_p()) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_iterate(rb_each, obj, grep_iter_i, pat);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE tmp, arg[2];
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
arg[0] = pat; arg[1] = tmp = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iterate(rb_each, obj, grep_i, (VALUE)arg);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RARRAY(tmp)->len == 0) return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct find_arg {
|
|
|
|
int found;
|
|
|
|
VALUE val;
|
|
|
|
};
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
find_i(i, arg)
|
|
|
|
VALUE i;
|
|
|
|
struct find_arg *arg;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
arg->found = Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
arg->val = i;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iter_break();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_find(argc, argv, obj)
|
|
|
|
int argc;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE* argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
struct find_arg arg;
|
|
|
|
VALUE if_none;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &if_none);
|
1999-01-19 23:59:39 -05:00
|
|
|
arg.found = Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iterate(rb_each, obj, find_i, (VALUE)&arg);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (arg.found) {
|
|
|
|
return arg.val;
|
|
|
|
}
|
|
|
|
if (!NIL_P(if_none)) {
|
|
|
|
rb_eval_cmd(if_none, Qnil);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
find_all_i(i, tmp)
|
|
|
|
VALUE i, tmp;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(tmp, i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_find_all(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
tmp = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_iterate(rb_each, obj, find_all_i, tmp);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
reject_i(i, tmp)
|
|
|
|
VALUE i, tmp;
|
|
|
|
{
|
|
|
|
if (!RTEST(rb_yield(i))) {
|
|
|
|
rb_ary_push(tmp, i);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_reject(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_ary_new();
|
|
|
|
rb_iterate(rb_each, obj, reject_i, tmp);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
collect_i(i, tmp)
|
|
|
|
VALUE i, tmp;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(tmp, rb_yield(i));
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_collect(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
tmp = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_iterate(rb_each, obj, collect_i, tmp);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
enum_all(i, ary)
|
|
|
|
VALUE i, ary;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, i);
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_to_a(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_iterate(rb_each, obj, enum_all, ary);
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_sort(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ary_sort(enum_to_a(obj));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
min_i(i, min)
|
|
|
|
VALUE i, *min;
|
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
|
|
|
if (NIL_P(*min))
|
|
|
|
*min = i;
|
|
|
|
else {
|
|
|
|
cmp = rb_funcall(i, id_cmp, 1, *min);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIX2LONG(cmp) < 0)
|
1998-01-16 07:13:05 -05:00
|
|
|
*min = i;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
min_ii(i, min)
|
|
|
|
VALUE i, *min;
|
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
|
|
|
if (NIL_P(*min))
|
|
|
|
*min = i;
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
cmp = rb_yield(rb_assoc_new(i, *min));
|
|
|
|
if (FIX2LONG(cmp) < 0)
|
1998-01-16 07:13:05 -05:00
|
|
|
*min = i;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_min(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE min = Qnil;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_iterate(rb_each, obj, rb_iterator_p()?min_ii:min_i, (VALUE)&min);
|
1998-01-16 07:13:05 -05:00
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
max_i(i, max)
|
|
|
|
VALUE i, *max;
|
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
|
|
|
if (NIL_P(*max))
|
|
|
|
*max = i;
|
|
|
|
else {
|
|
|
|
cmp = rb_funcall(i, id_cmp, 1, *max);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIX2LONG(cmp) > 0)
|
1998-01-16 07:13:05 -05:00
|
|
|
*max = i;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
max_ii(i, max)
|
|
|
|
VALUE i, *max;
|
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
|
|
|
if (NIL_P(*max))
|
|
|
|
*max = i;
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
cmp = rb_yield(rb_assoc_new(i, *max));
|
|
|
|
if (FIX2LONG(cmp) > 0)
|
1998-01-16 07:13:05 -05:00
|
|
|
*max = i;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_max(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE max = Qnil;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_iterate(rb_each, obj, rb_iterator_p()?max_ii:max_i, (VALUE)&max);
|
1998-01-16 07:13:05 -05:00
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct i_v_pair {
|
|
|
|
int i;
|
|
|
|
VALUE v;
|
|
|
|
int found;
|
|
|
|
};
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
index_i(item, iv)
|
|
|
|
VALUE item;
|
|
|
|
struct i_v_pair *iv;
|
|
|
|
{
|
|
|
|
if (rb_equal(item, iv->v)) {
|
|
|
|
iv->found = 1;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iter_break();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
iv->i++;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_index(obj, val)
|
|
|
|
VALUE obj, val;
|
|
|
|
{
|
|
|
|
struct i_v_pair iv;
|
|
|
|
|
|
|
|
iv.i = 0;
|
|
|
|
iv.v = val;
|
|
|
|
iv.found = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iterate(rb_each, obj, index_i, (VALUE)&iv);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (iv.found) return INT2FIX(iv.i);
|
|
|
|
return Qnil; /* not found */
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
member_i(item, iv)
|
|
|
|
VALUE item;
|
|
|
|
struct i_v_pair *iv;
|
|
|
|
{
|
|
|
|
if (rb_equal(item, iv->v)) {
|
|
|
|
iv->i = 1;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iter_break();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_member(obj, val)
|
|
|
|
VALUE obj, val;
|
|
|
|
{
|
|
|
|
struct i_v_pair iv;
|
|
|
|
|
|
|
|
iv.i = 0;
|
|
|
|
iv.v = val;
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iterate(rb_each, obj, member_i, (VALUE)&iv);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (iv.i) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
length_i(i, length)
|
|
|
|
VALUE i;
|
|
|
|
int *length;
|
|
|
|
{
|
|
|
|
(*length)++;
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
enum_length(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_iterate(rb_each, obj, length_i, (VALUE)&length);
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2FIX(length);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
|
|
|
rb_enum_length(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
return enum_length(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
each_with_index_i(val, indexp)
|
|
|
|
VALUE val;
|
|
|
|
int *indexp;
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
rb_yield(rb_assoc_new(val, INT2FIX(*indexp)));
|
|
|
|
#else
|
|
|
|
rb_yield(rb_ary_concat(rb_Array(val), INT2FIX(*indexp)));
|
|
|
|
#endif
|
|
|
|
(*indexp)++;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_each_with_index(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
rb_iterate(rb_each, obj, each_with_index_i, (VALUE)&index);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
|
|
|
Init_Enumerable()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mEnumerable = rb_define_module("Enumerable");
|
|
|
|
|
|
|
|
rb_define_method(rb_mEnumerable,"to_a", enum_to_a, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"entries", enum_to_a, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_mEnumerable,"sort", enum_sort, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"grep", enum_grep, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"find", enum_find, -1);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_method(rb_mEnumerable,"detect", enum_find, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_method(rb_mEnumerable,"select", enum_find_all, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"reject", enum_reject, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"min", enum_min, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"max", enum_max, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"index", enum_index, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"member?", enum_member, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"include?", enum_member, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"length", enum_length, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"size", enum_length, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
id_eqq = rb_intern("===");
|
|
|
|
id_each = rb_intern("each");
|
|
|
|
id_cmp = rb_intern("<=>");
|
|
|
|
}
|