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

Fix vector indexing of offset ranges with other ranges #41213

Merged
merged 1 commit into from
Jul 19, 2021

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Jun 13, 2021

From the test:

On nightly:

julia> struct ZeroBasedUnitRange{T,A<:AbstractUnitRange{T}} <: AbstractUnitRange{T}
               a :: A
               function ZeroBasedUnitRange(a::AbstractUnitRange{T}) where {T}
                   @assert !Base.has_offset_axes(a)
                   new{T, typeof(a)}(a)
               end
           end

julia> Base.parent(A::ZeroBasedUnitRange) = A.a

julia> Base.length(A::ZeroBasedUnitRange) = length(parent(A))

julia> Base.first(A::ZeroBasedUnitRange) = first(parent(A))

julia> Base.last(A::ZeroBasedUnitRange) = last(parent(A))

julia> Base.size(A::ZeroBasedUnitRange) = size(parent(A))

julia> Base.axes(A::ZeroBasedUnitRange) = map(x -> Base.IdentityUnitRange(0:x-1), size(parent(A)))

julia> Base.getindex(A::ZeroBasedUnitRange, i::Int) = parent(A)[i + 1]

julia> Base.getindex(A::ZeroBasedUnitRange, i::Integer) = parent(A)[i + 1]

julia> Base.firstindex(A::ZeroBasedUnitRange) = 0

julia> function Base.show(io::IO, A::ZeroBasedUnitRange)
               show(io, parent(A))
               print(io, " with indices $(axes(A,1))")
           end

julia> r = ZeroBasedUnitRange(5:8)
5:8 with indices Base.IdentityUnitRange(0:3)

julia> r[0:2]
4:6

After this PR:

julia> r[0:2]
5:7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants