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

Define nextind/prevind for ChainedVectorIndex #75

Merged
merged 3 commits into from
Sep 20, 2022
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
51 changes: 47 additions & 4 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ end

# custom index type used in eachindex
struct ChainedVectorIndex{A} <: Integer
arrays_i::Int
Copy link
Member

Choose a reason for hiding this comment

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

do we check somewhere that A type uses 1-based indexing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we do

array::A
array_i::Int
i::Int
Expand All @@ -212,11 +213,15 @@ Base.hash(x::ChainedVectorIndex, h::UInt) = hash(x.i, h)

@inline Base.getindex(x::ChainedVectorIndex) = @inbounds x.array[x.array_i]

@inline function Base.getindex(A::ChainedVector, x::ChainedVectorIndex)
Base.checkbounds(::Type{Bool}, A::ChainedVector, ind::ChainedVectorIndex) = 1 <= ind.array_i <= length(ind.array)
Copy link
Member

Choose a reason for hiding this comment

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

checkbounds probably should also check i?

Copy link
Member

Choose a reason for hiding this comment

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

ah - maybe it is ignored everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

we don't rely on i at all except for "display"-like purposes.


Base.@propagate_inbounds function Base.getindex(A::ChainedVector, x::ChainedVectorIndex)
@boundscheck checkbounds(A, x)
return @inbounds x.array[x.array_i]
end

@inline function Base.setindex!(A::ChainedVector, v, x::ChainedVectorIndex)
Base.@propagate_inbounds function Base.setindex!(A::ChainedVector, v, x::ChainedVectorIndex)
@boundscheck checkbounds(A, x)
@inbounds x.array[x.array_i] = v
return v
end
Expand All @@ -230,6 +235,44 @@ function Base.getindex(A::ChainedVector{T}, inds::AbstractVector{<:ChainedVector
return x
end

function Base.nextind(A::ChainedVector, x::ChainedVectorIndex)
chunkidx = x.arrays_i
chunk = x.array
chunk_i = x.array_i
i = x.i
if chunk_i < length(chunk)
chunk_i += 1
i += 1
elseif chunkidx < length(A.arrays)
chunkidx += 1
@inbounds chunk = A.arrays[chunkidx]
chunk_i = 1
i += 1
else
chunk_i += 1 # make sure this goes out of bounds
Copy link
Member

Choose a reason for hiding this comment

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

increment of i is missing. Maybe it should just be moved out of the if part and just i = x.i + 1 be written. Same for prevind.

end
return ChainedVectorIndex(chunkidx, chunk, chunk_i, i)
Copy link
Member

@bkamins bkamins Sep 18, 2022

Choose a reason for hiding this comment

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

Two questions:

  1. first is something we discussed some time ago, but can you please confirm that chunks cannot be empty?
  2. the function returns the same value when last element of the array is visited - is this intended? Other definitions behave as follows:
julia> nextind([1, 2], 0)
1

julia> nextind([1, 2], 1)
2

julia> nextind([1, 2], 2)
3

julia> nextind([1, 2], 3)
4

julia> nextind([1,2,3], CartesianIndex(0))
CartesianIndex(1,)

julia> nextind([1,2,3], CartesianIndex(1))
CartesianIndex(2,)

julia> nextind([1,2,3], CartesianIndex(2))
CartesianIndex(3,)

julia> nextind([1,2,3], CartesianIndex(3))
CartesianIndex(4,)

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. Correct. We have a cleanup! function that is called before operations where we require all non-empty chunks (and it is called by eachindex)
  2. I assumed the intended behavior was that you eventually get an ind which will throw a BoundsError, and so it doesn't matter if you keep incrementing the ind after that since you'll always get the same BoundsError behavior.

Copy link
Member

@bkamins bkamins Sep 18, 2022

Choose a reason for hiding this comment

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

Ad 2.

Yes, but your condition is:

if chunk_i < length(chunk)
    ...
elseif chunkidx < length(A.arrays)
    ...
end

so if chunk_i == length(chunk) && chunkidx == length(A.arrays) (so essentially it is a last element of an array) you do not move it to BoundsError, but leave it as is. This was my issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah shoot; good catch. I'll come up with a fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, pushed a fix

end

function Base.prevind(A::ChainedVector, x::ChainedVectorIndex)
chunkidx = x.arrays_i
chunk = x.array
chunk_i = x.array_i
i = x.i
if chunk_i > 1
chunk_i -= 1
i -= 1
elseif chunkidx > 1
chunkidx -= 1
@inbounds chunk = A.arrays[chunkidx]
chunk_i = length(chunk)
i -= 1
else
chunk_i -= 1 # make sure this goes out of bounds
end
return ChainedVectorIndex(chunkidx, chunk, chunk_i, i)
end

# efficient iteration via eachindex
struct IndexIterator{A}
arrays::Vector{A}
Expand All @@ -251,7 +294,7 @@ end
chunkidx = chunk_i = 1
@inbounds chunk = arrays[chunkidx]
# we already ran cleanup! so chunks are guaranteed non-empty
return ChainedVectorIndex(chunk, chunk_i, 1), (arrays, chunkidx, chunk, length(chunk), chunk_i + 1, 2)
return ChainedVectorIndex(chunkidx, chunk, chunk_i, 1), (arrays, chunkidx, chunk, length(chunk), chunk_i + 1, 2)
end

@inline function Base.iterate(x::IndexIterator, (arrays, chunkidx, chunk, chunklen, chunk_i, i))
Expand All @@ -262,7 +305,7 @@ end
chunklen = length(chunk)
chunk_i = 1
end
return ChainedVectorIndex(chunk, chunk_i, i), (arrays, chunkidx, chunk, chunklen, chunk_i + 1, i + 1)
return ChainedVectorIndex(chunkidx, chunk, chunk_i, i), (arrays, chunkidx, chunk, chunklen, chunk_i + 1, i + 1)
end

@inline function Base.iterate(A::ChainedVector)
Expand Down
21 changes: 20 additions & 1 deletion test/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
# https://github.com/JuliaData/SentinelArrays.jl/issues/57
cx1 = ChainedVector([[1, 2], [3]])
cx2 = ChainedVector([[1.1, 2.2], [3.3]])
@test ChainedVector([cx1, cx2]) isa ChainedVector{Float64, <:ChainedVector{Float64}}
@test ChainedVector([cx1, cx2]) isa ChainedVector{Float64}

x = ChainedVector([[1], [2], [3]])
y = map(v -> v == 1 ? missing : v, x)
Expand Down Expand Up @@ -510,3 +510,22 @@ end
append!(x, [2])
@test x[end] == 2
end

@testset "prevind/nextind ChainedVector" begin
x = ChainedVector([collect(1:i) for i = 10:100])
ind = first(eachindex(x))
for i = 1:length(x)
@test x[ind] == x[i]
ind = nextind(x, ind)
end
@test_throws BoundsError x[ind]
for i = length(x):-1:1
ind = prevind(x, ind)
@test x[ind] == x[i]
end
ind = prevind(x, ind)
@test_throws BoundsError x[ind]
# https://github.com/JuliaData/SentinelArrays.jl/issues/74
x = ChainedVector([[true], [false], [true]])
@test BitVector(x) == [true, false, true]
end