Skip to content
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

Undef initializers with size #58

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HybridArrays"
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
authors = ["Mateusz Baran <mateuszbaran89@gmail.com>"]
version = "0.4.13"
version = "0.4.14"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
5 changes: 5 additions & 0 deletions src/HybridArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down
12 changes: 12 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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