Skip to content

Commit 0220a87

Browse files
committed
Infinite IdOffsetRanges
1 parent 5bf955c commit 0220a87

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DistributedArrays = "0.6"
1313
Documenter = "0.27"
1414
EllipsisNotation = "1"
1515
FillArrays = "0.11, 0.13"
16+
InfiniteArrays = "0.12"
1617
StaticArrays = "1"
1718
julia = "0.7, 1"
1819

@@ -24,9 +25,10 @@ DistributedArrays = "aaf54ef3-cdf8-58ed-94cc-d582ad619b94"
2425
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
2526
EllipsisNotation = "da5c29d0-fa7d-589e-88eb-ea29b0a81949"
2627
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
28+
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
2729
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2830
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2931
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3032

3133
[targets]
32-
test = ["Aqua", "CatIndices", "DistributedArrays", "DelimitedFiles", "Documenter", "Test", "LinearAlgebra", "EllipsisNotation", "StaticArrays", "FillArrays"]
34+
test = ["Aqua", "CatIndices", "DistributedArrays", "DelimitedFiles", "Documenter", "InfiniteArrays", "Test", "LinearAlgebra", "EllipsisNotation", "StaticArrays", "FillArrays"]

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ 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+
@inline Base.checked_length(x::IdOffsetRange) = Base.checked_length(x.parent)
180181
@inline Base.isempty(r::IdOffsetRange) = isempty(r.parent)
181182
#= We specialize on reduced_indices to work around cases where the parent axis type doesn't
182183
support reduced_index, but the axes do support reduced_indices
@@ -275,7 +276,11 @@ Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Int
275276
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(big), r::IdOffsetRange) =
276277
IdOffsetRange(big.(r.parent), r.offset)
277278

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

280285
# Optimizations
281286
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)
@@ -287,6 +292,8 @@ if VERSION < v"1.5.2"
287292
Base.compute_linindex(parent, I) - stride1*first(Base.axes1(inds[1]))
288293
end
289294

295+
Base.IteratorSize(::Type{<:IdOffsetRange{<:Any,I}}) where {I} = Base.IteratorSize(I)
296+
290297
# This was deemed "too private" to extend: see issue #184
291298
# # Fixes an inference failure in Base.mapfirst!
292299
# # 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using DistributedArrays
77
using Documenter
88
using EllipsisNotation
99
using FillArrays
10+
using InfiniteArrays
1011
using LinearAlgebra
1112
using OffsetArrays
1213
using OffsetArrays: IdentityUnitRange, no_offset_view, IIUR, Origin, IdOffsetRange
@@ -401,6 +402,17 @@ end
401402
ind, st = iterate(ax, st)
402403
@test C[ind] == C[5]
403404
end
405+
406+
@testset "Infinite ranges" begin
407+
x = 1:
408+
y = IdOffsetRange(x)
409+
@test y[2] == x[2]
410+
@test first(x,2) == first(y,2)
411+
stry = repr(y)
412+
strx = repr(x)
413+
@test contains(stry, "values=$strx")
414+
@test contains(stry, "indices=$strx")
415+
end
404416
end
405417

406418
# used in testing the constructor

0 commit comments

Comments
 (0)