-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make partition support non-1-indexed vector. #40830
Conversation
For a high dimension array, at least `OffsetArrays.jl` return a 1-indexed vector after reshape, so the current implementation seems ok. I believe that the view of Base's Range types has been mapped to `getindex` correctly, so I think we don't need a separate routine.
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.
Tests are needed to make sure that this PR fixes something. There might be other types, but at least to test the following ones:
Base.IdentityUnitRange
OffsetArrays.OffsetArray
OffsetArrays.IdOffsetRange
base/iterators.jl
Outdated
@@ -1171,10 +1171,10 @@ function length(itr::PartitionIterator) | |||
return cld(l, itr.n) | |||
end | |||
|
|||
function iterate(itr::PartitionIterator{<:AbstractRange}, state=1) | |||
state > length(itr.c) && return nothing | |||
function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) |
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'm not clear with "invalid", if we add a dispatch for PartitionIterator{<:AbstractRange}
, then everything is the same except for replacing view
with getindex
.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
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.
on 1.6.1
julia> UnitRange <: AbstractRange <: AbstractVector
true
something changed in dev?
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
base/iterators.jl
Outdated
r = min(state + itr.n - 1, length(itr.c)) | ||
return @inbounds itr.c[state:r], r + 1 | ||
return @inbounds view(itr.c, state:r), r + 1 |
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.
If this change doesn't make a difference, there's no need to change it.
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 think the view makes it consistent with the docstring
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 am a bit hesitant about this, at least as part of this PR. Since this is supposed to just fix a bug can we remove it for now and perhaps propose that change in a separate PR?
base/iterators.jl
Outdated
r = min(state + itr.n - 1, length(itr.c)) | ||
return @inbounds itr.c[state:r], r + 1 | ||
return @inbounds view(itr.c, state:r), r + 1 | ||
end | ||
|
||
function iterate(itr::PartitionIterator{<:AbstractArray}, state=1) |
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.
Looks like the AbstractArray
method can be improved, too?
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.
Hmmm, actually, no need. Because here it's using LinearIndexing rules, which always assume 1-based.
base/iterators.jl
Outdated
@@ -1171,10 +1171,10 @@ function length(itr::PartitionIterator) | |||
return cld(l, itr.n) | |||
end | |||
|
|||
function iterate(itr::PartitionIterator{<:AbstractRange}, state=1) | |||
state > length(itr.c) && return nothing | |||
function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) |
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'm not clear with "invalid", if we add a dispatch for PartitionIterator{<:AbstractRange}
, then everything is the same except for replacing view
with getindex
.
move to offsetarray.jl
remove white-space
test/offsetarray.jl
Outdated
@@ -786,3 +786,8 @@ end | |||
@test b[i] == a[r[i]] | |||
end | |||
end | |||
|
|||
@testset "proper patition for non-1-indexed vector" begin | |||
@test partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector |
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.
and also partition(OffsetArray(collect(1:10),10), 5) |> collect
. It currently sigfaults with improper index.
base/iterators.jl
Outdated
function iterate(itr::PartitionIterator{<:AbstractRange}, state=1) | ||
state > length(itr.c) && return nothing | ||
function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) | ||
state > lastindex(itr.c) && return nothing | ||
r = min(state + itr.n - 1, length(itr.c)) |
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.
r = min(state + itr.n - 1, length(itr.c)) | |
r = min(state + itr.n - 1, lastindex(itr.c)) |
base/iterators.jl
Outdated
r = min(state + itr.n - 1, length(itr.c)) | ||
return @inbounds itr.c[state:r], r + 1 | ||
return @inbounds view(itr.c, state:r), r + 1 | ||
end | ||
|
||
function iterate(itr::PartitionIterator{<:AbstractArray}, state=1) |
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.
Hmmm, actually, no need. Because here it's using LinearIndexing rules, which always assume 1-based.
fix the depend
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.
The current version works for high-dimensional OffsetArray because linear indexing uses 1-based indexing rules. However, I still recommend adding the missing test for it (2d OffsetArray).
Co-authored-by: Johnny Chen <johnnychen94@hotmail.com>
Add offsetmatrix
Well, test for OffsetMatrix has been added. |
follow @simeonschaub's advice to retain the dispatch for AbstractRange
@simeonschaub the dispatch for AbstractRange has been retained. |
Sorry if I missed something above, but why does |
remove the the special case for abstractvector
That's right, didn't notice that. |
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.
Great, looks good now! Thanks for the contribution! (BTW, there is no need to close PRs if you are just pushing a new commit)
For a high dimension array, at least `OffsetArrays.jl` return a 1-indexed vector after reshape, so the current implementation seems ok. I believe that the view of Base's Range types has been mapped to `getindex` correctly, so I think we don't need a separate routine. Co-authored-by: Johnny Chen <johnnychen94@hotmail.com>
For a high dimension array, at least `OffsetArrays.jl` return a 1-indexed vector after reshape, so the current implementation seems ok. I believe that the view of Base's Range types has been mapped to `getindex` correctly, so I think we don't need a separate routine. Co-authored-by: Johnny Chen <johnnychen94@hotmail.com>
For a high dimension array, at least
OffsetArrays.jl
return a 1-indexed vector after reshape, so the current implementation seems ok.Since the view of Base's Range types has been mapped to
getindex
correctly, so I think we don't need a separate routine.