summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorAaron Patterson <[email protected]>2024-07-29 13:28:57 -0700
committerAaron Patterson <[email protected]>2024-07-29 14:18:11 -0700
commit2c1655314a0700a90b7ed12bf8c920ad2d78654f (patch)
treeec758c9e97cc71adf644b85a622c279766ad8646 /array.c
parentacbb8d4fb56ac3b5894991760a075dbef78d10e3 (diff)
Revert moving things to Ruby
This is slowing down benchmarks on x86, so lets revert it for now.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11275
Diffstat (limited to 'array.c')
-rw-r--r--array.c81
1 files changed, 75 insertions, 6 deletions
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 # => #<Enumerator: [:foo, "bar", 2]:map>
+ *
+ */
+
+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 # => #<Enumerator: [:foo, "bar", 2, :bam]: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];
@@ -6626,12 +6697,6 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
}
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)
{
return ary_sample(ec, ary, rb_cRandom, Qfalse, Qfalse);
@@ -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);