diff options
Diffstat (limited to 'array.rb')
| -rw-r--r-- | array.rb | 66 |
1 files changed, 45 insertions, 21 deletions
@@ -71,7 +71,7 @@ class Array # # The order of the result array is unrelated to the order of +self+. # - # Returns a new empty +Array+ if +self+ is empty: + # Returns a new empty array if +self+ is empty: # # [].sample(4) # => [] # @@ -140,8 +140,8 @@ class Array end # call-seq: - # last -> last_object or nil - # last(n) -> new_array + # last -> last_object or nil + # last(count) -> new_array # # Returns elements from +self+, or +nil+; +self+ is not modified. # @@ -152,8 +152,9 @@ class Array # a # => [:foo, "bar", 2] # [].last # => nil # - # With a non-negative integer argument +n+ given, - # returns a new array containing the trailing +n+ elements of +self+, as available: + # + # With non-negative integer argument +count+ given, + # returns a new array containing the trailing +count+ elements of +self+, as available: # # a = [:foo, 'bar', 2] # a.last(2) # => ["bar", 2] @@ -211,20 +212,20 @@ class Array indexes end - with_yjit do + with_jit do if Primitive.rb_builtin_basic_definition_p(:each) undef :each def each # :nodoc: - Primitive.attr! :inline_block, :c_trace + Primitive.attr! :inline_block, :c_trace, :without_interrupts unless defined?(yield) return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)' end - _i = 0 - value = nil - while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) }) - yield value + i = 0 + until Primitive.rb_jit_ary_at_end(i) + yield Primitive.rb_jit_ary_at(i) + i = Primitive.rb_jit_fixnum_inc(i) end self end @@ -234,17 +235,18 @@ class Array undef :map def map # :nodoc: - Primitive.attr! :inline_block, :c_trace + Primitive.attr! :inline_block, :c_trace, :without_interrupts unless defined?(yield) return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)' end - _i = 0 - value = nil + i = 0 result = Primitive.ary_sized_alloc - while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) }) - result << yield(value) + until Primitive.rb_jit_ary_at_end(i) + value = yield(Primitive.rb_jit_ary_at(i)) + Primitive.rb_jit_ary_push(result, value) + i = Primitive.rb_jit_fixnum_inc(i) end result end @@ -259,17 +261,20 @@ class Array undef :select def select # :nodoc: - Primitive.attr! :inline_block, :c_trace + Primitive.attr! :inline_block, :c_trace, :without_interrupts unless defined?(yield) return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)' end - _i = 0 - value = nil + i = 0 result = Primitive.ary_sized_alloc - while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) }) - result << value if yield value + until Primitive.rb_jit_ary_at_end(i) + value = Primitive.rb_jit_ary_at(i) + if yield value + Primitive.rb_jit_ary_push(result, value) + end + i = Primitive.rb_jit_fixnum_inc(i) end result end @@ -279,5 +284,24 @@ class Array alias filter select end end + + if Primitive.rb_builtin_basic_definition_p(:find) + undef :find + + def find(if_none_proc = nil) # :nodoc: + Primitive.attr! :inline_block, :c_trace, :without_interrupts + + unless defined?(yield) + return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)' + end + i = 0 + until Primitive.rb_jit_ary_at_end(i) + value = Primitive.rb_jit_ary_at(i) + return value if yield(value) + i = Primitive.rb_jit_fixnum_inc(i) + end + if_none_proc&.call + end + end end end |
