From 3aa2b579005d8c96c35b780a25df4f9eeb3957fb Mon Sep 17 00:00:00 2001 From: georgematheos Date: Thu, 13 Aug 2020 11:12:46 -0400 Subject: [PATCH 1/4] Fix bug in pvec `pop` resulting in empty tail When calling `pop` on a persistent vector with, eg., 33 elements, we have a `tail` of length 1. After this `pop` call, there was a bug where the resulting pvec had a tail of length `0` rather than a tail of length `32`. This causes errors, for instance when calling `iterate`, which expects a nonempty tail. This PR fixes the behavior by adding a branch in `pop` function for when the tail length is currently `1` (in which case we make the new tail be `pop(trie)`). --- src/PersistentVector.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/PersistentVector.jl b/src/PersistentVector.jl index 8ec580c..c3f5196 100644 --- a/src/PersistentVector.jl +++ b/src/PersistentVector.jl @@ -81,7 +81,11 @@ function assoc(v::PersistentVector{T}, i::Int, el) where T end function pop(v::PersistentVector{T}) where T - if isempty(v.tail) + 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 From dce8c784ecd7f09e61e7779fae155de8c6bc5073 Mon Sep 17 00:00:00 2001 From: georgematheos Date: Thu, 13 Aug 2020 11:14:10 -0400 Subject: [PATCH 2/4] Update PersistentVectorTest.jl --- test/PersistentVectorTest.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/PersistentVectorTest.jl b/test/PersistentVectorTest.jl index c70bbb4..0d2f337 100644 --- a/test/PersistentVectorTest.jl +++ b/test/PersistentVectorTest.jl @@ -131,5 +131,10 @@ 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 + end end From 6fffe50866ef8bf1b5ca284d70797841d53af831 Mon Sep 17 00:00:00 2001 From: georgematheos Date: Thu, 13 Aug 2020 16:34:34 -0400 Subject: [PATCH 3/4] Update PersistentVector.jl --- src/PersistentVector.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PersistentVector.jl b/src/PersistentVector.jl index c3f5196..775a15d 100644 --- a/src/PersistentVector.jl +++ b/src/PersistentVector.jl @@ -81,6 +81,9 @@ function assoc(v::PersistentVector{T}, i::Int, el) where T end function pop(v::PersistentVector{T}) where T + if length(v) == 1 + return PersistentVector{T}() + end taillen = length(v.tail) if taillen == 1 newtail = peek(v.trie) From 2bafd373bee3a39ef713e83c89dbcff0de2d1730 Mon Sep 17 00:00:00 2001 From: georgematheos Date: Thu, 13 Aug 2020 16:35:24 -0400 Subject: [PATCH 4/4] Update PersistentVectorTest.jl --- test/PersistentVectorTest.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/PersistentVectorTest.jl b/test/PersistentVectorTest.jl index 0d2f337..25a2fa8 100644 --- a/test/PersistentVectorTest.jl +++ b/test/PersistentVectorTest.jl @@ -135,6 +135,12 @@ 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