Skip to content

Commit

Permalink
Implement append!
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed May 17, 2019
1 parent d8d5e0a commit f1d235e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ function Base.push!(pv::PooledVector{S,R}, v::T) where {S,R,T}
return v
end

function Base.append!(pv::PooledVector, items::AbstractArray)
itemindices = eachindex(items)
l = length(pv)
n = length(itemindices)
resize!(pv.refs, l+n)
copyto!(pv, l+1, items, first(itemindices), n)
return pv
end

Base.pop!(pv::PooledVector) = pv.invpool[pop!(pv.refs)]

function Base.pushfirst!(pv::PooledVector{S,R}, v::T) where {S,R,T}
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ let a = rand(10), b = rand(10,10), c = rand(1:10, 1000)
@test maximum(values(pc.invpool)) == 11
@test length(pc.pool) == 11

pc2 = copy(pc)
@test append!(pc2, [3, 1]) === pc2
@test pc2 == [pc; 3; 1]

pc2 = copy(pc)
@test deleteat!(pc2, 2) === pc2
@test pc2 == pc[[1; 3:end]]
Expand Down

0 comments on commit f1d235e

Please sign in to comment.