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

Make partition support non-1-indexed vector. #40830

Merged
merged 15 commits into from
May 21, 2021
6 changes: 3 additions & 3 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member Author

@N5N3 N5N3 May 15, 2021

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.

Copy link
Member Author

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.

state > lastindex(itr.c) && return nothing
r = min(state + itr.n - 1, length(itr.c))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
r = min(state + itr.n - 1, length(itr.c))
r = min(state + itr.n - 1, lastindex(itr.c))

return @inbounds itr.c[state:r], r + 1
return @inbounds view(itr.c, state:r), r + 1
Copy link
Member

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.

Copy link
Contributor

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

Copy link
Member

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?

end

function iterate(itr::PartitionIterator{<:AbstractArray}, state=1)
Copy link
Member

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?

Copy link
Member

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.

Expand Down
5 changes: 5 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Base.Iterators
using Random
using Base: IdentityUnitRange

@test Base.IteratorSize(Any) isa Base.SizeUnknown

Expand Down Expand Up @@ -848,3 +849,7 @@ end
@test cumprod(x + 1 for x in 1:3) == [2, 6, 24]
@test accumulate(+, (x^2 for x in 1:3); init=100) == [101, 105, 114]
end

@testset "proper patition for non-1-indexed vector" begin
@test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange
end
5 changes: 5 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

@test partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange
end