diff --git a/Project.toml b/Project.toml index 2a8303b..ae37e3b 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/PooledArrays.jl b/src/PooledArrays.jl index 5829085..c70bd36 100644 --- a/src/PooledArrays.jl +++ b/src/PooledArrays.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index ee5f4cc..0a7a31d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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