2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
enum.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 1 15:15:19 JST 1993
|
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
Copyright (C) 1993-2002 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
2000-01-17 11:37:53 +03:00
|
|
|
#include "node.h"
|
2001-12-10 10:18:16 +03:00
|
|
|
#include "util.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_mEnumerable;
|
1998-01-16 15:13:05 +03:00
|
|
|
static ID id_each, id_eqq, id_cmp;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_each(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
return rb_funcall(obj, id_each, 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
1998-01-16 15:13:05 +03:00
|
|
|
grep_i(i, arg)
|
|
|
|
VALUE i, *arg;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(arg[1], i);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
1999-11-17 10:30:37 +03:00
|
|
|
grep_iter_i(i, arg)
|
|
|
|
VALUE i, *arg;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-11-17 10:30:37 +03:00
|
|
|
if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
|
|
|
|
rb_ary_push(arg[1], rb_yield(i));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_grep(obj, pat)
|
|
|
|
VALUE obj, pat;
|
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
VALUE arg[2];
|
1999-11-17 10:30:37 +03:00
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
arg[0] = pat;
|
|
|
|
arg[1] = ary;
|
|
|
|
|
|
|
|
rb_iterate(rb_each, obj, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)arg);
|
|
|
|
|
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
find_i(i, memo)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE i;
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u2.value = Qtrue;
|
|
|
|
memo->u1.value = i;
|
1998-01-16 15:19:22 +03:00
|
|
|
rb_iter_break();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_find(argc, argv, obj)
|
|
|
|
int argc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE* argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE obj;
|
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, Qfalse, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE if_none;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &if_none);
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_iterate(rb_each, obj, find_i, (VALUE)memo);
|
|
|
|
if (memo->u2.value) {
|
2002-03-18 04:48:56 +03:00
|
|
|
VALUE result = memo->u1.value;
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (!NIL_P(if_none)) {
|
2002-08-01 13:42:38 +04:00
|
|
|
return rb_funcall(if_none, rb_intern("call"), 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2002-07-05 12:58:57 +04:00
|
|
|
find_all_i(i, ary)
|
|
|
|
VALUE i, ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
2002-07-05 12:58:57 +04:00
|
|
|
rb_ary_push(ary, i);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_find_all(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
|
|
|
rb_iterate(rb_each, obj, find_all_i, ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
2002-07-05 12:58:57 +04:00
|
|
|
reject_i(i, ary)
|
|
|
|
VALUE i, ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
if (!RTEST(rb_yield(i))) {
|
2002-07-05 12:58:57 +04:00
|
|
|
rb_ary_push(ary, i);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_reject(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
|
|
|
rb_iterate(rb_each, obj, reject_i, ary);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
return ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2002-07-05 12:58:57 +04:00
|
|
|
collect_i(i, ary)
|
|
|
|
VALUE i, ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
rb_ary_push(ary, rb_yield(i));
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2001-03-13 08:45:13 +03:00
|
|
|
collect_all(i, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE i, ary;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, i);
|
2002-07-05 12:58:57 +04:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_to_a(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_iterate(rb_each, obj, collect_all, ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1999-10-29 13:25:48 +04:00
|
|
|
static VALUE
|
|
|
|
enum_collect(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-07-05 12:58:57 +04:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
rb_iterate(rb_each, obj, rb_block_given_p() ? collect_i : collect_all, ary);
|
1999-10-29 13:25:48 +04:00
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
return ary;
|
1999-10-29 13:25:48 +04:00
|
|
|
}
|
|
|
|
|
2001-01-09 10:26:21 +03:00
|
|
|
static VALUE
|
2002-03-18 04:46:33 +03:00
|
|
|
inject_i(i, memo)
|
2001-01-09 10:26:21 +03:00
|
|
|
VALUE i;
|
2002-03-18 04:46:33 +03:00
|
|
|
NODE *memo;
|
2001-01-09 10:26:21 +03:00
|
|
|
{
|
2002-03-18 04:46:33 +03:00
|
|
|
if (memo->u2.value) {
|
|
|
|
memo->u2.value = Qfalse;
|
|
|
|
memo->u1.value = i;
|
|
|
|
}
|
2002-03-18 04:48:56 +03:00
|
|
|
else {
|
2002-03-18 04:46:33 +03:00
|
|
|
memo->u1.value = rb_yield(rb_assoc_new(memo->u1.value, i));
|
2002-03-18 04:48:56 +03:00
|
|
|
}
|
2001-01-09 10:26:21 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-03-18 04:46:33 +03:00
|
|
|
enum_inject(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv, obj;
|
2001-01-09 10:26:21 +03:00
|
|
|
{
|
2002-03-18 04:46:33 +03:00
|
|
|
NODE *memo;
|
|
|
|
VALUE n;
|
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
if (rb_scan_args(argc, argv, "01", &n) == 1) {
|
2002-03-18 04:46:33 +03:00
|
|
|
memo = rb_node_newnode(NODE_MEMO, n, Qfalse, 0);
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-03-18 04:46:33 +03:00
|
|
|
memo = rb_node_newnode(NODE_MEMO, Qnil, Qtrue, 0);
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
2002-03-18 04:46:33 +03:00
|
|
|
rb_iterate(rb_each, obj, inject_i, (VALUE)memo);
|
|
|
|
n = memo->u1.value;
|
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2001-01-09 10:26:21 +03:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-05-01 13:41:50 +04:00
|
|
|
static VALUE
|
|
|
|
partition_i(i, ary)
|
|
|
|
VALUE i, *ary;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
|
|
|
rb_ary_push(ary[0], i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_ary_push(ary[1], i);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_partition(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE ary[2];
|
|
|
|
|
|
|
|
ary[0] = rb_ary_new();
|
2002-07-05 12:58:57 +04:00
|
|
|
ary[1] = rb_ary_new();
|
2002-05-01 13:41:50 +04:00
|
|
|
rb_iterate(rb_each, obj, partition_i, (VALUE)ary);
|
|
|
|
|
|
|
|
return rb_assoc_new(ary[0], ary[1]);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
enum_sort(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ary_sort(enum_to_a(obj));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-08-23 10:02:15 +04:00
|
|
|
static VALUE
|
2001-12-10 10:18:16 +03:00
|
|
|
sort_by_i(i, ary)
|
|
|
|
VALUE i, ary;
|
2001-08-23 10:02:15 +04:00
|
|
|
{
|
2001-11-27 13:00:35 +03:00
|
|
|
VALUE v, e;
|
|
|
|
|
|
|
|
v = rb_yield(i);
|
2001-12-10 10:18:16 +03:00
|
|
|
e = rb_assoc_new(v, i);
|
|
|
|
rb_ary_push(ary, e);
|
2001-08-23 10:02:15 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
static int
|
|
|
|
sort_by_cmp(a, b)
|
|
|
|
VALUE *a, *b;
|
2001-08-23 10:02:15 +04:00
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
VALUE retval;
|
|
|
|
|
|
|
|
retval = rb_funcall(RARRAY(*a)->ptr[0], id_cmp, 1, RARRAY(*b)->ptr[0]);
|
|
|
|
return rb_cmpint(retval);
|
2001-08-23 10:02:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_sort_by(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
VALUE ary;
|
2001-08-23 10:02:15 +04:00
|
|
|
long i;
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
ary = rb_ary_new2((TYPE(obj) == T_ARRAY) ? RARRAY(obj)->len : 2000);
|
|
|
|
rb_iterate(rb_each, obj, sort_by_i, ary);
|
2002-07-05 12:58:57 +04:00
|
|
|
if (RARRAY(ary)->len > 1) {
|
2002-06-13 07:55:44 +04:00
|
|
|
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE), sort_by_cmp);
|
|
|
|
}
|
2001-08-23 10:02:15 +04:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
VALUE e = RARRAY(ary)->ptr[i];
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->ptr[i] = RARRAY(e)->ptr[1];
|
2001-08-23 10:02:15 +04:00
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2001-03-13 08:45:13 +03:00
|
|
|
static VALUE
|
|
|
|
all_i(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
|
|
|
{
|
|
|
|
if (!RTEST(rb_yield(i))) {
|
|
|
|
memo->u1.value = Qfalse;
|
|
|
|
rb_iter_break();
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_all(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-03-18 04:48:56 +03:00
|
|
|
VALUE result;
|
2001-03-13 08:45:13 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
|
|
|
|
|
|
|
|
memo->u1.value = Qtrue;
|
|
|
|
rb_iterate(rb_each, obj, all_i, (VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
result = memo->u1.value;
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
return result;
|
2001-03-13 08:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
any_i(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
|
|
|
{
|
|
|
|
if (RTEST(rb_yield(i))) {
|
|
|
|
memo->u1.value = Qtrue;
|
|
|
|
rb_iter_break();
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_any(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-03-18 04:48:56 +03:00
|
|
|
VALUE result;
|
2001-03-13 08:45:13 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
|
|
|
|
|
|
|
|
memo->u1.value = Qfalse;
|
|
|
|
rb_iterate(rb_each, obj, any_i, (VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
result = memo->u1.value;
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
return result;
|
2001-03-13 08:45:13 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
min_i(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
if (NIL_P(memo->u1.value)) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2000-01-17 11:37:53 +03:00
|
|
|
cmp = rb_funcall(i, id_cmp, 1, memo->u1.value);
|
2002-07-05 12:58:57 +04:00
|
|
|
if (rb_cmpint(cmp) < 0) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
min_ii(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
if (NIL_P(memo->u1.value)) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2000-01-17 11:37:53 +03:00
|
|
|
cmp = rb_yield(rb_assoc_new(i, memo->u1.value));
|
2002-07-05 12:58:57 +04:00
|
|
|
if (rb_cmpint(cmp) < 0) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_min(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-03-18 04:48:56 +03:00
|
|
|
VALUE result;
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
rb_iterate(rb_each, obj, rb_block_given_p() ? min_ii : min_i, (VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
result = memo->u1.value;
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
max_i(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
if (NIL_P(memo->u1.value)) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2000-01-17 11:37:53 +03:00
|
|
|
cmp = rb_funcall(i, id_cmp, 1, memo->u1.value);
|
2002-07-05 12:58:57 +04:00
|
|
|
if (rb_cmpint(cmp) > 0) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
max_ii(i, memo)
|
|
|
|
VALUE i;
|
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE cmp;
|
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
if (NIL_P(memo->u1.value)) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2000-01-17 11:37:53 +03:00
|
|
|
cmp = rb_yield(rb_assoc_new(i, memo->u1.value));
|
2002-07-05 12:58:57 +04:00
|
|
|
if (rb_cmpint(cmp) > 0) {
|
2000-01-17 11:37:53 +03:00
|
|
|
memo->u1.value = i;
|
2002-07-05 12:58:57 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_max(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2002-03-18 04:48:56 +03:00
|
|
|
VALUE result;
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-07-05 12:58:57 +04:00
|
|
|
rb_iterate(rb_each, obj, rb_block_given_p() ? max_ii : max_i, (VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
result = memo->u1.value;
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-18 04:48:56 +03:00
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
member_i(item, memo)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE item;
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
if (rb_equal(item, memo->u1.value)) {
|
|
|
|
memo->u2.value = Qtrue;
|
1998-01-16 15:19:22 +03:00
|
|
|
rb_iter_break();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_member(obj, val)
|
|
|
|
VALUE obj, val;
|
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
VALUE result;
|
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, val, Qfalse, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_iterate(rb_each, obj, member_i, (VALUE)memo);
|
|
|
|
result = memo->u2.value;
|
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2000-01-17 11:37:53 +03:00
|
|
|
each_with_index_i(val, memo)
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE val;
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_yield(rb_assoc_new(val, INT2FIX(memo->u3.cnt)));
|
|
|
|
memo->u3.cnt++;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
enum_each_with_index(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
NODE *memo = rb_node_newnode(NODE_MEMO, 0, 0, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2000-01-17 11:37:53 +03:00
|
|
|
rb_iterate(rb_each, obj, each_with_index_i, (VALUE)memo);
|
|
|
|
rb_gc_force_recycle((VALUE)memo);
|
2002-03-11 11:02:04 +03:00
|
|
|
return obj;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
Init_Enumerable()
|
|
|
|
{
|
1999-01-20 07:59:39 +03: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);
|
2001-08-23 10:02:15 +04:00
|
|
|
rb_define_method(rb_mEnumerable,"sort_by", enum_sort_by, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"grep", enum_grep, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"find", enum_find, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_mEnumerable,"detect", enum_find, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0);
|
1999-08-13 09: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-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_define_method(rb_mEnumerable,"map", enum_collect, 0);
|
2002-03-18 04:46:33 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"inject", enum_inject, -1);
|
2002-05-01 13:41:50 +04:00
|
|
|
rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_mEnumerable,"min", enum_min, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"max", enum_max, 0);
|
|
|
|
rb_define_method(rb_mEnumerable,"member?", enum_member, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"include?", enum_member, 1);
|
|
|
|
rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
id_eqq = rb_intern("===");
|
|
|
|
id_each = rb_intern("each");
|
|
|
|
id_cmp = rb_intern("<=>");
|
|
|
|
}
|
2002-07-05 12:58:57 +04:00
|
|
|
|