From 9905c22ac045c9d1b05ed60ec0157e422e3907b5 Mon Sep 17 00:00:00 2001 From: Mateusz Baran Date: Wed, 26 Oct 2022 16:11:11 +0200 Subject: [PATCH] Undef initializers with size (#58) * Undef initializers with size * bump version --- Project.toml | 2 +- src/HybridArrays.jl | 5 +++++ test/abstractarray.jl | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0d246ba..36048da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "HybridArrays" uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9" authors = ["Mateusz Baran "] -version = "0.4.13" +version = "0.4.14" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/HybridArrays.jl b/src/HybridArrays.jl index 3e65141..ec6b535 100644 --- a/src/HybridArrays.jl +++ b/src/HybridArrays.jl @@ -138,6 +138,9 @@ end @inline HybridArray{S,T,N}(::UndefInitializer) where {S,T,N} = HybridArray{S,T,N,N}(undef) @inline HybridArray{S,T}(::UndefInitializer) where {S,T} = HybridArray{S,T,StaticArrays.tuple_length(S),StaticArrays.tuple_length(S)}(undef) + +@inline HybridArray{S,T}(::UndefInitializer, d::Integer...) where {S,T} = HybridArray{S,T}(Array{T}(undef, d)) + @generated function (::Type{HybridArray{S,T,N,M,TData}})(x::NTuple{L,Any}) where {S,T,N,M,TData,L} if L != StaticArrays.tuple_prod(S) error("Dimension mismatch") @@ -158,10 +161,12 @@ end HybridVector{S,T,M} = HybridArray{Tuple{S},T,1,M} @inline HybridVector{S}(a::TData) where {S,T,M,TData<:AbstractArray{T,M}} = HybridArray{Tuple{S},T,1,M,TData}(a) @inline HybridVector{S}(x::NTuple{L,T}) where {S,T,L} = HybridArray{Tuple{S},T,1,1,Vector{T}}(x) +@inline HybridVector{S,T}(::UndefInitializer, d::Integer) where {S,T} = HybridVector{S,T}(Vector{T}(undef, d)) HybridMatrix{S1,S2,T,M} = HybridArray{Tuple{S1,S2},T,2,M} @inline HybridMatrix{S1,S2}(a::TData) where {S1,S2,T,M,TData<:AbstractArray{T,M}} = HybridArray{Tuple{S1,S2},T,2,M,TData}(a) @inline HybridMatrix{S1,S2}(x::NTuple{L,T}) where {S1,S2,T,L} = HybridArray{Tuple{S1,S2},T,2,2,Matrix{T}}(x) +@inline HybridMatrix{S1,S2,T}(::UndefInitializer, d1::Integer, d2::Integer) where {S1,S2,T} = HybridMatrix{S1,S2,T}(Matrix{T}(undef, d1, d2)) export HybridArray, HybridMatrix, HybridVector diff --git a/test/abstractarray.jl b/test/abstractarray.jl index ef81ff4..e20f091 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -126,4 +126,16 @@ using StaticArrays: Dynamic end @test HybridArrays._totally_linear() === true + + @testset "undef initializers" begin + M = HybridVector{3,Int}(undef, 3) + @test isa(M, HybridVector{3,Int,1,Vector{Int}}) + @test_throws ErrorException HybridVector{3,Int}(undef, 4) + M2 = HybridMatrix{Dynamic(),4,Int}(undef, 6, 4) + @test isa(M2, HybridMatrix{Dynamic(),4,Int,2,Matrix{Int}}) + @test size(M2) === (6, 4) + M3 = HybridArray{Tuple{Dynamic(),3,4},Int}(undef, 5, 3, 4) + @test isa(M3, HybridArray{Tuple{Dynamic(),3,4},Int,3,3,Array{Int,3}}) + @test size(M3) === (5, 3, 4) + end end