Skip to content

Commit

Permalink
make push! and pushfirst! return array (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox authored Jun 25, 2020
1 parent 7bc598e commit 6f02758
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ end
##############################################################################

function Base.push!(pv::PooledVector{S,R}, v::T) where {S,R,T}
v = convert(S,v)
push!(pv.refs, getpoolidx(pv, v))
return v
return pv
end

function Base.append!(pv::PooledVector, items::AbstractArray)
Expand All @@ -393,9 +392,8 @@ end
Base.pop!(pv::PooledVector) = pv.invpool[pop!(pv.refs)]

function Base.pushfirst!(pv::PooledVector{S,R}, v::T) where {S,R,T}
v = convert(S,v)
pushfirst!(pv.refs, getpoolidx(pv, v))
return v
return pv
end

Base.popfirst!(pv::PooledVector) = pv.invpool[popfirst!(pv.refs)]
Expand Down
18 changes: 17 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ using Test
using PooledArrays
using DataAPI: refarray, refvalue, refpool

let a = rand(10), b = rand(10,10), c = rand(1:10, 1000)
@testset "PooledArrays" begin
a = rand(10)
b = rand(10,10)
c = rand(1:10, 1000)

@test PooledArray(a) == a
@test PooledArray(b) == b
pc = PooledArray(c)
Expand Down Expand Up @@ -76,4 +80,16 @@ let a = rand(10), b = rand(10,10), c = rand(1:10, 1000)
@test refvalue(s, refarray(s)[i]) == s[i]
end
@test refpool(s) == ["a", "b"]

@testset "push!" begin
xs = PooledArray([10, 20, 30])
@test xs === push!(xs, -100)
@test xs == [10, 20, 30, -100]
end

@testset "pushfirst!" begin
ys = PooledArray([10, 20, 30])
@test ys === pushfirst!(ys, -100)
@test ys == [-100, 10, 20, 30]
end
end

0 comments on commit 6f02758

Please sign in to comment.