Skip to content

Commit 13eeb03

Browse files
committed
Infinite IdOffsetRanges
1 parent 5bf955c commit 13eeb03

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.12.8"
3+
version = "1.12.9"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/OffsetArrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,9 @@ Base.empty!(A::OffsetVector) = (empty!(A.parent); A)
607607
# These functions keep the summary compact
608608
const OffsetIndices = Union{IdOffsetRange, IdentityUnitRange{<:IdOffsetRange}}
609609
function Base.inds2string(inds::Tuple{OffsetIndices, Vararg{OffsetIndices}})
610-
Base.inds2string(map(UnitRange, inds))
610+
Base.inds2string(map(no_offset_view, inds))
611611
end
612-
Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(UnitRange, (ind1, inds...))...)
612+
Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(no_offset_view, (ind1, inds...))...)
613613

614614
function Base.showarg(io::IO, @nospecialize(a::OffsetArray), toplevel)
615615
print(io, "OffsetArray(")

src/axes.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ if VERSION < v"1.8.2"
177177
end
178178
@inline Base.unsafe_indices(r::IdOffsetRange) = (axes1(r),)
179179
@inline Base.length(r::IdOffsetRange) = length(r.parent)
180+
if VERSION >= v"1.7"
181+
@inline Base.checked_length(x::IdOffsetRange) = Base.checked_length(x.parent)
182+
end
180183
@inline Base.isempty(r::IdOffsetRange) = isempty(r.parent)
181184
#= We specialize on reduced_indices to work around cases where the parent axis type doesn't
182185
support reduced_index, but the axes do support reduced_indices
@@ -275,7 +278,11 @@ Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Int
275278
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(big), r::IdOffsetRange) =
276279
IdOffsetRange(big.(r.parent), r.offset)
277280

278-
Base.show(io::IO, r::IdOffsetRange) = print(io, IdOffsetRange, "(values=",first(r), ':', last(r),", indices=",first(eachindex(r)),':',last(eachindex(r)), ")")
281+
function Base.show(io::IO, r::IdOffsetRange)
282+
values_offset = r.parent .+ r.offset
283+
indices_offset = axes(r.parent,1) .+ r.offset
284+
print(io, IdOffsetRange, "(values=",values_offset,", indices=",indices_offset,")")
285+
end
279286

280287
# Optimizations
281288
@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"
287294
Base.compute_linindex(parent, I) - stride1*first(Base.axes1(inds[1]))
288295
end
289296

297+
Base.IteratorSize(::Type{<:IdOffsetRange{<:Any,I}}) where {I} = Base.IteratorSize(I)
298+
290299
# This was deemed "too private" to extend: see issue #184
291300
# # Fixes an inference failure in Base.mapfirst!
292301
# # Test: A = OffsetArray(rand(4,4), (-3,5)); R = similar(A, (1:1, 6:9)); maximum!(R, A)

test/Project.toml

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ end
401401
ind, st = iterate(ax, st)
402402
@test C[ind] == C[5]
403403
end
404+
405+
@testset "Iterator" begin
406+
x = 1:4
407+
y = IdOffsetRange(x)
408+
@test y[2] == x[2]
409+
@test collect(Iterators.take(x,2)) == collect(Iterators.take(y,2))
410+
@test Base.IteratorSize(y) == Base.IteratorSize(x)
411+
s = Iterators.Stateful(y)
412+
@test collect(Iterators.take(s, 2)) == x[1:2]
413+
@test collect(Iterators.take(s, 2)) == x[3:4]
414+
end
404415
end
405416

406417
# used in testing the constructor

0 commit comments

Comments
 (0)