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

Infinite IdOffsetRanges #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(")
Expand Down
11 changes: 10 additions & 1 deletion src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 0 additions & 22 deletions test/Project.toml

This file was deleted.

11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down