Skip to content

Commit

Permalink
add insert! (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Nov 20, 2021
1 parent f87b540 commit 53f57d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PooledArrays"
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.3.0"
version = "1.4.0"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
11 changes: 10 additions & 1 deletion src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,20 @@ end
##
##############################################################################

function Base.push!(pv::PooledVector{S,R}, v::T) where {S,R,T}
function Base.push!(pv::PooledVector, v) # this function is not thread safe
push!(pv.refs, getpoolidx(pv, v))
return pv
end

function Base.insert!(pv::PooledVector, i::Integer, v) # this function is not thread safe
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
if !(1 <= i <= length(pv.refs) + 1)
throw(BoundsError("attempt to insert to a vector with length $(length(pv)) at index $i"))
end
insert!(pv.refs, i, getpoolidx(pv, v))
return pv
end

function Base.append!(pv::PooledVector, items::AbstractArray)
itemindices = eachindex(items)
l = length(pv)
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,19 @@ end
end
end
end

@testset "insert! test" begin
x = PooledArray([1, 2, 3])
@test insert!(x, 2, 10) == [1, 10, 2, 3]
@test_throws ArgumentError insert!(x, true, 10)
@test_throws BoundsError insert!(x, 0, 10)
@test x == [1, 10, 2, 3]
@test x.pool == [1, 2, 3, 10]
@test insert!(x, 3, 'c') == [1, 10, 99, 2, 3]
@test x.pool == [1, 2, 3, 10, 99]
@test insert!(x, 1, true) == [1, 1, 10, 99, 2, 3]
@test x.pool == [1, 2, 3, 10, 99]
@test insert!(x, 7, true) == [1, 1, 10, 99, 2, 3, 1]
@test_throws BoundsError insert!(x, 9, true)
@test x == [1, 1, 10, 99, 2, 3, 1]
end

2 comments on commit 53f57d4

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/49092

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" 53f57d4f6408f394c80114780f5140099a255d01
git push origin v1.4.0

Please sign in to comment.