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 2 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.(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)

Expand Down
20 changes: 20 additions & 0 deletions test/elasticarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ using Random, LinearAlgebra
end


@testset "deleteat!" begin

function deleteat_test()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wrap this in a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just consistency, since other functions are tested with a functionname_test(). But we can also call test_E() directly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can also call test_E() directly

Let's to that, it's simpler. I mainly wrapped tests if there are called several times with different arrays sizes or so.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense, I simplified it.

test_E() do E
oschulz marked this conversation as resolved.
Show resolved Hide resolved

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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an @inferred here?

@test selectdim(E, n, 2:s) == deleteat!(b, ntuple(i -> i == n ? 1 : (), n))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An @inferred might be nice here as well.

end
end

deleteat_test()
end

@testset "append! and prepend!" begin
test_A() do A
E = @inferred convert(ElasticArray{Float64}, A)
Expand Down