Skip to content

Commit

Permalink
Merge pull request #23 from nalimilan/nl/deleteat!
Browse files Browse the repository at this point in the history
Implement deleteat! and append!
  • Loading branch information
quinnj authored Jun 3, 2019
2 parents b20ed81 + f1d235e commit c30a671
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,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 All @@ -387,6 +396,8 @@ Base.popfirst!(pv::PooledVector) = pv.invpool[popfirst!(pv.refs)]

Base.empty!(pv::PooledVector) = (empty!(pv.refs); pv)

Base.deleteat!(pv::PooledVector, inds) = (deleteat!(pv.refs, inds); pv)

function _vcat!(c,a,b)
copyto!(c, 1, a, 1, length(a))
copyto!(c, length(a)+1, b, 1, length(b))
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ 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]]
@test deleteat!(pc2, [2, 4]) === pc2
@test pc2 == pc[[1; 4; 6:end]]

#@test issorted(pc.invpool)

@test map(identity, pc) == pc
Expand Down

0 comments on commit c30a671

Please sign in to comment.