diff --git a/src/elasticarray.jl b/src/elasticarray.jl index 736e364..d3d6159 100644 --- a/src/elasticarray.jl +++ b/src/elasticarray.jl @@ -116,6 +116,20 @@ end return A 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.(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)) + 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..d0f5c46 100644 --- a/test/elasticarray.jl +++ b/test/elasticarray.jl @@ -296,6 +296,22 @@ using Random, LinearAlgebra end + @testset "deleteat!" begin + + 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 == @inferred deleteat!(E, ntuple(_ -> (), n)) + @test selectdim(E, n, 2:s) == @inferred deleteat!(b, ntuple(i -> i == n ? 1 : (), n)) + end + + end + @testset "append! and prepend!" begin test_A() do A E = @inferred convert(ElasticArray{Float64}, A)