Skip to content

Commit

Permalink
Fix iteration issue when chains have zero-length
Browse files Browse the repository at this point in the history
Fixes #27. The issue here is that `iterate` for `ChainedVector` was
assuming that underlying array chains wouldn't have zero-length. If a
user is doing a lot of filtering/deleting, however, it might be the case
that certain chunks end up with zero-length (though we do try to prune
those out when possible, so I'm still a little unsure how we get in this
state). Nevertheless, this assumption is a bit optimistic, and we can do
better by just checking if the next chunk is zero-length when iterating
and moving on to the next chunk if so.
  • Loading branch information
quinnj committed Aug 14, 2020
1 parent c12508c commit e15c3bc
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,43 @@ end
# efficient iteration
@inline function Base.iterate(A::ChainedVector)
length(A) == 0 && return nothing
i = 2
i = 1
chunk = 1
chunk_i = 1
chunk_len = A.inds[1]
if i > chunk_len
while i > chunk_len
chunk += 1
chunk_i = 1
@inbounds chunk_len = A.inds[min(length(A.inds), chunk)]
@inbounds chunk_len = A.inds[chunk]
i <= chunk_len && break
end
x = A.arrays[chunk][1]
# find next valid index
i += 1
if i > chunk_len
while true
chunk += 1
chunk > length(A.inds) && break
@inbounds chunk_len = A.inds[chunk]
i <= chunk_len && break
end
else
chunk_i += 1
end
return A.arrays[1][1], (i, chunk, chunk_i, chunk_len, length(A))
return x, (i, chunk, chunk_i, chunk_len, length(A))
end

@inline function Base.iterate(A::ChainedVector, (i, chunk, chunk_i, chunk_len, len))
i > len && return nothing
@inbounds x = A.arrays[chunk][chunk_i]
i += 1
if i > chunk_len
chunk += 1
chunk_i = 1
@inbounds chunk_len = A.inds[min(length(A.inds), chunk)]
while true
chunk += 1
chunk > length(A.inds) && break
@inbounds chunk_len = A.inds[chunk]
i <= chunk_len && break
end
else
chunk_i += 1
end
Expand Down

0 comments on commit e15c3bc

Please sign in to comment.