From 0b0a95b1483fc7423a8fd5ef46764ded0525adb6 Mon Sep 17 00:00:00 2001 From: rafaqz Date: Sat, 9 Mar 2024 10:36:43 +0100 Subject: [PATCH] test Begin ENd --- src/Dimensions/indexing.jl | 1 + src/Lookups/beginend.jl | 23 +++++++++++++++-------- src/array/show.jl | 6 ++++-- src/stack/indexing.jl | 10 +++++++--- src/stack/show.jl | 2 +- test/indexing.jl | 29 +++++++++++++++++++++++++++-- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/Dimensions/indexing.jl b/src/Dimensions/indexing.jl index c594ac277..c99fe1754 100644 --- a/src/Dimensions/indexing.jl +++ b/src/Dimensions/indexing.jl @@ -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 diff --git a/src/Lookups/beginend.jl b/src/Lookups/beginend.jl index dee1dad6d..d1227532f 100644 --- a/src/Lookups/beginend.jl +++ b/src/Lookups/beginend.jl @@ -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) @@ -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 diff --git a/src/array/show.jl b/src/array/show.jl index 25ea992f6..6b7398284 100644 --- a/src/array/show.jl +++ b/src/array/show.jl @@ -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) @@ -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) diff --git a/src/stack/indexing.jl b/src/stack/indexing.jl index 7a148b167..84f8a857f 100644 --- a/src/stack/indexing.jl +++ b/src/stack/indexing.jl @@ -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( @@ -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 @@ -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) @@ -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 diff --git a/src/stack/show.jl b/src/stack/show.jl index 8f9de50de..861db1f52 100644 --- a/src/stack/show.jl +++ b/src/stack/show.jl @@ -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) diff --git a/test/indexing.jl b/test/indexing.jl index 06f491f90..781a2b30f 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -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)) @@ -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) @@ -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