Skip to content

Commit 3455ee0

Browse files
N5N3johnnychen94
authored andcommitted
Make partition support non-1-indexed vector. (JuliaLang#40830)
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 <[email protected]>
1 parent 700cad3 commit 3455ee0

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

base/iterators.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,15 +1171,15 @@ function length(itr::PartitionIterator)
11711171
return cld(l, itr.n)
11721172
end
11731173

1174-
function iterate(itr::PartitionIterator{<:AbstractRange}, state=1)
1175-
state > length(itr.c) && return nothing
1176-
r = min(state + itr.n - 1, length(itr.c))
1174+
function iterate(itr::PartitionIterator{<:AbstractRange}, state = firstindex(itr.c))
1175+
state > lastindex(itr.c) && return nothing
1176+
r = min(state + itr.n - 1, lastindex(itr.c))
11771177
return @inbounds itr.c[state:r], r + 1
11781178
end
11791179

1180-
function iterate(itr::PartitionIterator{<:AbstractArray}, state=1)
1181-
state > length(itr.c) && return nothing
1182-
r = min(state + itr.n - 1, length(itr.c))
1180+
function iterate(itr::PartitionIterator{<:AbstractArray}, state = firstindex(itr.c))
1181+
state > lastindex(itr.c) && return nothing
1182+
r = min(state + itr.n - 1, lastindex(itr.c))
11831183
return @inbounds view(itr.c, state:r), r + 1
11841184
end
11851185

test/iterators.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Base.Iterators
44
using Random
5+
using Base: IdentityUnitRange
56

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

@@ -848,3 +849,7 @@ end
848849
@test cumprod(x + 1 for x in 1:3) == [2, 6, 24]
849850
@test accumulate(+, (x^2 for x in 1:3); init=100) == [101, 105, 114]
850851
end
852+
853+
@testset "proper patition for non-1-indexed vector" begin
854+
@test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange
855+
end

test/offsetarray.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
44
using .Main.OffsetArrays
5+
import .Main.OffsetArrays: IdOffsetRange
56
using DelimitedFiles
67
using Random
78
using LinearAlgebra
@@ -786,3 +787,11 @@ end
786787
@test b[i] == a[r[i]]
787788
end
788789
end
790+
791+
@testset "proper patition for non-1-indexed vector" begin
792+
@test Iterators.partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector
793+
@test Iterators.partition(OffsetArray(collect(1:10),10), 5) |> collect == [1:5,6:10] # OffsetVector
794+
@test Iterators.partition(OffsetArray(reshape(1:9,3,3), (3,3)), 5) |> collect == [1:5,6:9] #OffsetMatrix
795+
@test Iterators.partition(OffsetArray(reshape(collect(1:9),3,3), (3,3)), 5) |> collect == [1:5,6:9] #OffsetMatrix
796+
@test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange
797+
end

0 commit comments

Comments
 (0)