Skip to content

Commit

Permalink
Make Indexable#to_a preallocate the array (crystal-lang#6079)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg authored and chris-huxtable committed Jun 6, 2018
1 parent 3aa8abb commit 98cb83b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/deque.cr
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,6 @@ class Deque(T)
self
end

# Returns an `Array` (shallow copy) that contains all the items of this deque.
def to_a
arr = Array(T).new(@size)
each do |x|
arr << x
end
arr
end

def to_s(io : IO)
inspect(io)
end
Expand Down
11 changes: 11 additions & 0 deletions src/indexable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ module Indexable(T)
self
end

# Returns an `Array` with all the elements in the collection.
#
# ```
# {1, 2, 3}.to_a # => [1, 2, 3]
# ```
def to_a
ary = Array(T).new(size)
each { |e| ary << e }
ary
end

# Returns `true` if `self` is empty, `false` otherwise.
#
# ```
Expand Down

0 comments on commit 98cb83b

Please sign in to comment.