diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1999-01-20 04:59:39 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1999-01-20 04:59:39 +0000 |
commit | 62e648e148b3cb9f96dcce808c55c02b7ccb4486 (patch) | |
tree | 9708892ece92e860d81559ab55e6b1f9400d7ffc /enum.c | |
parent | aeb049c573be4dc24dd20650f40e4777e0f698cf (diff) | |
download | ruby-62e648e148b3cb9f96dcce808c55c02b7ccb4486.tar.gz |
ruby 1.3 cycle
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 141 |
1 files changed, 74 insertions, 67 deletions
@@ -6,13 +6,13 @@ $Date$ created at: Fri Oct 1 15:15:19 JST 1993 - Copyright (C) 1993-1996 Yukihiro Matsumoto + Copyright (C) 1993-1998 Yukihiro Matsumoto ************************************************/ #include "ruby.h" -VALUE mEnumerable; +VALUE rb_mEnumerable; static ID id_each, id_eqq, id_cmp; VALUE @@ -27,7 +27,7 @@ grep_i(i, arg) VALUE i, *arg; { if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) { - ary_push(arg[1], i); + rb_ary_push(arg[1], i); } return Qnil; } @@ -46,14 +46,14 @@ static VALUE enum_grep(obj, pat) VALUE obj, pat; { - if (iterator_p()) { + if (rb_iterator_p()) { rb_iterate(rb_each, obj, grep_iter_i, pat); return obj; } else { VALUE tmp, arg[2]; - arg[0] = pat; arg[1] = tmp = ary_new(); + arg[0] = pat; arg[1] = tmp = rb_ary_new(); rb_iterate(rb_each, obj, grep_i, (VALUE)arg); return tmp; @@ -71,7 +71,7 @@ find_i(i, arg) struct find_arg *arg; { if (RTEST(rb_yield(i))) { - arg->found = TRUE; + arg->found = Qtrue; arg->val = i; rb_iter_break(); } @@ -88,7 +88,7 @@ enum_find(argc, argv, obj) VALUE if_none; rb_scan_args(argc, argv, "01", &if_none); - arg.found = FALSE; + arg.found = Qfalse; rb_iterate(rb_each, obj, find_i, (VALUE)&arg); if (arg.found) { return arg.val; @@ -104,7 +104,7 @@ find_all_i(i, tmp) VALUE i, tmp; { if (RTEST(rb_yield(i))) { - ary_push(tmp, i); + rb_ary_push(tmp, i); } return Qnil; } @@ -115,7 +115,7 @@ enum_find_all(obj) { VALUE tmp; - tmp = ary_new(); + tmp = rb_ary_new(); rb_iterate(rb_each, obj, find_all_i, tmp); return tmp; @@ -125,12 +125,7 @@ static VALUE collect_i(i, tmp) VALUE i, tmp; { - VALUE retval; - - retval = rb_yield(i); - if (RTEST(retval)) { - ary_push(tmp, retval); - } + rb_ary_push(tmp, rb_yield(i)); return Qnil; } @@ -140,37 +135,17 @@ enum_collect(obj) { VALUE tmp; - tmp = ary_new(); + tmp = rb_ary_new(); rb_iterate(rb_each, obj, collect_i, tmp); return tmp; } static VALUE -reverse_i(i, tmp) - VALUE i, tmp; -{ - ary_unshift(tmp, i); - return Qnil; -} - -static VALUE -enum_reverse(obj) - VALUE obj; -{ - VALUE tmp; - - tmp = ary_new(); - rb_iterate(rb_each, obj, reverse_i, tmp); - - return tmp; -} - -static VALUE enum_all(i, ary) VALUE i, ary; { - ary_push(ary, i); + rb_ary_push(ary, i); return Qnil; } @@ -180,7 +155,7 @@ enum_to_a(obj) { VALUE ary; - ary = ary_new(); + ary = rb_ary_new(); rb_iterate(rb_each, obj, enum_all, ary); return ary; @@ -190,7 +165,7 @@ static VALUE enum_sort(obj) VALUE obj; { - return ary_sort(enum_to_a(obj)); + return rb_ary_sort(enum_to_a(obj)); } static VALUE @@ -203,7 +178,7 @@ min_i(i, min) *min = i; else { cmp = rb_funcall(i, id_cmp, 1, *min); - if (FIX2INT(cmp) < 0) + if (FIX2LONG(cmp) < 0) *min = i; } return Qnil; @@ -218,8 +193,8 @@ min_ii(i, min) if (NIL_P(*min)) *min = i; else { - cmp = rb_yield(assoc_new(i, *min)); - if (FIX2INT(cmp) < 0) + cmp = rb_yield(rb_assoc_new(i, *min)); + if (FIX2LONG(cmp) < 0) *min = i; } return Qnil; @@ -231,7 +206,7 @@ enum_min(obj) { VALUE min = Qnil; - rb_iterate(rb_each, obj, iterator_p()?min_ii:min_i, (VALUE)&min); + rb_iterate(rb_each, obj, rb_iterator_p()?min_ii:min_i, (VALUE)&min); return min; } @@ -245,7 +220,7 @@ max_i(i, max) *max = i; else { cmp = rb_funcall(i, id_cmp, 1, *max); - if (FIX2INT(cmp) > 0) + if (FIX2LONG(cmp) > 0) *max = i; } return Qnil; @@ -260,8 +235,8 @@ max_ii(i, max) if (NIL_P(*max)) *max = i; else { - cmp = rb_yield(assoc_new(i, *max)); - if (FIX2INT(cmp) > 0) + cmp = rb_yield(rb_assoc_new(i, *max)); + if (FIX2LONG(cmp) > 0) *max = i; } return Qnil; @@ -273,7 +248,7 @@ enum_max(obj) { VALUE max = Qnil; - rb_iterate(rb_each, obj, iterator_p()?max_ii:max_i, (VALUE)&max); + rb_iterate(rb_each, obj, rb_iterator_p()?max_ii:max_i, (VALUE)&max); return max; } @@ -333,8 +308,8 @@ enum_member(obj, val) iv.i = 0; iv.v = val; rb_iterate(rb_each, obj, member_i, (VALUE)&iv); - if (iv.i) return TRUE; - return FALSE; + if (iv.i) return Qtrue; + return Qfalse; } static VALUE @@ -346,7 +321,7 @@ length_i(i, length) return Qnil; } -VALUE +static VALUE enum_length(obj) VALUE obj; { @@ -356,26 +331,58 @@ enum_length(obj) return INT2FIX(length); } +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; +} + void Init_Enumerable() { - mEnumerable = rb_define_module("Enumerable"); - - rb_define_method(mEnumerable,"to_a", enum_to_a, 0); - - rb_define_method(mEnumerable,"sort", enum_sort, 0); - rb_define_method(mEnumerable,"grep", enum_grep, 1); - rb_define_method(mEnumerable,"find", enum_find, -1); - rb_define_method(mEnumerable,"find_all", enum_find_all, 0); - rb_define_method(mEnumerable,"collect", enum_collect, 0); - rb_define_method(mEnumerable,"reverse", enum_reverse, 0); - rb_define_method(mEnumerable,"min", enum_min, 0); - rb_define_method(mEnumerable,"max", enum_max, 0); - rb_define_method(mEnumerable,"index", enum_index, 1); - rb_define_method(mEnumerable,"member?", enum_member, 1); - rb_define_method(mEnumerable,"include?", enum_member, 1); - rb_define_method(mEnumerable,"length", enum_length, 0); - rb_define_method(mEnumerable,"size", enum_length, 0); + 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); + rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0); + 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); id_eqq = rb_intern("==="); id_each = rb_intern("each"); |