-
Notifications
You must be signed in to change notification settings - Fork 41
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
Custom storage for field arrays #175
Comments
Normally, storage is changed with https://juliaarrays.github.io/StructArrays.jl/dev/#StructArrays.replace_storage. Once you have that, things will just work, as long as the For example: julia> struct VectorOfArrays{T, M, N} <: AbstractVector{Array{T, N}}
content::Array{T, M}
end
julia> VectorOfArrays(content::Array{T, M}) where {T, M} = VectorOfArrays{T, M, M - 1}(content)
VectorOfArrays
julia> Base.size(v::VectorOfArrays) = (last(size(v.content)),)
julia> Base.axes(v::VectorOfArrays) = (last(axes(v.content)),)
julia> function Base.getindex(v::VectorOfArrays, i::Int)
sz = Base.front(size(v.content))
p = prod(sz)
ptr = pointer(v.content, (i - 1) * p + 1)
return Base.unsafe_wrap(Array, ptr, sz)
end
julia> using StructArrays
julia> struct Foo{T}
x::T
end
julia> foos = StructArray(Foo(rand(100)) for _ in 1:10);
julia> foos_flat = StructArrays.replace_storage(foos) do v
VectorOfArrays(reduce(hcat, v))
end;
julia> foos_flat.x.content
100×10 Matrix{Float64}: (I think my |
Ah, I never thought to use |
I see here that you used In my actual code, I want to translate a Aside for just being confusing to not return the correct |
I am trying to figure out how to change what array backs a certain field of a
StructArray
. Here's a MWE to describe what I want:Ideally, I would want to be able to specify that for any
StructArray{Foo}
,foos.x
is a dense array. So, in the example, I would want aArray{Float64, 3}
with the last dimension being the vector dimension. Assuming I have a special structure that makes aArray{T, 3}
look like aMatrix{Vector{T}}
, is it possible to tell StructArrays how to use this structure forfoos.x
?I tried looking through the custom scheme docs, but it seemed like that was designed for a different purpose (rerouting
getproperty
?).The text was updated successfully, but these errors were encountered: