From 5bdd80b02ab11c3693e483bf0f46807f0478f144 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 18:25:56 +0800 Subject: [PATCH 01/13] Create iterators.jl 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. --- base/iterators.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/iterators.jl b/base/iterators.jl index dbebe900e0659..8e5f8284ba58f 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -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)) + state > lastindex(itr.c) && return nothing 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) From e9ff8090c1a1d23e4005abc07f902ee0366f1fa4 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:19:59 +0800 Subject: [PATCH 02/13] add test add test --- test/iterators.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/iterators.jl b/test/iterators.jl index c4b438d279693..b14c49933083f 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -2,6 +2,9 @@ using Base.Iterators using Random +isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl") +using .Main.OffsetArrays : OffsetArray, IdOffsetRange +using Base: IdentityUnitRange @test Base.IteratorSize(Any) isa Base.SizeUnknown @@ -848,3 +851,11 @@ 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(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector + @test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange + @test partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange +end + + From 97fa6320093817642dba5e47c77328c2eadee56c Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:23:31 +0800 Subject: [PATCH 03/13] Update iterators.jl move to offsetarray.jl --- test/iterators.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/iterators.jl b/test/iterators.jl index b14c49933083f..9a0f585ae19e2 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -2,8 +2,6 @@ using Base.Iterators using Random -isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl") -using .Main.OffsetArrays : OffsetArray, IdOffsetRange using Base: IdentityUnitRange @test Base.IteratorSize(Any) isa Base.SizeUnknown @@ -853,9 +851,7 @@ end end @testset "proper patition for non-1-indexed vector" begin - @test partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector @test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange - @test partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end From 5eb64b19f1ce186f93ceb13c121d2bab6c95fe7a Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:24:18 +0800 Subject: [PATCH 04/13] Update offsetarray.jl --- test/offsetarray.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 96a44c71e5483..677c39d1274a3 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -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 + @test partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange +end From af7e493567a521dca4693205d8b4e2812f5c18ce Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:26:50 +0800 Subject: [PATCH 05/13] Update iterators.jl remove white-space --- test/iterators.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/iterators.jl b/test/iterators.jl index 9a0f585ae19e2..de3dbda921cbd 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -849,9 +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 - - From 3ace109c2a11c13a96ef08c85dc694ac4f3fd49f Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:27:25 +0800 Subject: [PATCH 06/13] Update offsetarray.jl From d87b631899336c9fd2ebae0a4446112cd947da7f Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 19:44:50 +0800 Subject: [PATCH 07/13] Update iterators.jl fix --- base/iterators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/iterators.jl b/base/iterators.jl index 8e5f8284ba58f..441c1ab3c5f68 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -1173,7 +1173,7 @@ end function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) state > lastindex(itr.c) && return nothing - r = min(state + itr.n - 1, length(itr.c)) + r = min(state + itr.n - 1, lastindex(itr.c)) return @inbounds view(itr.c, state:r), r + 1 end From f1e8f175f4e322bca142ff6b0786ef0350e5f08b Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sat, 15 May 2021 22:20:42 +0800 Subject: [PATCH 08/13] Update offsetarray.jl fix the depend --- test/offsetarray.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 677c39d1274a3..d45ca906a42e3 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -2,6 +2,7 @@ isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl") using .Main.OffsetArrays +import .Main.OffsetArrays: IdOffsetRange using DelimitedFiles using Random using LinearAlgebra @@ -788,6 +789,6 @@ end end @testset "proper patition for non-1-indexed vector" begin - @test partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector - @test partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange + @test Iterators.partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector + @test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end From df2afa746aa4f9bfc604adba73a98f766e0d9c85 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sun, 16 May 2021 08:46:40 +0800 Subject: [PATCH 09/13] Update test/offsetarray.jl Co-authored-by: Johnny Chen --- test/offsetarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index d45ca906a42e3..305b33787892c 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -790,5 +790,6 @@ end @testset "proper patition for non-1-indexed vector" begin @test Iterators.partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector + @test Iterators.partition(OffsetArray(collect(1:10),10), 5) |> collect == [1:5,6:10] # OffsetVector @test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end From b5b3bdd0c9266a5a7190463464b1f74105423027 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sun, 16 May 2021 08:58:59 +0800 Subject: [PATCH 10/13] Update offsetarray.jl Add offsetmatrix --- test/offsetarray.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 305b33787892c..5e07e2195d6b4 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -791,5 +791,7 @@ end @testset "proper patition for non-1-indexed vector" begin @test Iterators.partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector @test Iterators.partition(OffsetArray(collect(1:10),10), 5) |> collect == [1:5,6:10] # OffsetVector + @test Iterators.partition(OffsetArray(reshape(1:9,3,3), (3,3), 5) |> collect == [1:5,6:9] #OffsetMatrix + @test Iterators.partition(OffsetArray(reshape(collect(1:9),3,3), (3,3), 5) |> collect == [1:5,6:9] #OffsetMatrix @test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end From 8962a99d0c4aa25c89a0f337fa071a9a8c6188b0 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sun, 16 May 2021 11:09:30 +0800 Subject: [PATCH 11/13] Update offsetarray.jl --- test/offsetarray.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 5e07e2195d6b4..6f4bd1995a02f 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -791,7 +791,7 @@ end @testset "proper patition for non-1-indexed vector" begin @test Iterators.partition(OffsetArray(1:10,10), 5) |> collect == [1:5,6:10] # OffsetVector @test Iterators.partition(OffsetArray(collect(1:10),10), 5) |> collect == [1:5,6:10] # OffsetVector - @test Iterators.partition(OffsetArray(reshape(1:9,3,3), (3,3), 5) |> collect == [1:5,6:9] #OffsetMatrix - @test Iterators.partition(OffsetArray(reshape(collect(1:9),3,3), (3,3), 5) |> collect == [1:5,6:9] #OffsetMatrix + @test Iterators.partition(OffsetArray(reshape(1:9,3,3), (3,3)), 5) |> collect == [1:5,6:9] #OffsetMatrix + @test Iterators.partition(OffsetArray(reshape(collect(1:9),3,3), (3,3)), 5) |> collect == [1:5,6:9] #OffsetMatrix @test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end From ad54565ce76dd049ecce3d69e5ce423a00b59068 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Thu, 20 May 2021 09:05:55 +0800 Subject: [PATCH 12/13] restore the dispatch for AbstractRange follow @simeonschaub's advice to retain the dispatch for AbstractRange --- base/iterators.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/base/iterators.jl b/base/iterators.jl index 441c1ab3c5f68..04d0e55fd9fc7 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -1171,6 +1171,12 @@ function length(itr::PartitionIterator) return cld(l, itr.n) end +function iterate(itr::PartitionIterator{<:AbstractRange}, state = firstindex(itr.c)) + state > lastindex(itr.c) && return nothing + r = min(state + itr.n - 1, lastindex(itr.c)) + return @inbounds itr.c[state:r], r + 1 +end + function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) state > lastindex(itr.c) && return nothing r = min(state + itr.n - 1, lastindex(itr.c)) From 980e4c6169b2e2448e9ca6993e35a70e2a716d05 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Thu, 20 May 2021 15:19:04 +0800 Subject: [PATCH 13/13] Update iterators.jl remove the the special case for abstractvector --- base/iterators.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/base/iterators.jl b/base/iterators.jl index 04d0e55fd9fc7..2ae6347a4b849 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -1177,18 +1177,12 @@ function iterate(itr::PartitionIterator{<:AbstractRange}, state = firstindex(itr return @inbounds itr.c[state:r], r + 1 end -function iterate(itr::PartitionIterator{<:AbstractVector}, state = firstindex(itr.c)) +function iterate(itr::PartitionIterator{<:AbstractArray}, state = firstindex(itr.c)) state > lastindex(itr.c) && return nothing r = min(state + itr.n - 1, lastindex(itr.c)) return @inbounds view(itr.c, state:r), r + 1 end -function iterate(itr::PartitionIterator{<:AbstractArray}, state=1) - state > length(itr.c) && return nothing - r = min(state + itr.n - 1, length(itr.c)) - return @inbounds view(itr.c, state:r), r + 1 -end - struct IterationCutShort; end function iterate(itr::PartitionIterator, state...)