From 6de8c91e3191ab8abcb7014c5efd16caeeadd8ee Mon Sep 17 00:00:00 2001 From: vlepori Date: Tue, 4 Apr 2023 11:11:31 +0200 Subject: [PATCH 1/4] add deleteat! method --- src/elasticarray.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/elasticarray.jl b/src/elasticarray.jl index 736e364..7fc404b 100644 --- a/src/elasticarray.jl +++ b/src/elasticarray.jl @@ -116,6 +116,15 @@ end return A end +function Base.deleteat!(A::ElasticArray{T,N}, idxs::Vararg{Union{Tuple{},Int,Vector{Int},UnitRange{Int}},N}) where {T,N} + d = ndims(A) + prod(isempty.(first(idxs, length(idxs) - 1))) || throw(ArgumentError("Can only delete elements in the last dimension of A")) + keep = setdiff(axes(A, d), last(idxs)) + copyto!(A, selectdim(A, d, keep)) + resize_lastdim!(A, length(keep)) + return A +end + @inline Base.sizehint!(A::ElasticArray{T,N}, dims::Vararg{Integer,N}) where {T,N} = sizehint!(A, dims) From 86c65507303cabc375eec02789b982bf90b93395 Mon Sep 17 00:00:00 2001 From: vlepori Date: Thu, 6 Apr 2023 10:36:32 +0200 Subject: [PATCH 2/4] Add vararg method + tests --- src/elasticarray.jl | 7 ++++++- test/elasticarray.jl | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/elasticarray.jl b/src/elasticarray.jl index 7fc404b..c240851 100644 --- a/src/elasticarray.jl +++ b/src/elasticarray.jl @@ -116,15 +116,20 @@ end return A end -function Base.deleteat!(A::ElasticArray{T,N}, idxs::Vararg{Union{Tuple{},Int,Vector{Int},UnitRange{Int}},N}) where {T,N} +function Base.deleteat!(A::ElasticArray{T,N}, idxs::NTuple{N,Union{Tuple{},Int,Vector{Int},UnitRange{Int}}}) where {T,N} d = ndims(A) prod(isempty.(first(idxs, length(idxs) - 1))) || throw(ArgumentError("Can only delete elements in the last dimension of A")) + issubset(last(idxs), axes(A, d)) || throw(BoundsError(A, (ntuple(_->:,d-1)..., last(idxs)))) keep = setdiff(axes(A, d), last(idxs)) copyto!(A, selectdim(A, d, keep)) resize_lastdim!(A, length(keep)) return A end +function Base.deleteat!(A::ElasticArray{T,N}, idxs::Vararg{Union{Tuple{},Int,Vector{Int},UnitRange{Int}},N}) where {T,N} + deleteat!(A, idxs) +end + @inline Base.sizehint!(A::ElasticArray{T,N}, dims::Vararg{Integer,N}) where {T,N} = sizehint!(A, dims) diff --git a/test/elasticarray.jl b/test/elasticarray.jl index 149af2f..9d44afa 100644 --- a/test/elasticarray.jl +++ b/test/elasticarray.jl @@ -296,6 +296,26 @@ using Random, LinearAlgebra end + @testset "deleteat!" begin + + function deleteat_test() + test_E() do E + + b = deepcopy(E) + n = ndims(E) + s = size(E,n) + + @test_throws ArgumentError deleteat!(E, ntuple(_ -> 1, ndims(E))) + @test_throws MethodError deleteat!(E, ntuple(_ -> (), ndims(E) - 1)) + @test_throws BoundsError deleteat!(E, ntuple(i -> i == n ? s + 1 : (), n)) + @test E == deleteat!(E, ntuple(_ -> (), n)) + @test selectdim(E, n, 2:s) == deleteat!(b, ntuple(i -> i == n ? 1 : (), n)) + end + end + + deleteat_test() + end + @testset "append! and prepend!" begin test_A() do A E = @inferred convert(ElasticArray{Float64}, A) From e2a4e825c2fa31c65f1561fdf92a2dafa0d89c66 Mon Sep 17 00:00:00 2001 From: vlepori Date: Thu, 6 Apr 2023 16:45:28 +0200 Subject: [PATCH 3/4] simplify tests --- test/elasticarray.jl | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/test/elasticarray.jl b/test/elasticarray.jl index 9d44afa..d0f5c46 100644 --- a/test/elasticarray.jl +++ b/test/elasticarray.jl @@ -298,22 +298,18 @@ using Random, LinearAlgebra @testset "deleteat!" begin - function deleteat_test() - test_E() do E - - b = deepcopy(E) - n = ndims(E) - s = size(E,n) + test_E() do E + b = deepcopy(E) + n = ndims(E) + s = size(E,n) - @test_throws ArgumentError deleteat!(E, ntuple(_ -> 1, ndims(E))) - @test_throws MethodError deleteat!(E, ntuple(_ -> (), ndims(E) - 1)) - @test_throws BoundsError deleteat!(E, ntuple(i -> i == n ? s + 1 : (), n)) - @test E == deleteat!(E, ntuple(_ -> (), n)) - @test selectdim(E, n, 2:s) == deleteat!(b, ntuple(i -> i == n ? 1 : (), n)) - end + @test_throws ArgumentError deleteat!(E, ntuple(_ -> 1, ndims(E))) + @test_throws MethodError deleteat!(E, ntuple(_ -> (), ndims(E) - 1)) + @test_throws BoundsError deleteat!(E, ntuple(i -> i == n ? s + 1 : (), n)) + @test E == @inferred deleteat!(E, ntuple(_ -> (), n)) + @test selectdim(E, n, 2:s) == @inferred deleteat!(b, ntuple(i -> i == n ? 1 : (), n)) end - deleteat_test() end @testset "append! and prepend!" begin From 34d0809ed801caa5700a05977eacc071e9f9cb26 Mon Sep 17 00:00:00 2001 From: vlepori Date: Wed, 19 Apr 2023 16:04:58 +0200 Subject: [PATCH 4/4] change subsetting for julia 1.0 compatibility --- src/elasticarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elasticarray.jl b/src/elasticarray.jl index c240851..d3d6159 100644 --- a/src/elasticarray.jl +++ b/src/elasticarray.jl @@ -118,7 +118,7 @@ end function Base.deleteat!(A::ElasticArray{T,N}, idxs::NTuple{N,Union{Tuple{},Int,Vector{Int},UnitRange{Int}}}) where {T,N} d = ndims(A) - prod(isempty.(first(idxs, length(idxs) - 1))) || throw(ArgumentError("Can only delete elements in the last dimension of A")) + prod(isempty.(idxs[1:end-1])) || throw(ArgumentError("Can only delete elements in the last dimension of A")) issubset(last(idxs), axes(A, d)) || throw(BoundsError(A, (ntuple(_->:,d-1)..., last(idxs)))) keep = setdiff(axes(A, d), last(idxs)) copyto!(A, selectdim(A, d, keep))