Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deleteat! method #56

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/elasticarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions test/elasticarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down