From 13eeb03285c845418a5d7c9e26e1ddb2f0e2f5d9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 23 Jan 2023 13:56:06 +0400 Subject: [PATCH] Infinite IdOffsetRanges --- Project.toml | 2 +- src/OffsetArrays.jl | 4 ++-- src/axes.jl | 11 ++++++++++- test/Project.toml | 22 ---------------------- test/runtests.jl | 11 +++++++++++ 5 files changed, 24 insertions(+), 26 deletions(-) delete mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index 799c5ac2..1e7b0fd6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OffsetArrays" uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.12.8" +version = "1.12.9" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index ec446abe..4a33ec34 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -607,9 +607,9 @@ Base.empty!(A::OffsetVector) = (empty!(A.parent); A) # These functions keep the summary compact const OffsetIndices = Union{IdOffsetRange, IdentityUnitRange{<:IdOffsetRange}} function Base.inds2string(inds::Tuple{OffsetIndices, Vararg{OffsetIndices}}) - Base.inds2string(map(UnitRange, inds)) + Base.inds2string(map(no_offset_view, inds)) end -Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(UnitRange, (ind1, inds...))...) +Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(no_offset_view, (ind1, inds...))...) function Base.showarg(io::IO, @nospecialize(a::OffsetArray), toplevel) print(io, "OffsetArray(") diff --git a/src/axes.jl b/src/axes.jl index 519f0dac..e4cf4c47 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -177,6 +177,9 @@ if VERSION < v"1.8.2" end @inline Base.unsafe_indices(r::IdOffsetRange) = (axes1(r),) @inline Base.length(r::IdOffsetRange) = length(r.parent) +if VERSION >= v"1.7" + @inline Base.checked_length(x::IdOffsetRange) = Base.checked_length(x.parent) +end @inline Base.isempty(r::IdOffsetRange) = isempty(r.parent) #= We specialize on reduced_indices to work around cases where the parent axis type doesn't support reduced_index, but the axes do support reduced_indices @@ -275,7 +278,11 @@ Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Int Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(big), r::IdOffsetRange) = IdOffsetRange(big.(r.parent), r.offset) -Base.show(io::IO, r::IdOffsetRange) = print(io, IdOffsetRange, "(values=",first(r), ':', last(r),", indices=",first(eachindex(r)),':',last(eachindex(r)), ")") +function Base.show(io::IO, r::IdOffsetRange) + values_offset = r.parent .+ r.offset + indices_offset = axes(r.parent,1) .+ r.offset + print(io, IdOffsetRange, "(values=",values_offset,", indices=",indices_offset,")") +end # Optimizations @inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset) @@ -287,6 +294,8 @@ if VERSION < v"1.5.2" Base.compute_linindex(parent, I) - stride1*first(Base.axes1(inds[1])) end +Base.IteratorSize(::Type{<:IdOffsetRange{<:Any,I}}) where {I} = Base.IteratorSize(I) + # This was deemed "too private" to extend: see issue #184 # # Fixes an inference failure in Base.mapfirst! # # Test: A = OffsetArray(rand(4,4), (-3,5)); R = similar(A, (1:1, 6:9)); maximum!(R, A) diff --git a/test/Project.toml b/test/Project.toml deleted file mode 100644 index 096d6bf5..00000000 --- a/test/Project.toml +++ /dev/null @@ -1,22 +0,0 @@ -[deps] -Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" -CatIndices = "aafaddc9-749c-510e-ac4f-586e18779b91" -DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" -DistributedArrays = "aaf54ef3-cdf8-58ed-94cc-d582ad619b94" -Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -EllipsisNotation = "da5c29d0-fa7d-589e-88eb-ea29b0a81949" -FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[compat] -Adapt = "2, 3" -Aqua = "0.5" -CatIndices = "0.2" -DistributedArrays = "0.6" -Documenter = "0.27" -EllipsisNotation = "1" -FillArrays = "0.11, 0.13" -StaticArrays = "1" diff --git a/test/runtests.jl b/test/runtests.jl index 7cd56bfb..c7bf123d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -401,6 +401,17 @@ end ind, st = iterate(ax, st) @test C[ind] == C[5] end + + @testset "Iterator" begin + x = 1:4 + y = IdOffsetRange(x) + @test y[2] == x[2] + @test collect(Iterators.take(x,2)) == collect(Iterators.take(y,2)) + @test Base.IteratorSize(y) == Base.IteratorSize(x) + s = Iterators.Stateful(y) + @test collect(Iterators.take(s, 2)) == x[1:2] + @test collect(Iterators.take(s, 2)) == x[3:4] + end end # used in testing the constructor