Skip to content

Commit

Permalink
test Begin ENd
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Mar 9, 2024
1 parent 3cd013e commit 0b0a95b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Dimensions/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ _unwrapdim(x) = x
@inline _dims2indices(dim::Dimension, ::Nothing) = Colon()
@inline _dims2indices(dim::Dimension, I::AbstractBeginEndRange) = I
@inline _dims2indices(dim::Dimension, I::StandardIndices) = I
@inline _dims2indices(dim::Dimension, i) = Base.to_indices(parent(dim), (i,))[1]
@inline _dims2indices(dim::Dimension, I::SelectorOrInterval) = selectindices(val(dim), I)

function _extent_as_intervals(extent::Extents.Extent{Keys}) where Keys
Expand Down
23 changes: 15 additions & 8 deletions src/Lookups/beginend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ Base.first(r::AbstractBeginEndRange) = r.start
Base.last(r::AbstractBeginEndRange) = r.stop
Base.step(r::BeginEndStepRange) = r.step

(::Colon)(a::LazyMath, b::LazyMath) = BeginEndRange(a, b)
(::Colon)(a::Union{Int,LazyMath}, b::Union{Type{Begin},Type{End}}) = BeginEndRange(a, b())
(::Colon)(a::Union{Type{Begin},Type{End}}, b::Union{Int,LazyMath}) = BeginEndRange(a(), b)
(::Colon)(a::Union{Type{Begin},Type{End}}, b::Union{Type{Begin},Type{End}}) = BeginEndRange(a(), b())
(::Colon)(a::Int, b::Union{Begin,End,Type{Begin},Type{End},LazyMath}) = BeginEndRange(a, _x(b))
(::Colon)(a::Union{Begin,End,Type{Begin},Type{End},LazyMath}, b::Int) = BeginEndRange(_x(a), b)
(::Colon)(a::Union{Begin,End,Type{Begin},Type{End},LazyMath}, b::Union{Begin,End,Type{Begin},Type{End},LazyMath}) =
BeginEndRange(_x(a), _x(b))

(::Colon)(a::LazyMath, step::Int, b::LazyMath) = BeginEndStepRange(a, step, b)
(::Colon)(a::Union{Int,LazyMath}, step::Int, b::Union{Type{Begin},Type{End}}) = BeginEndStepRange(a, step, b())
(::Colon)(a::Union{Type{Begin},Type{End}}, step::Int, b::Union{Int,LazyMath}) = BeginEndStepRange(a(), step, b)
(::Colon)(a::Union{Int,LazyMath}, step::Int, b::Union{Type{Begin},Type{End}}) = BeginEndStepRange(a, step, _x(b))
(::Colon)(a::Union{Type{Begin},Type{End}}, step::Int, b::Union{Int,LazyMath}) = BeginEndStepRange(_x(a), step, b)
(::Colon)(a::Union{Type{Begin},Type{End}}, step::Int, b::Union{Type{Begin},Type{End}}) =
BeginEndStepRange(a(), step, b())
BeginEndStepRange(_x(a), step, _x(b))

_x(T::Type) = T()
_x(x) = x

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])

_to_index(inds, a::Int) = a
_to_index(inds, ::Begin) = first(inds)
Expand All @@ -51,6 +56,8 @@ Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, ber::AbstractBeginEndRang
for f in (:+, :-, :*, :÷, :|, :&)
@eval Base.$f(::Type{T}, i::Int) where T <: Union{Begin,End} = LazyMath{T}(Base.Fix2($f, i))
@eval Base.$f(i::Int, ::Type{T}) where T <: Union{Begin,End} = LazyMath{T}(Base.Fix1($f, i))
@eval Base.$f(::T, i::Int) where T <: Union{Begin,End} = LazyMath{T}(Base.Fix2($f, i))
@eval Base.$f(i::Int, ::T) where T <: Union{Begin,End} = LazyMath{T}(Base.Fix1($f, i))
@eval Base.$f(x::LazyMath{T}, i::Int) where T = LazyMath{T}(Base.Fix2(x.f $f, i))
@eval Base.$f(i::Int, x::LazyMath{T}) where T = LazyMath{T}(Base.Fix1(x.f $f, i))
end
Expand Down
6 changes: 4 additions & 2 deletions src/array/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function print_metadata_block(io, mime, metadata; blockwidth=0, displaywidth)
else
metadata_lines = split(sprint(show, mime, metadata), "\n")
new_blockwidth = min(displaywidth-2, max(blockwidth, maximum(length, metadata_lines) + 4))
print_block_separator(io, "metadata", blockwidth, new_blockwidth)
new_blockwidth = print_block_separator(io, "metadata", blockwidth, new_blockwidth)
println(io)
print(io, " ")
show(io, mime, metadata)
Expand Down Expand Up @@ -179,7 +179,9 @@ function print_block_separator(io, label, prev_width, new_width=prev_width)
line = string('', ''^max(0, new_width - textwidth(label) - 2))
corner = ''
end
printstyled(io, string(line, ' ', label, ' ', corner); color=:light_black)
full = string(line, ' ', label, ' ', corner)
printstyled(io, full; color=:light_black)
return length(full) - 2
end

function print_block_close(io, blockwidth)
Expand Down
10 changes: 7 additions & 3 deletions src/stack/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
for f in (:getindex, :view, :dotview)
@eval Base.@assume_effects :foldable @propagate_inbounds Base.$f(s::AbstractDimStack, key::Symbol) =
DimArray(data(s)[key], dims(s, layerdims(s, key)), refdims(s), key, layermetadata(s, key))
@eval Base.@assume_effects :foldable @propagate_inbounds function Base.$f(s::AbstractDimStack, keys::Tuple)
@eval Base.@assume_effects :foldable @propagate_inbounds function Base.$f(s::AbstractDimStack, keys::NTuple{<:Any,Symbol})
rebuild_from_arrays(s, NamedTuple{keys}(map(k -> s[k], keys)))
end
@eval Base.@assume_effects :foldable @propagate_inbounds function Base.$f(
Expand All @@ -19,6 +19,9 @@ end
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,))...)
end
@propagate_inbounds function Base.$f(s::AbstractDimStack, i::Union{SelectorOrInterval,Extents.Extent})
Base.$f(s, dims2indices(s, i)...)
end
Expand Down Expand Up @@ -50,7 +53,7 @@ for f in (:getindex, :view, :dotview)
checkbounds(s, i)
end
end
@propagate_inbounds function Base.$f(s::AbstractDimStack, i1::SelectorOrStandard, i2, Is::SelectorOrStandard...)
@propagate_inbounds function Base.$f(s::AbstractDimStack, i1, i2, Is...)
I = to_indices(CartesianIndices(s), (i1, i2, Is...))
# Check we have the right number of dimensions
if length(dims(s)) > length(I)
Expand Down Expand Up @@ -108,11 +111,12 @@ for f in (:getindex, :view, :dotview)
D = (d1, ds...)
extradims = otherdims(D, dims(s))
length(extradims) > 0 && Dimensions._extradimswarn(extradims)
newlayers = map(layers(s)) do A
function f(A)
layerdims = dims(D, dims(A))
I = length(layerdims) > 0 ? layerdims : map(_ -> :, size(A))
Base.$f(A, I...)
end
newlayers = map(f, layers(s))
# Dicide to rewrap as an AbstractDimStack, or return a scalar
if any(map(v -> v isa AbstractDimArray, newlayers))
# Some scalars, re-wrap them as zero dimensional arrays
Expand Down
2 changes: 1 addition & 1 deletion src/stack/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function print_layers_block(io, mime, stack; blockwidth, displaywidth)
for key in keys(layers)
newblockwidth = min(displaywidth - 2, max(newblockwidth, length(sprint(print_layer, stack, key, keylen))))
end
print_block_separator(io, "layers", blockwidth, newblockwidth)
newblockwidth = print_block_separator(io, "layers", blockwidth, newblockwidth)
println(io)
for key in keys(layers)
print_layer(io, stack, key, keylen)
Expand Down
29 changes: 27 additions & 2 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ end
end

@testset "view" begin
sv = @inferred view(s, 1, 1)
sv = @inferred view(s, Begin, Begin)
@test parent(sv) == (one=fill(1.0), two=fill(2.0f0), three=fill(3))
@test dims(sv) == ()
sv = @inferred view(s, X(1:2), Y(3:3))
Expand All @@ -574,7 +574,7 @@ end
@test linear2d isa DimStack
@test parent(linear2d) == (one=fill(1.0), two=fill(2.0f0), three=fill(3))
@test_broken linear1d = @inferred view(s[X(1)], 1)
linear1d = view(s[X(1)], 1)
linear1d = view(s, 1)
@test linear1d isa DimStack
@test parent(linear1d) == (one=fill(1.0), two=fill(2.0f0), three=fill(3))
linear2d = view(s, 1:2)
Expand Down Expand Up @@ -641,3 +641,28 @@ end
@test @inferred view(A2, Ti(5)) == permutedims([5])
@test @inferred view(A3, Ti(5)) == permutedims([5])
end

@testset "Begin End indexng" begin
@testset "generic indexing" begin
@test (1:10)[Begin] == 1
@test (1:10)[Begin()] == 1
@test (1:10)[End] == 10
@test (1:10)[End()] == 10
@test (1:10)[Begin:End] == 1:10
@test (1:10)[Begin:10] == 1:10
@test (1:10)[1:End] == 1:10
@test (1:10)[Begin():End()] == 1:10
@test (1:10)[Begin+1:End-1] == 2:9
@test (1:10)[Begin()+1:End()-1] == 2:9
@test (1:10)[Begin:End÷2] == 1:5
@test (1:10)[Begin|3:End] == 3:10
@test (1:10)[Begin:End&3] == 1:2
@test (1:10)[Begin()+1:End()-1] == 2:9
end
@testset "dimension indexing" begin
A = DimArray((1:5)*(6:3:20)', (X, Y))
@test A[X=Begin, Y=End] == 18
@test A[X=End(), Y=Begin()] == 30
@test A[X=Begin:Begin+1, Y=End] == [18, 36]
end
end

0 comments on commit 0b0a95b

Please sign in to comment.