Skip to content
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
4 changes: 2 additions & 2 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,8 @@ function Base.resize!(A::ChainedVector{T, AT}, len) where {T, AT}
resize!(A.arrays, chunk)
resize!(A.inds, chunk)
# resize individual chunk
resize!(A.arrays[chunk], A.inds[chunk] - len)
A.inds[chunk] -= A.inds[chunk] - len
resize!(A.arrays[chunk], length(A.arrays[chunk]) - (A.inds[chunk] - len))
Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative (unsure what is more readable):

Suggested change
resize!(A.arrays[chunk], length(A.arrays[chunk]) - (A.inds[chunk] - len))
resize!(A.arrays[chunk], chunk == 1 ? len : len - A.inds[chunk - 1])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer branchless code and length should be O(1) in most cases... But its your call.

A.inds[chunk] = len
end
return A
end
Expand Down
14 changes: 14 additions & 0 deletions test/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ end
@test BitVector(x) == [true, false, true]
end

# https://github.com/JuliaData/SentinelArrays.jl/issues/110
@testset "Resizing ChainedVector" begin
c = ChainedVector([[:a],[:b1,:b2,:b3],[:c]])
@test length(resize!(c,6)) == 6
@test resize!(c,5) == [:a,:b1,:b2,:b3,:c]
@test resize!(c,4) == [:a,:b1,:b2,:b3]
@test resize!(c,3) == [:a,:b1,:b2]
@test resize!(c,2) == [:a,:b1]
@test resize!(c,1) == [:a]
@test resize!(c,0) == []

@test sum(unique!(ChainedVector([[1],[2],[3]]))) == 6
end

@testset "MissingVector resizing" begin
v = MissingVector(1)
@test isequal(push!(v, missing), [missing, missing])
Expand Down
Loading