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

Fix error in constructor #82

Merged
merged 3 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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
20 changes: 19 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,21 @@ 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 all(iszero, y.refs)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@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 y.refs == [0, 1, 0]
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@test y.pool == ["a"]
@test y.invpool == Dict("a" => 1)
end
end