Skip to content

Commit

Permalink
fix nightly bug (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz authored Mar 11, 2024
1 parent 55e8ecf commit be412a1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Dimensions/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ _unwrapdim(x) = x
# Nothing means nothing was passed for this dimension
@inline _dims2indices(dim::Dimension, i::AbstractBeginEndRange) = i
@inline _dims2indices(dim::Dimension, i::Union{LU.Begin,LU.End,Type{LU.Begin},Type{LU.End}}) =
to_indices(parent(dim), (i,))[1]
to_indices(parent(dim), LU._construct_types(i))[1]
@inline _dims2indices(dim::Dimension, ::Nothing) = Colon()
@inline _dims2indices(dim::Dimension, x) = Lookups.selectindices(val(dim), x)

Expand Down
18 changes: 13 additions & 5 deletions src/Lookups/beginend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Base.to_indices(A, inds, (r, args...)::Tuple{BeginEndRange,Vararg}) =
(_to_index(inds[1], r.start):_to_index(inds[1], r.stop), to_indices(A, Base.tail(inds), args)...)
Base.to_indices(A, inds, (r, args...)::Tuple{BeginEndStepRange,Vararg}) =
(_to_index(inds[1], r.start):r.step:_to_index(inds[1], r.stop), to_indices(A, Base.tail(inds), args)...)
Base._to_indices1(A, inds, ::Type{Begin}) = first(inds[1])
Base._to_indices1(A, inds, ::Type{End}) = last(inds[1])
Base._to_indices1(A, inds, ::Begin) = first(inds[1])
Base._to_indices1(A, inds, ::End) = last(inds[1])
Base.to_indices(A, inds, (r, args...)::Tuple{Begin,Vararg}) =
(first(inds[1]), to_indices(A, Base.tail(inds), args)...)
Base.to_indices(A, inds, (r, args...)::Tuple{End,Vararg}) =
(last(inds[1]), to_indices(A, Base.tail(inds), args)...)

_to_index(inds, a::Int) = a
_to_index(inds, ::Begin) = first(inds)
Expand Down Expand Up @@ -89,8 +89,16 @@ _print_f(T, f::Base.Fix2) = string(_print_f(T, f.f), f.x)
_pf(::typeof(div)) = "÷"
_pf(f) = string(f)

for T in (UnitRange, AbstractUnitRange, StepRange, StepRangeLen, LinRange)
for T in (UnitRange, AbstractUnitRange, StepRange, StepRangeLen, LinRange, Lookup)
for f in (:getindex, :view, :dotview)
@eval Base.$f(A::$T, i::AbstractBeginEndRange) = Base.$f(A, to_indices(A, (i,))...)
@eval Base.$f(A::$T, ::Type{Begin}) = Base.$f(A, firstindex(A))
@eval Base.$f(A::$T, ::Type{End}) = Base.$f(A, lastindex(A))
end
end

# These methods let us use Begin End end as types without constructing them.
@inline _construct_types(::Type{Begin}, I...) = (Begin(), _construct_types(I...)...)
@inline _construct_types(::Type{End}, I...) = (End(), _construct_types(I...)...)
@inline _construct_types(i, I...) = (i, _construct_types(I...)...)
@inline _construct_types() = ()
3 changes: 0 additions & 3 deletions src/Lookups/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ for f in (:getindex, :view, :dotview)
x = Base.$f(parent(l), i)
x isa AbstractArray ? rebuild(l; data=x) : x
end
@propagate_inbounds function Base.$f(l::Lookup, i::AbstractBeginEndRange)
l[Base.to_indices(l, (i,))...]
end
end
end
9 changes: 5 additions & 4 deletions src/array/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ for f in (:getindex, :view, :dotview)
@propagate_inbounds Base.$f(A::AbstractDimArray, I::CartesianIndices) =
rebuildsliced(Base.$f, A, Base.$f(parent(A), I), (I,))
@propagate_inbounds function Base.$f(A::AbstractDimVector, i)
x = Base.$f(parent(A), i)
x = Base.$f(parent(A), Lookups._construct_types(i))
if x isa AbstractArray
rebuildsliced(Base.$f, A, x, to_indices(A, (i,)))
else
x
end
end
@propagate_inbounds function Base.$f(A::AbstractDimArray, i1, i2, I...)
x = Base.$f(parent(A), i1, i2, I...)
@propagate_inbounds function Base.$f(A::AbstractDimArray, i1, i2, Is...)
I = Lookups._construct_types(i1, i2, Is...)
x = Base.$f(parent(A), I...)
if x isa AbstractArray
rebuildsliced(Base.$f, A, x, to_indices(A, (i1, i2, I...)))
rebuildsliced(Base.$f, A, x, to_indices(A, I))
else
x
end
Expand Down
4 changes: 2 additions & 2 deletions src/stack/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for f in (:getindex, :view, :dotview)
_dim_f = Symbol(:_dim_, f)
@eval begin
@propagate_inbounds function Base.$f(s::AbstractDimStack, i)
Base.$f(s, to_indices(CartesianIndices(s), (i,))...)
Base.$f(s, to_indices(CartesianIndices(s), Lookups._construct_types(i))...)
end
@propagate_inbounds function Base.$f(s::AbstractDimStack, i::Union{SelectorOrInterval,Extents.Extent})
Base.$f(s, dims2indices(s, i)...)
Expand Down Expand Up @@ -54,7 +54,7 @@ for f in (:getindex, :view, :dotview)
end
end
@propagate_inbounds function Base.$f(s::AbstractDimStack, i1, i2, Is...)
I = to_indices(CartesianIndices(s), (i1, i2, Is...))
I = to_indices(CartesianIndices(s), Lookups._construct_types(i1, i2, Is...))
# Check we have the right number of dimensions
if length(dims(s)) > length(I)
throw(BoundsError(dims(s), I))
Expand Down
4 changes: 4 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,12 @@ end
end
@testset "dimension indexing" begin
A = DimArray((1:5)*(6:3:20)', (X, Y))
@test A[Begin, End] == 18
@test A[Begin(), End()] == 18
@test A[X=Begin, Y=End] == 18
@test A[X=End(), Y=Begin()] == 30
@test A[Begin:Begin+1, End] == [18, 36]
@test A[Begin():Begin()+1, End()] == [18, 36]
@test A[X=Begin:Begin+1, Y=End] == [18, 36]
end
end

0 comments on commit be412a1

Please sign in to comment.