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 splice! for StructArrays? #251

Open
sairus7 opened this issue Oct 3, 2022 · 0 comments
Open

Add splice! for StructArrays? #251

sairus7 opened this issue Oct 3, 2022 · 0 comments

Comments

@sairus7
Copy link

sairus7 commented Oct 3, 2022

At a first glance, this seems to work. But maybe there are other corner cases or more performant versions...

using StructArrays

function Base.splice!(s::StructVector{T}, index::Integer, vals=T[]) where T
    valsT = StructVector(vals)
    t = map((v, val) -> splice!(v, index, val), StructArrays.components(s), StructArrays.components(valsT))
    return StructArrays.createinstance(T, t...)
end
function Base.splice!(s::StructVector{T}, indexes, vals=T[]) where T
    valsT = StructVector(vals)
    t = map((v, val) -> splice!(v, indexes, val), StructArrays.components(s), StructArrays.components(valsT))
    return StructVector{T}(t)
end

a = [(1,'a'),(2,'b'),(3,'c')] |> StructVector
new = (0,'x')
splice!(a, 1, [new]) == (1, 'a')
splice!(a, 1:0, [new,new]) |> isempty
splice!(a, 4:5) == [(2,'b'),(3,'c')]
a == [(0,'x'),(0,'x'),(0,'x')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant