jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
286 stars 34 forks source link

Extend KeepIndex to Vector indices #243

Open qua4tre opened 5 months ago

qua4tre commented 5 months ago

Using an example from the documentation:

julia> ca = ComponentArray(a=5, b=[4, 1], c=(a=2, b=[6, 30]))
ComponentVector{Int64}(a = 5, b = [4, 1], c = (a = 2, b = [6, 30]))

julia> ca[KeepIndex(:b)]
ComponentVector{Int64}(b = [4, 1])

julia> ca[KeepIndex(1)]
ComponentVector{Int64}(a = 5)

julia> ca[KeepIndex(2:3)]
ComponentVector{Int64}(b = [4, 1])

julia> ca[KeepIndex([2,3])]
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Vector{Int64}

How hard would it be to extend the KeepIndex functionality to more general types of indices?

jonniedie commented 3 months ago

Pretty tough if it needs to be performant. KeepIndex puts the indices into a type parameter so it can do the indexing type-stably. That wouldn't be possible with a Vector because it is not an isbits type, so it can't be held as a type parameter. But we could probably make this work with a SVector from StaticArrays, but it might be quite a bit of effort to get that working.