-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,7 @@ end | |
|
||
# custom index type used in eachindex | ||
struct ChainedVectorIndex{A} <: Integer | ||
arrays_i::Int | ||
array::A | ||
array_i::Int | ||
i::Int | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah - maybe it is ignored everywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't rely on |
||
|
||
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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. increment of |
||
end | ||
return ChainedVectorIndex(chunkidx, chunk, chunk_i, i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two questions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ad 2. Yes, but your condition is:
so if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah shoot; good catch. I'll come up with a fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
@@ -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)) | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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