From 2c1655314a0700a90b7ed12bf8c920ad2d78654f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 29 Jul 2024 13:28:57 -0700 Subject: Revert moving things to Ruby This is slowing down benchmarks on x86, so lets revert it for now. --- array.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 6 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index 0a4152f5e9..e5f9e3b54a 100644 --- a/array.c +++ b/array.c @@ -3630,6 +3630,41 @@ rb_ary_sort_by_bang(VALUE ary) return ary; } + +/* + * call-seq: + * array.map {|element| ... } -> new_array + * array.map -> new_enumerator + * + * Calls the block, if given, with each element of +self+; + * returns a new +Array+ whose elements are the return values from the block: + * + * a = [:foo, 'bar', 2] + * a1 = a.map {|element| element.class } + * a1 # => [Symbol, String, Integer] + * + * Returns a new Enumerator if no block given: + * a = [:foo, 'bar', 2] + * a1 = a.map + * a1 # => # + * + */ + +static VALUE +rb_ary_collect(VALUE ary) +{ + long i; + VALUE collect; + + RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length); + collect = rb_ary_new2(RARRAY_LEN(ary)); + for (i = 0; i < RARRAY_LEN(ary); i++) { + rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i))); + } + return collect; +} + + /* * call-seq: * array.map! {|element| ... } -> self @@ -3772,6 +3807,42 @@ rb_ary_values_at(int argc, VALUE *argv, VALUE ary) } +/* + * call-seq: + * array.select {|element| ... } -> new_array + * array.select -> new_enumerator + * + * Calls the block, if given, with each element of +self+; + * returns a new +Array+ containing those elements of +self+ + * for which the block returns a truthy value: + * + * a = [:foo, 'bar', 2, :bam] + * a1 = a.select {|element| element.to_s.start_with?('b') } + * a1 # => ["bar", :bam] + * + * Returns a new Enumerator if no block given: + * + * a = [:foo, 'bar', 2, :bam] + * a.select # => # + * + */ + +static VALUE +rb_ary_select(VALUE ary) +{ + VALUE result; + long i; + + RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length); + result = rb_ary_new2(RARRAY_LEN(ary)); + for (i = 0; i < RARRAY_LEN(ary); i++) { + if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) { + rb_ary_push(result, rb_ary_elt(ary, i)); + } + } + return result; +} + struct select_bang_arg { VALUE ary; long len[2]; @@ -6625,12 +6696,6 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE return result; } -static VALUE -ary_sized_alloc(rb_execution_context_t *ec, VALUE self) -{ - return rb_ary_new2(RARRAY_LEN(self)); -} - static VALUE ary_sample0(rb_execution_context_t *ec, VALUE ary) { @@ -8633,9 +8698,13 @@ Init_Array(void) rb_define_method(rb_cArray, "sort", rb_ary_sort, 0); rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0); rb_define_method(rb_cArray, "sort_by!", rb_ary_sort_by_bang, 0); + rb_define_method(rb_cArray, "collect", rb_ary_collect, 0); rb_define_method(rb_cArray, "collect!", rb_ary_collect_bang, 0); + rb_define_method(rb_cArray, "map", rb_ary_collect, 0); rb_define_method(rb_cArray, "map!", rb_ary_collect_bang, 0); + rb_define_method(rb_cArray, "select", rb_ary_select, 0); rb_define_method(rb_cArray, "select!", rb_ary_select_bang, 0); + rb_define_method(rb_cArray, "filter", rb_ary_select, 0); rb_define_method(rb_cArray, "filter!", rb_ary_select_bang, 0); rb_define_method(rb_cArray, "keep_if", rb_ary_keep_if, 0); rb_define_method(rb_cArray, "values_at", rb_ary_values_at, -1); -- cgit v1.2.3