Special data structures
ArrayOfVectorViews
ArrayOfVectorViews
is a data structure representing an Array
of vector views (specifically SubArray{T, 1} where T
). By arranging all data (of type T
) continuously in memory, this will significantly reduce the garbage collection time compared to using an Array{AbstractVector{T}}
. While the data in each view can be mutated, the length of each view is fixed after construction. This data structure provides two features not provided by ArraysOfArrays.jl
: Support of matrices and higher order arrays for storing vectors of different dimensions and efficient construction when the number of elements in each view is not known in advance.
Ferrite.CollectionsOfViews.ArrayOfVectorViews
— TypeArrayOfVectorViews(f!::Function, data::Vector{T}, dims::NTuple{N, Int}; sizehint)
Create an ArrayOfVectorViews
to store many vector views of potentially different sizes, emulating an Array{Vector{T}, N}
with size dims
. However, it avoids allocating each vector individually by storing all data in data
, and instead of Vector{T}
, the each element is a typeof(view(data, 1:2))
.
When the length of each vector is unknown, the ArrayOfVectorViews
can be created reasonably efficient with the following do-block, which creates an intermediate buffer::ConstructionBuffer
supporting the push_at_index!
function.
vector_views = ArrayOfVectorViews(data, dims; sizehint) do buffer
for (ind, val) in some_data
push_at_index!(buffer, val, ind)
end
end
sizehint
tells how much space to allocate for the index ind
if no val
has been added to that index before, or how much more space to allocate in case all previously allocated space for ind
has been used up.
ArrayOfVectorViews(b::CollectionsOfViews.ConstructionBuffer)
Creates the ArrayOfVectorViews
directly from the ConstructionBuffer
that was manually created and filled.
ArrayOfVectorViews(indices::Vector{Int}, data::Vector{T}, lin_idx::LinearIndices{N}; checkargs = true)
Creates the ArrayOfVectorViews
directly where the user is responsible for having the correct input data. Checking of the argument dimensions can be elided by setting checkargs = false
, but incorrect dimensions may lead to illegal out of bounds access later.
data
is indexed by indices[i]:indices[i+1]
, where i = lin_idx[idx...]
and idx...
are the user-provided indices to the ArrayOfVectorViews
.
Ferrite.CollectionsOfViews.ConstructionBuffer
— TypeConstructionBuffer(data::Vector, dims::NTuple{N, Int}, sizehint)
Create a buffer for creating an ArrayOfVectorViews
, representing an array with N
axes. sizehint
sets the number of elements in data
allocated when a new index is added via push_at_index!
, or when the current storage for the index is full, how much many additional elements are reserved for that index. Any content in data
is overwritten, but performance is improved by pre-allocating it to a reasonable size or by sizehint!
ing it.
Ferrite.CollectionsOfViews.push_at_index!
— Functionpush_at_index!(b::ConstructionBuffer, val, indices::Int...)
push!
the value val
to the Vector
view at the index given by indices
, typically called inside the ArrayOfVectorViews
constructor do-block. But can also be used when manually creating a ConstructionBuffer
.