Skip to content

Commit

Permalink
Fix error in constructor (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Apr 30, 2022
1 parent d84c4be commit 65e3316
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PooledArrays"
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.4.1"
version = "1.4.2"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand All @@ -12,6 +12,7 @@ julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"

[targets]
test = ["Test"]
test = ["OffsetArrays", "Test"]
29 changes: 17 additions & 12 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,25 @@ function _label(xs::AbstractArray,
) where {T, I<:Integer}

@inbounds for i in start:length(xs)
x = xs[i]
lbl = get(invpool, x, zero(I))
if lbl !== zero(I)
labels[i] = lbl
idx = i + firstindex(xs) - 1
if !isassigned(xs, idx)
labels[i] = zero(I)
else
if nlabels == typemax(I)
I2 = _widen(I)
return _label(xs, T, I2, i, convert(Vector{I2}, labels),
convert(Dict{T, I2}, invpool), pool, nlabels)
x = xs[idx]
lbl = get(invpool, x, zero(I))
if lbl !== zero(I)
labels[i] = lbl
else
if nlabels == typemax(I)
I2 = _widen(I)
return _label(xs, T, I2, i, convert(Vector{I2}, labels),
convert(Dict{T, I2}, invpool), pool, nlabels)
end
nlabels += 1
labels[i] = nlabels
invpool[x] = nlabels
push!(pool, x)
end
nlabels += 1
labels[i] = nlabels
invpool[x] = nlabels
push!(pool, x)
end
end
labels, invpool, pool
Expand Down
24 changes: 23 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test
using Test, OffsetArrays
using PooledArrays
using DataAPI: refarray, refvalue, refpool, invrefpool
using PooledArrays: refcount
Expand Down Expand Up @@ -601,3 +601,25 @@ end
@test popfirst!(x) == "1"
@test x == ["2"]
end

@testset "constructor corner cases" begin
x = Vector{Any}(undef, 3)
y = PooledArray(x)
@test y isa PooledArray{Any}
@test !any(i -> isassigned(y, i), eachindex(y))
@test all(iszero, y.refs)
@test isempty(y.pool)
@test isempty(y.invpool)

x[2] = "a"
for v in (x, OffsetVector(x, -5))
y = PooledArray(v)
@test y isa PooledArray{Any}
@test !isassigned(x, 1)
@test x[2] == "a"
@test !isassigned(x, 3)
@test y.refs == [0, 1, 0]
@test y.pool == ["a"]
@test y.invpool == Dict("a" => 1)
end
end

2 comments on commit 65e3316

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/59410

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.2 -m "<description of version>" 65e3316b7d6db5f4420fec3f412d0a1a84eb77f3
git push origin v1.4.2

Please sign in to comment.