зеркало из https://github.com/github/ruby.git
Revert moving things to Ruby
This is slowing down benchmarks on x86, so lets revert it for now.
This commit is contained in:
Родитель
acbb8d4fb5
Коммит
2c1655314a
81
array.c
81
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];
|
||||
|
@ -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);
|
||||
|
|
67
array.rb
67
array.rb
|
@ -55,73 +55,6 @@ class Array
|
|||
self
|
||||
end
|
||||
|
||||
# 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>
|
||||
def map
|
||||
Primitive.attr! :inline_block
|
||||
|
||||
unless defined?(yield)
|
||||
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
|
||||
end
|
||||
|
||||
_i = 0
|
||||
value = nil
|
||||
result = Primitive.ary_sized_alloc
|
||||
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
|
||||
result << yield(value)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
alias collect map
|
||||
|
||||
# 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>
|
||||
def select
|
||||
Primitive.attr! :inline_block
|
||||
|
||||
unless defined?(yield)
|
||||
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
|
||||
end
|
||||
|
||||
_i = 0
|
||||
value = nil
|
||||
result = Primitive.ary_sized_alloc
|
||||
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
|
||||
result << value if yield value
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
alias filter select
|
||||
|
||||
# call-seq:
|
||||
# array.shuffle!(random: Random) -> array
|
||||
#
|
||||
|
|
45
numeric.c
45
numeric.c
|
@ -5713,6 +5713,50 @@ int_downto_size(VALUE from, VALUE args, VALUE eobj)
|
|||
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(-1), FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* downto(limit) {|i| ... } -> self
|
||||
* downto(limit) -> enumerator
|
||||
*
|
||||
* Calls the given block with each integer value from +self+ down to +limit+;
|
||||
* returns +self+:
|
||||
*
|
||||
* a = []
|
||||
* 10.downto(5) {|i| a << i } # => 10
|
||||
* a # => [10, 9, 8, 7, 6, 5]
|
||||
* a = []
|
||||
* 0.downto(-5) {|i| a << i } # => 0
|
||||
* a # => [0, -1, -2, -3, -4, -5]
|
||||
* 4.downto(5) {|i| fail 'Cannot happen' } # => 4
|
||||
*
|
||||
* With no block given, returns an Enumerator.
|
||||
*
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
int_downto(VALUE from, VALUE to)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
|
||||
if (FIXNUM_P(from) && FIXNUM_P(to)) {
|
||||
long i, end;
|
||||
|
||||
end = FIX2LONG(to);
|
||||
for (i=FIX2LONG(from); i >= end; i--) {
|
||||
rb_yield(LONG2FIX(i));
|
||||
}
|
||||
}
|
||||
else {
|
||||
VALUE i = from, c;
|
||||
|
||||
while (!(c = rb_funcall(i, '<', 1, to))) {
|
||||
rb_yield(i);
|
||||
i = rb_funcall(i, '-', 1, INT2FIX(1));
|
||||
}
|
||||
if (NIL_P(c)) rb_cmperr(i, to);
|
||||
}
|
||||
return from;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
|
||||
{
|
||||
|
@ -6320,6 +6364,7 @@ Init_Numeric(void)
|
|||
rb_define_method(rb_cInteger, "anybits?", int_anybits_p, 1);
|
||||
rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
|
||||
rb_define_method(rb_cInteger, "upto", int_upto, 1);
|
||||
rb_define_method(rb_cInteger, "downto", int_downto, 1);
|
||||
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
||||
rb_define_method(rb_cInteger, "next", int_succ, 0);
|
||||
rb_define_method(rb_cInteger, "pred", int_pred, 0);
|
||||
|
|
30
numeric.rb
30
numeric.rb
|
@ -241,36 +241,6 @@ class Integer
|
|||
self
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# downto(limit) {|i| ... } -> self
|
||||
# downto(limit) -> enumerator
|
||||
#
|
||||
# Calls the given block with each integer value from +self+ down to +limit+;
|
||||
# returns +self+:
|
||||
#
|
||||
# a = []
|
||||
# 10.downto(5) {|i| a << i } # => 10
|
||||
# a # => [10, 9, 8, 7, 6, 5]
|
||||
# a = []
|
||||
# 0.downto(-5) {|i| a << i } # => 0
|
||||
# a # => [0, -1, -2, -3, -4, -5]
|
||||
# 4.downto(5) {|i| fail 'Cannot happen' } # => 4
|
||||
#
|
||||
# With no block given, returns an Enumerator.
|
||||
def downto to
|
||||
Primitive.attr! :inline_block
|
||||
unless defined?(yield)
|
||||
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 1, &to, int_downto_size)'
|
||||
end
|
||||
|
||||
from = self
|
||||
while from >= to
|
||||
yield from
|
||||
from = from.pred
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# to_i -> self
|
||||
#
|
||||
|
|
|
@ -223,15 +223,15 @@ class TestBacktrace < Test::Unit::TestCase
|
|||
@res = caller_locations(2, 1).inspect
|
||||
end
|
||||
@line = __LINE__ + 1
|
||||
[1].map!.map { [1].map!.map { foo } }
|
||||
assert_equal("[\"#{__FILE__}:#{@line}:in 'Array#map!'\"]", @res)
|
||||
[1].map.map { [1].map.map { foo } }
|
||||
assert_equal("[\"#{__FILE__}:#{@line}:in 'Array#map'\"]", @res)
|
||||
end
|
||||
|
||||
def test_caller_location_path_cfunc_iseq_no_pc
|
||||
def self.foo
|
||||
@res = caller_locations(2, 1)[0].path
|
||||
end
|
||||
[1].map!.map { [1].map!.map { foo } }
|
||||
[1].map.map { [1].map.map { foo } }
|
||||
assert_equal(__FILE__, @res)
|
||||
end
|
||||
|
||||
|
|
|
@ -680,8 +680,10 @@ CODE
|
|||
#
|
||||
[:c_return, 1, "xyzzy", TracePoint, :trace, TracePoint, nil, nil],
|
||||
[:line, 4, 'xyzzy', self.class, method, self, :outer, :nothing],
|
||||
[:c_call, 4, 'xyzzy', Integer, :times, 1, nil, nil],
|
||||
[:line, 4, 'xyzzy', self.class, method, self, nil, :nothing],
|
||||
[:line, 5, 'xyzzy', self.class, method, self, :inner, :nothing],
|
||||
[:c_return, 4, "xyzzy", Integer, :times, 1, nil, nil],
|
||||
[:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing],
|
||||
[:c_call, 7, "xyzzy", Class, :inherited, Object, nil, nil],
|
||||
[:c_return, 7, "xyzzy", Class, :inherited, Object, nil, nil],
|
||||
|
@ -1067,12 +1069,10 @@ CODE
|
|||
# pp events
|
||||
# expected_events =
|
||||
[[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
|
||||
[:call, :map, Array, Array, nil],
|
||||
[:c_call, :map, Array, Array, nil],
|
||||
[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
|
||||
[:b_return, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, 3],
|
||||
[:c_call, :<<, Array, Array, nil],
|
||||
[:c_return, :<<, Array, Array, [3]],
|
||||
[:return, :map, Array, Array, [3]],
|
||||
[:c_return, :map, Array, Array, [3]],
|
||||
[:call, :method_for_test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
|
||||
[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
|
||||
[:b_return, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, 4],
|
||||
|
@ -1387,10 +1387,9 @@ CODE
|
|||
}
|
||||
}
|
||||
assert_equal([
|
||||
:b_call,
|
||||
:call,
|
||||
:b_call,
|
||||
:c_call,
|
||||
:b_call,
|
||||
:call,
|
||||
:b_call,
|
||||
], events, "TracePoint log:\n#{ log.join("\n") }\n")
|
||||
|
@ -1414,7 +1413,6 @@ CODE
|
|||
assert_equal([
|
||||
:b_return,
|
||||
:c_return,
|
||||
:return,
|
||||
:b_return,
|
||||
:return,
|
||||
:b_return
|
||||
|
|
Загрузка…
Ссылка в новой задаче