Skip to content

Fix bug in pvec pop resulting in empty tail #64

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion src/PersistentVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ function assoc(v::PersistentVector{T}, i::Int, el) where T
end

function pop(v::PersistentVector{T}) where T
if isempty(v.tail)
if length(v) == 1
return PersistentVector{T}()
end
taillen = length(v.tail)
if taillen == 1
newtail = peek(v.trie)
PersistentVector{T}(pop(v.trie), newtail, v.length - 1)
elseif taillen == 0
newtail = peek(v.trie)[1:end-1]
PersistentVector{T}(pop(v.trie), newtail, v.length - 1)
else
Expand Down
11 changes: 11 additions & 0 deletions test/PersistentVectorTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,16 @@ end
@test convert(PersistentVector{Int}, pv) === pv
@test typeof(convert(PersistentVector{Float64}, pv)) == PersistentVector{Float64}
end

@testset "pop" begin
p = pvec(1:33)
@test length(pop(p).tail) == 32

p = pvec(1:100)
for _=1:100
p = pop(p)
end
@test p == pvec{Int}()
end

end