Skip to content

Commit

Permalink
Preserve infinite step ranges in views (#203)
Browse files Browse the repository at this point in the history
* Preserve infinite step ranges in views

* Remove unnecessary method
  • Loading branch information
jishnub authored Dec 2, 2024
1 parent a9b8a1b commit 66441ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ function getindex(Ac::AdjOrTrans{<:Any,<:InfRanges}, k::Integer, j::InfAxes)
parent(Ac)[j]
end

# views may preserve InfStepRanges
Base.@propagate_inbounds Base.view(r::InfStepRange, i::AbstractUnitRange{<:Integer}) = r[i]
Base.@propagate_inbounds Base.view(r::InfStepRange, i::InfStepRange{<:Integer}) = r[i]
Base.@propagate_inbounds Base.view(r::InfStepRange, i::StepRange{<:Integer}) = r[i]

show(io::IO, r::InfUnitRange) = print(io, repr(first(r)), ':', repr(∞))
show(io::IO, r::OneToInf{Int}) = print(io, "OneToInf()")
Expand Down
12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ end
@test (1:-1:-∞)[1:∞] == 1:-1:-
end

@testset "view(InfStepRange, inf-range)" begin
r = 2:5:
@test view(r, axes(r,1)) === r
@test view(r, 1:1:∞) === r
@test view(r, 2:2:∞) === 7:10:
r2 = 2:5:10_000 # arbitrary high upper cutoff
@test view(r, 4:10) == view(r2, 4:10)
@test view(r, 4:7:50) == view(r2, 4:7:50)
end

@testset "OneToInf" begin
r = OneToInf()
@test !isempty(r)
Expand Down Expand Up @@ -1265,4 +1275,4 @@ end
include("test_infconv.jl")
include("test_infblock.jl")
include("test_infbanded.jl")
include("test_infblockbanded.jl")
include("test_infblockbanded.jl")

0 comments on commit 66441ec

Please sign in to comment.