Skip to content

Commit 781dc77

Browse files
authored
fix LazyMath begin end indexing (#685)
* fix LazyMath indexing * fixes and tests
1 parent b69b48f commit 781dc77

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/Dimensions/indexing.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ _unwrapdim(x) = x
110110
@inline _dims2indices(dim::Dimension, ::Type{<:Dimension}) = Colon()
111111
# Nothing means nothing was passed for this dimension
112112
@inline _dims2indices(dim::Dimension, i::AbstractBeginEndRange) = i
113-
@inline _dims2indices(dim::Dimension, i::Union{LU.Begin,LU.End,Type{LU.Begin},Type{LU.End}}) =
113+
@inline _dims2indices(dim::Dimension, i::Union{LU.Begin,LU.End,Type{LU.Begin},Type{LU.End},LU.LazyMath}) =
114114
to_indices(parent(dim), LU._construct_types(i))[1]
115115
@inline _dims2indices(dim::Dimension, ::Nothing) = Colon()
116116
@inline _dims2indices(dim::Dimension, x) = Lookups.selectindices(val(dim), x)

src/Lookups/beginend.jl

+4-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ Base.to_indices(A, inds, (r, args...)::Tuple{BeginEndRange,Vararg}) =
3939
(_to_index(inds[1], r.start):_to_index(inds[1], r.stop), to_indices(A, Base.tail(inds), args)...)
4040
Base.to_indices(A, inds, (r, args...)::Tuple{BeginEndStepRange,Vararg}) =
4141
(_to_index(inds[1], r.start):r.step:_to_index(inds[1], r.stop), to_indices(A, Base.tail(inds), args)...)
42-
Base.to_indices(A, inds, (r, args...)::Tuple{Begin,Vararg}) =
43-
(first(inds[1]), to_indices(A, Base.tail(inds), args)...)
44-
Base.to_indices(A, inds, (r, args...)::Tuple{End,Vararg}) =
45-
(last(inds[1]), to_indices(A, Base.tail(inds), args)...)
42+
Base.to_indices(A, inds, (r, args...)::Tuple{<:Union{Begin,End,<:LazyMath},Vararg}) =
43+
(_to_index(inds[1], r), to_indices(A, Base.tail(inds), args)...)
4644

4745
_to_index(inds, a::Int) = a
4846
_to_index(inds, ::Begin) = first(inds)
@@ -92,8 +90,8 @@ _pf(f) = string(f)
9290
for T in (UnitRange, AbstractUnitRange, StepRange, StepRangeLen, LinRange, Lookup)
9391
for f in (:getindex, :view, :dotview)
9492
@eval Base.$f(A::$T, i::AbstractBeginEndRange) = Base.$f(A, to_indices(A, (i,))...)
95-
@eval Base.$f(A::$T, ::Type{Begin}) = Base.$f(A, firstindex(A))
96-
@eval Base.$f(A::$T, ::Type{End}) = Base.$f(A, lastindex(A))
93+
@eval Base.$f(A::$T, i::Union{Type{Begin},Type{End},Begin,End,LazyMath}) =
94+
Base.$f(A, to_indices(A, _construct_types(i))...)
9795
end
9896
end
9997

test/indexing.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ end
3131
@testset "lookup" begin
3232
@testset "Points" begin
3333
l = Sampled(2.0:2.0:10, ForwardOrdered(), Regular(2.0), Points(), nothing)
34-
@test l[:] == l[Begin:End] == l
35-
@test l[Begin:5] == l[1:5] == l
34+
@test l[:] == l[Begin:End] == l[1:End] == l[Begin:5] == l[1:5] == l
35+
@test l[Begin+1:End-1] ==l[Begin+1:4] == l[2:End-1] == l[2:4]
3636
@test l[Begin:End] isa typeof(l)
3737
@test l[1:5] isa typeof(l)
3838
@test l[[1, 3, 4]] == view(l, [1, 3, 4]) ==
@@ -161,6 +161,7 @@ end
161161
@test @inferred d[1:5] == d
162162
@test d[1:5] isa typeof(d)
163163
@test @inferred d[Begin:End] == d
164+
@test d[Begin+1:End-1] == d[2:-1+End] == d[1+Begin:4] == d[2:4]
164165
@test d[Begin:End] isa typeof(d)
165166
# TODO properly handle index mashing arrays: here Regular should become Irregular
166167
# @test d[[1, 3, 4]] == X(Sampled([2.0, 6.0, 8.0], ForwardOrdered(), Regular(2.0), Points(), nothing))

0 commit comments

Comments
 (0)