I am trying to construct a struct that refers to a view of a vector. However, there appears to be an implicit conversion, and I end up with a copy of the “view”.
mutable struct A{
IT,
MBT, # matrix-entry buffer type
IJT, # index buffer type
}
buffer_length::IT
matbuffer::MBT
rowbuffer::IJT
end
function _task_local_assembler(a::AT, buffer_range) where {AT}
buffer_length = maximum(buffer_range) - minimum(buffer_range) + 1
matbuffer = view(a.matbuffer, buffer_range)
rowbuffer = view(a.rowbuffer, buffer_range)
@show typeof(matbuffer)
_a = AT(
buffer_length,
matbuffer,
rowbuffer
)
@show typeof(_a)
return _a
end
a = A(1, zeros(Float64, 10), zeros(Int, 10))
_a = _task_local_assembler(a, 1:5)
a.matbuffer .= -1
@show a
@show _a
On Windows 11, Julia 1.10, I end up with:
julia> include(raw"playo.jl")
typeof(matbuffer) = SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}
typeof(_a) = A{Int64, Vector{Float64}, Vector{Int64}}
a = A{Int64, Vector{Float64}, Vector{Int64}}(1, [-1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00, -1.00000e+00], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
_a = A{Int64, Vector{Float64}, Vector{Int64}}(5, [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00], [0, 0, 0, 0, 0])
A{Int64, Vector{Float64}, Vector{Int64}}(5, [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00], [0, 0, 0, 0, 0])