Skip to content

Commit 0b99716

Browse files
jecspieveraplavin
authoredMar 1, 2024
Added Base.keepat! for StructVector (#301)
Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> Co-authored-by: Alexander Plavin <alexander@plav.in>
1 parent 33cd6c2 commit 0b99716

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎src/structarray.jl

+7
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ function Base.deleteat!(s::StructVector{T}, idxs) where T
413413
return StructVector{T}(t)
414414
end
415415

416+
@static if VERSION >= v"1.7.0"
417+
function Base.keepat!(s::StructVector{T}, idxs) where T
418+
t = map(Base.Fix2(keepat!, idxs), components(s))
419+
return StructVector{T}(t)
420+
end
421+
end
422+
416423
Base.copyto!(I::StructArray, J::StructArray) = (foreachfield(copyto!, I, J); I)
417424

418425
function Base.copyto!(I::StructArray, doffs::Integer, J::StructArray, soffs::Integer, n::Integer)

‎test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ end
259259
@test d == c == StructArray(a=[1,10,2,3], b=[2,20,3,4], c=["a","A","b","c"])
260260
d = deleteat!(c, 2)
261261
@test d == c == StructArray(a=[1,2,3], b=[2,3,4], c=["a","b","c"])
262+
if Base.VERSION >= v"1.7.0"
263+
d = keepat!(c, 2)
264+
@test d == c == StructArray(a=[2], b=[3], c=["b"])
265+
end
262266

263267
c = StructArray(a=[1], b=[2], c=["a"])
264268
d = [(a=10, b=20, c="A")]
@@ -295,6 +299,10 @@ end
295299
@test d == c == StructArray{C}(a=[1,10,2,3], b=[2,20,3,4], c=["a","A","b","c"])
296300
d = deleteat!(c, 2)
297301
@test d == c == StructArray{C}(a=[1,2,3], b=[2,3,4], c=["a","b","c"])
302+
if Base.VERSION >= v"1.7.0"
303+
d = keepat!(c, 2)
304+
@test d == c == StructArray{C}(a=[2], b=[3], c=["b"])
305+
end
298306

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

0 commit comments

Comments
 (0)
Please sign in to comment.