Skip to content

Commit f0d4442

Browse files
committed
Add support to SA[1;;1]
1 parent f3fa1be commit f0d4442

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/initializers.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,27 @@ end
5959
@inline Base.typed_hvcat(sa::Type{SA}, rows::Dims, xs::Number...) = _SA_typed_hvcat(sa, rows, xs)
6060
@inline Base.typed_hvcat(sa::Type{SA{T}}, rows::Dims, xs::Number...) where T = _SA_typed_hvcat(sa, rows, xs)
6161

62+
if VERSION >= v"1.7"
63+
@generated function reorder(x::NTuple{M,Any}, ::Val{N1}, ::Val{N2}) where {M,N1,N2}
64+
a = Base.PermutedDimsArray(reshape(1:M, N1, N2, :), (2, 1, 3))
65+
args = (:(x[$i]) for i in a)
66+
return :(tuple($(args...)))
67+
end
68+
69+
@inline function _SA_typed_hvncat(sa, dimsshape, row_first, xs)
70+
msize = Size(dimsshape)
71+
xs′ = row_first ? reorder(xs, Val(msize[1]), Val(msize[2])) : xs
72+
x = similar_type(sa, msize)(xs′)
73+
end
74+
75+
@inline Base.typed_hvncat(sa::Type{SA}, dimsshape::Dims, row_first::Bool, xs::Number...) = _SA_typed_hvncat(sa, dimsshape, row_first, xs)
76+
@inline Base.typed_hvncat(sa::Type{SA{T}}, dimsshape::Dims, row_first::Bool, xs::Number...) where T = _SA_typed_hvncat(sa, dimsshape, row_first, xs)
77+
78+
@inline function _SA_typed_hvncat(sa, ::Val{dim}, xs) where {dim}
79+
msize = Size(ntuple(_->1, Val(dim))..., length(xs))
80+
x = similar_type(sa, msize)(xs)
81+
end
82+
83+
@inline Base.typed_hvncat(sa::Type{SA}, dim::Int, xs::Number...) = _SA_typed_hvncat(sa, Val(dim-1), xs)
84+
@inline Base.typed_hvncat(sa::Type{SA{T}}, dim::Int, xs::Number...) where T = _SA_typed_hvncat(sa, Val(dim-1), xs)
85+
end

test/initializers.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ SA_test_hvcat(x,T) = SA{T}[1 x x;
4242
@test_inlined SA[1;2]
4343
@test_inlined SA_test_hvcat(3)
4444

45+
SA_test_hvncat1(x) = SA[1 x;x 2;;;x 2;1 x]
46+
SA_test_hvncat2(x) = SA[1;x;;x;2;;;x;2;;1;x]
47+
if VERSION >= v"1.7.0"
48+
@test SA[1;;;2] === SArray{Tuple{1,1,2}}(1,2)
49+
@test SA[1;2;;1;2] === SMatrix{2,2}(1,2,1,2)
50+
@test SA[1 2;1 2 ;;; 1 2;1 2] === SArray{Tuple{2,2,2}}(Tuple([1 2;1 2 ;;; 1 2;1 2]))
51+
@test_inlined SA_test_hvncat1(3)
52+
@test_inlined SA_test_hvncat2(2)
53+
@test_throws ArgumentError SA[1;2;;3]
54+
end
55+
4556
# https://github.com/JuliaArrays/StaticArrays.jl/pull/685
4657
@test Union{}[] isa Vector{Union{}}
4758
@test Base.typed_vcat(Union{}) isa Vector{Union{}}

0 commit comments

Comments
 (0)