Skip to content

Commit 6ac68a5

Browse files
saolofpiever
andauthored
Add Base.pop! method to StructVector (#190)
* Add Base.pop! method to StructVector Exact implementation could vary from this one, but Base.pop! is desirable to have as a utility method, especially if Base.push! is already there. * Added tests. * Update src/structarray.jl Simplified implementation Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * Fixed tests * Formatting Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com>
1 parent c91e285 commit 6ac68a5

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/structarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ function Base.push!(s::StructVector, vals)
365365
return s
366366
end
367367

368+
function Base.pop!(s::StructVector{T}) where T
369+
t = map(pop!, components(s))
370+
return createinstance(T, t...)
371+
end
372+
368373
function Base.append!(s::StructVector, vals::StructVector)
369374
foreachfield(append!, s, vals)
370375
return s

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ end
222222
push!(t, (1, 2))
223223
@test getproperty(t, 1) == [2, 1]
224224
@test getproperty(t, 2) == [3.0, 2.0]
225+
@test pop!(t) == (1, 2.0)
226+
@test getproperty(t, 1) == [2]
227+
@test getproperty(t, 2) == [3.0]
225228
end
226229

227230
@testset "constructor from slices" begin
@@ -309,6 +312,9 @@ end
309312

310313
@testset "concat" begin
311314
t = StructArray{Pair}(([3, 5], ["a", "b"]))
315+
push!(t, (2 => "d"))
316+
@test t == StructArray{Pair}(([3, 5, 2], ["a", "b", "d"]))
317+
@test pop!(t) == (2 => "d")
312318
push!(t, (2 => "c"))
313319
@test t == StructArray{Pair}(([3, 5, 2], ["a", "b", "c"]))
314320
append!(t, t)

0 commit comments

Comments
 (0)