From be412a178c5f52ff8a29ffcf38cf09a0819e5c00 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Mon, 11 Mar 2024 01:20:44 +0100 Subject: [PATCH] fix nightly bug (#665) --- src/Dimensions/indexing.jl | 2 +- src/Lookups/beginend.jl | 18 +++++++++++++----- src/Lookups/indexing.jl | 3 --- src/array/indexing.jl | 9 +++++---- src/stack/indexing.jl | 4 ++-- test/indexing.jl | 4 ++++ 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Dimensions/indexing.jl b/src/Dimensions/indexing.jl index b590e9aaa..9ce4420ec 100644 --- a/src/Dimensions/indexing.jl +++ b/src/Dimensions/indexing.jl @@ -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) diff --git a/src/Lookups/beginend.jl b/src/Lookups/beginend.jl index d1227532f..75fc355d9 100644 --- a/src/Lookups/beginend.jl +++ b/src/Lookups/beginend.jl @@ -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) @@ -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() = () diff --git a/src/Lookups/indexing.jl b/src/Lookups/indexing.jl index 87c95b7c1..5c86a1882 100644 --- a/src/Lookups/indexing.jl +++ b/src/Lookups/indexing.jl @@ -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 diff --git a/src/array/indexing.jl b/src/array/indexing.jl index 0f8ef31fb..8d0da02ff 100644 --- a/src/array/indexing.jl +++ b/src/array/indexing.jl @@ -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 diff --git a/src/stack/indexing.jl b/src/stack/indexing.jl index 84f8a857f..f7f2ff386 100644 --- a/src/stack/indexing.jl +++ b/src/stack/indexing.jl @@ -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)...) @@ -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)) diff --git a/test/indexing.jl b/test/indexing.jl index 781a2b30f..b98cd20c7 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -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