Skip to content

Commit

Permalink
Merge pull request #17006 from JuliaLang/teh/append
Browse files Browse the repository at this point in the history
Support `append!(::Vector, iter)`
  • Loading branch information
vtjnash authored Jun 23, 2016
2 parents fbe6e5a + e909c1c commit 7176754
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions base/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ export
peek


# Some algorithms that can be defined only after infrastructure is in place
Base.append!(a::Vector, iter) = _append!(a, Base.iteratorsize(iter), iter)

function _append!(a, ::Base.HasLength, iter)
n = length(a)
resize!(a, n+length(iter))
@inbounds for (i,item) in zip(n+1:length(a), iter)
a[i] = item
end
a
end

function _append!(a, ::Base.IteratorSize, iter)
for item in iter
push!(a, item)
end
a
end

# Heap operations on flat arrays
# ------------------------------

Expand Down
4 changes: 4 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,10 @@ A = [1,2]
@test append!(A, A) == [1,2,1,2]
@test prepend!(A, A) == [1,2,1,2,1,2,1,2]

A = [1,2]
s = Set([1,2,3])
@test sort(append!(A, s)) == [1,1,2,2,3]

# cases where shared arrays can/can't be grown
A = [1 3;2 4]
B = reshape(A, 4)
Expand Down

0 comments on commit 7176754

Please sign in to comment.