Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make push! and pushfirst! return array #35

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
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