Skip to content

Commit

Permalink
Added Base.keepat! for StructVector (#301)
Browse files Browse the repository at this point in the history

Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com>
Co-authored-by: Alexander Plavin <alexander@plav.in>
  • Loading branch information
3 people authored Mar 1, 2024
1 parent 33cd6c2 commit 0b99716
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ function Base.deleteat!(s::StructVector{T}, idxs) where T
return StructVector{T}(t)
end

@static if VERSION >= v"1.7.0"
function Base.keepat!(s::StructVector{T}, idxs) where T
t = map(Base.Fix2(keepat!, idxs), components(s))
return StructVector{T}(t)
end
end

Base.copyto!(I::StructArray, J::StructArray) = (foreachfield(copyto!, I, J); I)

function Base.copyto!(I::StructArray, doffs::Integer, J::StructArray, soffs::Integer, n::Integer)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ end
@test d == c == StructArray(a=[1,10,2,3], b=[2,20,3,4], c=["a","A","b","c"])
d = deleteat!(c, 2)
@test d == c == StructArray(a=[1,2,3], b=[2,3,4], c=["a","b","c"])
if Base.VERSION >= v"1.7.0"
d = keepat!(c, 2)
@test d == c == StructArray(a=[2], b=[3], c=["b"])
end

c = StructArray(a=[1], b=[2], c=["a"])
d = [(a=10, b=20, c="A")]
Expand Down Expand Up @@ -295,6 +299,10 @@ end
@test d == c == StructArray{C}(a=[1,10,2,3], b=[2,20,3,4], c=["a","A","b","c"])
d = deleteat!(c, 2)
@test d == c == StructArray{C}(a=[1,2,3], b=[2,3,4], c=["a","b","c"])
if Base.VERSION >= v"1.7.0"
d = keepat!(c, 2)
@test d == c == StructArray{C}(a=[2], b=[3], c=["b"])
end

c = StructArray{C}(a=[1], b=[2], c=["a"])
d = [C(10, 20, "A")]
Expand Down

0 comments on commit 0b99716

Please sign in to comment.